Skip to content

Commit 73d5eb9

Browse files
committed
add support in check-compliance command
Signed-off-by: NucleonGodX <racerpro41@gmail.com>
1 parent dc1ef78 commit 73d5eb9

File tree

2 files changed

+66
-4
lines changed

2 files changed

+66
-4
lines changed

scanpipe/management/commands/check-compliance.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,40 @@ def handle(self, *args, **options):
5656
for model_alerts in compliance_alerts.values()
5757
for issues_by_severity in model_alerts.values()
5858
)
59-
if not compliance_alerts_count:
59+
60+
extra_data = self.project.extra_data or {}
61+
clarity_alert = extra_data.get("clarity_compliance_alert")
62+
63+
severity_map = {"ok": 0, "warning": 1, "error": 2}
64+
fail_level_map = {"MISSING": 0, "WARNING": 1, "ERROR": 2}
65+
66+
clarity_alert_severity = severity_map.get(clarity_alert, 0)
67+
fail_level_severity = fail_level_map.get(fail_level.upper(), 2)
68+
69+
clarity_issue_count = (
70+
1
71+
if clarity_alert_severity >= fail_level_severity and clarity_alert != "ok"
72+
else 0
73+
)
74+
75+
total_issues = compliance_alerts_count + clarity_issue_count
76+
77+
if total_issues == 0:
6078
sys.exit(0)
6179

6280
if self.verbosity > 0:
63-
msg = [
64-
f"{compliance_alerts_count} compliance issues detected on this project."
65-
]
81+
msg = [f"{total_issues} compliance issues detected on this project."]
6682
for label, issues in compliance_alerts.items():
6783
msg.append(f"[{label}]")
6884
for severity, entries in issues.items():
6985
msg.append(f" > {severity.upper()}: {len(entries)}")
7086
if self.verbosity > 1:
7187
msg.append(" " + "\n ".join(entries))
7288

89+
if clarity_issue_count:
90+
msg.append("[License Clarity Compliance]")
91+
msg.append(f" > Alert Level: {clarity_alert}")
92+
7393
self.stderr.write("\n".join(msg))
7494

7595
sys.exit(1)

scanpipe/tests/test_commands.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,6 +1187,48 @@ def test_scanpipe_management_command_check_compliance(self):
11871187
)
11881188
self.assertEqual(expected, out_value)
11891189

1190+
def test_scanpipe_management_command_check_clarity_compliance_only(self):
1191+
project = Project.objects.create(name="my_project_clarity")
1192+
1193+
project.extra_data = {"clarity_compliance_alert": "error"}
1194+
project.save(update_fields=["extra_data"])
1195+
1196+
out = StringIO()
1197+
options = ["--project", project.name]
1198+
with self.assertRaises(SystemExit) as cm:
1199+
call_command("check-compliance", *options, stderr=out)
1200+
self.assertEqual(cm.exception.code, 1)
1201+
out_value = out.getvalue().strip()
1202+
expected = (
1203+
"1 compliance issues detected on this project."
1204+
"\n[License Clarity Compliance]\n > Alert Level: error"
1205+
)
1206+
self.assertEqual(expected, out_value)
1207+
1208+
def test_scanpipe_management_command_check_both_compliance_and_clarity(self):
1209+
project = Project.objects.create(name="my_project_both")
1210+
1211+
make_package(
1212+
project,
1213+
package_url="pkg:generic/name@1.0",
1214+
compliance_alert=CodebaseResource.Compliance.ERROR,
1215+
)
1216+
project.extra_data = {"clarity_compliance_alert": "warning"}
1217+
project.save(update_fields=["extra_data"])
1218+
1219+
out = StringIO()
1220+
options = ["--project", project.name, "--fail-level", "WARNING"]
1221+
with self.assertRaises(SystemExit) as cm:
1222+
call_command("check-compliance", *options, stderr=out)
1223+
self.assertEqual(cm.exception.code, 1)
1224+
out_value = out.getvalue().strip()
1225+
expected = (
1226+
"2 compliance issues detected on this project."
1227+
"\n[packages]\n > ERROR: 1"
1228+
"\n[License Clarity Compliance]\n > Alert Level: warning"
1229+
)
1230+
self.assertEqual(expected, out_value)
1231+
11901232
def test_scanpipe_management_command_report(self):
11911233
project1 = make_project("project1")
11921234
label1 = "label1"

0 commit comments

Comments
 (0)