From 4abfb205ef066cd6ca04a3a4141b308bf74c3dce Mon Sep 17 00:00:00 2001 From: Matias Bordese Date: Mon, 23 Sep 2024 15:01:59 -0300 Subject: [PATCH] Ignore resolve condition template if source resolving is disabled (#5049) Related to https://github.com/grafana/support-escalations/issues/12528 --- engine/apps/alerts/models/alert_group.py | 3 +- engine/apps/alerts/tests/test_alert_group.py | 38 ++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/engine/apps/alerts/models/alert_group.py b/engine/apps/alerts/models/alert_group.py index 44bdab903..489607a43 100644 --- a/engine/apps/alerts/models/alert_group.py +++ b/engine/apps/alerts/models/alert_group.py @@ -129,7 +129,8 @@ def get_or_create_grouping(self, channel, channel_filter, group_data, received_a pass # If it's an "OK" alert, try to return the latest resolved group - if group_data.is_resolve_signal: + # (only if the channel allows source base resolving and the alert is a resolve signal) + if channel.allow_source_based_resolving and group_data.is_resolve_signal: try: return self.filter(**search_params, resolved=True).latest(), False except self.model.DoesNotExist: diff --git a/engine/apps/alerts/tests/test_alert_group.py b/engine/apps/alerts/tests/test_alert_group.py index ae2aeceed..ddafe102f 100644 --- a/engine/apps/alerts/tests/test_alert_group.py +++ b/engine/apps/alerts/tests/test_alert_group.py @@ -1,3 +1,4 @@ +import hashlib from unittest.mock import call, patch import pytest @@ -758,3 +759,40 @@ def test_update_state_by_backsync( assert (last_log.action_source, last_log.author, last_log.step_specific_info) == expected_log_data assert last_log.type == to_firing_log_type mock_start_escalation_if_needed.assert_called_once() + + +@pytest.mark.django_db +def test_alert_group_created_if_resolve_condition_but_auto_resolving_disabled( + make_organization, + make_alert_receive_channel, + make_alert_group, +): + organization = make_organization() + # grouping condition will match. resolve condition will evaluate to True, but auto resolving is disabled + grouping_distinction = "abcdef" + alert_receive_channel = make_alert_receive_channel( + organization, + grouping_id_template=grouping_distinction, + resolve_condition_template="True", + allow_source_based_resolving=False, + ) + # existing alert group, resolved, with a matching grouping distinction + resolved_alert_group = make_alert_group( + alert_receive_channel, + resolved=True, + distinction=hashlib.md5(grouping_distinction.encode()).hexdigest(), + ) + + # an alert for the same integration is received + alert = Alert.create( + title="the title", + message="the message", + alert_receive_channel=alert_receive_channel, + raw_request_data={}, + integration_unique_data={}, + image_url=None, + link_to_upstream_details=None, + ) + + # the alert will create a new alert group + assert alert.group != resolved_alert_group