File tree Expand file tree Collapse file tree 2 files changed +41
-1
lines changed Expand file tree Collapse file tree 2 files changed +41
-1
lines changed Original file line number Diff line number Diff line change 23
23
from collections import defaultdict
24
24
25
25
from scanpipe .models import PACKAGE_URL_FIELDS
26
+ from scanpipe .models import ComplianceAlertMixin
26
27
from scanpipe .pipes import flag
27
28
from scanpipe .pipes import scancode
28
29
@@ -72,9 +73,22 @@ def group_compliance_alerts_by_severity(queryset):
72
73
string representations of the instances associated with that severity.
73
74
"""
74
75
compliance_alerts = defaultdict (list )
76
+ severity_levels = ComplianceAlertMixin .COMPLIANCE_SEVERITY_MAP
77
+
75
78
for instance in queryset :
76
79
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
78
92
79
93
80
94
def 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):
53
53
"resources" : {"warning" : ["path/" ]},
54
54
}
55
55
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