From bd142927b590f8b8ea2ec63efe1ac1155a6fce3a Mon Sep 17 00:00:00 2001 From: Matias Bordese Date: Mon, 29 May 2023 15:52:24 -0300 Subject: [PATCH] Update find contact point name, receiver could be missing key (#2046) Fixes issue when syncing contact points and there are receiver configs with no `grafana_managed_receiver_configs` key. (eg. `{"name": "autogen-contact-point-default"}`) --- CHANGELOG.md | 1 + .../grafana_alerting_sync.py | 2 +- .../tests/test_grafana_alerting_sync.py | 26 +++++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 engine/apps/alerts/tests/test_grafana_alerting_sync.py diff --git a/CHANGELOG.md b/CHANGELOG.md index b6554cdba6..a83f903d72 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Helm chart: fix bugs in helm chart with external postgresql configuration by @alexintech ([#2036](https://github.com/grafana/oncall/pull/2036)) - Properly address `Organization.DoesNotExist` exceptions thrown which result in HTTP 500 for the Slack `interactive_api_endpoint` endpoint by @joeyorlando ([#2040](https://github.com/grafana/oncall/pull/2040)) +- Fix issue when trying to sync Grafana contact point and config receivers miss a key ([#2046](https://github.com/grafana/oncall/pull/2046)) ### Changed diff --git a/engine/apps/alerts/grafana_alerting_sync_manager/grafana_alerting_sync.py b/engine/apps/alerts/grafana_alerting_sync_manager/grafana_alerting_sync.py index 1a70cb6d2a..6662a8ffea 100644 --- a/engine/apps/alerts/grafana_alerting_sync_manager/grafana_alerting_sync.py +++ b/engine/apps/alerts/grafana_alerting_sync_manager/grafana_alerting_sync.py @@ -540,7 +540,7 @@ def _find_name_of_contact_point_by_uid(self, contact_point_uid, receivers) -> st name_in_alerting = None # find name of contact point in alerting config by contact point uid for receiver in receivers: - receiver_configs = receiver["grafana_managed_receiver_configs"] + receiver_configs = receiver.get("grafana_managed_receiver_configs", []) for receiver_config in receiver_configs: if receiver_config["uid"] == contact_point_uid: name_in_alerting = receiver_config["name"] diff --git a/engine/apps/alerts/tests/test_grafana_alerting_sync.py b/engine/apps/alerts/tests/test_grafana_alerting_sync.py new file mode 100644 index 0000000000..d7c5f2cb57 --- /dev/null +++ b/engine/apps/alerts/tests/test_grafana_alerting_sync.py @@ -0,0 +1,26 @@ +import pytest + +from apps.alerts.grafana_alerting_sync_manager.grafana_alerting_sync import GrafanaAlertingSyncManager + + +@pytest.mark.django_db +def test_find_name_of_contact_point_grafana_datasource(make_organization, make_alert_receive_channel): + organization = make_organization() + alert_receive_channel = make_alert_receive_channel(organization) + sync_manager = GrafanaAlertingSyncManager(alert_receive_channel) + + receivers = [ + {"name": "autogen-contact-point-default"}, + { + "name": "testing", + "grafana_managed_receiver_configs": [ + { + "uid": "some-uid", + "name": "testing", + } + ], + }, + ] + + name = sync_manager.find_name_of_contact_point("some-uid", True, receivers) + assert name == "testing"