Skip to content

Commit

Permalink
fix types
Browse files Browse the repository at this point in the history
  • Loading branch information
taranvohra committed Oct 31, 2023
1 parent 6361283 commit 19f8625
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 59 deletions.
21 changes: 13 additions & 8 deletions integrations/github/src/components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -45,15 +50,15 @@ export const configBlock = createComponent<
...element,
state: {
...element.state,
installation: parseInt(action.installation, 10),
installation: action.installation,
},
};
case 'select.repository':
return {
...element,
state: {
...element.state,
repository: parseInt(action.repository, 10),
repository: action.repository,
},
};
case 'select.branch':
Expand Down
12 changes: 6 additions & 6 deletions integrations/github/src/installation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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;
Expand All @@ -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
Expand All @@ -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,
Expand Down
93 changes: 50 additions & 43 deletions integrations/github/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,67 +7,74 @@ export type GitHubSpaceConfiguration = {
expires_at: number;
refresh_token?: string;
};
} & ConfigureProps['spaceInstallation']['configuration'];
} & SpaceInstallationConfiguration;

export type GithubRuntimeEnvironment = RuntimeEnvironment<{}, GitHubSpaceConfiguration>;
export type GithubRuntimeContext = RuntimeContext<GithubRuntimeEnvironment>;

export type ConfigureAction =
export type GithubConfigureAction =
| { action: 'select.installation'; installation: string }
| { action: 'select.repository'; repository: string }
| { action: 'select.branch'; branch: string }
| { action: 'toggle.customTemplate'; withCustomTemplate: boolean }
| { 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;
};
4 changes: 2 additions & 2 deletions integrations/github/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(
Expand Down

0 comments on commit 19f8625

Please sign in to comment.