From 9f8ed200444e165cfd004ea7c480a324535be152 Mon Sep 17 00:00:00 2001 From: Kyle Consalus Date: Fri, 9 Oct 2026 12:59:56 -0700 Subject: [PATCH] ref(notifications): Add and use NotificationOrigin.is_test_notification --- src/sentry/integrations/opsgenie/client.py | 4 ++-- .../integrations/slack/actions/notification.py | 4 ++-- .../notifications/notification_action/types.py | 6 +++--- .../notifications/platform/slack/renderers/issue.py | 6 +++--- src/sentry/notifications/types.py | 5 +++++ tests/sentry/notifications/test_helpers.py | 12 ++++++++++++ 6 files changed, 27 insertions(+), 10 deletions(-) diff --git a/src/sentry/integrations/opsgenie/client.py b/src/sentry/integrations/opsgenie/client.py index cca2e8793872..266822da8556 100644 --- a/src/sentry/integrations/opsgenie/client.py +++ b/src/sentry/integrations/opsgenie/client.py @@ -11,7 +11,7 @@ from sentry.integrations.services.integration.model import RpcIntegration from sentry.integrations.types import IntegrationProviderSlug from sentry.models.group import Group -from sentry.notifications.types import TEST_NOTIFICATION_ID, NotificationOrigin +from sentry.notifications.types import NotificationOrigin from sentry.notifications.utils.links import create_link_to_workflow from sentry.services.eventstore.models import Event, GroupEvent from sentry.shared_integrations.exceptions import ApiError @@ -57,7 +57,7 @@ def _get_workflow_links( workflow_id = rule.workflow_id if workflow_id is None: # Test notifications have no backing workflow, so nothing to link to. - if rule.legacy_rule_id != TEST_NOTIFICATION_ID: + if not rule.is_test_notification(): logger.warning( "opsgenie.issue_alert.missing_workflow_id", extra={ diff --git a/src/sentry/integrations/slack/actions/notification.py b/src/sentry/integrations/slack/actions/notification.py index 36e1d5173e64..44fe8f6d7543 100644 --- a/src/sentry/integrations/slack/actions/notification.py +++ b/src/sentry/integrations/slack/actions/notification.py @@ -31,7 +31,7 @@ from sentry.notifications.additional_attachment_manager import get_additional_attachment from sentry.notifications.platform.shadow.capture import record_legacy_render from sentry.notifications.platform.types import NotificationProviderKey -from sentry.notifications.types import TEST_NOTIFICATION_ID, NotificationOrigin, RuleFuture +from sentry.notifications.types import NotificationOrigin, RuleFuture from sentry.notifications.utils.open_period import open_period_start_for_group from sentry.rules.actions import IntegrationEventAction from sentry.rules.base import CallbackFuture @@ -243,7 +243,7 @@ def _send_notification_action_notification( ) return - if context and context.origin.legacy_rule_id == TEST_NOTIFICATION_ID: + if context and context.origin.is_test_notification(): self._send_notification( event=event, futures=futures, diff --git a/src/sentry/notifications/notification_action/types.py b/src/sentry/notifications/notification_action/types.py index e14498f9e857..473f278a365c 100644 --- a/src/sentry/notifications/notification_action/types.py +++ b/src/sentry/notifications/notification_action/types.py @@ -230,7 +230,7 @@ def build_rule_data_from_action( ) -> RuleData: action_blob = cls.build_rule_action_blob(action, detector.linked_project.organization.id) - if origin.legacy_rule_id == TEST_NOTIFICATION_ID: + if origin.is_test_notification(): action_blob["legacy_rule_id"] = TEST_NOTIFICATION_ID else: assert origin.workflow_id is not None @@ -238,7 +238,7 @@ def build_rule_data_from_action( if origin.legacy_rule_id is not None: action_blob["legacy_rule_id"] = origin.legacy_rule_id - if origin.legacy_rule_id == TEST_NOTIFICATION_ID and action.type == Action.Type.EMAIL: + if origin.is_test_notification() and action.type == Action.Type.EMAIL: action_blob["skipDigests"] = True return {"actions": [action_blob]} @@ -393,7 +393,7 @@ def invoke_legacy_registry(cls, invocation: ActionInvocation) -> None: ) # Execute the futures - if context.origin.legacy_rule_id == TEST_NOTIFICATION_ID: + if context.origin.is_test_notification(): cls.send_test_notification(invocation.event_data, futures) else: cls.execute_futures(invocation.event_data, futures) diff --git a/src/sentry/notifications/platform/slack/renderers/issue.py b/src/sentry/notifications/platform/slack/renderers/issue.py index 5e045e4b6d8c..1a45e3c3b5ee 100644 --- a/src/sentry/notifications/platform/slack/renderers/issue.py +++ b/src/sentry/notifications/platform/slack/renderers/issue.py @@ -12,7 +12,6 @@ NotificationRenderedTemplate, NotificationSource, ) -from sentry.notifications.types import TEST_NOTIFICATION_ID from sentry.workflow_engine.tasks.utils import fetch_event @@ -44,7 +43,8 @@ def render[DataT: NotificationData]( else None ) scopes = set(integration.metadata.get("scopes") or []) if integration else set() - send_nudge = data.rule.id != TEST_NOTIFICATION_ID and should_send_nudge_block( + origin = data.rule.to_notification_origin() + send_nudge = not origin.is_test_notification() and should_send_nudge_block( organization=group.organization, notification_uuid=data.notification_uuid ) @@ -52,7 +52,7 @@ def render[DataT: NotificationData]( group=group, event=event, tags=set(data.tags) if data.tags else None, - rules=[data.rule.to_notification_origin()], + rules=[origin], notes=data.notes, send_nudge=send_nudge, has_mentions_read_scope=SlackScope.APP_MENTIONS_READ in scopes, diff --git a/src/sentry/notifications/types.py b/src/sentry/notifications/types.py index f8b4fc72ab83..031e5bd86454 100644 --- a/src/sentry/notifications/types.py +++ b/src/sentry/notifications/types.py @@ -21,6 +21,11 @@ class NotificationOrigin: workflow_id: int | None legacy_rule_id: int | None + def is_test_notification(self) -> bool: + return ( + self.workflow_id == TEST_NOTIFICATION_ID or self.legacy_rule_id == TEST_NOTIFICATION_ID + ) + @classmethod def from_legacy_rule(cls, rule: Rule) -> NotificationOrigin: return cls.from_legacy_data( diff --git a/tests/sentry/notifications/test_helpers.py b/tests/sentry/notifications/test_helpers.py index c1232fb265ac..59f9ca1be39f 100644 --- a/tests/sentry/notifications/test_helpers.py +++ b/tests/sentry/notifications/test_helpers.py @@ -8,6 +8,7 @@ ) from sentry.notifications.models.notificationsettingoption import NotificationSettingOption from sentry.notifications.types import ( + TEST_NOTIFICATION_ID, NotificationOrigin, NotificationSettingEnum, NotificationSettingsOptionEnum, @@ -148,6 +149,17 @@ def test_notification_origin_normalizes_legacy_identity(self) -> None: assert origin.label == rule.label assert origin.legacy_rule_id == rule.id assert origin.workflow_id == workflow_id + assert not origin.is_test_notification() + + def test_notification_origin_identifies_test_notification(self) -> None: + origin = NotificationOrigin.from_legacy_data( + label="Test notification", + environment_id=None, + data={}, + fallback_legacy_rule_id=TEST_NOTIFICATION_ID, + ) + + assert origin.is_test_notification() def test_get_email_link_extra_params(self) -> None: rule: Rule = self.create_project_rule(self.project)