From 5326d945e0ab947c94577d3c4bd4bb8dca468ccd Mon Sep 17 00:00:00 2001 From: Matias Bordese Date: Tue, 5 Mar 2024 14:11:47 -0300 Subject: [PATCH] Allow setting integration_filter to null in webhooks internal API (#4011) Fixes https://github.com/grafana/oncall/issues/4006 --- CHANGELOG.md | 1 + engine/apps/api/serializers/webhook.py | 12 ++++++++---- engine/apps/api/tests/test_webhooks.py | 18 ++++++++++++++++++ 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9a703813..20fb0aef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - Fix selecting values in notification settings ([#9566](https://github.com/grafana/support-escalations/issues/9566)) +- Allow setting integration_filter to null in webhooks internal API ([#4011](https://github.com/grafana/oncall/pull/4011)) ## v1.3.109 (2024-03-04) diff --git a/engine/apps/api/serializers/webhook.py b/engine/apps/api/serializers/webhook.py index f95a29f6..227271d0 100644 --- a/engine/apps/api/serializers/webhook.py +++ b/engine/apps/api/serializers/webhook.py @@ -117,6 +117,10 @@ class WebhookSerializer(LabelsSerializerMixin, serializers.ModelSerializer): data["password"] = webhook.password if data.get("authorization_header") == WEBHOOK_FIELD_PLACEHOLDER: data["authorization_header"] = webhook.authorization_header + + if not data.get("integration_filter"): + data["integration_filter"] = [] + return super().to_internal_value(data) def _validate_template_field(self, template): @@ -185,13 +189,13 @@ class WebhookSerializer(LabelsSerializerMixin, serializers.ModelSerializer): for controlled_field in preset_metadata.controlled_fields: if controlled_field in self.initial_data: if self.instance: - if self.initial_data[controlled_field] is not None and self.initial_data[ - controlled_field - ] != getattr(self.instance, controlled_field): + if bool(self.initial_data[controlled_field]) and self.initial_data[controlled_field] != getattr( + self.instance, controlled_field + ): raise serializers.ValidationError( detail=f"{controlled_field} is controlled by preset, cannot update" ) - elif self.initial_data[controlled_field] is not None: + elif bool(self.initial_data[controlled_field]): raise serializers.ValidationError( detail=f"{controlled_field} is controlled by preset, cannot create" ) diff --git a/engine/apps/api/tests/test_webhooks.py b/engine/apps/api/tests/test_webhooks.py index 28cb3ad0..2f96ab68 100644 --- a/engine/apps/api/tests/test_webhooks.py +++ b/engine/apps/api/tests/test_webhooks.py @@ -354,6 +354,24 @@ def test_webhook_integration_filter(webhook_internal_api_setup, make_alert_recei assert list(webhook.filtered_integrations.all()) == [] assert response.json()["integration_filter"] == [] + # clear filter also works if set to None + url = reverse("api-internal:webhooks-detail", kwargs={"pk": webhook.public_primary_key}) + data = { + "name": "github_button_updated", + "url": "https://github.com/", + "trigger_type": Webhook.TRIGGER_ALERT_GROUP_CREATED, + "http_method": "POST", + "team": None, + "integration_filter": None, + } + response = client.put( + url, data=json.dumps(data), content_type="application/json", **make_user_auth_headers(user, token) + ) + webhook.refresh_from_db() + assert response.status_code == status.HTTP_200_OK + assert list(webhook.filtered_integrations.all()) == [] + assert response.json()["integration_filter"] == [] + @pytest.mark.django_db @pytest.mark.parametrize(