:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(tasks): wrap bare URLs in Slack mrkdwn emphasis so bold renders (#62848)
Vojta Bartoše6b9adedf918
diff --git a/products/tasks/backend/temporal/slack_relay/activities.py b/products/tasks/backend/temporal/slack_relay/activities.pyindex 458859eae390..984613e588f1 100644--- a/products/tasks/backend/temporal/slack_relay/activities.py+++ b/products/tasks/backend/temporal/slack_relay/activities.py@@ -29,6 +29,17 @@ _RE_MD_LINK = re.compile(r"\[([^\]]+)\]\(([^)]+)\)")# are left alone rather than silently rewritten into a different broken shape._RE_LINK_TRAILING_MARKER = re.compile(r"(?<![*_~])(\*+|_+|~+)<([^<>]+?)\1>(?![*_~])")+# Repair pattern: a bare ``http(s)`` URL wrapped directly in emphasis markers,+# e.g. ``**https://example.com**``. The converter halves the markers in place+# and emits ``*https://example.com*``; Slack then auto-links the URL but+# renders the surrounding ``*`` as literal text because there is no whitespace+# flanking the markers. Pre-wrapping the URL in ``<>`` lets the converter emit+# the well-formed ``*<https://example.com>*`` — a clean bolded clickable link.+# The URL group excludes whitespace and angle brackets so already well-formed+# links (``**<url>**``) and bracketed markdown links (``**[text](url)**``) are+# left alone.+_RE_BARE_URL_IN_EMPHASIS = re.compile(r"(?<![*_~])(\*+|_+|~+)(https?://[^\s<>]+?)\1(?![*_~])")+class _RelayAlreadyRecorded(Exception):"""Raised when a relay was already recorded while holding the row lock."""@@ -41,12 +52,14 @@ def _markdown_to_slack_mrkdwn(text: str) -> str:Slack ``mrkdwn`` is rendered in a proportional font — pipe-separated rows donot line up. A fenced code block forces monospace and the columns align.- Misplaced link markers (e.g. ``**<url**>``) are normalized first so the+ Misplaced link markers (e.g. ``**<url**>``) and bare URLs wrapped in+ emphasis (e.g. ``**https://example.com**``) are normalized first so theconverter sees well-formed input."""if not text:return text- return _CONVERTER.convert(_tables_to_fenced_code_blocks(_repair_link_trailing_markers(text)))+ repaired = _wrap_bare_urls_in_emphasis(_repair_link_trailing_markers(text))+ return _CONVERTER.convert(_tables_to_fenced_code_blocks(repaired))def _repair_link_trailing_markers(text: str) -> str:@@ -60,6 +73,17 @@ def _repair_link_trailing_markers(text: str) -> str:return _RE_LINK_TRAILING_MARKER.sub(r"\1<\2>\1", text)+def _wrap_bare_urls_in_emphasis(text: str) -> str:+ """Wrap bare ``http(s)`` URLs adjacent to emphasis markers with angle brackets.++ ``**https://example.com**`` becomes ``**<https://example.com>**`` so the+ downstream converter produces a properly formatted Slack link. Already+ bracketed URLs (``**<url>**``) and markdown links (``**[label](url)**``)+ are left untouched because the URL group rejects ``<`` and ``[``.+ """+ return _RE_BARE_URL_IN_EMPHASIS.sub(r"\1<\2>\1", text)++def _tables_to_fenced_code_blocks(text: str) -> str:"""Replace pipe-syntax markdown tables with fenced code blocks of padded columns.diff --git a/products/tasks/backend/temporal/slack_relay/tests/test_activities.py b/products/tasks/backend/temporal/slack_relay/tests/test_activities.pyindex e5b559ac0b4e..7bef16d9e1b6 100644--- a/products/tasks/backend/temporal/slack_relay/tests/test_activities.py+++ b/products/tasks/backend/temporal/slack_relay/tests/test_activities.py@@ -20,6 +20,7 @@ from products.tasks.backend.temporal.slack_relay.activities import (_markdown_to_slack_mrkdwn,_repair_link_trailing_markers,_split_markdown_for_slack,+ _wrap_bare_urls_in_emphasis,relay_slack_message,)@@ -180,6 +181,20 @@ class TestMarkdownToSlackMrkdwn(unittest.TestCase):"**<https://us.posthog.com/project/2/llm-analytics/skills/pr-shepherd|pr-shepherd**>","*<https://us.posthog.com/project/2/llm-analytics/skills/pr-shepherd|pr-shepherd>*",),+ # Bare URL wrapped directly in markdown bold. Without the pre-wrap pass the+ # converter halves the markers in place and emits ``*https://x.com*``, which+ # Slack renders as literal asterisks around an auto-linked URL — the exact+ # papercut on the PR-completion message that prompted this repair.+ (+ "agent_typo_bare_url_in_bold",+ "Draft PR opened: **https://github.com/PostHog/posthog.com/pull/17450**",+ "Draft PR opened: *<https://github.com/PostHog/posthog.com/pull/17450>*",+ ),+ (+ "agent_typo_bare_url_in_italic_asterisk",+ "see *https://example.com*",+ "see _<https://example.com>_",+ ),("plain_text_unchanged", "Hello world", "Hello world"),("inline_code_preserved", "Use `git commit`", "Use `git commit`"),]@@ -241,6 +256,50 @@ class TestRepairLinkTrailingMarkers(unittest.TestCase):assert _repair_link_trailing_markers(text) == expected+class TestWrapBareUrlsInEmphasis(unittest.TestCase):+ @parameterized.expand(+ [+ ("bold_bare_url", "**https://x.com**", "**<https://x.com>**"),+ ("italic_bare_url", "*https://x.com*", "*<https://x.com>*"),+ ("underscore_bare_url", "_https://x.com_", "_<https://x.com>_"),+ ("strike_bare_url", "~~https://x.com~~", "~~<https://x.com>~~"),+ (+ "url_with_path_and_query",+ "**https://github.com/PostHog/posthog.com/pull/17450?foo=bar**",+ "**<https://github.com/PostHog/posthog.com/pull/17450?foo=bar>**",+ ),+ (+ "two_bare_urls_in_one_line",+ "**https://a.com** and *https://b.com*",+ "**<https://a.com>** and *<https://b.com>*",+ ),+ # Surrounded by sentence text — only the wrapped URL should be touched.+ (+ "url_inside_sentence",+ "Draft PR opened: **https://x.com/pr/1**",+ "Draft PR opened: **<https://x.com/pr/1>**",+ ),+ # Already bracketed — leave alone so we don't double-wrap.+ ("autolink_already_bracketed", "**<https://x.com>**", "**<https://x.com>**"),+ # Standard markdown link — handled correctly by the converter as-is.+ ("markdown_link_in_bold_unchanged", "**[label](https://x.com)**", "**[label](https://x.com)**"),+ # Non-URL bold spans must not be rewritten.+ ("plain_bold_unchanged", "**hello world**", "**hello world**"),+ ("plain_text_unchanged", "Visit https://x.com without bolding", "Visit https://x.com without bolding"),+ # A bare URL not directly adjacent to the marker shouldn't be wrapped — the+ # surrounding text means the emphasis already flanks whitespace and Slack+ # renders it correctly without help.+ (+ "url_inside_bold_span_with_surrounding_text",+ "**check https://x.com later**",+ "**check https://x.com later**",+ ),+ ]+ )+ def test_wrap(self, _name, text, expected):+ assert _wrap_bare_urls_in_emphasis(text) == expected++class TestSplitTextForSlack(TestCase):def test_short_text_returns_single_chunk(self):assert _split_markdown_for_slack("hello world") == ["hello world"]