Truncate resolution note text in slack message to satisfy block limits (#3351)

This should help with some retrying tasks.
This commit is contained in:
Matias Bordese 2023-11-16 10:15:04 -03:00 committed by GitHub
parent 13318577d4
commit e1e56fc414
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 48 additions and 3 deletions

View file

@ -5,6 +5,7 @@ from django.utils.text import Truncator
from apps.alerts.incident_appearance.renderers.base_renderer import AlertBaseRenderer, AlertGroupBaseRenderer
from apps.alerts.incident_appearance.templaters import AlertSlackTemplater
from apps.slack.constants import BLOCK_SECTION_TEXT_MAX_SIZE
from apps.slack.scenarios.scenario_step import ScenarioStep
from apps.slack.types import Block
from common.utils import is_string_with_visible_characters, str_or_backup
@ -23,7 +24,6 @@ class AlertSlackRenderer(AlertBaseRenderer):
return AlertSlackTemplater
def render_alert_blocks(self) -> Block.AnyBlocks:
BLOCK_SECTION_TEXT_MAX_SIZE = 2800
blocks: Block.AnyBlocks = []
title = Truncator(str_or_backup(self.templated_alert.title, "Alert"))

View file

@ -12,6 +12,7 @@ SLACK_RATE_LIMIT_TIMEOUT = datetime.timedelta(minutes=5)
SLACK_RATE_LIMIT_DELAY = 10
CACHE_UPDATE_INCIDENT_SLACK_MESSAGE_LIFETIME = 60 * 10
BLOCK_SECTION_TEXT_MAX_SIZE = 2800
PRIVATE_METADATA_MAX_LENGTH = 3000
DIVIDER: Block.Divider = {"type": "divider"}

View file

@ -4,9 +4,10 @@ import logging
import typing
from django.db.models import Q
from django.utils.text import Truncator
from apps.api.permissions import RBACPermission
from apps.slack.constants import DIVIDER
from apps.slack.constants import BLOCK_SECTION_TEXT_MAX_SIZE, DIVIDER
from apps.slack.errors import (
SlackAPIChannelArchivedError,
SlackAPIChannelInactiveError,
@ -295,9 +296,10 @@ class UpdateResolutionNoteStep(scenario_step.ScenarioStep):
def get_resolution_note_blocks(self, resolution_note: "ResolutionNote") -> Block.AnyBlocks:
blocks: Block.AnyBlocks = []
author_verbal = resolution_note.author_verbal(mention=False)
resolution_note_text = Truncator(resolution_note.text)
resolution_note_text_block = {
"type": "section",
"text": {"type": "mrkdwn", "text": resolution_note.text},
"text": {"type": "mrkdwn", "text": resolution_note_text.chars(BLOCK_SECTION_TEXT_MAX_SIZE)},
}
blocks.append(resolution_note_text_block)
context_block = {

View file

@ -4,6 +4,7 @@ from unittest.mock import patch
import pytest
from apps.slack.client import SlackClient
from apps.slack.constants import BLOCK_SECTION_TEXT_MAX_SIZE
from apps.slack.errors import SlackAPIViewNotFoundError
from apps.slack.scenarios.scenario_step import ScenarioStep
from apps.slack.tests.conftest import build_slack_response
@ -106,6 +107,47 @@ def test_get_resolution_notes_blocks_non_empty(
assert blocks == expected_blocks
@pytest.mark.django_db
def test_get_resolution_note_blocks_truncate_text(
make_organization_and_user_with_slack_identities,
make_alert_receive_channel,
make_alert_group,
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)
resolution_note = make_resolution_note(alert_group=alert_group, author=user, message_text="a" * 3000)
author_verbal = resolution_note.author_verbal(mention=False)
blocks = step.get_resolution_note_blocks(resolution_note)
expected_blocks = [
{
"type": "section",
"text": {
"type": "mrkdwn",
# text is truncated, ellipsis added
"text": resolution_note.text[: BLOCK_SECTION_TEXT_MAX_SIZE - 1] + "",
},
},
{
"type": "context",
"elements": [
{
"type": "mrkdwn",
"text": f"{author_verbal} resolution note from {resolution_note.get_source_display()}.",
}
],
},
]
assert blocks == expected_blocks
@pytest.mark.django_db
def test_get_resolution_notes_blocks_latest_limit(
make_organization_and_user_with_slack_identities,