: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(integrations): Use GitHub OAuth fast path on web (#60408)
Michael Matlokad8bca052bb58
diff --git a/posthog/api/test/test_user_integration.py b/posthog/api/test/test_user_integration.pyindex 7e706c579195..286de5c8af5d 100644--- a/posthog/api/test/test_user_integration.py+++ b/posthog/api/test/test_user_integration.py@@ -163,12 +163,8 @@ class TestUserIntegrationEndpoints(APIBaseTest):self.assertIn("client_id=gh_client_123", url)self.assertIn("redirect_uri=https%3A%2F%2Fus.posthog.com%2Fcomplete%2Fgithub-link%2F", url)- @override_settings(GITHUB_APP_CLIENT_ID="gh_client_123")- @patch(- "posthog.api.github_callback.personal_state.get_instance_settings",- return_value={"GITHUB_APP_SLUG": "posthog-dev"},- )- def test_github_start_returns_install_url_even_when_team_has_github_integration(self, _mock_settings):+ @override_settings(GITHUB_APP_CLIENT_ID="gh_client_123", SITE_URL="https://us.posthog.com")+ def test_github_start_web_fast_path_returns_oauth_url_when_team_has_github_integration(self):Integration.objects.create(team=self.team,kind="github",@@ -181,8 +177,11 @@ class TestUserIntegrationEndpoints(APIBaseTest):self.assertEqual(response.status_code, status.HTTP_200_OK)data = response.json()self.assertIn("install_url", data)- self.assertEqual(data.get("connect_flow"), "app_install")- self.assertIn("github.com/apps/posthog-dev/installations/new", data["install_url"])+ self.assertEqual(data.get("connect_flow"), "oauth_authorize")+ url = data["install_url"]+ self.assertIn("github.com/login/oauth/authorize", url)+ self.assertIn("client_id=gh_client_123", url)+ self.assertIn("redirect_uri=https%3A%2F%2Fus.posthog.com%2Fcomplete%2Fgithub-link%2F", url)@override_settings(GITHUB_APP_CLIENT_ID="gh_client_123", SITE_URL="https://us.posthog.com", GITHUB_APP_CLIENT_SECRET="s"diff --git a/posthog/api/user_integration.py b/posthog/api/user_integration.pyindex ee4f057fa63b..0b3711f19a1f 100644--- a/posthog/api/user_integration.py+++ b/posthog/api/user_integration.py@@ -296,12 +296,12 @@ class UserIntegrationViewSet(viewsets.GenericViewSet):page handles both cases: orgs where the app is installed show "Configure"(no admin needed), orgs where it isn't show "Install" (needs admin).- **First-party app fast path:** when ``connect_from`` is one of- ``APP_CONNECT_FROM_VALUES`` (e.g. ``"posthog_code"`` or ``"posthog_mobile"``),- the current project already has a team-level GitHub installation, and the- user has no ``UserIntegration`` for that installation yet, we skip the org- picker and redirect straight to ``/login/oauth/authorize`` so the user- only authorizes themselves and returns to the originating client immediately.+ **OAuth fast path:** when the current project already has a team-level+ GitHub installation, and the user has no ``UserIntegration`` for that+ installation yet, we skip the org picker and redirect straight to+ ``/login/oauth/authorize`` so the user only authorizes themselves.+ ``connect_from`` is preserved for first-party clients so they return to+ the originating client immediately.In both cases the response key is ``install_url`` for compatibility with callers."""@@ -313,9 +313,10 @@ class UserIntegrationViewSet(viewsets.GenericViewSet):team = _resolve_team_for_github_start(user, self.request)connect_from = request.data.get("connect_from")+ if fast_path_response := _attempt_app_oauth_fast_path(user, team, token, state, connect_from):+ return fast_path_response+if connect_from in APP_CONNECT_FROM_VALUES:- if fast_path_response := _attempt_app_oauth_fast_path(user, team, token, state, connect_from):- return fast_path_responseif _team_github_installation_id(team) is None:cache.set(f"{GITHUB_INSTALL_STATE_CACHE_PREFIX}{token}",@@ -436,10 +437,9 @@ def _attempt_app_oauth_fast_path(user: User, team: Any, token: str, state: str, connect_from: str | None) -> Response | None:"""If the team has a GitHub installation the user hasn't linked yet, return- an OAuth-only ``/login/oauth/authorize`` redirect so first-party app users- (PostHog Code, mobile) authorize and return immediately — no org picker- needed. ``connect_from`` is preserved so the callback returns to the right- client.+ an OAuth-only ``/login/oauth/authorize`` redirect so users authorize against+ the already-installed App — no org picker needed. ``connect_from`` is+ preserved so first-party app callbacks return to the right client.Returns ``None`` when the fast path doesn't apply (no team integration,user already linked, or missing config).diff --git a/products/integrations/frontend/generated/api.ts b/products/integrations/frontend/generated/api.tsindex daa093c55af7..4315496b9c11 100644--- a/products/integrations/frontend/generated/api.ts+++ b/products/integrations/frontend/generated/api.ts@@ -873,12 +873,12 @@ user can pick any GitHub org (new or already connected). GitHub's installpage handles both cases: orgs where the app is installed show "Configure"(no admin needed), orgs where it isn't show "Install" (needs admin).-**First-party app fast path:** when ``connect_from`` is one of-``APP_CONNECT_FROM_VALUES`` (e.g. ``"posthog_code"`` or ``"posthog_mobile"``),-the current project already has a team-level GitHub installation, and the-user has no ``UserIntegration`` for that installation yet, we skip the org-picker and redirect straight to ``/login/oauth/authorize`` so the user-only authorizes themselves and returns to the originating client immediately.+**OAuth fast path:** when the current project already has a team-level+GitHub installation, and the user has no ``UserIntegration`` for that+installation yet, we skip the org picker and redirect straight to+``/login/oauth/authorize`` so the user only authorizes themselves.+``connect_from`` is preserved for first-party clients so they return to+the originating client immediately.In both cases the response key is ``install_url`` for compatibility with callers.* @summary Start GitHub personal integration linkingdiff --git a/products/integrations/frontend/generated/api.zod.ts b/products/integrations/frontend/generated/api.zod.tsindex 02255a45e8e0..81e90e4b8814 100644--- a/products/integrations/frontend/generated/api.zod.ts+++ b/products/integrations/frontend/generated/api.zod.ts@@ -366,12 +366,12 @@ user can pick any GitHub org (new or already connected). GitHub's installpage handles both cases: orgs where the app is installed show "Configure"(no admin needed), orgs where it isn't show "Install" (needs admin).-**First-party app fast path:** when ``connect_from`` is one of-``APP_CONNECT_FROM_VALUES`` (e.g. ``"posthog_code"`` or ``"posthog_mobile"``),-the current project already has a team-level GitHub installation, and the-user has no ``UserIntegration`` for that installation yet, we skip the org-picker and redirect straight to ``/login/oauth/authorize`` so the user-only authorizes themselves and returns to the originating client immediately.+**OAuth fast path:** when the current project already has a team-level+GitHub installation, and the user has no ``UserIntegration`` for that+installation yet, we skip the org picker and redirect straight to+``/login/oauth/authorize`` so the user only authorizes themselves.+``connect_from`` is preserved for first-party clients so they return to+the originating client immediately.In both cases the response key is ``install_url`` for compatibility with callers.* @summary Start GitHub personal integration linking