Skip to content

Commit ee5e986

Browse files
committed
add filter by policy rule
Signed-off-by: tdruez <tdruez@aboutcode.org>
1 parent b482699 commit ee5e986

4 files changed

Lines changed: 26 additions & 2 deletions

File tree

policy/rules.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ def count_violations(self, product, threshold, parameters):
2222
"""Count objects violating the rule for the given product."""
2323
raise NotImplementedError
2424

25+
def get_package_filter(self):
26+
"""Return queryset filter kwargs for ProductPackage to identify violating packages."""
27+
return {}
28+
2529

2630
class PackageBaseRule(BaseRule):
2731
"""Base for rules that count packages matching a fixed filter within a product."""
@@ -38,6 +42,9 @@ def count_violations(self, product, threshold, parameters):
3842

3943
return count if count > threshold else 0
4044

45+
def get_package_filter(self):
46+
return {f"package__{key}": value for key, value in self.package_filter.items()}
47+
4148

4249
class LicensePolicyErrorRule(PackageBaseRule):
4350
rule_type = "license_policy_error"
@@ -74,6 +81,9 @@ class VulnerabilityDetectedRule(BaseRule):
7481
"min_risk_score": "Minimum risk score (0.0-10.0). Default: any vulnerability.",
7582
}
7683

84+
def get_package_filter(self):
85+
return {"package__risk_score__isnull": False}
86+
7787
def count_violations(self, product, threshold, parameters):
7888
Package = apps.get_model("component_catalog", "package")
7989

product_portfolio/filters.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from dje.widgets import DropDownRightWidget
3232
from dje.widgets import DropDownWidget
3333
from license_library.models import License
34+
from policy.rules import RULE_REGISTRY
3435
from product_portfolio.models import CodebaseResource
3536
from product_portfolio.models import Product
3637
from product_portfolio.models import ProductComponent
@@ -407,6 +408,7 @@ class ProductPackageFilterSet(BaseProductRelationFilterSet):
407408
field_name="package__usage_policy__compliance_alert",
408409
distinct=True,
409410
)
411+
policy_rule = django_filters.CharFilter(method="filter_by_policy_rule")
410412

411413
class Meta:
412414
model = ProductPackage
@@ -422,6 +424,13 @@ class Meta:
422424
"exploitability",
423425
]
424426

427+
def filter_by_policy_rule(self, queryset, name, value):
428+
"""Filter packages that triggered the given policy rule type."""
429+
handler = RULE_REGISTRY.get(value)
430+
if not handler:
431+
return queryset
432+
return queryset.filter(**handler.get_package_filter())
433+
425434
def __init__(self, *args, **kwargs):
426435
super().__init__(*args, **kwargs)
427436
self.filters["vulnerability_analyses__state"].extra["null_label"] = "(No values)"

product_portfolio/templates/product_portfolio/compliance/compliance_panels.html

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,12 @@ <h3 class="fs-6 fw-medium mb-0">{% trans "Policy violations" %}</h3>
250250
<tr>
251251
<td>{{ violation.rule_label }}</td>
252252
<td class="text-body-secondary small">{{ violation.rule_description }}</td>
253-
<td>{{ violation.violation_count }}</td>
253+
<td>
254+
<a href="{{ product_url }}?inventory-policy_rule={{ violation.rule_type }}#inventory"
255+
class="text-decoration-none">
256+
{{ violation.violation_count }}
257+
</a>
258+
</td>
254259
<td class="text-body-tertiary small">{{ violation.detected_date|date:"N j, Y" }}</td>
255260
</tr>
256261
{% endfor %}

product_portfolio/templates/product_portfolio/compliance/metric_cards.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
{% else %}
8585
<div class="fs-4 fw-medium lh-sm text-danger">{{ policy_violation_count }}</div>
8686
<div class="text-body-tertiary fs-xs mt-1">
87-
<a href="#policy-violations" class="text-body-tertiary text-decoration-none">
87+
<a href="#policy-violations" class="text-decoration-none">
8888
{{ policy_violation_count }} {% trans "rule" %}{{ policy_violation_count|pluralize }} {% trans "triggered" %}
8989
</a>
9090
</div>

0 commit comments

Comments
 (0)