Skip to content

Commit dcfdd8c

Browse files
authored
Merge pull request #7 from GabrielSalla/slack-notification-edge-cases
Handle some edge cases with Slack notifications
2 parents 90419f8 + f64bb64 commit dcfdd8c

File tree

2 files changed

+142
-0
lines changed

2 files changed

+142
-0
lines changed

src/services/slack/slack_notification.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,9 @@ async def _send_mention(
335335

336336
async def _delete_mention(notification: Notification):
337337
"""Send a mention message to a message thread"""
338+
if notification.data is None:
339+
return
340+
338341
channel = notification.data.get("channel")
339342
mention_ts = notification.data.get("mention_ts")
340343

@@ -356,6 +359,12 @@ async def notification_mention(
356359
if notification_options.mention is None:
357360
return
358361

362+
if notification.data is None:
363+
return
364+
365+
if notification.data.get("ts") is None:
366+
return
367+
359368
# Check if should have a mention message
360369
if not _should_have_mention(alert, notification_options):
361370
await _delete_mention(notification)

tests/services/slack/test_slack_notification.py

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,29 @@ async def test_delete_mention_no_mention_ts(mocker, sample_monitor: Monitor):
897897
assert loaded_notification.data == {"channel": "channel", "ts": "33.55", "mention_ts": None}
898898

899899

900+
async def test_delete_mention_no_notification_data(mocker, sample_monitor: Monitor):
901+
"""'_delete_mention' should just return if the notification data is 'None'"""
902+
slack_delete_spy: MagicMock = mocker.spy(slack, "delete")
903+
904+
alert = await Alert.create(
905+
monitor_id=sample_monitor.id,
906+
)
907+
notification = await Notification.create(
908+
monitor_id=sample_monitor.id,
909+
alert_id=alert.id,
910+
target="slack",
911+
data=None,
912+
)
913+
914+
await slack_notification._delete_mention(notification=notification)
915+
916+
slack_delete_spy.assert_not_called()
917+
918+
loaded_notification = await Notification.get_by_id(notification.id)
919+
assert loaded_notification is not None
920+
assert loaded_notification.data is None
921+
922+
900923
async def test_delete_mention_none_mention_ts(mocker, sample_monitor: Monitor):
901924
"""'_delete_mention' should just return if the notification doesn't have a mention message"""
902925
slack_delete_spy: MagicMock = mocker.spy(slack, "delete")
@@ -976,6 +999,7 @@ async def test_notification_mention_no_mention(mocker, monkeypatch, sample_monit
976999
"""'notification_mention' should not send a mention message if the 'mention' parameter is not
9771000
set in the notification options"""
9781001
monkeypatch.setattr(slack_notification, "_should_have_mention", lambda *args: True)
1002+
delete_mention_spy: AsyncMock = mocker.spy(slack_notification, "_delete_mention")
9791003
send_mention_spy: AsyncMock = mocker.spy(slack_notification, "_send_mention")
9801004

9811005
alert = await Alert.create(
@@ -999,6 +1023,115 @@ async def test_notification_mention_no_mention(mocker, monkeypatch, sample_monit
9991023
sample_monitor, alert, notification, notification_options
10001024
)
10011025

1026+
delete_mention_spy.assert_not_called()
1027+
send_mention_spy.assert_not_called()
1028+
1029+
1030+
async def test_notification_mention_no_notification_data(
1031+
mocker,
1032+
monkeypatch,
1033+
sample_monitor: Monitor
1034+
):
1035+
"""'notification_mention' should not send a mention message if the notification data is 'None'
1036+
"""
1037+
monkeypatch.setattr(slack_notification, "_should_have_mention", lambda *args: True)
1038+
delete_mention_spy: AsyncMock = mocker.spy(slack_notification, "_delete_mention")
1039+
send_mention_spy: AsyncMock = mocker.spy(slack_notification, "_send_mention")
1040+
1041+
alert = await Alert.create(
1042+
monitor_id=sample_monitor.id,
1043+
)
1044+
notification_options = slack_notification.SlackNotification(
1045+
channel="channel",
1046+
title="title",
1047+
issues_fields=["col"],
1048+
mention="mention",
1049+
min_priority_to_mention=5,
1050+
)
1051+
notification = await Notification.create(
1052+
monitor_id=sample_monitor.id,
1053+
alert_id=alert.id,
1054+
target="slack",
1055+
data=None,
1056+
)
1057+
1058+
await slack_notification.notification_mention(
1059+
sample_monitor, alert, notification, notification_options
1060+
)
1061+
1062+
delete_mention_spy.assert_not_called()
1063+
send_mention_spy.assert_not_called()
1064+
1065+
1066+
async def test_notification_mention_notification_no_ts(
1067+
mocker,
1068+
monkeypatch,
1069+
sample_monitor: Monitor
1070+
):
1071+
"""'notification_mention' should not send a mention message if the notification doesn't have the
1072+
'ts' field"""
1073+
monkeypatch.setattr(slack_notification, "_should_have_mention", lambda *args: True)
1074+
delete_mention_spy: AsyncMock = mocker.spy(slack_notification, "_delete_mention")
1075+
send_mention_spy: AsyncMock = mocker.spy(slack_notification, "_send_mention")
1076+
1077+
alert = await Alert.create(
1078+
monitor_id=sample_monitor.id,
1079+
)
1080+
notification_options = slack_notification.SlackNotification(
1081+
channel="channel",
1082+
title="title",
1083+
issues_fields=["col"],
1084+
mention="mention",
1085+
min_priority_to_mention=5,
1086+
)
1087+
notification = await Notification.create(
1088+
monitor_id=sample_monitor.id,
1089+
alert_id=alert.id,
1090+
target="slack",
1091+
data={"channel": "channel"},
1092+
)
1093+
1094+
await slack_notification.notification_mention(
1095+
sample_monitor, alert, notification, notification_options
1096+
)
1097+
1098+
delete_mention_spy.assert_not_called()
1099+
send_mention_spy.assert_not_called()
1100+
1101+
1102+
async def test_notification_mention_notification_none_ts(
1103+
mocker,
1104+
monkeypatch,
1105+
sample_monitor: Monitor
1106+
):
1107+
"""'notification_mention' should not send a mention message if the notification doesn't have the
1108+
'ts' field"""
1109+
monkeypatch.setattr(slack_notification, "_should_have_mention", lambda *args: True)
1110+
delete_mention_spy: AsyncMock = mocker.spy(slack_notification, "_delete_mention")
1111+
send_mention_spy: AsyncMock = mocker.spy(slack_notification, "_send_mention")
1112+
1113+
alert = await Alert.create(
1114+
monitor_id=sample_monitor.id,
1115+
)
1116+
notification_options = slack_notification.SlackNotification(
1117+
channel="channel",
1118+
title="title",
1119+
issues_fields=["col"],
1120+
mention="mention",
1121+
min_priority_to_mention=5,
1122+
)
1123+
notification = await Notification.create(
1124+
monitor_id=sample_monitor.id,
1125+
alert_id=alert.id,
1126+
target="slack",
1127+
data={"channel": "channel", "ts": None},
1128+
)
1129+
1130+
await slack_notification.notification_mention(
1131+
sample_monitor, alert, notification, notification_options
1132+
)
1133+
1134+
delete_mention_spy.assert_not_called()
10021135
send_mention_spy.assert_not_called()
10031136

10041137

0 commit comments

Comments
 (0)