Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

feat(devex): flag new admin save hooks and page overrides as workflows (#130749)

Julian Bez849caf68df5e
diff --git a/.semgrep/rules/devex/no-new-custom-admin-views.py b/.semgrep/rules/devex/no-new-custom-admin-views.py
index 926f02f61518..5554de1eda25 100644
--- a/.semgrep/rules/devex/no-new-custom-admin-views.py
+++ b/.semgrep/rules/devex/no-new-custom-admin-views.py
@@ -41,3 +41,34 @@ api_urlpatterns = [
# ok: no-new-custom-admin-views
path("api/projects/", projects_view),
]
+
+
+class DeletionRequestAdmin(admin.ModelAdmin):
+ # ok: no-new-admin-workflow-hooks
+ readonly_fields = ("status",)
+
+ # ok: no-new-admin-workflow-hooks
+ def get_readonly_fields(self, request, obj=None):
+ return self.readonly_fields
+
+ # ruleid: no-new-admin-workflow-hooks
+ def save_model(self, request, obj, form, change):
+ obj.status = "draft"
+ super().save_model(request, obj, form, change)
+
+ # ruleid: no-new-admin-workflow-hooks
+ def save_form(self, request, form, change):
+ return form.save(commit=False)
+
+ # ruleid: no-new-admin-workflow-hooks
+ def change_view(self, request, object_id, form_url="", extra_context=None):
+ return super().change_view(request, object_id, form_url, extra_context)
+
+ # ruleid: no-new-admin-workflow-hooks
+ def response_change(self, request, obj):
+ return super().response_change(request, obj)
+
+
+# ok: no-new-admin-workflow-hooks
+def save_model(request, obj):
+ return obj
diff --git a/.semgrep/rules/devex/no-new-custom-admin-views.yaml b/.semgrep/rules/devex/no-new-custom-admin-views.yaml
index 6d5aa99556be..5550eaf99557 100644
--- a/.semgrep/rules/devex/no-new-custom-admin-views.yaml
+++ b/.semgrep/rules/devex/no-new-custom-admin-views.yaml
@@ -49,6 +49,41 @@ rules:
category: maintainability
subcategory: audit
confidence: HIGH
+ - id: no-new-admin-workflow-hooks
+ message: |
+ Django admin stays for plain CRUD: a ModelAdmin declares fields, filters, inlines and
+ permissions, and saving writes the row and nothing else. Overriding a save or delete hook,
+ a page view or a response turns the admin page into a workflow, for example resetting a
+ status, notifying staff or starting a job. Put that workflow in a PostHog Admin console
+ operation instead; see posthog/admin/console/AGENTS.md.
+ Pre-existing findings are grandfathered; only newly introduced findings block CI.
+ languages: [python]
+ severity: WARNING
+ patterns:
+ - pattern-inside: |
+ class $CLASS(...):
+ ...
+ # Report the name, not the whole method, so editing a grandfathered method keeps the same finding.
+ - pattern: |
+ def $FUNC(self, ...):
+ ...
+ - metavariable-regex:
+ metavariable: $FUNC
+ regex: ^(save_form|save_model|delete_model|save_related|save_formset|delete_queryset|add_view|change_view|changeform_view|changelist_view|delete_view|history_view|response_add|response_change|response_delete)$
+ - focus-metavariable: $FUNC
+ paths:
+ include:
+ - '**/admin.py'
+ - '**/admin/**/*.py'
+ exclude:
+ - posthog/admin/console/**
+ - '**/test/**'
+ - '**/tests/**'
+ - '**/test_*.py'
+ metadata:
+ category: maintainability
+ subcategory: audit
+ confidence: HIGH
- id: no-new-custom-admin-templates
message: |
Django admin picks up a template under templates/admin/ by its path alone, so this file