Skip to content

Commit 8055350

Browse files
committed
fire event
Signed-off-by: tdruez <tdruez@aboutcode.org>
1 parent f55f1ff commit 8055350

4 files changed

Lines changed: 39 additions & 9 deletions

File tree

policy/admin.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,9 @@ class PolicyRuleAdmin(DataspacedAdmin):
185185
"When the number of detected issues exceeds the configured threshold, a "
186186
"ProductPolicyViolation is recorded and an optional notification event is fired.\n"
187187
"Set the rule type to match a registered evaluation handler, configure the "
188-
"threshold (0 means any violation triggers the rule), and provide an event name "
189-
"to send a webhook notification when violations are detected or resolved."
188+
"threshold (0 means any violation triggers the rule), and select a notification "
189+
"event to dispatch alerts across all registered channels when violations are "
190+
"detected or resolved."
190191
)
191192

192193
def parameters_schema_hint(self, obj):

policy/engine.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from django.utils import timezone
1010

11-
from notification.models import fire_webhooks
11+
from policy.events import fire_event
1212
from policy.models import PolicyRule
1313
from policy.rules import RULE_REGISTRY
1414
from product_portfolio.models import ProductPolicyViolation
@@ -65,11 +65,10 @@ def evaluate_rules(product):
6565

6666

6767
def fire_violation_event(policy_rule, product, violation_count):
68-
fire_webhooks(
68+
fire_event(
6969
policy_rule.event_name,
70-
instance=None,
7170
dataspace=policy_rule.dataspace,
72-
payload_override={
71+
payload={
7372
"rule": policy_rule.name,
7473
"rule_type": policy_rule.rule_type,
7574
"violation_count": violation_count,
@@ -79,11 +78,10 @@ def fire_violation_event(policy_rule, product, violation_count):
7978

8079

8180
def fire_resolution_event(policy_rule, product):
82-
fire_webhooks(
81+
fire_event(
8382
policy_rule.event_name,
84-
instance=None,
8583
dataspace=policy_rule.dataspace,
86-
payload_override={
84+
payload={
8785
"rule": policy_rule.name,
8886
"status": "resolved",
8987
"product": str(product),

policy/events.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#
2+
# Copyright (c) nexB Inc. and others. All rights reserved.
3+
# DejaCode is a trademark of nexB Inc.
4+
# SPDX-License-Identifier: AGPL-3.0-only
5+
# See https://github.com/aboutcode-org/dejacode for support or download.
6+
# See https://aboutcode.org for more information about AboutCode FOSS projects.
7+
#
8+
9+
from notification.models import fire_webhooks
10+
11+
POLICY_EVENTS = {
12+
"policy.license_alert": "License-related policy rule violation detected or resolved.",
13+
"policy.security_alert": "Security-related policy rule violation detected or resolved.",
14+
"policy.compliance_alert": "Any policy rule violation detected or resolved.",
15+
}
16+
17+
18+
def fire_event(event_name, dataspace, payload):
19+
"""Dispatch a policy event to all registered notification channels."""
20+
fire_webhooks(event_name, instance=None, dataspace=dataspace, payload_override=payload)

policy/forms.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88

99
from django import forms
1010
from django.contrib.contenttypes.models import ContentType
11+
from django.db.models import BLANK_CHOICE_DASH
1112

1213
from dje.forms import ColorCodeFormMixin
1314
from dje.forms import DataspacedAdminForm
15+
from policy.events import POLICY_EVENTS
1416
from policy.models import PolicyRule
1517
from policy.rules import RULE_REGISTRY
1618

@@ -106,9 +108,18 @@ def __init__(self, *args, **kwargs):
106108
self.fields["rule_type"].widget = forms.Select(
107109
choices=[(key, handler.label) for key, handler in RULE_REGISTRY.items()]
108110
)
111+
self.fields["event_name"].widget = forms.Select(
112+
choices=BLANK_CHOICE_DASH + list(POLICY_EVENTS.items())
113+
)
109114

110115
def clean_rule_type(self):
111116
value = self.cleaned_data["rule_type"]
112117
if value not in RULE_REGISTRY:
113118
raise forms.ValidationError(f"Unknown rule type: {value}")
114119
return value
120+
121+
def clean_event_name(self):
122+
value = self.cleaned_data.get("event_name")
123+
if value and value not in POLICY_EVENTS:
124+
raise forms.ValidationError(f"Unknown event: {value}")
125+
return value

0 commit comments

Comments
 (0)