Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix build escalation snapshot #2954

Merged
merged 4 commits into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

- Fix for Cloud plugin install not refreshing page after completion ([2974](https://github.com/grafana/oncall/issues/2874))
- Fix escalation snapshot building if user was deleted @Ferril ([#2954](https://github.com/grafana/oncall/pull/2954))

## v1.3.30 (2023-08-31)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class EscalationPolicySnapshotSerializer(serializers.ModelSerializer):
num_alerts_in_window = serializers.IntegerField(allow_null=True, default=None)
num_minutes_in_window = serializers.IntegerField(allow_null=True, default=None)
pause_escalation = serializers.BooleanField(default=False)
last_notified_user = PrimaryKeyRelatedFieldWithNoneValue(allow_null=True, queryset=User.objects)

class Meta:
model = EscalationPolicy
Expand Down
52 changes: 52 additions & 0 deletions engine/apps/alerts/tests/test_escalation_snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,3 +266,55 @@ def test_escalation_snapshot_non_sequential_orders(

policy_ids = [p.id for p in escalation_snapshot.executed_escalation_policy_snapshots]
assert policy_ids == [step_1.id, step_2.id]


@pytest.mark.django_db
def test_serialize_escalation_snapshot_with_deleted_user(
make_organization_and_user,
make_user_for_organization,
make_alert_receive_channel,
make_channel_filter,
make_escalation_chain,
make_escalation_policy,
make_alert_group,
):
organization, user = make_organization_and_user()
alert_receive_channel = make_alert_receive_channel(organization)
escalation_chain = make_escalation_chain(organization)
channel_filter = make_channel_filter(
alert_receive_channel,
escalation_chain=escalation_chain,
notification_backends={"BACKEND": {"channel_id": "abc123"}},
)

notify_users_queue = make_escalation_policy(
escalation_chain=channel_filter.escalation_chain,
escalation_policy_step=EscalationPolicy.STEP_NOTIFY_USERS_QUEUE,
last_notified_user=user,
)
notify_users_queue.notify_to_users_queue.set([user])

alert_group = make_alert_group(alert_receive_channel, channel_filter=channel_filter)
alert_group.raw_escalation_snapshot = alert_group.build_raw_escalation_snapshot()
alert_group.save()
escalation_snapshot = alert_group.escalation_snapshot

assert notify_users_queue.last_notified_user == user
assert escalation_snapshot.escalation_policies_snapshots[0].last_notified_user == user
assert len(escalation_snapshot.escalation_policies_snapshots[0].notify_to_users_queue) == 1

# delete user
user.is_active = None
user.save()

alert_group.raw_escalation_snapshot = alert_group.build_raw_escalation_snapshot()
# clear cached_property
del alert_group.escalation_snapshot
alert_group.save()

escalation_snapshot = alert_group.escalation_snapshot

assert notify_users_queue.last_notified_user == user
assert escalation_snapshot is not None
assert escalation_snapshot.escalation_policies_snapshots[0].last_notified_user is None
assert len(escalation_snapshot.escalation_policies_snapshots[0].notify_to_users_queue) == 0