Allow setting integration_filter to null in webhooks internal API (#4011)

Fixes https://github.com/grafana/oncall/issues/4006
This commit is contained in:
Matias Bordese 2024-03-05 14:11:47 -03:00 committed by GitHub
parent e5ab057490
commit 5326d945e0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 27 additions and 4 deletions

View file

@ -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)

View file

@ -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"
)

View file

@ -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(