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.

5.2 KB
Raw Blame
import { MakeLogicType, connect, kea, path, props } from 'kea'
import { forms } from 'kea-forms'
import type { DeepPartial, DeepPartialMap, FieldName, ValidationErrorType } from 'kea-forms'

import api from 'lib/api'
import { integrationsLogic } from 'lib/integrations/integrationsLogic'
import { lemonToast } from 'lib/lemon-ui/LemonToast'

export interface GitLabSetupModalLogicProps {
    isOpen: boolean
    onComplete: (integrationId?: number) => void
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface gitlabSetupModalLogicValues {
    gitlabIntegration: {
        hostname: string
        projectAccessToken: string
        projectId: string
    }
    gitlabIntegrationAllErrors: Record<string, any>
    gitlabIntegrationChanged: boolean
    gitlabIntegrationErrors: DeepPartialMap<
        {
            hostname: string
            projectAccessToken: string
            projectId: string
        },
        ValidationErrorType
    >
    gitlabIntegrationHasErrors: boolean
    gitlabIntegrationManualErrors: Record<string, any>
    gitlabIntegrationTouched: boolean
    gitlabIntegrationTouches: Record<string, boolean>
    gitlabIntegrationValidationErrors: DeepPartialMap<
        {
            hostname: string
            projectAccessToken: string
            projectId: string
        },
        ValidationErrorType
    >
    isGitlabIntegrationSubmitting: boolean
    isGitlabIntegrationValid: boolean
    showGitlabIntegrationErrors: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface gitlabSetupModalLogicActions {
    loadIntegrations: (_?: any) => any // integrationsLogic
    resetGitlabIntegration: (values?: { hostname: string; projectAccessToken: string; projectId: string }) => {
        values?: {
            hostname: string
            projectAccessToken: string
            projectId: string
        }
    }
    setGitlabIntegrationManualErrors: (errors: Record<string, any>) => {
        errors: Record<string, any>
    }
    setGitlabIntegrationValue: (
        key: FieldName,
        value: any
    ) => {
        name: FieldName
        value: any
    }
    setGitlabIntegrationValues: (
        values: DeepPartial<{
            hostname: string
            projectAccessToken: string
            projectId: string
        }>
    ) => {
        values: DeepPartial<{
            hostname: string
            projectAccessToken: string
            projectId: string
        }>
    }
    submitGitlabIntegration: () => {
        value: boolean
    }
    submitGitlabIntegrationFailure: (
        error: Error,
        errors: Record<string, any>
    ) => {
        error: Error
        errors: Record<string, any>
    }
    submitGitlabIntegrationRequest: (gitlabIntegration: {
        hostname: string
        projectAccessToken: string
        projectId: string
    }) => {
        gitlabIntegration: {
            hostname: string
            projectAccessToken: string
            projectId: string
        }
    }
    submitGitlabIntegrationSuccess: (gitlabIntegration: {
        hostname: string
        projectAccessToken: string
        projectId: string
    }) => {
        gitlabIntegration: {
            hostname: string
            projectAccessToken: string
            projectId: string
        }
    }
    touchGitlabIntegrationField: (key: string) => {
        key: string
    }
}

export type gitlabSetupModalLogicType = MakeLogicType<
    gitlabSetupModalLogicValues,
    gitlabSetupModalLogicActions,
    GitLabSetupModalLogicProps
>

export const gitlabSetupModalLogic = kea<gitlabSetupModalLogicType>([
    path(['integrations', 'gitlab', 'gitlabSetupModalLogic']),
    props({} as GitLabSetupModalLogicProps),
    connect(() => ({
        actions: [integrationsLogic, ['loadIntegrations']],
    })),
    forms(({ props, actions, values }) => ({
        gitlabIntegration: {
            defaults: {
                hostname: 'https://gitlab.com',
                projectId: '',
                projectAccessToken: '',
            },
            errors: ({ hostname, projectId, projectAccessToken }) => ({
                hostname: hostname.trim() ? undefined : 'Hostname is required',
                projectId: projectId.trim() ? undefined : 'Project ID is required',
                projectAccessToken: projectAccessToken.trim() ? undefined : 'Project access token is required',
            }),
            submit: async () => {
                try {
                    const integration = await api.integrations.create({
                        kind: 'gitlab',
                        config: {
                            hostname: values.gitlabIntegration.hostname,
                            project_id: values.gitlabIntegration.projectId,
                            project_access_token: values.gitlabIntegration.projectAccessToken,
                        },
                    })
                    actions.loadIntegrations()
                    lemonToast.success('GitLab integration created successfully!')
                    props.onComplete(integration.id)
                } catch (error: any) {
                    lemonToast.error(error.detail || 'Failed to create GitLab integration')
                    throw error
                }
            },
        },
    })),
])