File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 2323from collections import defaultdict
2424
2525from scanpipe .models import PACKAGE_URL_FIELDS
26+ from scanpipe .models import ComplianceAlertMixin
2627from scanpipe .pipes import flag
2728from 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
8094def get_project_compliance_alerts (project , fail_level = "error" ):
Original file line number Diff line number Diff 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 )
You can’t perform that action at this time.
0 commit comments