: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
chore(signals): simplify slack inbox notification functions (#100820)
posthog[bot]206264254a41
diff --git a/products/signals/backend/slack_inbox_notifications.py b/products/signals/backend/slack_inbox_notifications.pyindex 7e4bc472d911..40b37bb297d0 100644--- a/products/signals/backend/slack_inbox_notifications.py+++ b/products/signals/backend/slack_inbox_notifications.py@@ -17,10 +17,11 @@ from __future__ import annotationsimport jsonimport logging-from collections.abc import Iterable+from collections.abc import Callable, Iterablefrom django.conf import settings+from posthog.dataclasses import frozenfrom posthog.event_usage import groupsfrom posthog.helpers.slack_markdown import slack_markdown_block as _markdown_blockfrom posthog.models import User@@ -417,39 +418,60 @@ def _signal_source_line(source_product: str, source_type: str, extra: dict | Nonreturn f"{product_label} · {type_label}" if type_label else product_label-def _signal_detail_parts(source_product: str, extra: dict) -> list[str]:- """A compact, source-specific metadata line mirroring the inbox SignalCard footer."""+def _labelled_detail_parts(extra: dict, fields: tuple[tuple[str, str], ...]) -> list[str]:+ """`Label: value` parts for each named key that carries a truthy value, in the given order."""+ return [f"{label}: {_escape_mrkdwn(str(extra[key]))}" for key, label in fields if extra.get(key)]+++def _github_detail_parts(extra: dict) -> list[str]:parts: list[str] = []- if source_product == "github":- number = extra.get("number")- if number is not None:- parts.append(f"#{_escape_mrkdwn(str(number))}")- labels = extra.get("labels")- if isinstance(labels, list) and labels:- parts.append(", ".join(_escape_mrkdwn(str(label)) for label in labels[:5]))- if _is_safe_http_url(extra.get("html_url")):- parts.append(f"<{extra['html_url']}|View on GitHub>")- elif source_product == "zendesk":- if extra.get("priority"):- parts.append(f"Priority: {_escape_mrkdwn(str(extra['priority']))}")- if extra.get("status"):- parts.append(f"Status: {_escape_mrkdwn(str(extra['status']))}")- if _is_safe_http_url(extra.get("url")):- parts.append(f"<{extra['url']}|Open ticket>")- elif source_product == "llm_analytics":- if extra.get("model"):- parts.append(f"Model: {_escape_mrkdwn(str(extra['model']))}")- if extra.get("provider"):- parts.append(f"Provider: {_escape_mrkdwn(str(extra['provider']))}")- trace_id = extra.get("trace_id")- if trace_id:- parts.append(f"Trace: `{_escape_mrkdwn(str(trace_id)[:12])}…`")- elif source_product == "session_replay":- if extra.get("problem_type"):- parts.append(f"Problem: {_escape_mrkdwn(str(extra['problem_type']).replace('_', ' '))}")+ number = extra.get("number")+ if number is not None:+ parts.append(f"#{_escape_mrkdwn(str(number))}")+ labels = extra.get("labels")+ if isinstance(labels, list) and labels:+ parts.append(", ".join(_escape_mrkdwn(str(label)) for label in labels[:5]))+ if _is_safe_http_url(extra.get("html_url")):+ parts.append(f"<{extra['html_url']}|View on GitHub>")return parts+def _zendesk_detail_parts(extra: dict) -> list[str]:+ parts = _labelled_detail_parts(extra, (("priority", "Priority"), ("status", "Status")))+ if _is_safe_http_url(extra.get("url")):+ parts.append(f"<{extra['url']}|Open ticket>")+ return parts+++def _llm_analytics_detail_parts(extra: dict) -> list[str]:+ parts = _labelled_detail_parts(extra, (("model", "Model"), ("provider", "Provider")))+ trace_id = extra.get("trace_id")+ if trace_id:+ parts.append(f"Trace: `{_escape_mrkdwn(str(trace_id)[:12])}…`")+ return parts+++def _session_replay_detail_parts(extra: dict) -> list[str]:+ problem_type = extra.get("problem_type")+ if not problem_type:+ return []+ return [f"Problem: {_escape_mrkdwn(str(problem_type).replace('_', ' '))}"]+++_SIGNAL_DETAIL_BUILDERS: dict[str, Callable[[dict], list[str]]] = {+ "github": _github_detail_parts,+ "zendesk": _zendesk_detail_parts,+ "llm_analytics": _llm_analytics_detail_parts,+ "session_replay": _session_replay_detail_parts,+}+++def _signal_detail_parts(source_product: str, extra: dict) -> list[str]:+ """A compact, source-specific metadata line mirroring the inbox SignalCard footer."""+ builder = _SIGNAL_DETAIL_BUILDERS.get(source_product)+ return builder(extra) if builder is not None else []++def _build_signal_thread_blocks(signal: dict) -> tuple[list[dict], str]:"""Render one evidence signal as Slack blocks, mirroring an inbox SignalCard."""source_product = str(signal.get("source_product") or "")@@ -519,6 +541,67 @@ class _ChannelRoute:self.users: list[User] = []+@frozen+class _Destination:+ integration: Integration+ channel: str+ is_team_channel: bool+++class _RouteTable:+ """The destinations one report is posted to, each collecting the reviewers it mentions.++ Keyed by (integration_id, channel_id) so a reviewer's own channel and the team default+ collapse into one post when they resolve to the same Slack channel.+ """++ def __init__(self) -> None:+ self._routes: dict[tuple[int, str], _ChannelRoute] = {}++ def add(self, destination: _Destination, *, user: User | None = None) -> None:+ key = (destination.integration.id, _channel_id_from_target(destination.channel))+ route = self._routes.get(key)+ if route is None:+ route = _ChannelRoute(+ destination.integration, destination.channel, is_team_channel=destination.is_team_channel+ )+ self._routes[key] = route+ elif destination.is_team_channel:+ route.is_team_channel = True+ if user is not None:+ route.users.append(user)++ def routes(self) -> list[_ChannelRoute]:+ return list(self._routes.values())+++def _reviewer_destination(+ *,+ own_config: SignalUserAutonomyConfig | None,+ priority: str | None,+ team_integration: Integration | None,+ team_channel: str | None,+) -> _Destination | None:+ """Where one suggested reviewer is notified: their own target if set, else the team default.++ `None` means this reviewer gets no notification at all.+ """+ if own_config is not None:+ if not _meets_min_priority(priority, own_config.slack_notification_min_priority):+ return None+ integration = own_config.slack_notification_integration+ channel = own_config.slack_notification_channel+ is_team_channel = False+ elif team_integration is not None and team_channel:+ integration, channel, is_team_channel = team_integration, team_channel, True+ else:+ return None++ if integration is None or not channel:+ return None+ return _Destination(integration=integration, channel=channel, is_team_channel=is_team_channel)++def _build_reviewer_routes(report: SignalReport,*,@@ -539,51 +622,29 @@ def _build_reviewer_routes(reviewer_users = {user.id: user for user in User.objects.filter(id__in=reviewer_user_ids)}own_configs = _own_target_configs_by_user(report.team_id, reviewer_user_ids)- # Keyed by (integration_id, channel_id) so a reviewer's own channel and the team- # default collapse into one post when they resolve to the same Slack channel.- routes: dict[tuple[int, str], _ChannelRoute] = {}-- def _route_for(integration: Integration, channel: str, *, is_team_channel: bool) -> _ChannelRoute:- key = (integration.id, _channel_id_from_target(channel))- route = routes.get(key)- if route is None:- route = _ChannelRoute(integration, channel, is_team_channel=is_team_channel)- routes[key] = route- elif is_team_channel:- route.is_team_channel = True- return route-+ table = _RouteTable()for user_id in sorted(reviewer_user_ids):user = reviewer_users.get(user_id)if user is None:continue-- config = own_configs.get(user_id)- if config is not None:- if not _meets_min_priority(priority, config.slack_notification_min_priority):- continue- integration = config.slack_notification_integration- channel = config.slack_notification_channel- is_team_channel = False- elif team_integration is not None and team_channel:- integration = team_integration- channel = team_channel- is_team_channel = True- else:- continue-- if integration is None or not channel:+ destination = _reviewer_destination(+ own_config=own_configs.get(user_id),+ priority=priority,+ team_integration=team_integration,+ team_channel=team_channel,+ )+ if destination is None:continue- _route_for(integration, channel, is_team_channel=is_team_channel).users.append(user)+ table.add(destination, user=user)# No suggested reviewer resolved to a PostHog user: deliver to the team-default channel (if# configured) so the team is still notified, without @-mentions — the message omits the# suggested-reviewers section when there is nobody to tag. Per-user own channels are reviewer# notifications, so they are not used here.if not reviewer_user_ids and team_integration is not None and team_channel:- _route_for(team_integration, team_channel, is_team_channel=True)+ table.add(_Destination(integration=team_integration, channel=team_channel, is_team_channel=True))- return list(routes.values())+ return table.routes()def _deliver_route_notification(@@ -637,6 +698,32 @@ def _deliver_route_notification(return delivered+def _deliver_to_routes(+ report: SignalReport,+ routes: list[_ChannelRoute],+ *,+ priority: str | None,+ source_products: list[str] | None,+ trigger: str,+ signals: list[dict] | None = None,+) -> int:+ """Post the report to every route, returning how many top-level messages were sent."""+ repository = _report_repository(report)+ sent = 0+ for route in routes:+ if _deliver_route_notification(+ report,+ route,+ priority=priority,+ source_products=source_products or [],+ repository=repository,+ trigger=trigger,+ signals=signals,+ ):+ sent += 1+ return sent++def _capture_notification_delivered(report: SignalReport, route: _ChannelRoute, *, trigger: str, delivered: bool) -> None:@@ -730,21 +817,14 @@ def dispatch_inbox_item_notifications()return 0- sources = source_products or []- repository = _report_repository(report)-- sent = 0- for route in routes:- if _deliver_route_notification(- report,- route,- priority=priority,- source_products=sources,- repository=repository,- trigger="report_ready",- signals=signals,- ):- sent += 1+ sent = _deliver_to_routes(+ report,+ routes,+ priority=priority,+ source_products=source_products,+ trigger="report_ready",+ signals=signals,+ )logger.info("dispatch_inbox_item_notifications: complete",extra={"report_id": report_id, "team_id": team_id, "messages_sent": sent, "routes": len(routes)},@@ -752,6 +832,55 @@ def dispatch_inbox_item_notifications(return sent+def _stripped_nonempty(values: Iterable[str] | None) -> set[str]:+ return {value.strip() for value in values or () if value and value.strip()}+++def _resolve_added_reviewer_user_ids(+ report: SignalReport, *, logins: set[str], user_uuids: set[str], exclude_user_id: int | None+) -> set[int]:+ """PostHog user IDs for the reviewers just added, restricted to the project's access set.++ A reviewer added by uuid may have no GitHub login at all, so both identities are resolved: the+ personal ping is the whole point of this path, and losing it would leave them unnotified.+ """+ resolved_users = [+ *(resolve_org_github_login_to_users(report.team_id, logins).values() if logins else []),+ *(resolve_org_users_by_uuid(report.team_id, user_uuids).values() if user_uuids else []),+ ]+ user_ids = {user.id for user in resolved_users if user.id != exclude_user_id}+ if not user_ids:+ return set()+ # Org membership alone isn't enough: on a private project an org member without project+ # access must not receive the report's contents, so intersect with the project's access set.+ return user_ids & set(report.team.all_users_with_access().filter(id__in=user_ids).values_list("id", flat=True))+++def _build_added_reviewer_routes(team_id: int, user_ids: set[int], priority: str | None) -> list[_ChannelRoute]:+ """Route each added reviewer to their own configured target, with no team-default fallback."""+ own_configs = _own_target_configs_by_user(team_id, user_ids)+ users_by_id = {user.id: user for user in User.objects.filter(id__in=user_ids)}++ table = _RouteTable()+ for user_id in sorted(user_ids):+ user = users_by_id.get(user_id)+ config = own_configs.get(user_id)+ if user is None or config is None:+ continue+ # An unprioritized report still pings a reviewer with no min-priority threshold: the+ # initial path would have delivered it via the team channel, but this path has no+ # fallback, so `_meets_min_priority`'s no-priority-never-notifies rule would lose it.+ min_priority = config.slack_notification_min_priority+ if (priority is not None or min_priority is not None) and not _meets_min_priority(priority, min_priority):+ continue+ integration = config.slack_notification_integration+ channel = config.slack_notification_channel+ if integration is None or not channel:+ continue+ table.add(_Destination(integration=integration, channel=channel, is_team_channel=False), user=user)+ return table.routes()++def dispatch_reviewer_added_notifications(report_id: str,team_id: int,@@ -774,8 +903,8 @@ def dispatch_reviewer_added_notifications(fires for reports that would themselves have notified. `exclude_user_id` drops the actorso someone adding themselves isn't pinged. Best-effort; returns messages sent."""- added_logins = {s.strip().lower() for s in added_github_logins if s and s.strip()}- added_uuids = {s.strip() for s in (added_user_uuids or []) if s and s.strip()}+ added_logins = {login.lower() for login in _stripped_nonempty(added_github_logins)}+ added_uuids = _stripped_nonempty(added_user_uuids)if not added_logins and not added_uuids:return 0@@ -794,66 +923,24 @@ def dispatch_reviewer_added_notifications(if report.status != SignalReport.Status.READY or _latest_actionability(report) not in _ACTIONABLE_VALUES:return 0- # A reviewer added by uuid may have no GitHub login at all, so resolve both identities: the- # personal ping is the whole point of this path, and losing it would leave them unnotified.- resolved_users = [- *(resolve_org_github_login_to_users(team_id, added_logins).values() if added_logins else []),- *(resolve_org_users_by_uuid(team_id, added_uuids).values() if added_uuids else []),- ]- user_ids = {user.id for user in resolved_users if user.id != exclude_user_id}- # Org membership alone isn't enough: on a private project an org member without project- # access must not receive the report's contents, so intersect with the project's access set.- if user_ids:- user_ids &= set(report.team.all_users_with_access().filter(id__in=user_ids).values_list("id", flat=True))+ user_ids = _resolve_added_reviewer_user_ids(+ report, logins=added_logins, user_uuids=added_uuids, exclude_user_id=exclude_user_id+ )if not user_ids:return 0priority = _latest_priority(report)-- # Personal targets only — a manual add never falls back to the team channel.- own_configs = _own_target_configs_by_user(team_id, user_ids)- users_by_id = {user.id: user for user in User.objects.filter(id__in=user_ids)}-- routes: dict[tuple[int, str], _ChannelRoute] = {}- for user_id in sorted(user_ids):- user = users_by_id.get(user_id)- config = own_configs.get(user_id)- if user is None or config is None:- continue- # An unprioritized report still pings a reviewer with no min-priority threshold: the- # initial path would have delivered it via the team channel, but this path has no- # fallback, so `_meets_min_priority`'s no-priority-never-notifies rule would lose it.- min_priority = config.slack_notification_min_priority- if (priority is not None or min_priority is not None) and not _meets_min_priority(priority, min_priority):- continue- integration = config.slack_notification_integration- channel = config.slack_notification_channel- if integration is None or not channel:- continue- key = (integration.id, _channel_id_from_target(channel))- route = routes.get(key)- if route is None:- route = _ChannelRoute(integration, channel, is_team_channel=False)- routes[key] = route- route.users.append(user)-+ routes = _build_added_reviewer_routes(team_id, user_ids, priority)if not routes:return 0- sources = source_products or []- repository = _report_repository(report)-- sent = 0- for route in routes.values():- if _deliver_route_notification(- report,- route,- priority=priority,- source_products=sources,- repository=repository,- trigger="reviewer_added",- ):- sent += 1+ sent = _deliver_to_routes(+ report,+ routes,+ priority=priority,+ source_products=source_products,+ trigger="reviewer_added",+ )logger.info("dispatch_reviewer_added_notifications: complete",extra={"report_id": report_id, "team_id": team_id, "messages_sent": sent, "routes": len(routes)},diff --git a/products/signals/backend/test/test_slack_inbox_notifications.py b/products/signals/backend/test/test_slack_inbox_notifications.pyindex dba63dd55aae..b576ec36c63e 100644--- a/products/signals/backend/test/test_slack_inbox_notifications.py+++ b/products/signals/backend/test/test_slack_inbox_notifications.py@@ -899,6 +899,62 @@ def test_build_signal_thread_blocks_renders_header_content_and_github_details()assert fallback.startswith("GitHub · Issue:")+ ("source_product", "extra", "expected_detail"),+ [+ (+ "zendesk",+ {"priority": "urgent", "status": "pending", "url": "https://support.example.com/tickets/7"},+ "Priority: urgent · Status: pending · <https://support.example.com/tickets/7|Open ticket>",+ ),+ (+ "llm_analytics",+ {"model": "claude-opus-5", "provider": "anthropic", "trace_id": "0123456789abcdefghij"},+ "Model: claude-opus-5 · Provider: anthropic · Trace: `0123456789ab…`",+ ),+ ("session_replay", {"problem_type": "rage_click_loop"}, "Problem: rage click loop"),+ ],+)+def test_build_signal_thread_blocks_renders_source_specific_details(+ source_product: str, extra: dict, expected_detail: str+) -> None:+ signal = {"source_product": source_product, "source_type": "ticket", "content": "body", "extra": extra}+ blocks, _ = _build_signal_thread_blocks(signal)+ assert blocks[2]["elements"][0]["text"] == expected_detail+++ ("source_product", "extra"),+ [+ ("zendesk", {}),+ ("llm_analytics", {}),+ ("session_replay", {}),+ ("logs", {"service": "ingestion"}),+ ],+)+def test_build_signal_thread_blocks_omits_empty_detail_block(source_product: str, extra: dict) -> None:+ signal = {"source_product": source_product, "source_type": "ticket", "content": "body", "extra": extra}+ blocks, _ = _build_signal_thread_blocks(signal)+ assert [block["type"] for block in blocks] == ["context", "markdown"]+++def test_build_signal_thread_blocks_escapes_mrkdwn_in_source_specific_details() -> None:+ # Detail values come from the source payload, so they must not carry a live mention into Slack.+ signal = {+ "source_product": "llm_analytics",+ "source_type": "evaluation",+ "content": "body",+ "extra": {"model": "<@U42>", "provider": "<!channel>"},+ }+ blocks, _ = _build_signal_thread_blocks(signal)+ detail = blocks[2]["elements"][0]["text"]+ assert "<@U42>" not in detail+ assert "<!channel>" not in detail+ # Both values must survive as escaped text, so dropping a field outright also fails.+ assert "Model: <@U42>" in detail+ assert "Provider: <!channel>" in detail++def test_build_signal_thread_blocks_escapes_content_to_block_mention_injection() -> None:signal = {"source_product": "logs",