Repository: getsentry/sentry

Issue:
[BE] Add analytics for when users opt out and re-enable alerts
Add analytics events for when a user opts out of a migration or re-enables an alert.


Discussion:

Resolving patch:
diff --git a/src/sentry/analytics/events/__init__.py b/src/sentry/analytics/events/__init__.py
--- a/src/sentry/analytics/events/__init__.py
+++ b/src/sentry/analytics/events/__init__.py
@@ -61,6 +61,7 @@
 from .release_set_commits import *  # noqa: F401,F403
 from .repo_linked import *  # noqa: F401,F403
 from .rule_disable_opt_out import *  # noqa: F401,F403
+from .rule_reenable import *  # noqa: F401,F403
 from .rule_snooze import *  # noqa: F401,F403
 from .search_saved import *  # noqa: F401,F403
 from .second_platform_added import *  # noqa: F401,F403
diff --git a/src/sentry/analytics/events/rule_reenable.py b/src/sentry/analytics/events/rule_reenable.py
new file mode 100644
--- /dev/null
+++ b/src/sentry/analytics/events/rule_reenable.py
@@ -0,0 +1,15 @@
+from sentry import analytics
+
+
+class RuleReenable(analytics.Event):
+    """Re-enable a rule that was disabled"""
+
+    type = "rule.reenable"
+    attributes = (
+        analytics.Attribute("rule_id"),
+        analytics.Attribute("user_id"),
+        analytics.Attribute("organization_id"),
+    )
+
+
+analytics.register(RuleReenable)
diff --git a/src/sentry/api/endpoints/project_rule_enable.py b/src/sentry/api/endpoints/project_rule_enable.py
--- a/src/sentry/api/endpoints/project_rule_enable.py
+++ b/src/sentry/api/endpoints/project_rule_enable.py
@@ -2,7 +2,7 @@
 from rest_framework.request import Request
 from rest_framework.response import Response
 
-from sentry import audit_log
+from sentry import analytics, audit_log
 from sentry.api.api_owners import ApiOwner
 from sentry.api.api_publish_status import ApiPublishStatus
 from sentry.api.base import region_silo_endpoint
@@ -61,4 +61,10 @@ def put(self, request: Request, project, rule_id) -> Response:
             event=audit_log.get_event_id("RULE_EDIT"),
             data=rule.get_audit_log_data(),
         )
+        analytics.record(
+            "rule.reenable",
+            rule_id=rule.id,
+            user_id=request.user.id,
+            organization_id=project.organization.id,
+        )
         return Response(status=202)


Test patch:
diff --git a/tests/sentry/api/endpoints/test_project_rule_enable.py b/tests/sentry/api/endpoints/test_project_rule_enable.py
--- a/tests/sentry/api/endpoints/test_project_rule_enable.py
+++ b/tests/sentry/api/endpoints/test_project_rule_enable.py
@@ -1,3 +1,5 @@
+from unittest.mock import patch
+
 from rest_framework import status
 
 from sentry import audit_log
@@ -18,7 +20,8 @@ def setUp(self):
         self.rule = self.create_project_rule(project=self.project)
         self.login_as(user=self.user)
 
-    def test_simple(self):
+    @patch("sentry.analytics.record")
+    def test_simple(self, record_analytics):
         self.rule.status = ObjectStatus.DISABLED
         self.rule.save()
         with outbox_runner():
@@ -35,6 +38,13 @@ def test_simple(self):
                 target_object=self.rule.id,
                 event=audit_log.get_event_id("RULE_EDIT"),
             ).exists()
+        assert self.analytics_called_with_args(
+            record_analytics,
+            "rule.reenable",
+            rule_id=self.rule.id,
+            user_id=self.user.id,
+            organization_id=self.organization.id,
+        )
 
     def test_rule_enabled(self):
         """Test that we do not accept an enabled rule"""