Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/sentry/notifications/platform/discord/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
NotificationTargetResourceType,
NotificationTextBlock,
NotificationTextBlockType,
join_rendered_text_blocks,
)
from sentry.organizations.services.organization.model import RpcOrganizationSummary
from sentry.shared_integrations.exceptions import IntegrationError
Expand Down Expand Up @@ -136,7 +137,7 @@ def render_text_blocks(
texts.append(f"[{block.text}]({block.url})")
else:
texts.append(f"{block.text} ({block.url})")
return " ".join(texts)
return join_rendered_text_blocks(texts)


@provider_registry.register(NotificationProviderKey.DISCORD)
Expand Down
5 changes: 3 additions & 2 deletions src/sentry/notifications/platform/email/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
NotificationTargetResourceType,
NotificationTextBlock,
NotificationTextBlockType,
join_rendered_text_blocks,
)
from sentry.organizations.services.organization.model import RpcOrganizationSummary
from sentry.utils.email.address import get_from_email_domain
Expand Down Expand Up @@ -139,7 +140,7 @@ def render_text_blocks_to_html_string(cls, blocks: list[NotificationTextBlock])
escaped_url = escape(block.url)
texts.append(f'<a href="{escaped_url}">{escaped_text}</a>')

return " ".join(texts)
return join_rendered_text_blocks(texts)

@classmethod
def render_body_blocks_to_txt_string(cls, blocks: list[NotificationSection]) -> str:
Expand Down Expand Up @@ -167,7 +168,7 @@ def render_text_blocks_to_txt_string(cls, blocks: list[NotificationTextBlock]) -
texts.append(f"`{block.text}`")
elif block.type == NotificationTextBlockType.LINK and isinstance(block, LinkTextBlock):
texts.append(f"{block.text} ({block.url})")
return " ".join(texts)
return join_rendered_text_blocks(texts)


@provider_registry.register(NotificationProviderKey.EMAIL)
Expand Down
3 changes: 2 additions & 1 deletion src/sentry/notifications/platform/msteams/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
NotificationTargetResourceType,
NotificationTextBlock,
NotificationTextBlockType,
join_rendered_text_blocks,
)
from sentry.organizations.services.organization.model import RpcOrganizationSummary
from sentry.shared_integrations.exceptions import IntegrationError
Expand Down Expand Up @@ -152,7 +153,7 @@ def render_text_blocks(cls, blocks: list[NotificationTextBlock]) -> str:
texts.append(f"`{block.text}`")
elif block.type == NotificationTextBlockType.LINK and isinstance(block, LinkTextBlock):
texts.append(f"[{block.text}]({block.url})")
return " ".join(texts)
return join_rendered_text_blocks(texts)


@provider_registry.register(NotificationProviderKey.MSTEAMS)
Expand Down
3 changes: 2 additions & 1 deletion src/sentry/notifications/platform/slack/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
NotificationTargetResourceType,
NotificationTextBlock,
NotificationTextBlockType,
join_rendered_text_blocks,
)
from sentry.organizations.services.organization.model import RpcOrganizationSummary
from sentry.shared_integrations.exceptions import IntegrationError
Expand Down Expand Up @@ -159,7 +160,7 @@ def _render_text_blocks(cls, blocks: list[NotificationTextBlock]) -> str:
texts.append(f"`{block.text}`")
elif block.type == NotificationTextBlockType.LINK and isinstance(block, LinkTextBlock):
texts.append(f"<{block.url}|{block.text}>")
return " ".join(texts)
return join_rendered_text_blocks(texts)


@provider_registry.register(NotificationProviderKey.SLACK)
Expand Down
25 changes: 24 additions & 1 deletion src/sentry/notifications/platform/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,29 @@ class NotificationRenderedImage:
"""


# A rendered block that starts with one of these attaches to the block before it.
_NO_SPACE_BEFORE = (",", ".", ";", ":", "!", "?", ")")


def join_rendered_text_blocks(texts: list[str]) -> str:
"""
Join rendered text blocks with a space, except where the space would sit next to whitespace
or come before closing punctuation, such as a comma after a bold mention.
"""
joined = ""
for text in texts:
if (
joined
and text
and not joined[-1].isspace()
and not text[0].isspace()
and not text.startswith(_NO_SPACE_BEFORE)
):
joined += " "
joined += text
return joined


@dataclass(frozen=True)
class NotificationRenderedTemplate:
subject: str | list[NotificationTextBlock]
Expand All @@ -315,7 +338,7 @@ def render_text_blocks(blocks: list[NotificationTextBlock]) -> str:
text.append(f"{block.text} ({block.url})")
else:
text.append(block.text)
return " ".join(text)
return join_rendered_text_blocks(text)

@property
def subject_blocks(self) -> list[NotificationTextBlock]:
Expand Down
36 changes: 36 additions & 0 deletions tests/sentry/notifications/platform/test_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from sentry.notifications.platform.email.provider import EmailRenderer
from sentry.notifications.platform.slack.provider import SlackRenderer
from sentry.notifications.platform.types import (
BoldTextBlock,
PlainTextBlock,
join_rendered_text_blocks,
)


def test_join_rendered_text_blocks_separates_words() -> None:
assert join_rendered_text_blocks(["Hi", "*@Jane Doe*", "take a look"]) == (
"Hi *@Jane Doe* take a look"
)


def test_join_rendered_text_blocks_attaches_closing_punctuation() -> None:
assert join_rendered_text_blocks(["Hi", "*@Jane Doe*", ", thoughts?"]) == (
"Hi *@Jane Doe*, thoughts?"
)
assert join_rendered_text_blocks(["(see", "`PROJ-1`", ")."]) == "(see `PROJ-1`)."


def test_join_rendered_text_blocks_skips_space_next_to_whitespace() -> None:
assert join_rendered_text_blocks(["First\n\n", "*@Jane Doe*", "\n\nSecond"]) == (
"First\n\n*@Jane Doe*\n\nSecond"
)


def test_renderers_attach_punctuation_after_bold() -> None:
blocks = [BoldTextBlock(text="@Jane Doe"), PlainTextBlock(text=", thoughts?")]

assert SlackRenderer._render_text_blocks(blocks) == "*@Jane Doe*, thoughts?"
assert EmailRenderer.render_text_blocks_to_html_string(blocks) == (
"<strong>@Jane Doe</strong>, thoughts?"
)
assert EmailRenderer.render_text_blocks_to_txt_string(blocks) == "**@Jane Doe**, thoughts?"
Loading