diff --git a/src/sentry/rules/actions/notify_event_service.py b/src/sentry/rules/actions/notify_event_service.py index 31fe09bf88bc9a..08ae4f1a40d086 100644 --- a/src/sentry/rules/actions/notify_event_service.py +++ b/src/sentry/rules/actions/notify_event_service.py @@ -103,12 +103,10 @@ def find_alert_rule_action_ui_component(app_platform_event: AppPlatformEvent) -> Loop through the triggers for the alert rule event. For each trigger, check if an action is an alert rule UI Component """ - triggers = ( - getattr(app_platform_event, "data", {}) - .get("metric_alert", {}) - .get("alert_rule", {}) - .get("triggers", []) - ) + data = getattr(app_platform_event, "data", {}) or {} + metric_alert = data.get("metric_alert") or {} + alert_rule = metric_alert.get("alert_rule") or {} + triggers = alert_rule.get("triggers") or [] actions = [ action diff --git a/tests/sentry/rules/actions/test_notify_event_service.py b/tests/sentry/rules/actions/test_notify_event_service.py index ac43f7bc7fb8ad..eef9d7770cec56 100644 --- a/tests/sentry/rules/actions/test_notify_event_service.py +++ b/tests/sentry/rules/actions/test_notify_event_service.py @@ -2,10 +2,13 @@ from django.utils import timezone -from sentry.rules.actions.notify_event_service import NotifyEventServiceAction +from sentry.rules.actions.notify_event_service import ( + NotifyEventServiceAction, + find_alert_rule_action_ui_component, +) from sentry.sentry_apps.tasks.sentry_apps import notify_sentry_app from sentry.silo.base import SiloMode -from sentry.testutils.cases import RuleTestCase +from sentry.testutils.cases import RuleTestCase, TestCase from sentry.testutils.silo import assume_test_silo_mode from sentry.testutils.skips import requires_snuba @@ -90,3 +93,30 @@ def test_sentry_app_installed(self) -> None: results = rule.get_services() assert len(results) == 0 + + +class FindAlertRuleActionUiComponentTest(TestCase): + def test_returns_false_when_metric_alert_null(self) -> None: + app_platform_event = MagicMock() + app_platform_event.data = {"metric_alert": None} + + assert find_alert_rule_action_ui_component(app_platform_event) is False + + def test_detects_ui_component(self) -> None: + app_platform_event = MagicMock() + app_platform_event.data = { + "metric_alert": { + "alert_rule": { + "triggers": [ + { + "actions": [ + {"type": "email"}, + {"type": "sentry_app", "settings": {"key": "value"}}, + ] + } + ] + } + } + } + + assert find_alert_rule_action_ui_component(app_platform_event) is True