diff --git a/src/sentry/notifications/platform/discord/provider.py b/src/sentry/notifications/platform/discord/provider.py
index fbb4be7113f6..81ead0964151 100644
--- a/src/sentry/notifications/platform/discord/provider.py
+++ b/src/sentry/notifications/platform/discord/provider.py
@@ -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
@@ -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)
diff --git a/src/sentry/notifications/platform/email/provider.py b/src/sentry/notifications/platform/email/provider.py
index 1c524b9f1258..f212f276ccba 100644
--- a/src/sentry/notifications/platform/email/provider.py
+++ b/src/sentry/notifications/platform/email/provider.py
@@ -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
@@ -139,7 +140,7 @@ def render_text_blocks_to_html_string(cls, blocks: list[NotificationTextBlock])
escaped_url = escape(block.url)
texts.append(f'{escaped_text}')
- return " ".join(texts)
+ return join_rendered_text_blocks(texts)
@classmethod
def render_body_blocks_to_txt_string(cls, blocks: list[NotificationSection]) -> str:
@@ -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)
diff --git a/src/sentry/notifications/platform/msteams/provider.py b/src/sentry/notifications/platform/msteams/provider.py
index ff30a89cd94a..cfe9eca6560b 100644
--- a/src/sentry/notifications/platform/msteams/provider.py
+++ b/src/sentry/notifications/platform/msteams/provider.py
@@ -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
@@ -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)
diff --git a/src/sentry/notifications/platform/slack/provider.py b/src/sentry/notifications/platform/slack/provider.py
index 19d4f7b70eeb..c1f5d362fb44 100644
--- a/src/sentry/notifications/platform/slack/provider.py
+++ b/src/sentry/notifications/platform/slack/provider.py
@@ -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
@@ -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)
diff --git a/src/sentry/notifications/platform/types.py b/src/sentry/notifications/platform/types.py
index 030951818189..6ae5106fa3bd 100644
--- a/src/sentry/notifications/platform/types.py
+++ b/src/sentry/notifications/platform/types.py
@@ -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]
@@ -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]:
diff --git a/tests/sentry/notifications/platform/test_types.py b/tests/sentry/notifications/platform/test_types.py
new file mode 100644
index 000000000000..ae3cc1808421
--- /dev/null
+++ b/tests/sentry/notifications/platform/test_types.py
@@ -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) == (
+ "@Jane Doe, thoughts?"
+ )
+ assert EmailRenderer.render_text_blocks_to_txt_string(blocks) == "**@Jane Doe**, thoughts?"