Skip to content

Filter updated control files #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions content_test_filtering.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/python3
import logging
import json, logging
from sys import stderr
from ctf import cli, diff_analysis, connect_to_labels, ContentTests, DiffLogging
from ctf.diff import git_wrapper
Expand Down Expand Up @@ -30,7 +30,7 @@
changed_files = git_wrapper.git_diff_files(options.base_branch,
new_branch=options.branch,
pr_number=options.pr_number)

controls_files = []
# Analyze each file separately and make set of tests for each one
while True:
if not changed_files: # Finish when all files are analysed
Expand All @@ -43,6 +43,10 @@
if file_record["filepath"].startswith(".github"):
continue

# Filter the updated control files
if "controls/" in file_record["filepath"]:
controls_files.append(file_record["filepath"])

try:
diff_structure = diff_analysis.analyse_file(file_record)
except diff_analysis.UnknownAnalysisFileType:
Expand All @@ -55,11 +59,19 @@
already_analysed.append(file_record["filepath"])
# If change affected any other file -> analyse it
changed_files.extend(diff_structure.affected_files)

list_of_tests = connect_to_labels.get_labels(tests, options.output)
if options.output == "json":
logs.print_json(list_of_tests)
else:
logs.print_all_logs(list_of_tests, output_format=options.output_format)

# Save the updated controls to a file for syncing OSCAL catalog
logger.debug(f"The updated controls: {controls_files}")
if options.output == "json":
controls_updates = {"controls": controls_files}
try:
with open('controls_updates.json', 'w', encoding='utf-8') as file:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think that would be helpful to save the output for other changes, in profiles and maybe rules? If so, we could create a generic function to save files.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeap. Sounds good. Updated. All the updates will be saved to output.json
python content_test_filtering.py pr --base 4266a65aa118d6840c05d33d40a1612ad10bbf1c --remote_repo https://github.com/ComplianceAsCode/content --verbose --rule --profile --output json 13176 > output.json

json.dump(controls_updates, file, ensure_ascii=False, indent=4)
logger.debug("Controls saved to controls_updates.json successfully.")
except Exception as e:
logger.error(f"Error saving controls updates: {e}")
logger.debug("Finished")