Skip to content

Commit a102e00

Browse files
committed
add updated check-compliance
Signed-off-by: NucleonGodX <[email protected]>
1 parent 50eaa16 commit a102e00

File tree

2 files changed

+57
-3
lines changed

2 files changed

+57
-3
lines changed

scanpipe/management/commands/check-compliance.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,28 @@ def check_compliance(self, fail_level):
7474
len(issues) for model in alerts.values() for issues in model.values()
7575
)
7676

77-
if count and self.verbosity > 0:
78-
self.stderr.write(f"{count} compliance issues detected.")
77+
extra_data = self.project.extra_data or {}
78+
clarity_alert = extra_data.get("clarity_compliance_alert")
79+
80+
# Count clarity issue only if alert is not 'ok' or None
81+
clarity_issue_count = 1 if clarity_alert and clarity_alert != "ok" else 0
82+
83+
total_issues = count + clarity_issue_count
84+
85+
if total_issues and self.verbosity > 0:
86+
self.stderr.write(f"{total_issues} compliance issues detected.")
7987
for label, model in alerts.items():
8088
self.stderr.write(f"[{label}]")
8189
for severity, entries in model.items():
8290
self.stderr.write(f" > {severity.upper()}: {len(entries)}")
8391
if self.verbosity > 1:
8492
self.stderr.write(" " + "\n ".join(entries))
8593

86-
return count > 0
94+
if clarity_issue_count:
95+
self.stderr.write("[License Clarity Compliance]")
96+
self.stderr.write(f" > Alert Level: {clarity_alert}")
97+
98+
return total_issues > 0
8799

88100
def check_vulnerabilities(self):
89101
packages = self.project.discoveredpackages.vulnerable_ordered()

scanpipe/tests/test_commands.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,6 +1213,48 @@ def test_scanpipe_management_command_check_compliance(self):
12131213
)
12141214
self.assertEqual(expected, out_value)
12151215

1216+
def test_scanpipe_management_command_check_clarity_compliance_only(self):
1217+
project = make_project(name="my_project_clarity")
1218+
1219+
project.extra_data = {"clarity_compliance_alert": "error"}
1220+
project.save(update_fields=["extra_data"])
1221+
1222+
out = StringIO()
1223+
options = ["--project", project.name]
1224+
with self.assertRaises(SystemExit) as cm:
1225+
call_command("check-compliance", *options, stderr=out)
1226+
self.assertEqual(cm.exception.code, 1)
1227+
out_value = out.getvalue().strip()
1228+
expected = (
1229+
"1 compliance issues detected."
1230+
"\n[License Clarity Compliance]\n > Alert Level: error"
1231+
)
1232+
self.assertEqual(expected, out_value)
1233+
1234+
def test_scanpipe_management_command_check_both_compliance_and_clarity(self):
1235+
project = make_project(name="my_project_both")
1236+
1237+
make_package(
1238+
project,
1239+
package_url="pkg:generic/[email protected]",
1240+
compliance_alert=CodebaseResource.Compliance.ERROR,
1241+
)
1242+
project.extra_data = {"clarity_compliance_alert": "warning"}
1243+
project.save(update_fields=["extra_data"])
1244+
1245+
out = StringIO()
1246+
options = ["--project", project.name, "--fail-level", "WARNING"]
1247+
with self.assertRaises(SystemExit) as cm:
1248+
call_command("check-compliance", *options, stderr=out)
1249+
self.assertEqual(cm.exception.code, 1)
1250+
out_value = out.getvalue().strip()
1251+
expected = (
1252+
"2 compliance issues detected."
1253+
"\n[packages]\n > ERROR: 1"
1254+
"\n[License Clarity Compliance]\n > Alert Level: warning"
1255+
)
1256+
self.assertEqual(expected, out_value)
1257+
12161258
def test_scanpipe_management_command_check_compliance_vulnerabilities(self):
12171259
project = make_project(name="my_project")
12181260
package1 = make_package(project, package_url="pkg:generic/[email protected]")

0 commit comments

Comments
 (0)