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.

18.4 KB
Raw Blame
import { MakeLogicType, actions, connect, events, kea, listeners, path, reducers, selectors } from 'kea'
import { loaders } from 'kea-loaders'

import { lemonToast } from '@posthog/lemon-ui'

import api from 'lib/api'
import { githubInstallRequestsLogic } from 'lib/integrations/githubInstallRequestsLogic'
import { describeGithubLinkError } from 'lib/integrations/githubSetupErrors'
import { integrationsLogic } from 'lib/integrations/integrationsLogic'
import { teamLogic } from 'scenes/teamLogic'

import {
    usersIntegrationsGithubDestroy,
    usersIntegrationsGithubStartCreate,
    usersIntegrationsSlackDestroy,
    usersIntegrationsSlackLinkableWorkspacesRetrieve,
    usersIntegrationsSlackStartCreate,
} from '~/generated/core/api'
import type { UserGitHubIntegrationItemApi } from '~/generated/core/api.schemas'

import type { TeamPublicType, TeamType } from '../../../types'

export interface PersonalGitHubIntegration {
    kind: 'github'
    installation_id: string | null
    repository_selection: string | null
    account: { type: string; name: string } | null
    /** The connected user's own GitHub login (distinct from `account`, which is the installation org/user). */
    github_login: string | null
    uses_shared_installation: boolean
    /** When false, disconnecting also uninstalls the App from GitHub. */
    installation_shared?: UserGitHubIntegrationItemApi['installation_shared']
    /** `unavailable` once the App was removed or suspended on GitHub. */
    installation_status?: UserGitHubIntegrationItemApi['installation_status']
    needs_reauthorization?: UserGitHubIntegrationItemApi['needs_reauthorization']
    needs_reauthorization_at?: UserGitHubIntegrationItemApi['needs_reauthorization_at']
    created_at: string | null
}

export interface PersonalSlackIntegration {
    id: string
    kind: 'slack'
    slack_user_id: string
    slack_team_id: string
    slack_team_name: string | null
    slack_email_at_link: string | null
    created_at: string | null
}

export interface LinkableSlackWorkspace {
    posthog_team_id: number
    posthog_team_name: string
    posthog_organization_name: string
    slack_team_id: string
    slack_team_name: string | null
}

const SLACK_LINK_ERROR_MESSAGES: Record<string, string> = {
    access_denied: 'Slack authorization was canceled.',
    invalid_state: 'The Slack link request expired or could not be verified. Please try again.',
    workspace_not_found: 'The Slack workspace is no longer connected to PostHog.',
    flag_off: "Slack identity linking isn't enabled for this organization.",
    exchange_failed: 'Slack rejected the authorization. Please try again.',
    team_mismatch: 'You signed in to a different Slack workspace than the one that started this flow.',
    org_mismatch: "You aren't a member of the PostHog organization connected to this Slack workspace.",
    session_mismatch:
        'This Slack link was started in a different PostHog session. Please start the link again from settings.',
    not_configured: 'Slack is not configured for this PostHog instance.',
}
const SLACK_LINK_ERROR_FALLBACK = 'Could not connect Slack. Please try again.'

const PERSONAL_INTEGRATIONS_POLL_INTERVAL_MS = 30_000

/** Key for stashing the ``connect_from`` URL param across the GitHub install roundtrip.
 *
 * The install flow leaves posthog.com for github.com and comes back, which drops the query
 * string that brought the user here. sessionStorage survives the roundtrip because it's
 * scoped to the tab, not the navigation. */
const CONNECT_FROM_STORAGE_KEY = 'personal_integrations_connect_from'

function readConnectFromStorage(): string | null {
    try {
        return sessionStorage.getItem(CONNECT_FROM_STORAGE_KEY)
    } catch {
        return null
    }
}

function writeConnectFromStorage(value: string | null): void {
    try {
        if (value) {
            sessionStorage.setItem(CONNECT_FROM_STORAGE_KEY, value)
        } else {
            sessionStorage.removeItem(CONNECT_FROM_STORAGE_KEY)
        }
    } catch {
        console.warn('Failed to write connect_from value for account linking redirect, skipping', value)
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface personalIntegrationsLogicValues {
    currentTeam: TeamPublicType | TeamType | null // teamLogic
    githubConnecting: boolean
    healthyIntegrations: PersonalGitHubIntegration[]
    integrations: PersonalGitHubIntegration[]
    integrationsLoading: boolean
    linkableSlackWorkspaces: LinkableSlackWorkspace[]
    linkableSlackWorkspacesLoading: boolean
    pollingSubscribers: number
    slackConnect: boolean
    slackConnectLoading: boolean
    slackIntegrations: PersonalSlackIntegration[]
    slackIntegrationsLoading: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface personalIntegrationsLogicActions {
    loadProjectIntegrations: (_?: any) => any // integrationsLogic
    projectIntegrationsLoaded: (
        integrations: import('../../../types').IntegrationType[],
        payload?: any
    ) => {
        integrations: import('../../../types').IntegrationType[]
        payload?: any
    } // integrationsLogic
    connectGitHub: () => {
        value: true
    }
    connectGitHubFailure: () => {
        value: true
    }
    connectSlack: (payload?: { workspace?: LinkableSlackWorkspace }) => {
        workspace?: LinkableSlackWorkspace
    }
    connectSlackFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    connectSlackSuccess: (
        slackConnect: boolean,
        payload?: {
            workspace?: LinkableSlackWorkspace
        }
    ) => {
        slackConnect: boolean
        payload?: {
            workspace?: LinkableSlackWorkspace
        }
    }
    disconnectGitHub: (installationId: string) => {
        installationId: string
    }
    disconnectSlack: (slackUserId: string) => {
        slackUserId: string
    }
    loadIntegrations: () => any
    loadIntegrationsFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadIntegrationsSuccess: (
        integrations: PersonalGitHubIntegration[],
        payload?: any
    ) => {
        integrations: PersonalGitHubIntegration[]
        payload?: any
    }
    loadLinkableSlackWorkspaces: () => any
    loadLinkableSlackWorkspacesFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadLinkableSlackWorkspacesSuccess: (
        linkableSlackWorkspaces: LinkableSlackWorkspace[],
        payload?: any
    ) => {
        linkableSlackWorkspaces: LinkableSlackWorkspace[]
        payload?: any
    }
    loadSlackIntegrations: () => any
    loadSlackIntegrationsFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadSlackIntegrationsSuccess: (
        slackIntegrations: PersonalSlackIntegration[],
        payload?: any
    ) => {
        slackIntegrations: PersonalSlackIntegration[]
        payload?: any
    }
    startPolling: () => {
        value: true
    }
    stopPolling: () => {
        value: true
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface personalIntegrationsLogicMeta {
    __keaTypeGenInternalSelectorTypes: {
        healthyIntegrations: (integrations: PersonalGitHubIntegration[]) => PersonalGitHubIntegration[]
    }
}

export type personalIntegrationsLogicType = MakeLogicType<
    personalIntegrationsLogicValues,
    personalIntegrationsLogicActions,
    Record<string, any>,
    personalIntegrationsLogicMeta
>

export const personalIntegrationsLogic = kea<personalIntegrationsLogicType>([
    path(['scenes', 'settings', 'user', 'personalIntegrationsLogic']),

    connect(() => ({
        values: [teamLogic, ['currentTeam']],
        actions: [
            integrationsLogic,
            ['loadIntegrations as loadProjectIntegrations', 'loadIntegrationsSuccess as projectIntegrationsLoaded'],
        ],
    })),

    actions({
        connectGitHub: true,
        connectGitHubFailure: true,
        disconnectGitHub: (installationId: string) => ({ installationId }),
        disconnectSlack: (slackUserId: string) => ({ slackUserId }),
        startPolling: true,
        stopPolling: true,
    }),

    reducers({
        // Guards against double-submission: the install request redirects to github.com on success
        // (so it never needs resetting then), and resets on failure so the user can retry.
        githubConnecting: [
            false,
            {
                connectGitHub: () => true,
                connectGitHubFailure: () => false,
            },
        ],
        pollingSubscribers: [
            0,
            {
                startPolling: (state) => state + 1,
                stopPolling: (state) => Math.max(0, state - 1),
            },
        ],
    }),

    loaders(() => ({
        integrations: [
            [] as PersonalGitHubIntegration[],
            {
                loadIntegrations: async () => {
                    // The list endpoint returns a flat `{results: [...]}` payload,
                    // but drf-spectacular auto-wraps `list()` viewset methods in a
                    // paginated schema, so the generated helper would mis-type the
                    // response — call `api.get` directly until the backend serializer
                    // is decorated to bypass pagination.
                    // nosemgrep: prefer-codegen-api -- Legacy raw API call with a hand-written URL and an unchecked response type. Use usersIntegrationsList() from '~/generated/core/api' instead.
                    const response = await api.get<{ results: PersonalGitHubIntegration[] }>(
                        'api/users/@me/integrations/'
                    )
                    return response.results
                },
            },
        ],
        slackIntegrations: [
            [] as PersonalSlackIntegration[],
            {
                loadSlackIntegrations: async () => {
                    // nosemgrep: prefer-codegen-api -- Legacy raw API call with a hand-written URL and an unchecked response type. Use usersIntegrationsList() from '~/generated/core/api' instead.
                    const response = await api.get<{ results: PersonalSlackIntegration[] }>(
                        'api/users/@me/integrations/?kind=slack'
                    )
                    return response.results
                },
            },
        ],
        linkableSlackWorkspaces: [
            [] as LinkableSlackWorkspace[],
            {
                loadLinkableSlackWorkspaces: async () => {
                    const response = await usersIntegrationsSlackLinkableWorkspacesRetrieve('@me')
                    return response.results as LinkableSlackWorkspace[]
                },
            },
        ],
        slackConnect: [
            false as boolean,
            {
                connectSlack: async (payload: { workspace?: LinkableSlackWorkspace } = {}) => {
                    try {
                        const body = payload.workspace
                            ? {
                                  team_id: payload.workspace.posthog_team_id,
                                  slack_team_id: payload.workspace.slack_team_id,
                              }
                            : {}
                        const response = await usersIntegrationsSlackStartCreate('@me', body)
                        window.location.href = response.install_url
                        return true
                    } catch (error: unknown) {
                        // DRF errors come through as ApiError with `.detail` (string|null),
                        // `.data` (the parsed body — often `{detail: '…'}` or an array of strings),
                        // and `.message`. Read `detail` first, then fall back to `data.detail`
                        // / first array entry, then the error message itself. Without this, a
                        // ValidationError like "You're already linked to this Slack workspace."
                        // surfaces only as the generic fallback because `detail` is null on
                        // non-`detail`-shaped DRF bodies.
                        const message =
                            error instanceof Error
                                ? (error as any).detail ||
                                  (error as any).data?.detail ||
                                  (Array.isArray((error as any).data) ? (error as any).data[0] : undefined) ||
                                  error.message
                                : undefined
                        lemonToast.error(message || 'Could not start Slack linking.')
                        return false
                    }
                },
            },
        ],
    })),

    selectors({
        // A flagged row holds no usable credentials, so the connect surface reads this instead of the
        // full list. Otherwise a user whose only install was flagged is offered "add another".
        healthyIntegrations: [
            (s) => [s.integrations],
            (integrations: PersonalGitHubIntegration[]): PersonalGitHubIntegration[] =>
                integrations.filter((integration) => !integration.needs_reauthorization),
        ],
    }),

    listeners(({ actions, values, cache }) => ({
        // Keep the personal GitHub rows current while the section is open, so an App removed on
        // GitHub surfaces its "unavailable" notice without a manual reload. Mirrors the project-side
        // refresh in integrationsLogic; only runs while a mounted surface has subscribed.
        startPolling: () => {
            if (cache.disposables.registry.has('poll')) {
                return
            }
            cache.disposables.add(() => {
                const pollTimer = window.setInterval(
                    () => actions.loadIntegrations(),
                    PERSONAL_INTEGRATIONS_POLL_INTERVAL_MS
                )
                return () => clearInterval(pollTimer)
            }, 'poll')
            cache.disposables.add(
                () => {
                    const onFocus = (): void => actions.loadIntegrations()
                    window.addEventListener('focus', onFocus)
                    return () => window.removeEventListener('focus', onFocus)
                },
                'focusRefetch',
                { pauseOnPageHidden: false }
            )
        },
        stopPolling: () => {
            if (values.pollingSubscribers > 0) {
                return
            }
            cache.disposables.dispose('poll')
            cache.disposables.dispose('focusRefetch')
        },
        projectIntegrationsLoaded: () => {
            // When a project-level integration is added/removed, the backend may
            // auto-create a user-level integration. Reload to pick it up.
            actions.loadIntegrations()
        },
        disconnectSlack: async ({ slackUserId }) => {
            try {
                await usersIntegrationsSlackDestroy('@me', slackUserId)
                lemonToast.success('Unlinked your Slack account')
                actions.loadSlackIntegrations()
                // Refresh linkable so the just-unlinked workspace re-appears
                // in the connect picker without a page reload.
                actions.loadLinkableSlackWorkspaces()
            } catch {
                lemonToast.error('Could not unlink your Slack account.')
            }
        },
        connectGitHub: async () => {
            try {
                const connectFrom = readConnectFromStorage()
                const body: { connect_from?: 'posthog_code'; team_id?: number } = {}
                if (connectFrom === 'posthog_code') {
                    body.connect_from = 'posthog_code'
                }
                if (values.currentTeam?.id) {
                    body.team_id = values.currentTeam.id
                }
                const response = await usersIntegrationsGithubStartCreate('@me', body)
                window.location.href = response.install_url
            } catch (error: unknown) {
                actions.connectGitHubFailure()
                const message = error instanceof Error && 'detail' in error ? (error as any).detail : undefined
                lemonToast.error(message || 'Could not start GitHub installation.')
            }
        },
        disconnectGitHub: async ({ installationId }) => {
            try {
                await usersIntegrationsGithubDestroy('@me', installationId)
                lemonToast.success('Disconnected GitHub installation')
                actions.loadIntegrations()
                actions.loadProjectIntegrations()
            } catch {
                lemonToast.error('Could not disconnect GitHub installation.')
            }
        },
    })),

    events(({ actions }) => ({
        afterMount: () => {
            actions.loadIntegrations()
            actions.loadSlackIntegrations()
            actions.loadLinkableSlackWorkspaces()
            const params = new URLSearchParams(window.location.search)

            // Stash ``connect_from`` so the post-roundtrip success toast can surface a
            // "Return to PostHog Desktop" CTA.
            const connectFrom = params.get('connect_from')
            if (connectFrom) {
                writeConnectFromStorage(connectFrom)
            }

            if (params.has('github_link_success')) {
                writeConnectFromStorage(null)
                lemonToast.success('GitHub connected.')
            } else if (params.has('github_link_error')) {
                writeConnectFromStorage(null)
                const linkError = params.get('github_link_error')
                if (linkError === 'github_install_pending') {
                    // Not a failure: GitHub is waiting on an org owner. The install-requests banner carries the state.
                    lemonToast.info(describeGithubLinkError(linkError))
                    githubInstallRequestsLogic.findMounted()?.actions.loadInstallRequests()
                } else {
                    lemonToast.error(describeGithubLinkError(linkError))
                }
            }

            if (params.has('slack_link_success')) {
                lemonToast.success('Slack connected.')
            } else if (params.has('slack_link_error')) {
                const reason = params.get('slack_link_error') ?? ''
                lemonToast.error(SLACK_LINK_ERROR_MESSAGES[reason] ?? SLACK_LINK_ERROR_FALLBACK)
            }
        },
    })),
])