Browse Source

ref(alerts): Soft deprecate projectalertruledetails get method (#53999)

Companion PR to https://github.com/getsentry/sentry/pull/53970 that
combines the GET endpoints.
Colleen O'Rourke 1 year ago
parent
commit
819997676b

+ 51 - 51
src/sentry/incidents/endpoints/organization_alert_rule_details.py

@@ -15,6 +15,56 @@ from sentry.services.hybrid_cloud.app import app_service
 from sentry.services.hybrid_cloud.user.service import user_service
 
 
+def fetch_alert_rule(request: Request, organization, alert_rule):
+    # Serialize Alert Rule
+    expand = request.GET.getlist("expand", [])
+    serialized_rule = serialize(
+        alert_rule, request.user, DetailedAlertRuleSerializer(expand=expand)
+    )
+
+    # Prepare AlertRuleTriggerActions that are SentryApp components
+    errors = []
+    for trigger in serialized_rule.get("triggers", []):
+        for action in trigger.get("actions", []):
+            if action.get("_sentry_app_installation") and action.get("_sentry_app_component"):
+                installation = SentryAppInstallation(**action.get("_sentry_app_installation", {}))
+                component = installation.prepare_ui_component(
+                    SentryAppComponent(**action.get("_sentry_app_component")),
+                    None,
+                    action.get("settings"),
+                )
+                if component is None:
+                    errors.append(
+                        {"detail": f"Could not fetch details from {installation.sentry_app.name}"}
+                    )
+                    action["disabled"] = True
+                    continue
+
+                action["formFields"] = component.schema.get("settings", {})
+
+                # Delete meta fields
+                del action["_sentry_app_installation"]
+                del action["_sentry_app_component"]
+
+    if len(errors):
+        serialized_rule["errors"] = errors
+
+    rule_snooze = RuleSnooze.objects.filter(
+        Q(user_id=request.user.id) | Q(user_id=None), alert_rule=alert_rule
+    ).first()
+    if rule_snooze:
+        serialized_rule["snooze"] = True
+        if request.user.id == rule_snooze.owner_id:
+            serialized_rule["snoozeCreatedBy"] = "You"
+        else:
+            user = user_service.get_user(rule_snooze.owner_id)
+            if user:
+                serialized_rule["snoozeCreatedBy"] = user.get_display_name()
+        serialized_rule["snoozeForEveryone"] = rule_snooze.user_id is None
+
+    return Response(serialized_rule)
+
+
 def remove_alert_rule(request: Request, organization, alert_rule):
     try:
         delete_alert_rule(alert_rule, user=request.user, ip_address=request.META.get("REMOTE_ADDR"))
@@ -31,57 +81,7 @@ class OrganizationAlertRuleDetailsEndpoint(OrganizationAlertRuleEndpoint):
         ``````````````````
         :auth: required
         """
-        # Serialize Alert Rule
-        expand = request.GET.getlist("expand", [])
-        serialized_rule = serialize(
-            alert_rule, request.user, DetailedAlertRuleSerializer(expand=expand)
-        )
-
-        # Prepare AlertRuleTriggerActions that are SentryApp components
-        errors = []
-        for trigger in serialized_rule.get("triggers", []):
-            for action in trigger.get("actions", []):
-                if action.get("_sentry_app_installation") and action.get("_sentry_app_component"):
-                    installation = SentryAppInstallation(
-                        **action.get("_sentry_app_installation", {})
-                    )
-                    component = installation.prepare_ui_component(
-                        SentryAppComponent(**action.get("_sentry_app_component")),
-                        None,
-                        action.get("settings"),
-                    )
-                    if component is None:
-                        errors.append(
-                            {
-                                "detail": f"Could not fetch details from {installation.sentry_app.name}"
-                            }
-                        )
-                        action["disabled"] = True
-                        continue
-
-                    action["formFields"] = component.schema.get("settings", {})
-
-                    # Delete meta fields
-                    del action["_sentry_app_installation"]
-                    del action["_sentry_app_component"]
-
-        if len(errors):
-            serialized_rule["errors"] = errors
-
-        rule_snooze = RuleSnooze.objects.filter(
-            Q(user_id=request.user.id) | Q(user_id=None), alert_rule=alert_rule
-        ).first()
-        if rule_snooze:
-            serialized_rule["snooze"] = True
-            if request.user.id == rule_snooze.owner_id:
-                serialized_rule["snoozeCreatedBy"] = "You"
-            else:
-                user = user_service.get_user(rule_snooze.owner_id)
-                if user:
-                    serialized_rule["snoozeCreatedBy"] = user.get_display_name()
-            serialized_rule["snoozeForEveryone"] = rule_snooze.user_id is None
-
-        return Response(serialized_rule)
+        return fetch_alert_rule(request, organization, alert_rule)
 
     def put(self, request: Request, organization, alert_rule) -> Response:
         serializer = DrfAlertRuleSerializer(

+ 6 - 6
src/sentry/incidents/endpoints/project_alert_rule_details.py

@@ -4,9 +4,11 @@ from rest_framework.response import Response
 
 from sentry.api.base import region_silo_endpoint
 from sentry.api.serializers import serialize
-from sentry.api.serializers.models.alert_rule import AlertRuleSerializer
 from sentry.incidents.endpoints.bases import ProjectAlertRuleEndpoint
-from sentry.incidents.endpoints.organization_alert_rule_details import remove_alert_rule
+from sentry.incidents.endpoints.organization_alert_rule_details import (
+    fetch_alert_rule,
+    remove_alert_rule,
+)
 from sentry.incidents.logic import get_slack_actions_with_async_lookups
 from sentry.incidents.serializers import AlertRuleSerializer as DrfAlertRuleSerializer
 from sentry.incidents.utils.sentry_apps import trigger_sentry_app_action_creators_for_incidents
@@ -18,13 +20,11 @@ from sentry.tasks.integrations.slack import find_channel_id_for_alert_rule
 class ProjectAlertRuleDetailsEndpoint(ProjectAlertRuleEndpoint):
     def get(self, request: Request, project, alert_rule) -> Response:
         """
-        Fetch an alert rule.
+        Fetch a metric alert rule. @deprecated. Use OrganizationAlertRuleDetailsEndpoint instead.
         ``````````````````
         :auth: required
         """
-        serialized_alert_rule = serialize(alert_rule, request.user, AlertRuleSerializer())
-
-        return Response(serialized_alert_rule)
+        return fetch_alert_rule(request, project.organization, alert_rule)
 
     def put(self, request: Request, project, alert_rule) -> Response:
         data = request.data

+ 9 - 0
tests/sentry/incidents/endpoints/test_organization_alert_rule_details.py

@@ -107,6 +107,15 @@ class AlertRuleDetailsGetEndpointTest(AlertRuleDetailsBase, APITestCase):
 
         assert resp.data == serialize(self.alert_rule, serializer=DetailedAlertRuleSerializer())
 
+    def test_aggregate_translation(self):
+        self.create_team(organization=self.organization, members=[self.user])
+        self.login_as(self.user)
+        alert_rule = self.create_alert_rule(aggregate="count_unique(tags[sentry:user])")
+        with self.feature("organizations:incidents"):
+            resp = self.get_success_response(self.organization.slug, alert_rule.id)
+            assert resp.data["aggregate"] == "count_unique(user)"
+            assert alert_rule.snuba_query.aggregate == "count_unique(tags[sentry:user])"
+
     def test_expand_latest_incident(self):
         self.create_team(organization=self.organization, members=[self.user])
         self.login_as(self.user)

+ 2 - 33
tests/sentry/incidents/endpoints/test_project_alert_rule_details.py

@@ -9,6 +9,7 @@ from rest_framework.exceptions import ErrorDetail
 
 from sentry import audit_log
 from sentry.api.serializers import serialize
+from sentry.api.serializers.models.alert_rule import DetailedAlertRuleSerializer
 from sentry.incidents.models import (
     AlertRule,
     AlertRuleStatus,
@@ -90,27 +91,6 @@ class AlertRuleDetailsBase(APITestCase):
         self.method = original_method
         return serialized_alert_rule
 
-    def test_invalid_rule_id(self):
-        self.login_as(self.owner_user)
-        with self.feature("organizations:incidents"):
-            resp = self.get_response(self.organization.slug, self.project.slug, 1234)
-
-        assert resp.status_code == 404
-
-    def test_permissions(self):
-        self.login_as(self.create_user())
-        with self.feature("organizations:incidents"):
-            resp = self.get_response(self.organization.slug, self.project.slug, self.alert_rule.id)
-
-        assert resp.status_code == 403
-
-    def test_no_feature(self):
-        self.login_as(self.owner_user)
-        resp = self.get_response(self.organization.slug, self.project.slug, self.alert_rule.id)
-        # Without incidents feature flag, allow delete
-        status = 204 if self.method == "delete" else 404
-        assert resp.status_code == status
-
 
 @region_silo_test(stable=True)
 class AlertRuleDetailsGetEndpointTest(AlertRuleDetailsBase):
@@ -120,18 +100,7 @@ class AlertRuleDetailsGetEndpointTest(AlertRuleDetailsBase):
             resp = self.get_success_response(
                 self.organization.slug, self.project.slug, self.alert_rule.id
             )
-
-        assert resp.data == serialize(self.alert_rule)
-
-    def test_aggregate_translation(self):
-        self.login_as(self.owner_user)
-        alert_rule = self.create_alert_rule(aggregate="count_unique(tags[sentry:user])")
-        with self.feature("organizations:incidents"):
-            resp = self.get_success_response(
-                self.organization.slug, self.project.slug, alert_rule.id
-            )
-            assert resp.data["aggregate"] == "count_unique(user)"
-            assert alert_rule.snuba_query.aggregate == "count_unique(tags[sentry:user])"
+        assert resp.data == serialize(self.alert_rule, serializer=DetailedAlertRuleSerializer())
 
 
 class AlertRuleDetailsPutEndpointTest(AlertRuleDetailsBase):