Skip to content

Commit 76b0a21

Browse files
committed
add parameters system to the rule model
Signed-off-by: tdruez <tdruez@aboutcode.org>
1 parent b32b73c commit 76b0a21

3 files changed

Lines changed: 20 additions & 0 deletions

File tree

policy/admin.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from policy.models import AssociatedPolicy
2929
from policy.models import PolicyRule
3030
from policy.models import UsagePolicy
31+
from policy.rules import RULE_REGISTRY
3132

3233
License = apps.get_model("license_library", "license")
3334

@@ -168,6 +169,7 @@ class PolicyRuleAdmin(DataspacedAdmin):
168169
form = PolicyRuleForm
169170
list_display = ("name", "rule_type", "threshold", "is_active", "event_name", "get_dataspace")
170171
list_filter = DataspacedAdmin.list_filter + ("rule_type", "is_active")
172+
readonly_fields = DataspacedAdmin.readonly_fields + ("parameters_schema_hint",)
171173
activity_log = False
172174
actions = []
173175
actions_to_remove = ["copy_to", "compare_with"]
@@ -186,3 +188,12 @@ class PolicyRuleAdmin(DataspacedAdmin):
186188
"threshold (0 means any violation triggers the rule), and provide an event name "
187189
"to send a webhook notification when violations are detected or resolved."
188190
)
191+
192+
def parameters_schema_hint(self, obj):
193+
handler = RULE_REGISTRY.get(obj.rule_type)
194+
if not handler or not handler.parameters_schema:
195+
return "No parameters supported for this rule type."
196+
lines = [f"{key}: {desc}" for key, desc in handler.parameters_schema.items()]
197+
return mark_safe("<br>".join(lines))
198+
199+
parameters_schema_hint.short_description = "Supported parameters"

policy/migrations/0003_policyrule.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class Migration(migrations.Migration):
2424
('threshold', models.PositiveIntegerField(default=0, help_text='Minimum number of violations required to trigger this rule (0 means any).')),
2525
('is_active', models.BooleanField(default=True, help_text='Only active rules are evaluated.')),
2626
('event_name', models.CharField(blank=True, help_text='Notification event to fire when violations are detected.', max_length=100)),
27+
('parameters', models.JSONField(blank=True, default=dict, help_text='Optional rule-specific parameters as a JSON object. Supported keys depend on the chosen rule type.')),
2728
('dataspace', models.ForeignKey(editable=False, help_text='A Dataspace is an independent, exclusive set of DejaCode data, which can be either nexB master reference data or installation-specific data.', on_delete=django.db.models.deletion.PROTECT, to='dje.dataspace')),
2829
],
2930
options={

policy/models.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,14 @@ class PolicyRule(DataspacedModel):
273273
blank=True,
274274
help_text=_("Notification event to fire when violations are detected."),
275275
)
276+
parameters = models.JSONField(
277+
blank=True,
278+
default=dict,
279+
help_text=_(
280+
"Optional rule-specific parameters as a JSON object. "
281+
"Supported keys depend on the chosen rule type."
282+
),
283+
)
276284

277285
objects = PolicyRuleQuerySet.as_manager()
278286

0 commit comments

Comments
 (0)