: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(signals): post inbox slack notifications when reviewer lookup fails (#115061)
posthog[bot]845f30f9eeaa
diff --git a/products/signals/backend/slack_inbox_notifications.py b/products/signals/backend/slack_inbox_notifications.pyindex 5d5781fd2fb2..cfb5fd1b0817 100644--- a/products/signals/backend/slack_inbox_notifications.py+++ b/products/signals/backend/slack_inbox_notifications.py@@ -21,6 +21,9 @@ from collections.abc import Callable, Iterablefrom django.conf import settings+from rest_framework import serializers+from slack_sdk.errors import SlackApiError+from posthog.dataclasses import frozenfrom posthog.event_usage import groupsfrom posthog.models import User@@ -272,9 +275,17 @@ def _posthog_user_display_name(user: User) -> str:def _resolve_reviewer_mentions(slack: SlackIntegration, reviewer_users: list[User]) -> list[str]:# `<@U…>` mention when the reviewer's email resolves in this workspace, else escaped name.+ # A failed lookup (a missing users:read.email scope, a spent lookup budget) costs only the+ # mention. The next lookup fails the same way, so the remaining reviewers get names too.mentions: list[str] = []+ lookups_available = Truefor user in reviewer_users[:_MAX_REVIEWER_MENTIONS]:- slack_user_id = lookup_slack_user_id_by_email(slack, user.email) if user.email else None+ slack_user_id = None+ if user.email and lookups_available:+ try:+ slack_user_id = lookup_slack_user_id_by_email(slack, user.email)+ except (SlackApiError, serializers.ValidationError):+ lookups_available = Falsementions.append(f"<@{slack_user_id}>" if slack_user_id else _escape_mrkdwn(_posthog_user_display_name(user)))return mentionsdiff --git a/products/signals/backend/test/test_slack_inbox_notifications.py b/products/signals/backend/test/test_slack_inbox_notifications.pyindex 4751d095a088..065920f73b96 100644--- a/products/signals/backend/test/test_slack_inbox_notifications.py+++ b/products/signals/backend/test/test_slack_inbox_notifications.py@@ -5,6 +5,8 @@ from unittest.mock import MagicMock, patchfrom django.conf import settings+from rest_framework.exceptions import ValidationError+from slack_sdk.errors import SlackApiErrorfrom social_django.models import UserSocialAuthfrom posthog.constants import AvailableFeature@@ -210,14 +212,29 @@ def test_resolve_reviewer_mentions_uses_slack_mention_when_lookup_succeeds() ->assert _resolve_reviewer_mentions(slack, [user]) == ["<@U_SLACK>"]-def test_resolve_reviewer_mentions_falls_back_to_name_when_slack_user_not_found() -> None:- user = User(first_name="Marcus", last_name="Twix", email="[email protected]")+ ("lookup_result", "expected_lookups"),+ [+ (None, 2),+ # A lookup error stops the remaining lookups, because each one fails the same way.+ (SlackApiError("missing_scope", {"ok": False, "error": "missing_scope"}), 1),+ (ValidationError({"slack_notification_direct_message": "Slack is unavailable"}), 1),+ ],+ ids=["slack_user_not_found", "missing_scope", "lookup_budget_spent"],+)+def test_resolve_reviewer_mentions_falls_back_to_name(lookup_result: object, expected_lookups: int) -> None:+ users = [+ User(first_name="Marcus", last_name="Twix", email="[email protected]"),+ User(first_name="Ada", last_name="Lime", email="[email protected]"),+ ]slack = MagicMock()with patch("products.signals.backend.slack_inbox_notifications.lookup_slack_user_id_by_email",- return_value=None,- ):- assert _resolve_reviewer_mentions(slack, [user]) == ["Marcus Twix"]+ side_effect=lookup_result if isinstance(lookup_result, Exception) else None,+ return_value=lookup_result,+ ) as lookup:+ assert _resolve_reviewer_mentions(slack, users) == ["Marcus Twix", "Ada Lime"]+ assert lookup.call_count == expected_lookupsdef test_resolve_reviewer_mentions_caps_at_five() -> None: