Skip to content

Commit 6dc9132

Browse files
authored
Define a COMPLIANCE_SEVERITY_MAP on the ComplianceAlertMixin #1581 (#1664)
Signed-off-by: tdruez <tdruez@nexb.com>
1 parent a76aa17 commit 6dc9132

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

scanpipe/models.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2508,6 +2508,16 @@ class Compliance(models.TextChoices):
25082508
ERROR = "error"
25092509
MISSING = "missing"
25102510

2511+
# Map each compliance status to a severity level.
2512+
# Higher numbers indicate more severe compliance issues.
2513+
# This allows consistent comparison and sorting of compliance states.
2514+
COMPLIANCE_SEVERITY_MAP = {
2515+
Compliance.OK: 0,
2516+
Compliance.MISSING: 1,
2517+
Compliance.WARNING: 2,
2518+
Compliance.ERROR: 3,
2519+
}
2520+
25112521
compliance_alert = models.CharField(
25122522
max_length=10,
25132523
blank=True,
@@ -2541,7 +2551,7 @@ def save(self, codebase=None, *args, **kwargs):
25412551
Injects policies, if the feature is enabled, when the
25422552
``license_expression_field`` field value has changed.
25432553
2544-
`codebase` is not used in this context but required for compatibility
2554+
``codebase`` is not used in this context but required for compatibility
25452555
with the commoncode.resource.Codebase class API.
25462556
"""
25472557
if self.policies_enabled:
@@ -2563,7 +2573,10 @@ def policies_enabled(self):
25632573
return self.project.policies_enabled
25642574

25652575
def compute_compliance_alert(self):
2566-
"""Compute and return the compliance_alert value from the licenses policies."""
2576+
"""
2577+
Compute and return the compliance_alert value from the license policies.
2578+
Chooses the most severe compliance_alert found among licenses.
2579+
"""
25672580
license_expression = getattr(self, self.license_expression_field, "")
25682581
if not license_expression:
25692582
return ""
@@ -2583,17 +2596,12 @@ def compute_compliance_alert(self):
25832596
else:
25842597
alerts.append(self.Compliance.MISSING)
25852598

2586-
compliance_ordered_by_severity = [
2587-
self.Compliance.ERROR,
2588-
self.Compliance.WARNING,
2589-
self.Compliance.MISSING,
2590-
]
2591-
2592-
for compliance_severity in compliance_ordered_by_severity:
2593-
if compliance_severity in alerts:
2594-
return compliance_severity
2599+
if not alerts:
2600+
return self.Compliance.OK
25952601

2596-
return self.Compliance.OK
2602+
# Return the most severe alert based on the defined severity
2603+
severity = self.COMPLIANCE_SEVERITY_MAP.get
2604+
return max(alerts, key=severity)
25972605

25982606

25992607
class FileClassifierFieldsModelMixin(models.Model):

0 commit comments

Comments
 (0)