diff --git a/src/sentry/notifications/notification_action/metric_alert_registry/handlers/slack_metric_alert_handler.py b/src/sentry/notifications/notification_action/metric_alert_registry/handlers/slack_metric_alert_handler.py index 78652b813ab1..9c51b14fbb58 100644 --- a/src/sentry/notifications/notification_action/metric_alert_registry/handlers/slack_metric_alert_handler.py +++ b/src/sentry/notifications/notification_action/metric_alert_registry/handlers/slack_metric_alert_handler.py @@ -11,7 +11,6 @@ NotificationContext, OpenPeriodContext, ) -from sentry.integrations.metric_alerts import incident_attachment_info from sentry.integrations.slack.utils.notifications import send_incident_alert_notification from sentry.models.groupopenperiod import GroupOpenPeriod from sentry.models.organization import Organization @@ -21,6 +20,7 @@ ) from sentry.notifications.notification_action.registry import metric_alert_handler_registry from sentry.notifications.notification_action.types import BaseMetricAlertHandler +from sentry.notifications.notification_action.utils import metric_alert_notification_data_factory from sentry.notifications.platform.service import NotificationService from sentry.notifications.platform.target import IntegrationNotificationTarget from sentry.notifications.platform.templates.metric_alert import MetricAlertNotificationData @@ -53,14 +53,6 @@ def _send_via_notification_platform( if notification_context.target_identifier is None: raise ValueError("Slack channel is None") - attachment_info = incident_attachment_info( - organization=organization, - alert_context=alert_context, - metric_issue_context=metric_issue_context, - notification_uuid=notification_uuid, - referrer="metric_alert_slack", - ) - chart_url = None if features.has("organizations:metric-alert-chartcuterie", organization): try: @@ -75,18 +67,15 @@ def _send_via_notification_platform( except Exception as e: sentry_sdk.capture_exception(e) - data = MetricAlertNotificationData( - group_id=metric_issue_context.id, - organization_id=organization.id, - notification_uuid=notification_uuid, - action_id=notification_context.id, + data = metric_alert_notification_data_factory( + action_type=Action.Type.SLACK, + notification_context=notification_context, + alert_context=alert_context, + metric_issue_context=metric_issue_context, open_period_context=open_period_context, - new_status=metric_issue_context.new_status.value, - title=attachment_info["title"], - title_link=attachment_info["title_link"], - text=attachment_info["text"], + organization=organization, + notification_uuid=notification_uuid, chart_url=chart_url, - notes=notification_context.notes, ) target = IntegrationNotificationTarget( diff --git a/src/sentry/notifications/notification_action/types.py b/src/sentry/notifications/notification_action/types.py index ec94b8491813..5bdc908b83cb 100644 --- a/src/sentry/notifications/notification_action/types.py +++ b/src/sentry/notifications/notification_action/types.py @@ -465,7 +465,14 @@ def invoke_legacy_registry(cls, invocation: ActionInvocation) -> None: invocation, NotificationSource.METRIC_ALERT, lambda legacy_render: metric_alert_notification_data_factory( - issue_notification_context, chart_url=legacy_render.chart_url + action_type=invocation.action.type, + notification_context=issue_notification_context.notification_context, + alert_context=issue_notification_context.alert_context, + metric_issue_context=issue_notification_context.metric_issue_context, + open_period_context=issue_notification_context.open_period_context, + organization=issue_notification_context.organization, + notification_uuid=invocation.notification_uuid, + chart_url=legacy_render.chart_url, ), ): notification_context = issue_notification_context.notification_context diff --git a/src/sentry/notifications/notification_action/utils.py b/src/sentry/notifications/notification_action/utils.py index a81c17166b2f..9ca7cd15aed0 100644 --- a/src/sentry/notifications/notification_action/utils.py +++ b/src/sentry/notifications/notification_action/utils.py @@ -1,8 +1,15 @@ import logging from sentry.incidents.grouptype import MetricIssue +from sentry.incidents.typings.metric_detector import ( + AlertContext, + MetricIssueContext, + NotificationContext, + OpenPeriodContext, +) from sentry.integrations.metric_alerts import incident_attachment_info from sentry.models.activity import Activity +from sentry.models.organization import Organization from sentry.notifications.notification_action.registry import ( activity_handler_registry, group_type_notification_registry, @@ -18,9 +25,9 @@ SerializableRuleProxy, ) from sentry.notifications.platform.templates.metric_alert import MetricAlertNotificationData -from sentry.notifications.utils.issue_notification_context import IssueNotificationContext from sentry.services.eventstore.models import GroupEvent from sentry.utils.registry import NoRegistrationExistsError +from sentry.workflow_engine.models import Action from sentry.workflow_engine.types import ActionInvocation logger = logging.getLogger(__name__) @@ -162,35 +169,36 @@ def issue_notification_data_factory(invocation: ActionInvocation) -> IssueNotifi def metric_alert_notification_data_factory( - issue_notif_context: IssueNotificationContext, *, + action_type: str, + notification_context: NotificationContext, + alert_context: AlertContext, + metric_issue_context: MetricIssueContext, + open_period_context: OpenPeriodContext, + organization: Organization, + notification_uuid: str, chart_url: str | None, ) -> MetricAlertNotificationData: - notification_context = issue_notif_context.notification_context - alert_context = issue_notif_context.alert_context - metric_issue_context = issue_notif_context.metric_issue_context - open_period_context = issue_notif_context.open_period_context - organization = issue_notif_context.organization - if notification_context.integration_id is None: raise ValueError("Integration ID is None") if notification_context.target_identifier is None: raise ValueError("Target identifier is None") - referrer = f"metric_alert_{issue_notif_context.action_type}" + if action_type == Action.Type.SLACK_STAGING: + action_type = Action.Type.SLACK attachment_info = incident_attachment_info( organization=organization, alert_context=alert_context, metric_issue_context=metric_issue_context, - notification_uuid=issue_notif_context.notification_uuid, - referrer=referrer, + notification_uuid=notification_uuid, + referrer=f"metric_alert_{action_type}", ) return MetricAlertNotificationData( group_id=metric_issue_context.id, organization_id=organization.id, - notification_uuid=issue_notif_context.notification_uuid, + notification_uuid=notification_uuid, action_id=notification_context.id, open_period_context=open_period_context, new_status=metric_issue_context.new_status.value, diff --git a/tests/sentry/notifications/notification_action/metric_alert_registry/test_slack_metric_alert_handler.py b/tests/sentry/notifications/notification_action/metric_alert_registry/test_slack_metric_alert_handler.py index 24645c58f896..0c3d3c3b6a34 100644 --- a/tests/sentry/notifications/notification_action/metric_alert_registry/test_slack_metric_alert_handler.py +++ b/tests/sentry/notifications/notification_action/metric_alert_registry/test_slack_metric_alert_handler.py @@ -25,7 +25,6 @@ get_detector_serializer, ) from sentry.notifications.notification_action.utils import metric_alert_notification_data_factory -from sentry.notifications.utils.issue_notification_context import IssueNotificationContext from sentry.testutils.helpers.datetime import freeze_time from sentry.testutils.helpers.features import with_feature from sentry.testutils.helpers.options import override_options @@ -107,6 +106,7 @@ def test_send_alert_via_np_sends_to_slack_channel( mock_client_instance.chat_postMessage.assert_called_once() call_kwargs = mock_client_instance.chat_postMessage.call_args.kwargs assert call_kwargs["channel"] == "channel123" + assert "referrer=metric_alert_slack&" in call_kwargs["text"] assert call_kwargs["attachments"] is not None attachments: list[Any] = call_kwargs["attachments"] assert len(attachments) == 1 @@ -343,7 +343,14 @@ def test_invoke_legacy_registry_with_activity(self, mock_send_alert: mock.MagicM ) notification_data = metric_alert_notification_data_factory( - IssueNotificationContext(invocation), chart_url=None + action_type=self.action.type, + notification_context=notification_context, + alert_context=alert_context, + metric_issue_context=metric_issue_context, + open_period_context=open_period_context, + organization=organization, + notification_uuid=notification_uuid, + chart_url=None, ) assert notification_data.notes == "Check the runbook" assert notification_data.new_status == IncidentStatus.CLOSED.value diff --git a/tests/sentry/notifications/platform/shadow/test_shadow_read.py b/tests/sentry/notifications/platform/shadow/test_shadow_read.py index 62eec051193b..1fd73a1e83b8 100644 --- a/tests/sentry/notifications/platform/shadow/test_shadow_read.py +++ b/tests/sentry/notifications/platform/shadow/test_shadow_read.py @@ -479,19 +479,10 @@ def test_slack_compares_with_the_chart_that_was_sent(self, mock_chart: mock.Magi attachments = client.return_value.chat_postMessage.call_args.kwargs["attachments"] assert "https://chart.example" in attachments - def test_slack_staging_differs_by_referrer(self) -> None: + def test_slack_staging_matches(self) -> None: action = self.create_shadow_action("slack_staging") - observation, _ = self.send(self.invocation(action)) - - assert observation.outcome == ShadowOutcome.MISMATCH - log = observation.mismatch - assert log is not None - [entry] = log["diff"] - assert entry.startswith("text: ") - legacy, platform = (payload["text"] for payload in observation.payloads) - assert "referrer=metric_alert_slack&" in legacy - assert "referrer=metric_alert_slack_staging&" in platform + self.assert_match(self.invocation(action)) @mock.patch(f"{SLACK_METRIC_HANDLER}._send_via_notification_platform") @mock.patch(f"{SLACK_METRIC_HANDLER}.NotificationService.has_access", return_value=True)