Skip to content

Commit 83695a5

Browse files
Adding sorting layer to the compliance alerts #1581 (#1632)
Signed-off-by: Abanoub Aziz <abanoubsamy2341@gmail.com>
1 parent 89e092e commit 83695a5

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

scanpipe/pipes/compliance.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from collections import defaultdict
2424

2525
from scanpipe.models import PACKAGE_URL_FIELDS
26+
from scanpipe.models import ComplianceAlertMixin
2627
from scanpipe.pipes import flag
2728
from scanpipe.pipes import scancode
2829

@@ -72,9 +73,22 @@ def group_compliance_alerts_by_severity(queryset):
7273
string representations of the instances associated with that severity.
7374
"""
7475
compliance_alerts = defaultdict(list)
76+
severity_levels = ComplianceAlertMixin.COMPLIANCE_SEVERITY_MAP
77+
7578
for instance in queryset:
7679
compliance_alerts[instance.compliance_alert].append(str(instance))
77-
return dict(compliance_alerts)
80+
81+
# Sort keys for consistent ordering (["error", "warning", "missing"])
82+
sorted_keys = sorted(
83+
compliance_alerts.keys(),
84+
key=lambda label: severity_levels.get(label, len(severity_levels)),
85+
reverse=True,
86+
)
87+
88+
sorted_compliance_alerts = {
89+
label: compliance_alerts[label] for label in sorted_keys
90+
}
91+
return sorted_compliance_alerts
7892

7993

8094
def get_project_compliance_alerts(project, fail_level="error"):

scanpipe/tests/pipes/test_compliance.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,29 @@ def test_scanpipe_compliance_get_project_compliance_alerts(self):
5353
"resources": {"warning": ["path/"]},
5454
}
5555
self.assertEqual(expected, compliance_alerts)
56+
57+
# Testing the compliance alert ordering by severity
58+
make_resource_file(
59+
project,
60+
path="path2/",
61+
compliance_alert=CodebaseResource.Compliance.ERROR,
62+
)
63+
make_package(
64+
project,
65+
package_url="pkg:generic/name@2.0",
66+
compliance_alert=CodebaseResource.Compliance.ERROR,
67+
)
68+
make_package(
69+
project,
70+
package_url="pkg:generic/name@3.0",
71+
compliance_alert=CodebaseResource.Compliance.MISSING,
72+
)
73+
compliance_alerts = get_project_compliance_alerts(project, fail_level="missing")
74+
expected = {
75+
"packages": {
76+
"error": ["pkg:generic/name@1.0", "pkg:generic/name@2.0"],
77+
"missing": ["pkg:generic/name@3.0"],
78+
},
79+
"resources": {"error": ["path2/"], "warning": ["path/"]},
80+
}
81+
self.assertEqual(expected, compliance_alerts)

0 commit comments

Comments
 (0)