Avoid msg_too_long errors when posting/updating slack resolution note (#3372)

This commit is contained in:
Matias Bordese 2023-11-20 09:17:07 -03:00 committed by GitHub
parent 6214ffbd66
commit 3b90c6544b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 76 additions and 2 deletions

View file

@ -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:

View file

@ -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,