diff --git a/integrations/github/src/components.tsx b/integrations/github/src/components.tsx index 3eb05d2e5..28dc48d68 100644 --- a/integrations/github/src/components.tsx +++ b/integrations/github/src/components.tsx @@ -5,23 +5,28 @@ import { createComponent } from '@gitbook/runtime'; import { extractTokenCredentialsOrThrow } from './api'; import { saveSpaceConfiguration } from './installation'; -import { ConfigureAction, ConfigureProps, ConfigureState, GithubRuntimeContext } from './types'; +import { + GithubConfigureAction, + GithubConfigureProps, + GithubConfigureState, + GithubRuntimeContext, +} from './types'; import { assertIsDefined, getGitSyncCommitMessage, GITSYNC_DEFAULT_COMMIT_MESSAGE } from './utils'; /** * ContentKit component to configure the GitHub integration. */ export const configBlock = createComponent< - ConfigureProps, - ConfigureState, - ConfigureAction, + GithubConfigureProps, + GithubConfigureState, + GithubConfigureAction, GithubRuntimeContext >({ componentId: 'configure', initialState: (props) => { return { - installation: props.spaceInstallation.configuration?.installation, - repository: props.spaceInstallation.configuration?.repository, + installation: `${props.spaceInstallation.configuration?.installation}`, + repository: `${props.spaceInstallation.configuration?.repository}`, branch: props.spaceInstallation.configuration?.branch, projectDirectory: props.spaceInstallation.configuration?.projectDirectory, withCustomTemplate: Boolean( @@ -45,7 +50,7 @@ export const configBlock = createComponent< ...element, state: { ...element.state, - installation: parseInt(action.installation, 10), + installation: action.installation, }, }; case 'select.repository': @@ -53,7 +58,7 @@ export const configBlock = createComponent< ...element, state: { ...element.state, - repository: parseInt(action.repository, 10), + repository: action.repository, }, }; case 'select.branch': diff --git a/integrations/github/src/installation.ts b/integrations/github/src/installation.ts index c82605cca..418f06e3e 100644 --- a/integrations/github/src/installation.ts +++ b/integrations/github/src/installation.ts @@ -5,7 +5,7 @@ import { Logger } from '@gitbook/runtime'; import { fetchRepository } from './api'; import { triggerExport, triggerImport } from './sync'; -import { GithubRuntimeContext, GitHubSpaceConfiguration } from './types'; +import { GithubConfigureState, GithubRuntimeContext, GitHubSpaceConfiguration } from './types'; import { assertIsDefined, computeConfigQueryKey } from './utils'; const logger = Logger('github:installation'); @@ -15,7 +15,7 @@ const logger = Logger('github:installation'); */ export async function saveSpaceConfiguration( context: GithubRuntimeContext, - state: GitHubSpaceConfiguration + state: GithubConfigureState ) { const { api, environment } = context; const spaceInstallation = environment.spaceInstallation; @@ -26,8 +26,8 @@ export async function saveSpaceConfiguration( throw httpError(400, 'Incomplete configuration'); } - const installationId = state.installation; - const repoID = state.repository; + const installationId = parseInt(state.installation, 10); + const repoID = parseInt(state.repository, 10); /** * We need to update the space installation external IDs to make sure @@ -42,8 +42,8 @@ export async function saveSpaceConfiguration( const configurationBody: GitHubSpaceConfiguration = { ...spaceInstallation.configuration, key: crypto.randomUUID(), - installation: state.installation, - repository: state.repository, + installation: installationId, + repository: repoID, branch: state.branch, commitMessageTemplate: state.commitMessageTemplate, previewExternalBranches: state.previewExternalBranches, diff --git a/integrations/github/src/types.ts b/integrations/github/src/types.ts index a5df7be3e..94cbd024e 100644 --- a/integrations/github/src/types.ts +++ b/integrations/github/src/types.ts @@ -7,12 +7,12 @@ export type GitHubSpaceConfiguration = { expires_at: number; refresh_token?: string; }; -} & ConfigureProps['spaceInstallation']['configuration']; +} & SpaceInstallationConfiguration; export type GithubRuntimeEnvironment = RuntimeEnvironment<{}, GitHubSpaceConfiguration>; export type GithubRuntimeContext = RuntimeContext; -export type ConfigureAction = +export type GithubConfigureAction = | { action: 'select.installation'; installation: string } | { action: 'select.repository'; repository: string } | { action: 'select.branch'; branch: string } @@ -20,54 +20,61 @@ export type ConfigureAction = | { action: 'preview.commitMessage' } | { action: 'save.config' }; -export type ConfigureProps = { +type SpaceInstallationConfiguration = { + /** + * A key to uniquely identify the configuration. + */ + key?: string; + /** + * The installation ID of the GitHub App. + */ + installation?: number; + /** + * Owner of the repository + */ + accountName?: string; + /** + * The repository ID to be used for the integration. + */ + repository?: number; + /** + * Name of the repository for the selected repository ID. + */ + repoName?: string; + /** + * The branch to be used for the integration. + */ + branch?: string; + /** + * Root folder to use for monorepos with multiple spaces synced. + */ + projectDirectory?: string; + /** + * Template to use for commit messages. + */ + commitMessageTemplate?: string; + /** + * Whether to generate preview from branches external to the repository + */ + previewExternalBranches?: boolean; + priority: 'github' | 'gitbook'; +}; + +export type GithubConfigureProps = { installation: { configuration?: IntegrationInstallationConfiguration; }; spaceInstallation: { - configuration?: { - /** - * A key to uniquely identify the configuration. - */ - key?: string; - /** - * The installation ID of the GitHub App. - */ - installation?: number; - /** - * Owner of the repository - */ - accountName?: string; - /** - * The repository ID to be used for the integration. - */ - repository?: number; - /** - * Name of the repository for the selected repository ID. - */ - repoName?: string; - /** - * The branch to be used for the integration. - */ - branch?: string; - /** - * Root folder to use for monorepos with multiple spaces synced. - */ - projectDirectory?: string; - /** - * Template to use for commit messages. - */ - commitMessageTemplate?: string; - /** - * Whether to generate preview from branches external to the repository - */ - previewExternalBranches?: boolean; - priority: 'github' | 'gitbook'; - }; + configuration?: SpaceInstallationConfiguration; }; }; -export type ConfigureState = ConfigureProps['spaceInstallation']['configuration'] & { +export type GithubConfigureState = Omit< + SpaceInstallationConfiguration, + 'installation' | 'repository' +> & { + installation?: string; + repository?: string; withCustomTemplate?: boolean; commitMessagePreview?: string; }; diff --git a/integrations/github/src/utils.ts b/integrations/github/src/utils.ts index 58f2bc0cf..81e86ed6d 100644 --- a/integrations/github/src/utils.ts +++ b/integrations/github/src/utils.ts @@ -67,8 +67,8 @@ export function computeConfigQueryKey( ref: string, previewExternalBranches?: boolean ): string { - const base = `ins:${installationId}/rep:${repoID}/br:${ref}`; - return previewExternalBranches ? `${base}/prv:${previewExternalBranches}` : base; + const base = `ins:${installationId}:rep:${repoID}:br:${ref}`; + return previewExternalBranches ? `${base}:prv:${previewExternalBranches}` : base; } export function assertIsDefined(