Skip to content

: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.

fix(data-warehouse): keep the source kind when reconnecting an integration (#111193)

Tom Owers5ce35209ffbf
diff --git a/frontend/src/lib/integrations/IntegrationScopesWarning.tsx b/frontend/src/lib/integrations/IntegrationScopesWarning.tsx
index 2b669a9234a9..af69fd1f756c 100644
--- a/frontend/src/lib/integrations/IntegrationScopesWarning.tsx
+++ b/frontend/src/lib/integrations/IntegrationScopesWarning.tsx
@@ -1,7 +1,6 @@
import { useActions } from 'kea'
import { useMemo } from 'react'
-import api from 'lib/api'
import { LemonBanner } from 'lib/lemon-ui/LemonBanner'
import { Link } from 'lib/lemon-ui/Link'
import { eventUsageLogic } from 'lib/utils/eventUsageLogic'
@@ -9,6 +8,7 @@ import { eventUsageLogic } from 'lib/utils/eventUsageLogic'
import { IntegrationType } from '~/types'
import { useIntegrationManagementRestriction } from './integrationPermissions'
+import { integrationAuthorizeUrl, reconnectReturnUrl } from './integrationsLogic'
/**
* Extract the granted OAuth scopes from an integration's stored config. Tolerates the
@@ -69,9 +69,9 @@ export function IntegrationScopesWarning({
action={{
children: 'Reconnect',
disableClientSideRouting: true,
- to: api.integrations.authorizeUrl({
+ to: integrationAuthorizeUrl({
kind: integration.kind,
- next: window.location.pathname,
+ next: reconnectReturnUrl(window.location.pathname, window.location.search),
}),
onClick: () =>
reportIntegrationConnectClicked(integration.kind, integration.kind, 'missing_scopes_reconnect'),
diff --git a/frontend/src/lib/integrations/IntegrationView.tsx b/frontend/src/lib/integrations/IntegrationView.tsx
index 8c741d73a1b3..2b7317d43188 100644
--- a/frontend/src/lib/integrations/IntegrationView.tsx
+++ b/frontend/src/lib/integrations/IntegrationView.tsx
@@ -19,7 +19,7 @@ import { urls } from 'scenes/urls'
import { IntegrationType } from '~/types'
-import { integrationsLogic } from './integrationsLogic'
+import { integrationAuthorizeUrl, integrationsLogic, reconnectReturnUrl } from './integrationsLogic'
import { DARK_MODE_INVERT_ICON_KINDS, getIntegrationNameFromKind } from './utils'
export function IntegrationView({
@@ -174,9 +174,9 @@ export function IntegrationView({
type="secondary"
size="small"
disableClientSideRouting
- to={api.integrations.authorizeUrl({
+ to={integrationAuthorizeUrl({
kind: integration.kind,
- next: window.location.pathname,
+ next: reconnectReturnUrl(window.location.pathname, window.location.search),
})}
onClick={() =>
reportIntegrationConnectClicked(
@@ -200,9 +200,9 @@ export function IntegrationView({
action={{
children: 'Reconnect',
disableClientSideRouting: true,
- to: api.integrations.authorizeUrl({
+ to: integrationAuthorizeUrl({
kind: integration.kind,
- next: window.location.pathname,
+ next: reconnectReturnUrl(window.location.pathname, window.location.search),
}),
onClick: () =>
reportIntegrationConnectClicked(
diff --git a/frontend/src/lib/integrations/integrationsLogic.test.ts b/frontend/src/lib/integrations/integrationsLogic.test.ts
index 7bff2ff2809e..3c0c559d2679 100644
--- a/frontend/src/lib/integrations/integrationsLogic.test.ts
+++ b/frontend/src/lib/integrations/integrationsLogic.test.ts
@@ -19,7 +19,7 @@ import type {
IntegrationConfigApi,
} from 'products/integrations/frontend/generated/api.schemas'
-import { integrationsLogic } from './integrationsLogic'
+import { integrationsLogic, reconnectReturnUrl } from './integrationsLogic'
const githubIntegration = (overrides: Partial<IntegrationType> = {}): IntegrationType =>
({
@@ -358,6 +358,29 @@ describe('integrationsLogic', () => {
})
})
+ describe('reconnectReturnUrl', () => {
+ it.each([
+ [
+ '/project/1/data-warehouse/new-source',
+ '?kind=googlesearchconsole',
+ '/project/1/data-warehouse/new-source?kind=googlesearchconsole',
+ ],
+ [
+ '/project/1/data-warehouse/new-source',
+ '?kind=googlesearchconsole&integration_id=5&integration_error=access_denied',
+ '/project/1/data-warehouse/new-source?kind=googlesearchconsole',
+ ],
+ [
+ '/project/1/data-warehouse/connect',
+ '?kind=GoogleAds',
+ '/project/1/data-warehouse/connect?kind=GoogleAds',
+ ],
+ ['/project/1/data-warehouse/sources/abc', '', '/project/1/data-warehouse/sources/abc'],
+ ])('returns a reconnect from %s%s to the same wizard step', (pathname, search, expected) => {
+ expect(reconnectReturnUrl(pathname, search)).toEqual(expected)
+ })
+ })
+
describe('handleOauthCallback', () => {
it('redirects stripe marketplace callbacks to the confirmation page without POSTing', async () => {
await expectLogic(logic, () => {
diff --git a/frontend/src/lib/integrations/integrationsLogic.ts b/frontend/src/lib/integrations/integrationsLogic.ts
index ccffee890862..b4c8d3b3d3a8 100644
--- a/frontend/src/lib/integrations/integrationsLogic.ts
+++ b/frontend/src/lib/integrations/integrationsLogic.ts
@@ -71,6 +71,22 @@ function landsOnErrorAwarePage(pathname: string): boolean {
// pinned: URL search param — the callback redirect and the landing page both read it.
export const OAUTH_INTEGRATION_ID_PARAM = 'integration_id'
+export function integrationAuthorizeUrl(params: Parameters<typeof api.integrations.authorizeUrl>[0]): string {
+ // nosemgrep: prefer-codegen-api-namespaced-integrations -- generated client does not support the authorize query parameters
+ return api.integrations.authorizeUrl(params)
+}
+
+/** Where an in-place reconnect returns to. Pages like the data warehouse source wizard keep their
+ * state in the query (`?kind=`), so a bare pathname drops the user somewhere else. The previous
+ * callback's result params are dropped because the new callback sets its own. */
+export function reconnectReturnUrl(pathname: string, search: string): string {
+ const params = new URLSearchParams(search)
+ params.delete(OAUTH_INTEGRATION_ID_PARAM)
+ params.delete(INTEGRATION_ERROR_PARAM)
+ const query = params.toString()
+ return query ? `${pathname}?${query}` : pathname
+}
+
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface integrationsLogicValues {
preflight: PreflightStatus | null // preflightLogic
diff --git a/products/data_warehouse/frontend/shared/components/forms/IntegrationAccountSelector.test.tsx b/products/data_warehouse/frontend/shared/components/forms/IntegrationAccountSelector.test.tsx
index 1836efb069c0..ac7cb00ce309 100644
--- a/products/data_warehouse/frontend/shared/components/forms/IntegrationAccountSelector.test.tsx
+++ b/products/data_warehouse/frontend/shared/components/forms/IntegrationAccountSelector.test.tsx
@@ -1,4 +1,4 @@
-import { accountsDropdownEmptyMessage, reconnectReturnUrl } from './IntegrationAccountSelector'
+import { accountsDropdownEmptyMessage } from './IntegrationAccountSelector'
describe('IntegrationAccountSelector', () => {
// A failed listing request leaves the same empty list as a connection that reaches no accounts,
@@ -9,20 +9,4 @@ describe('IntegrationAccountSelector', () => {
)
expect(accountsDropdownEmptyMessage(null)).toContain('No accounts accessible')
})
-
- it.each([
- [
- '/project/1/data-warehouse/new-source',
- '?kind=googlesearchconsole',
- '/project/1/data-warehouse/new-source?kind=googlesearchconsole',
- ],
- [
- '/project/1/data-warehouse/new-source',
- '?kind=googlesearchconsole&integration_id=5&integration_error=access_denied',
- '/project/1/data-warehouse/new-source?kind=googlesearchconsole',
- ],
- ['/project/1/data-warehouse/sources/abc', '', '/project/1/data-warehouse/sources/abc'],
- ])('returns a reconnect from %s%s to the same wizard step', (pathname, search, expected) => {
- expect(reconnectReturnUrl(pathname, search)).toEqual(expected)
- })
})
diff --git a/products/data_warehouse/frontend/shared/components/forms/IntegrationAccountSelector.tsx b/products/data_warehouse/frontend/shared/components/forms/IntegrationAccountSelector.tsx
index 7a873e63068f..f25834b3ac84 100644
--- a/products/data_warehouse/frontend/shared/components/forms/IntegrationAccountSelector.tsx
+++ b/products/data_warehouse/frontend/shared/components/forms/IntegrationAccountSelector.tsx
@@ -4,10 +4,8 @@ import { useContext, useEffect, useMemo, useRef } from 'react'
import { LemonInput, LemonInputSelect, LemonSkeleton, LemonTag, Link } from '@posthog/lemon-ui'
-import api from 'lib/api'
import { integrationAccountsLogic } from 'lib/integrations/integrationAccountsLogic'
-import { integrationsLogic } from 'lib/integrations/integrationsLogic'
-import { INTEGRATION_ERROR_PARAM } from 'lib/integrations/oauthCallbackErrors'
+import { integrationAuthorizeUrl, integrationsLogic, reconnectReturnUrl } from 'lib/integrations/integrationsLogic'
import { getIntegrationNameFromKind } from 'lib/integrations/utils'
import { LemonField } from 'lib/lemon-ui/LemonField'
import type { LemonInputSelectOption } from 'lib/lemon-ui/LemonInputSelect/LemonInputSelect'
@@ -192,17 +190,6 @@ function captionHelp(caption?: string): JSX.Element | undefined {
return caption ? <LemonMarkdown className="text-xs">{caption}</LemonMarkdown> : undefined
}
-/** Where the in-place reconnect returns to. The wizard keeps the chosen source in `?kind=`, so a
- * bare pathname lands the user back on the source catalog. The previous callback's result params
- * are dropped because the new callback sets its own. */
-export function reconnectReturnUrl(pathname: string, search: string): string {
- const params = new URLSearchParams(search)
- params.delete('integration_id')
- params.delete(INTEGRATION_ERROR_PARAM)
- const query = params.toString()
- return query ? `${pathname}?${query}` : pathname
-}
-
/** Re-run OAuth for the connected integration in place, so a failed account load is recoverable
* without hunting for the disconnect/reconnect action elsewhere on the page. */
function ReconnectLink({ integrationKind }: { integrationKind: string }): JSX.Element {
@@ -211,7 +198,7 @@ function ReconnectLink({ integrationKind }: { integrationKind: string }): JSX.El
return (
<Link
disableClientSideRouting
- to={api.integrations.authorizeUrl({
+ to={integrationAuthorizeUrl({
kind: integrationKind,
next: reconnectReturnUrl(window.location.pathname, window.location.search),
})}