Skip to content

Commit

Permalink
Quickfix for getting alert group from slack message payload (#3195)
Browse files Browse the repository at this point in the history
# What this PR does

## Which issue(s) this PR fixes
grafana/oncall-private#2217

## Checklist

- [ ] Unit, integration, and e2e (if applicable) tests updated
- [ ] Documentation added (or `pr:no public docs` PR label added if not
required)
- [ ] `CHANGELOG.md` updated (or `pr:no changelog` PR label added if not
required)
  • Loading branch information
Ferril authored Oct 25, 2023
1 parent a3aeb0e commit f239fd7
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,8 @@ def _alert_group_action_value(self, **kwargs):
data = {
"organization_id": self.alert_group.channel.organization_id,
"alert_group_pk": self.alert_group.pk,
# eventually replace using alert_group_pk with alert_group_public_pk in slack payload
"alert_group_ppk": self.alert_group.public_primary_key,
**kwargs,
}
return json.dumps(data) # Slack block elements allow to pass value as string only (max 2000 chars)
34 changes: 32 additions & 2 deletions engine/apps/slack/scenarios/step_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,27 @@ def _get_alert_group_from_action(self, payload: EventPayload) -> AlertGroup | No
except (TypeError, json.JSONDecodeError):
return None

# New slack messages from OnCall contain alert group primary key
try:
alert_group_ppk = value["alert_group_ppk"]
return AlertGroup.objects.get(public_primary_key=alert_group_ppk)
except (KeyError, TypeError):
pass

try:
alert_group_pk = value["alert_group_pk"]
organization_pk = value["organization_id"]
except (KeyError, TypeError):
return None

return AlertGroup.objects.get(pk=alert_group_pk)
try:
# check organization as well for cases when the organization was migrated from "us" cluster to "eu" and
# slack message has an outdated payload with incorrect alert group id
alert_group = AlertGroup.objects.get(pk=alert_group_pk, channel__organization_id=organization_pk)
except AlertGroup.DoesNotExist:
return None

return alert_group

def _get_alert_group_from_message(self, payload: EventPayload) -> AlertGroup | None:
"""
Expand All @@ -128,12 +143,27 @@ def _get_alert_group_from_message(self, payload: EventPayload) -> AlertGroup | N
except (TypeError, json.JSONDecodeError):
continue

# New slack messages from OnCall contain alert group primary key
try:
alert_group_ppk = value["alert_group_ppk"]
return AlertGroup.objects.get(public_primary_key=alert_group_ppk)
except (KeyError, TypeError):
pass

try:
alert_group_pk = value["alert_group_pk"]
organization_pk = value["organization_id"]
except (KeyError, TypeError):
continue

return AlertGroup.objects.get(pk=alert_group_pk)
try:
# check the organization as well for cases organization was migrated from "us" cluster to "eu" and
# the slack message has an outdated payload with incorrect alert group id
alert_group = AlertGroup.objects.get(pk=alert_group_pk, channel__organization_id=organization_pk)
except AlertGroup.DoesNotExist:
return None

return alert_group
return None

def _get_alert_group_from_slack_message_in_db(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,21 @@ def test_get_alert_group_from_message(
],
"message": {
"ts": "RANDOM_MESSAGE_TS",
"attachments": [{"blocks": [{"elements": [{"value": json.dumps({"alert_group_pk": alert_group.pk})}]}]}],
"attachments": [
{
"blocks": [
{
"elements": [
{
"value": json.dumps(
{"alert_group_pk": alert_group.pk, "organization_id": organization.pk}
)
}
]
}
]
}
],
},
"channel": {"id": "RANDOM_CHANNEL_ID"},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,16 @@ def manage_responders_setup(

@pytest.mark.django_db
def test_initial_state(manage_responders_setup):
organization, user, slack_team_identity, slack_user_identity = manage_responders_setup
payload = {
"trigger_id": TRIGGER_ID,
"actions": [
{
"type": "button",
"value": json.dumps({"organization_id": ORGANIZATION_ID, "alert_group_pk": ALERT_GROUP_ID}),
"value": json.dumps({"organization_id": organization.pk, "alert_group_pk": ALERT_GROUP_ID}),
}
],
}

organization, user, slack_team_identity, slack_user_identity = manage_responders_setup

step = StartManageResponders(slack_team_identity, organization, user)
with patch.object(step._slack_client, "views_open") as mock_slack_api_call:
step.process_scenario(slack_user_identity, slack_team_identity, payload)
Expand Down
42 changes: 36 additions & 6 deletions engine/apps/slack/tests/test_slack_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ def test_slack_renderer_acknowledge_button(make_organization, make_alert_receive

button = elements[0]
assert button["text"]["text"] == "Acknowledge"
assert json.loads(button["value"]) == {"organization_id": organization.pk, "alert_group_pk": alert_group.pk}
assert json.loads(button["value"]) == {
"organization_id": organization.pk,
"alert_group_pk": alert_group.pk,
"alert_group_ppk": alert_group.public_primary_key,
}


@pytest.mark.django_db
Expand All @@ -33,7 +37,11 @@ def test_slack_renderer_unacknowledge_button(

button = elements[0]
assert button["text"]["text"] == "Unacknowledge"
assert json.loads(button["value"]) == {"organization_id": organization.pk, "alert_group_pk": alert_group.pk}
assert json.loads(button["value"]) == {
"organization_id": organization.pk,
"alert_group_pk": alert_group.pk,
"alert_group_ppk": alert_group.public_primary_key,
}


@pytest.mark.django_db
Expand All @@ -47,7 +55,11 @@ def test_slack_renderer_resolve_button(make_organization, make_alert_receive_cha

button = elements[1]
assert button["text"]["text"] == "Resolve"
assert json.loads(button["value"]) == {"organization_id": organization.pk, "alert_group_pk": alert_group.pk}
assert json.loads(button["value"]) == {
"organization_id": organization.pk,
"alert_group_pk": alert_group.pk,
"alert_group_ppk": alert_group.public_primary_key,
}


@pytest.mark.django_db
Expand All @@ -61,7 +73,11 @@ def test_slack_renderer_unresolve_button(make_organization, make_alert_receive_c

button = elements[0]
assert button["text"]["text"] == "Unresolve"
assert json.loads(button["value"]) == {"organization_id": organization.pk, "alert_group_pk": alert_group.pk}
assert json.loads(button["value"]) == {
"organization_id": organization.pk,
"alert_group_pk": alert_group.pk,
"alert_group_ppk": alert_group.public_primary_key,
}


@pytest.mark.django_db
Expand Down Expand Up @@ -96,6 +112,7 @@ def test_slack_renderer_stop_invite_button(
assert json.loads(action["value"]) == {
"organization_id": organization.pk,
"alert_group_pk": alert_group.pk,
"alert_group_ppk": alert_group.public_primary_key,
"invitation_id": invitation.pk,
}

Expand All @@ -114,7 +131,12 @@ def test_slack_renderer_silence_button(make_organization, make_alert_receive_cha

values = [json.loads(option["value"]) for option in button["options"]]
assert values == [
{"organization_id": organization.pk, "alert_group_pk": alert_group.pk, "delay": delay}
{
"organization_id": organization.pk,
"alert_group_pk": alert_group.pk,
"alert_group_ppk": alert_group.public_primary_key,
"delay": delay,
}
for delay, _ in AlertGroup.SILENCE_DELAY_OPTIONS
]

Expand All @@ -133,6 +155,7 @@ def test_slack_renderer_unsilence_button(make_organization, make_alert_receive_c
assert json.loads(button["value"]) == {
"organization_id": organization.pk,
"alert_group_pk": alert_group.pk,
"alert_group_ppk": alert_group.public_primary_key,
}


Expand All @@ -150,6 +173,7 @@ def test_slack_renderer_attach_button(make_organization, make_alert_receive_chan
assert json.loads(button["value"]) == {
"organization_id": organization.pk,
"alert_group_pk": alert_group.pk,
"alert_group_ppk": alert_group.public_primary_key,
}


Expand All @@ -170,6 +194,7 @@ def test_slack_renderer_unattach_button(make_organization, make_alert_receive_ch
assert json.loads(action["value"]) == {
"organization_id": organization.pk,
"alert_group_pk": alert_group.pk,
"alert_group_ppk": alert_group.public_primary_key,
}


Expand All @@ -186,7 +211,11 @@ def test_slack_renderer_format_alert_button(

button = elements[5]
assert button["text"]["text"] == ":mag: Format Alert"
assert json.loads(button["value"]) == {"organization_id": organization.pk, "alert_group_pk": alert_group.pk}
assert json.loads(button["value"]) == {
"organization_id": organization.pk,
"alert_group_pk": alert_group.pk,
"alert_group_ppk": alert_group.public_primary_key,
}


@pytest.mark.django_db
Expand All @@ -205,5 +234,6 @@ def test_slack_renderer_resolution_notes_button(
assert json.loads(button["value"]) == {
"organization_id": organization.pk,
"alert_group_pk": alert_group.pk,
"alert_group_ppk": alert_group.public_primary_key,
"resolution_note_window_action": "edit",
}

0 comments on commit f239fd7

Please sign in to comment.