Add labels to alert group API responses (#3292)

# What this PR does

Adds a field to the internal API alert group serializer to display
labels.

## Which issue(s) this PR fixes

Related to https://github.com/grafana/oncall-private/issues/2179

## 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)
This commit is contained in:
Vadim Stepanov 2023-11-08 09:18:15 +00:00 committed by GitHub
parent a01cc8b277
commit 1997d41e5b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 0 deletions

View file

@ -56,6 +56,19 @@ class AlertGroupFieldsCacheSerializerMixin(AlertsFieldCacheBusterMixin):
return field
class AlertGroupLabelSerializer(serializers.Serializer):
class KeySerializer(serializers.Serializer):
id = serializers.CharField(source="key_name")
name = serializers.CharField(source="key_name")
class ValueSerializer(serializers.Serializer):
id = serializers.CharField(source="value_name")
name = serializers.CharField(source="value_name")
key = KeySerializer(source="*")
value = ValueSerializer(source="*")
class ShortAlertGroupSerializer(AlertGroupFieldsCacheSerializerMixin, serializers.ModelSerializer):
pk = serializers.CharField(read_only=True, source="public_primary_key")
alert_receive_channel = FastAlertReceiveChannelSerializer(source="channel")
@ -105,6 +118,8 @@ class AlertGroupListSerializer(EagerLoadingMixin, AlertGroupFieldsCacheSerialize
render_for_web = serializers.SerializerMethodField()
render_for_classic_markdown = serializers.SerializerMethodField()
labels = AlertGroupLabelSerializer(many=True, read_only=True)
PREFETCH_RELATED = [
"dependent_alert_groups",
"log_records__author",
@ -150,6 +165,7 @@ class AlertGroupListSerializer(EagerLoadingMixin, AlertGroupFieldsCacheSerialize
"declare_incident_link",
"team",
"grafana_incident_id",
"labels",
]
@extend_schema_field(

View file

@ -2121,3 +2121,24 @@ def test_delete(mock_delete_alert_group, make_user_auth_headers, alert_group_int
url = reverse("api-internal:alertgroup-detail", kwargs={"pk": "potato"})
response = client.delete(url, **auth_headers)
assert response.status_code == status.HTTP_404_NOT_FOUND
@pytest.mark.django_db
def test_alert_group_list_labels(
alert_group_internal_api_setup,
make_alert_group_label_association,
make_alert_receive_channel,
make_alert_group,
make_user_auth_headers,
):
user, token, alert_groups = alert_group_internal_api_setup
make_alert_group_label_association(user.organization, alert_groups[0], key_name="a", value_name="b")
client = APIClient()
url = reverse("api-internal:alertgroup-list")
response = client.get(url, **make_user_auth_headers(user, token))
assert response.status_code == status.HTTP_200_OK
assert response.json()["results"][-1]["labels"] == [
{"key": {"id": "a", "name": "a"}, "value": {"id": "b", "name": "b"}}
]