|
1 | 1 | #!/usr/bin/python3
|
2 |
| -import logging |
| 2 | +import json, logging |
3 | 3 | from sys import stderr
|
4 | 4 | from ctf import cli, diff_analysis, connect_to_labels, ContentTests, DiffLogging
|
5 | 5 | from ctf.diff import git_wrapper
|
|
30 | 30 | changed_files = git_wrapper.git_diff_files(options.base_branch,
|
31 | 31 | new_branch=options.branch,
|
32 | 32 | pr_number=options.pr_number)
|
33 |
| - |
| 33 | + controls_files = [] |
34 | 34 | # Analyze each file separately and make set of tests for each one
|
35 | 35 | while True:
|
36 | 36 | if not changed_files: # Finish when all files are analysed
|
|
43 | 43 | if file_record["filepath"].startswith(".github"):
|
44 | 44 | continue
|
45 | 45 |
|
| 46 | + # Filter the updated control files |
| 47 | + if "controls/" in file_record["filepath"]: |
| 48 | + controls_files.append(file_record["filepath"]) |
| 49 | + |
46 | 50 | try:
|
47 | 51 | diff_structure = diff_analysis.analyse_file(file_record)
|
48 | 52 | except diff_analysis.UnknownAnalysisFileType:
|
|
55 | 59 | already_analysed.append(file_record["filepath"])
|
56 | 60 | # If change affected any other file -> analyse it
|
57 | 61 | changed_files.extend(diff_structure.affected_files)
|
58 |
| - |
59 | 62 | list_of_tests = connect_to_labels.get_labels(tests, options.output)
|
60 | 63 | if options.output == "json":
|
61 | 64 | logs.print_json(list_of_tests)
|
62 | 65 | else:
|
63 | 66 | logs.print_all_logs(list_of_tests, output_format=options.output_format)
|
64 |
| - |
| 67 | + # Save the updated controls to a file for syncing OSCAL catalog |
| 68 | + logger.debug(f"The updated controls: {controls_files}") |
| 69 | + if options.output == "json": |
| 70 | + controls_updates = {"controls": controls_files} |
| 71 | + try: |
| 72 | + with open('controls_updates.json', 'w', encoding='utf-8') as file: |
| 73 | + json.dump(controls_updates, file, ensure_ascii=False, indent=4) |
| 74 | + logger.debug("Controls saved to controls_updates.json successfully.") |
| 75 | + except Exception as e: |
| 76 | + logger.error(f"Error saving controls updates: {e}") |
65 | 77 | logger.debug("Finished")
|
0 commit comments