From f38e79f41f7ec98c8a966a7125aef08717cd1c4d Mon Sep 17 00:00:00 2001 From: Matias Bordese Date: Thu, 7 Jul 2022 16:36:04 -0300 Subject: [PATCH] Limit number of slack messages in resolution notes popup --- .../apps/slack/scenarios/resolution_note.py | 25 +++++- .../test_resolution_note.py | 77 +++++++++++++++++++ 2 files changed, 100 insertions(+), 2 deletions(-) diff --git a/engine/apps/slack/scenarios/resolution_note.py b/engine/apps/slack/scenarios/resolution_note.py index a82ccb69..f8bea2c3 100644 --- a/engine/apps/slack/scenarios/resolution_note.py +++ b/engine/apps/slack/scenarios/resolution_note.py @@ -237,6 +237,7 @@ class ResolutionNoteModalStep(CheckAlertIsUnarchivedMixin, scenario_step.Scenari ] RESOLUTION_NOTE_TEXT_BLOCK_ID = "resolution_note_text" + RESOLUTION_NOTE_MESSAGES_MAX_COUNT = 25 def process_scenario(self, slack_user_identity, slack_team_identity, payload, action=None, data=None): AlertGroup = apps.get_model("alerts", "AlertGroup") @@ -299,7 +300,27 @@ class ResolutionNoteModalStep(CheckAlertIsUnarchivedMixin, scenario_step.Scenari blocks = [] other_resolution_notes = alert_group.resolution_notes.filter(~Q(source=ResolutionNote.Source.SLACK)) - resolution_note_slack_messages = alert_group.resolution_note_slack_messages.filter(posted_by_bot=False) + resolution_note_slack_messages = alert_group.resolution_note_slack_messages.filter( + posted_by_bot=False + ).order_by("-pk") + if resolution_note_slack_messages.count() > self.RESOLUTION_NOTE_MESSAGES_MAX_COUNT: + blocks.extend( + [ + { + "type": "divider", + }, + { + "type": "section", + "text": { + "type": "mrkdwn", + "text": ( + ":warning: Listing up to last {} thread messages, " + "you can still add any other message using contextual menu actions." + ).format(self.RESOLUTION_NOTE_MESSAGES_MAX_COUNT), + }, + }, + ] + ) if action_resolve: blocks.extend( [ @@ -333,7 +354,7 @@ class ResolutionNoteModalStep(CheckAlertIsUnarchivedMixin, scenario_step.Scenari ] ) - for message in resolution_note_slack_messages: + for message in resolution_note_slack_messages[: self.RESOLUTION_NOTE_MESSAGES_MAX_COUNT]: user_verbal = message.user.get_user_verbal_for_team_for_slack(mention=True) blocks.append( { diff --git a/engine/apps/slack/tests/test_scenario_steps/test_resolution_note.py b/engine/apps/slack/tests/test_scenario_steps/test_resolution_note.py index 080f5d40..edea824e 100644 --- a/engine/apps/slack/tests/test_scenario_steps/test_resolution_note.py +++ b/engine/apps/slack/tests/test_scenario_steps/test_resolution_note.py @@ -101,3 +101,80 @@ def test_get_resolution_notes_blocks_non_empty( ] assert blocks == expected_blocks + + +@pytest.mark.django_db +def test_get_resolution_notes_blocks_latest_limit( + make_organization_and_user_with_slack_identities, + make_alert_receive_channel, + make_alert_group, + make_resolution_note_slack_message, +): + SlackResolutionNoteModalStep = ScenarioStep.get_step("resolution_note", "ResolutionNoteModalStep") + organization, user, slack_team_identity, _ = make_organization_and_user_with_slack_identities() + step = SlackResolutionNoteModalStep(slack_team_identity) + + alert_receive_channel = make_alert_receive_channel(organization) + alert_group = make_alert_group(alert_receive_channel) + + max_count = SlackResolutionNoteModalStep.RESOLUTION_NOTE_MESSAGES_MAX_COUNT + messages = [ + make_resolution_note_slack_message(alert_group=alert_group, user=user, added_by_user=user, ts=i, text=i) + for i in range(max_count * 2) + ] + + blocks = step.get_resolution_notes_blocks(alert_group, "", False) + + expected_blocks = [ + { + "type": "divider", + }, + { + "type": "section", + "text": { + "type": "mrkdwn", + "text": ( + ":warning: Listing up to last {} thread messages, " + "you can still add any other message using contextual menu actions." + ).format(max_count), + }, + }, + ] + for m in list(reversed(messages))[:max_count]: + expected_blocks += [ + { + "type": "divider", + }, + { + "type": "section", + "text": { + "type": "mrkdwn", + "text": "{} \n{}".format( + m.user.get_user_verbal_for_team_for_slack(mention=True), + float(m.ts), + m.text, + ), + }, + "accessory": { + "type": "button", + "style": "primary", + "text": { + "type": "plain_text", + "text": "Add", + "emoji": True, + }, + "action_id": "AddRemoveThreadMessageStep", + "value": json.dumps( + { + "resolution_note_window_action": "edit", + "msg_value": "add", + "message_pk": m.pk, + "resolution_note_pk": None, + "alert_group_pk": alert_group.pk, + } + ), + }, + }, + ] + + assert blocks == expected_blocks