Skip to content

Commit

Permalink
Return alert group ID in direct paging API (#1241)
Browse files Browse the repository at this point in the history
# What this PR does
Make direct paging internal API endpoint return an alert group ID.

## Which issue(s) this PR fixes
Related to #823

## Checklist

- [x] Tests updated
  • Loading branch information
vstpme authored Jan 30, 2023
1 parent 8609f41 commit f80271a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
6 changes: 4 additions & 2 deletions engine/apps/alerts/paging.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any
from typing import Any, Optional
from uuid import uuid4

from django.db import transaction
Expand Down Expand Up @@ -142,7 +142,7 @@ def direct_paging(
schedules: ScheduleNotifications = None,
escalation_chain: EscalationChain = None,
alert_group: AlertGroup = None,
) -> None:
) -> Optional[AlertGroup]:
"""Trigger escalation targeting given users/schedules.
If an alert group is given, update escalation to include the specified users.
Expand Down Expand Up @@ -185,6 +185,8 @@ def direct_paging(
)
notify_user_task.apply_async((u.pk, alert_group.pk), {"important": important})

return alert_group


def unpage_user(alert_group: AlertGroup, user: User, from_user: User) -> None:
"""Remove user from alert group escalation."""
Expand Down
13 changes: 13 additions & 0 deletions engine/apps/alerts/tests/test_paging.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,19 @@ def test_direct_paging_custom_chain(
assert ag.escalation_chain_with_respect_to_escalation_snapshot == custom_chain


@pytest.mark.django_db
def test_direct_paging_returns_alert_group(make_organization, make_user_for_organization):
organization = make_organization()
user = make_user_for_organization(organization)
from_user = make_user_for_organization(organization)

with patch("apps.alerts.paging.notify_user_task"):
alert_group = direct_paging(organization, None, from_user, title="Help!", message="Fire", users=[(user, False)])

# check alert group returned by direct paging is the same as the one created
assert alert_group == AlertGroup.all_objects.get()


@pytest.mark.django_db
def test_unpage_user_not_exists(
make_organization, make_user_for_organization, make_alert_receive_channel, make_alert_group
Expand Down
2 changes: 2 additions & 0 deletions engine/apps/api/tests/test_paging.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def test_direct_paging_new_alert_group(
)

assert response.status_code == status.HTTP_200_OK
assert "alert_group_id" in response.json()


@pytest.mark.django_db
Expand Down Expand Up @@ -104,6 +105,7 @@ def test_direct_paging_existing_alert_group(
)

assert response.status_code == status.HTTP_200_OK
assert response.json()["alert_group_id"] == alert_group.public_primary_key


@pytest.mark.django_db
Expand Down
4 changes: 2 additions & 2 deletions engine/apps/api/views/paging.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def post(self, request):
(schedule["instance"], schedule["important"]) for schedule in serializer.validated_data["schedules"]
]

direct_paging(
alert_group = direct_paging(
organization=organization,
team=team,
from_user=from_user,
Expand All @@ -42,4 +42,4 @@ def post(self, request):
alert_group=serializer.validated_data["alert_group"],
)

return Response(status=status.HTTP_200_OK)
return Response(data={"alert_group_id": alert_group.public_primary_key}, status=status.HTTP_200_OK)

0 comments on commit f80271a

Please sign in to comment.