From 3b90c6544b399ff4e28010409837b1b5ca9bdbc3 Mon Sep 17 00:00:00 2001 From: Matias Bordese Date: Mon, 20 Nov 2023 09:17:07 -0300 Subject: [PATCH] Avoid msg_too_long errors when posting/updating slack resolution note (#3372) --- .../apps/slack/scenarios/resolution_note.py | 6 +- .../test_resolution_note.py | 72 +++++++++++++++++++ 2 files changed, 76 insertions(+), 2 deletions(-) diff --git a/engine/apps/slack/scenarios/resolution_note.py b/engine/apps/slack/scenarios/resolution_note.py index 8a0f9c5a..af291729 100644 --- a/engine/apps/slack/scenarios/resolution_note.py +++ b/engine/apps/slack/scenarios/resolution_note.py @@ -222,11 +222,12 @@ class UpdateResolutionNoteStep(scenario_step.ScenarioStep): blocks = self.get_resolution_note_blocks(resolution_note) if resolution_note_slack_message is None: + resolution_note_text = Truncator(resolution_note.text) try: result = self._slack_client.chat_postMessage( channel=alert_group_slack_message.channel_id, thread_ts=alert_group_slack_message.slack_id, - text=resolution_note.text, + text=resolution_note_text.chars(BLOCK_SECTION_TEXT_MAX_SIZE), blocks=blocks, ) except RESOLUTION_NOTE_EXCEPTIONS: @@ -256,11 +257,12 @@ class UpdateResolutionNoteStep(scenario_step.ScenarioStep): resolution_note.resolution_note_slack_message = resolution_note_slack_message resolution_note.save(update_fields=["resolution_note_slack_message"]) elif resolution_note_slack_message.posted_by_bot: + resolution_note_text = Truncator(resolution_note_slack_message.text) try: self._slack_client.chat_update( channel=alert_group_slack_message.channel_id, ts=resolution_note_slack_message.ts, - text=resolution_note_slack_message.text, + text=resolution_note_text.chars(BLOCK_SECTION_TEXT_MAX_SIZE), blocks=blocks, ) except RESOLUTION_NOTE_EXCEPTIONS: 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 a64d0208..1b4a87e6 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 @@ -148,6 +148,78 @@ def test_get_resolution_note_blocks_truncate_text( assert blocks == expected_blocks +@pytest.mark.django_db +def test_post_or_update_resolution_note_in_thread_truncate_message_text( + make_organization_and_user_with_slack_identities, + make_alert_receive_channel, + make_alert_group, + make_slack_message, + make_resolution_note, +): + UpdateResolutionNoteStep = ScenarioStep.get_step("resolution_note", "UpdateResolutionNoteStep") + organization, user, slack_team_identity, _ = make_organization_and_user_with_slack_identities() + step = UpdateResolutionNoteStep(slack_team_identity) + + alert_receive_channel = make_alert_receive_channel(organization) + alert_group = make_alert_group(alert_receive_channel) + make_slack_message(alert_group=alert_group, channel_id="RANDOM_CHANNEL_ID", slack_id="RANDOM_MESSAGE_ID") + resolution_note = make_resolution_note(alert_group=alert_group, author=user, message_text="a" * 3000) + + with patch("apps.slack.client.SlackClient.api_call") as mock_slack_api_call: + mock_slack_api_call.return_value = { + "ts": "timestamp", + "message": {"ts": "timestamp"}, + "permalink": "https://link.to.message", + } + step.post_or_update_resolution_note_in_thread(resolution_note) + + assert mock_slack_api_call.called + post_message_call = mock_slack_api_call.mock_calls[0] + assert post_message_call.args[0] == "chat.postMessage" + assert post_message_call.kwargs["json"]["text"] == resolution_note.text[: BLOCK_SECTION_TEXT_MAX_SIZE - 1] + "…" + + +@pytest.mark.django_db +def test_post_or_update_resolution_note_in_thread_update_truncate_message_text( + make_organization_and_user_with_slack_identities, + make_alert_receive_channel, + make_alert_group, + make_slack_message, + make_resolution_note, + make_resolution_note_slack_message, +): + UpdateResolutionNoteStep = ScenarioStep.get_step("resolution_note", "UpdateResolutionNoteStep") + organization, user, slack_team_identity, _ = make_organization_and_user_with_slack_identities() + step = UpdateResolutionNoteStep(slack_team_identity) + + alert_receive_channel = make_alert_receive_channel(organization) + alert_group = make_alert_group(alert_receive_channel) + make_slack_message(alert_group=alert_group, channel_id="RANDOM_CHANNEL_ID", slack_id="RANDOM_MESSAGE_ID") + resolution_note = make_resolution_note(alert_group=alert_group, author=user, message_text="a" * 3000) + make_resolution_note_slack_message( + alert_group=alert_group, + resolution_note=resolution_note, + user=user, + posted_by_bot=True, + added_by_user=user, + ts=1, + text=resolution_note.text, + ) + + with patch("apps.slack.client.SlackClient.api_call") as mock_slack_api_call: + mock_slack_api_call.return_value = { + "ts": "timestamp", + "message": {"ts": "timestamp"}, + "permalink": "https://link.to.message", + } + step.post_or_update_resolution_note_in_thread(resolution_note) + + assert mock_slack_api_call.called + post_message_call = mock_slack_api_call.mock_calls[0] + assert post_message_call.args[0] == "chat.update" + assert post_message_call.kwargs["json"]["text"] == resolution_note.text[: BLOCK_SECTION_TEXT_MAX_SIZE - 1] + "…" + + @pytest.mark.django_db def test_get_resolution_notes_blocks_latest_limit( make_organization_and_user_with_slack_identities,