: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): lazily create team config so notification setup works (#62197)
Joshua Snyder0e380c1e7b22
diff --git a/products/signals/backend/test/test_signal_team_config_api.py b/products/signals/backend/test/test_signal_team_config_api.pyindex 36f8df159317..c7ba5d041f7d 100644--- a/products/signals/backend/test/test_signal_team_config_api.py+++ b/products/signals/backend/test/test_signal_team_config_api.py@@ -22,10 +22,29 @@ class TestSignalTeamConfigAPI(APIBaseTest):assert data["default_slack_notification_channel"] is Noneassert data["default_autostart_priority"] == "P0"- def test_get_config_returns_404_when_no_config_exists(self):+ def test_get_config_lazily_creates_when_no_config_exists(self):self.config.delete()+ assert not SignalTeamConfig.objects.filter(team=self.team).exists()response = self.client.get(self._url())- assert response.status_code == status.HTTP_404_NOT_FOUND+ data = response.json()+ assert response.status_code == status.HTTP_200_OK, data+ assert data["default_autostart_priority"] == "P0"+ assert data["default_slack_notification_channel"] is None+ assert SignalTeamConfig.objects.filter(team=self.team).exists()++ def test_post_config_lazily_creates_when_no_config_exists(self):+ self.config.delete()+ assert not SignalTeamConfig.objects.filter(team=self.team).exists()+ response = self.client.post(+ self._url(),+ data={"default_slack_notification_channel": "C123|#posthog-signals"},+ format="json",+ )+ data = response.json()+ assert response.status_code == status.HTTP_200_OK, data+ assert data["default_slack_notification_channel"] == "C123|#posthog-signals"+ config = SignalTeamConfig.objects.get(team=self.team)+ assert config.default_slack_notification_channel == "C123|#posthog-signals"@parameterized.expand([diff --git a/products/signals/backend/views.py b/products/signals/backend/views.pyindex 66c6bdc0e420..07b897d43d64 100644--- a/products/signals/backend/views.py+++ b/products/signals/backend/views.py@@ -45,6 +45,7 @@ from posthog.api.routing import TeamAndOrgViewSetMixinfrom posthog.auth import InternalAPIAuthentication, OAuthAccessTokenAuthentication, PersonalAPIKeyAuthenticationfrom posthog.models import Team, Userfrom posthog.models.integration import Integration+from posthog.models.team.extensions import get_or_create_team_extensionfrom posthog.permissions import APIScopePermissionfrom posthog.temporal.common.client import sync_connectfrom posthog.user_permissions import UserPermissions@@ -298,10 +299,11 @@ class SignalTeamConfigViewSet(TeamAndOrgViewSetMixin, viewsets.GenericViewSet):return ["task:write"]def _get_config(self) -> SignalTeamConfig:- try:- return SignalTeamConfig.objects.get(team=self.team)- except SignalTeamConfig.DoesNotExist:- raise exceptions.NotFound("No signal config exists for this team.")+ # Singleton per team with safe defaults. A post_save signal creates it on team+ # creation, but teams predating that signal (or where it failed) have no row, so+ # lazily create it here — otherwise the first read/write (e.g. connecting a default+ # notification channel) would 404.+ return get_or_create_team_extension(self.team, SignalTeamConfig)@extend_schema(exclude=True)def list(self, request: Request, *args, **kwargs) -> Response: