Skip to content

Commit 427e392

Browse files
authored
Merge pull request #85 from GabrielSalla/slack-add-mention-on-update
Slack: add mention on update option
2 parents 766644b + ee4404f commit 427e392

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

docs/plugins/slack.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,11 @@ Parameters:
7070
- `channel`: The Slack channel where notifications will be sent.
7171
- `title`: A title for the notification to help users to identify the problem.
7272
- `issues_fields`: A list of fields from the issue data to include in the notification.
73+
- `min_priority_to_send`: Minimum alert priority that triggers a notification. Notifications will be sent if the alert is not acknowledged at the current priority level and it's is greater than or equal to this setting. Defaults to `low` (P4).
7374
- `mention`: Slack user or group to mention if the alert reaches a specified priority. Provide the Slack identifier for a user (e.g., `U0011223344`) or a group (e.g., `G0011223344`). Set to `None` to avoid mentioning anyone. Defaults to `None`.
7475
- `min_priority_to_mention`: Minimum alert priority that triggers a mention. Mentions will occur if the alert is not acknowledged at the current priority level and it's is greater than or equal to this setting. Defaults to `moderate` (P3).
75-
- `issue_show_limit`: Maximum number of issues to show in the notification. If the limit is reached, the message `XXX more...` will be shown at the and of the issues list, where `XXX` is the number of issues that are not being shown. Defaults to 10.
76+
- `mention_on_update`: If set to 'False', the mention will be sent when the alert becomes unacknowledged and the priority is greater than or equal to the minimum priority to mention. If the alert is updated and the alert continues to be unacknowledged, the mention will persist. When set to 'True', the mention will be deleted and sent again every time alert is updated, if the alert is not acknowledged and the priority is greater than or equal to the minimum priority to mention. This option can be used as a renotification. Defaults to `False`.
77+
- `issue_show_limit`: Maximum number of issues to show in the notification. If the limit is reached, the message `XXX more...` will be shown at the and of the issues list, where `XXX` is the number of issues not being shown. Defaults to 10.
7678

7779
The Slack message will show the alert and its issues information. The notification will persist and will be updated until the alert is detected as solved, even if its priority falls to P5.
7880

src/plugins/slack/notifications/slack_notification.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,32 @@ class SlackNotification:
4040
- `channel`: The Slack channel where notifications will be sent (e.g., `C0011223344`).
4141
- `title`: A title for the notification to help users to identify the problem.
4242
- `issues_fields`: A list of fields from the issue data to include in the notification.
43+
- `min_priority_to_send`: Minimum alert priority that triggers a notification. Notifications
44+
will be sent if the alert is not acknowledged at the current priority level and it's greater
45+
than or equal to this setting. Defaults to `low` (P4).
4346
- `mention`: Slack user or group to mention if the alert reaches a specified priority. Provide
4447
the Slack identifier for a user (e.g., `U0011223344`) or a group (e.g., `G0011223344`). Set to
4548
`None` to avoid mentioning anyone. Defaults to `None`.
4649
- `min_priority_to_mention`: Minimum alert priority that triggers a mention. Mentions will
4750
occur if the alert is not acknowledged at the current priority level and it's is greater than
4851
or equal to this setting. Defaults to `moderate` (P3).
52+
- `mention_on_update`: If set to 'False', the mention will be sent when the alert becomes
53+
unacknowledged and the priority is greater than or equal to the minimum priority to mention. If
54+
the alert is updated and the alert continues to be unacknowledged, the mention will persist.
55+
When set to 'True', the mention will be deleted and sent again every time alert is updated, if
56+
the alert is not acknowledged and the priority is greater than or equal to the minimum priority
57+
to mention. This option can be used as a renotification. Defaults to `False`.
58+
- `issue_show_limit`: Maximum number of issues to show in the notification. If the limit is
59+
reached, the message `XXX more...` will be shown at the and of the issues list, where `XXX` is
60+
the number of issues not being shown. Defaults to `10`.
4961
"""
5062

5163
channel: str
5264
title: str
5365
issues_fields: list[str]
5466
min_priority_to_send: int = AlertPriority.low
5567
mention: str | None = None
68+
mention_on_update: bool = False
5669
min_priority_to_mention: int = AlertPriority.moderate
5770
issue_show_limit: int = 10
5871

@@ -372,7 +385,11 @@ async def notification_mention(
372385
await _delete_mention(notification)
373386
return
374387

375-
# If already has a mention message, just return
388+
# If the mention should be sent on update, delete the previous mention message
389+
if notification_options.mention_on_update:
390+
await _delete_mention(notification)
391+
392+
# If the mention message already exists, do not send it again
376393
if notification.data.get("mention_ts") is not None:
377394
return
378395

tests/plugins/slack/notifications/test_slack_notification.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,6 +1196,42 @@ async def test_notification_mention_already_sent(mocker, monkeypatch, sample_mon
11961196
send_mention_spy.assert_not_called()
11971197

11981198

1199+
async def test_notification_mention_mention_on_update(mocker, monkeypatch, sample_monitor: Monitor):
1200+
"""'notification_mention' should resend a mention message if the notification already has
1201+
a notification mention message and the option 'mention_on_update' is set to 'True'"""
1202+
monkeypatch.setattr(slack_mock, "response_ts", "123123")
1203+
monkeypatch.setattr(slack_notification, "_should_have_mention", lambda *args: True)
1204+
delete_mention_spy: AsyncMock = mocker.spy(slack_notification, "_delete_mention")
1205+
send_mention_spy: AsyncMock = mocker.spy(slack_notification, "_send_mention")
1206+
1207+
alert = await Alert.create(
1208+
monitor_id=sample_monitor.id,
1209+
)
1210+
notification_options = slack_notification.SlackNotification(
1211+
channel="channel",
1212+
title="title",
1213+
issues_fields=["col"],
1214+
mention="mention",
1215+
min_priority_to_mention=5,
1216+
mention_on_update=True,
1217+
)
1218+
notification = await Notification.create(
1219+
monitor_id=sample_monitor.id,
1220+
alert_id=alert.id,
1221+
target="slack",
1222+
data={"channel": "channel", "ts": "33.55", "mention_ts": "11.22"},
1223+
)
1224+
1225+
await slack_notification.notification_mention(
1226+
sample_monitor, alert, notification, notification_options
1227+
)
1228+
1229+
delete_mention_spy.assert_awaited_once()
1230+
send_mention_spy.assert_awaited_once()
1231+
await notification.refresh()
1232+
assert notification.data == {"channel": "channel", "ts": "33.55", "mention_ts": "123123"}
1233+
1234+
11991235
async def test_handle_slack_notification_no_alert(mocker):
12001236
"""'_handle_slack_notification' should just return if couldn't find an alert with the provided
12011237
id"""

0 commit comments

Comments
 (0)