From 6c248ed1c8ea1343716c4764bae8d49bbfb2068b Mon Sep 17 00:00:00 2001 From: Vadim Stepanov Date: Wed, 17 Jan 2024 13:00:25 +0000 Subject: [PATCH] Fix posting Slack message when route is deleted (#3702) # What this PR does Fixes https://github.com/grafana/oncall/issues/3646 ## Checklist - [x] Unit, integration, and e2e (if applicable) tests updated - [x] Documentation added (or `pr:no public docs` PR label added if not required) - [x] `CHANGELOG.md` updated (or `pr:no changelog` PR label added if not required) --- CHANGELOG.md | 1 + .../apps/slack/scenarios/distribute_alerts.py | 7 ++++- .../test_distribute_alerts.py | 28 +++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 75c35b02..4025b42e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - Fixed Webhooks UI not allowing simple webhooks to be created ([#3691](https://github.com/grafana/oncall/pull/3691)) +- Fix posting Slack message when route is deleted by @vadimkerr ([#3702](https://github.com/grafana/oncall/pull/3702)) ## v1.3.88 (2024-01-16) diff --git a/engine/apps/slack/scenarios/distribute_alerts.py b/engine/apps/slack/scenarios/distribute_alerts.py index e6a30191..873410f9 100644 --- a/engine/apps/slack/scenarios/distribute_alerts.py +++ b/engine/apps/slack/scenarios/distribute_alerts.py @@ -76,7 +76,12 @@ class AlertShootingStep(scenario_step.ScenarioStep): if num_updated_rows == 1: try: - channel_id = alert.group.channel_filter.slack_channel_id_or_general_log_id + channel_id = ( + alert.group.channel_filter.slack_channel_id_or_general_log_id + if alert.group.channel_filter + # if channel filter is deleted mid escalation, use default Slack channel + else alert.group.channel.organization.general_log_channel_id + ) self._send_first_alert(alert, channel_id) except SlackAPIError: AlertGroup.objects.filter(pk=alert.group.pk).update(slack_message_sent=False) diff --git a/engine/apps/slack/tests/test_scenario_steps/test_distribute_alerts.py b/engine/apps/slack/tests/test_scenario_steps/test_distribute_alerts.py index bb8a39c5..1eba2b9a 100644 --- a/engine/apps/slack/tests/test_scenario_steps/test_distribute_alerts.py +++ b/engine/apps/slack/tests/test_scenario_steps/test_distribute_alerts.py @@ -5,6 +5,7 @@ import pytest from apps.alerts.models import AlertGroup from apps.slack.errors import SlackAPIRestrictedActionError from apps.slack.models import SlackMessage +from apps.slack.scenarios.distribute_alerts import AlertShootingStep from apps.slack.scenarios.scenario_step import ScenarioStep from apps.slack.tests.conftest import build_slack_response @@ -36,3 +37,30 @@ def test_restricted_action_error( assert alert_group.slack_message is None assert SlackMessage.objects.count() == 0 assert not alert.delivered + + +@patch.object(AlertShootingStep, "_post_alert_group_to_slack") +@pytest.mark.django_db +def test_alert_shooting_no_channel_filter( + mock_post_alert_group_to_slack, + make_slack_team_identity, + make_organization, + make_alert_receive_channel, + make_alert_group, + make_alert, +): + slack_team_identity = make_slack_team_identity() + organization = make_organization( + slack_team_identity=slack_team_identity, general_log_channel_id="DEFAULT_CHANNEL_ID" + ) + alert_receive_channel = make_alert_receive_channel(organization) + + # simulate an alert group with channel filter deleted in the middle of the escalation + alert_group = make_alert_group(alert_receive_channel, channel_filter=None) + alert = make_alert(alert_group, raw_request_data={}) + + step = AlertShootingStep(slack_team_identity, organization) + step.process_signal(alert) + + mock_post_alert_group_to_slack.assert_called_once() + assert mock_post_alert_group_to_slack.call_args[1]["channel_id"] == "DEFAULT_CHANNEL_ID"