Skip to content

Commit

Permalink
Fix branch selection when typing as an input (#311)
Browse files Browse the repository at this point in the history
  • Loading branch information
taranvohra authored Nov 29, 2023
1 parent 5a7832c commit c299134
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 21 deletions.
4 changes: 1 addition & 3 deletions integrations/github/src/components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ export const configBlock = createComponent<
...element,
state: {
...element.state,
branch: action.branch.includes('refs/heads/')
? action.branch
: `refs/heads/${action.branch}`,
branch: action.branch,
},
};
case 'toggle.customTemplate':
Expand Down
3 changes: 2 additions & 1 deletion integrations/github/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { configBlock } from './components';
import { getGitHubAppJWT } from './provider';
import { triggerExport, updateCommitWithPreviewLinks } from './sync';
import type { GithubRuntimeContext } from './types';
import { BRANCH_REF_PREFIX } from './utils';
import { handlePullRequestEvents, handlePushEvent, verifyGitHubWebhookSignature } from './webhooks';

const logger = Logger('github');
Expand Down Expand Up @@ -296,7 +297,7 @@ const handleFetchEvent: FetchEventCallback<GithubRuntimeContext> = async (reques
if (!hasSelectedBranch) {
data.push({
id: querySelectedBranch,
label: querySelectedBranch.replace('refs/heads/', ''),
label: querySelectedBranch.replace(BRANCH_REF_PREFIX, ''),
});
}
}
Expand Down
7 changes: 6 additions & 1 deletion integrations/github/src/installation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Logger } from '@gitbook/runtime';
import { fetchRepository } from './api';
import { triggerExport, triggerImport } from './sync';
import { GithubConfigureState, GithubRuntimeContext, GitHubSpaceConfiguration } from './types';
import { assertIsDefined, computeConfigQueryKey } from './utils';
import { assertIsDefined, BRANCH_REF_PREFIX, computeConfigQueryKey } from './utils';

const logger = Logger('github:installation');

Expand All @@ -26,6 +26,11 @@ export async function saveSpaceConfiguration(
throw httpError(400, 'Incomplete configuration');
}

// Make sure the branch is prefixed with refs/heads/
state.branch = state.branch.startsWith(BRANCH_REF_PREFIX)
? state.branch
: BRANCH_REF_PREFIX + state.branch;

const installationId = parseInt(state.installation, 10);
const repoID = parseInt(state.repository, 10);

Expand Down
2 changes: 2 additions & 0 deletions integrations/github/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { GitSyncOperationState, IntegrationSpaceInstallation } from '@gitbook/ap

import type { GitHubSpaceConfiguration } from './types';

export const BRANCH_REF_PREFIX = 'refs/heads/';

/**
* The default commit message to use when a change request is merged in GitBook
*/
Expand Down
4 changes: 1 addition & 3 deletions integrations/gitlab/src/components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,7 @@ export const configBlock = createComponent<
...element,
state: {
...element.state,
branch: action.branch.includes('refs/heads/')
? action.branch
: `refs/heads/${action.branch}`,
branch: action.branch,
},
};
}
Expand Down
9 changes: 7 additions & 2 deletions integrations/gitlab/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import { configBlock } from './components';
import { uninstallWebhook } from './provider';
import { triggerExport, updateCommitWithPreviewLinks } from './sync';
import type { GitLabRuntimeContext, GitLabSpaceConfiguration } from './types';
import { getSpaceConfigOrThrow, assertIsDefined, verifySignature } from './utils';
import {
getSpaceConfigOrThrow,
assertIsDefined,
verifySignature,
BRANCH_REF_PREFIX,
} from './utils';
import { handleMergeRequestEvent, handlePushEvent } from './webhooks';

const logger = Logger('gitlab');
Expand Down Expand Up @@ -203,7 +208,7 @@ const handleFetchEvent: FetchEventCallback<GitLabRuntimeContext> = async (reques
if (!hasSelectedBranch) {
data.push({
id: querySelectedBranch,
label: querySelectedBranch.replace('refs/heads/', ''),
label: querySelectedBranch.replace(BRANCH_REF_PREFIX, ''),
});
}
}
Expand Down
27 changes: 16 additions & 11 deletions integrations/gitlab/src/installation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { fetchProject } from './api';
import { createGitLabWebhookURL, installWebhook } from './provider';
import { triggerExport, triggerImport } from './sync';
import { GitlabConfigureState, GitLabRuntimeContext, GitLabSpaceConfiguration } from './types';
import { assertIsDefined, computeConfigQueryKey, signResponse } from './utils';
import { assertIsDefined, BRANCH_REF_PREFIX, computeConfigQueryKey, signResponse } from './utils';

const logger = Logger('gitlab:installation');

Expand All @@ -16,39 +16,44 @@ const logger = Logger('gitlab:installation');
*/
export async function saveSpaceConfiguration(
context: GitLabRuntimeContext,
config: GitlabConfigureState
state: GitlabConfigureState
) {
const { api, environment } = context;
const spaceInstallation = environment.spaceInstallation;

assertIsDefined(spaceInstallation, { label: 'spaceInstallation' });

if (!config.project || !config.branch) {
if (!state.project || !state.branch) {
throw httpError(400, 'Incomplete configuration');
}

const projectId = parseInt(config.project, 10);
const projectId = parseInt(state.project, 10);

// Make sure the branch is prefixed with refs/heads/
state.branch = state.branch.startsWith(BRANCH_REF_PREFIX)
? state.branch
: BRANCH_REF_PREFIX + state.branch;

/**
* We need to update the space installation external IDs to make sure
* we can query it later when there is a webhook event.
*/
const externalIds: string[] = [];
externalIds.push(computeConfigQueryKey(projectId, config.branch));
externalIds.push(computeConfigQueryKey(projectId, state.branch));

const glProject = await fetchProject(spaceInstallation.configuration, projectId);

const configurationBody: GitLabSpaceConfiguration = {
...spaceInstallation.configuration,
key: config.key || crypto.randomUUID(),
key: state.key || crypto.randomUUID(),
configuredAt: new Date().toISOString(),
project: projectId,
projectName: glProject.path_with_namespace,
branch: config.branch,
projectDirectory: config.projectDirectory,
commitMessageTemplate: config.commitMessageTemplate,
priority: config.priority,
customInstanceUrl: config.customInstanceUrl,
branch: state.branch,
projectDirectory: state.projectDirectory,
commitMessageTemplate: state.commitMessageTemplate,
priority: state.priority,
customInstanceUrl: state.customInstanceUrl,
};

logger.debug(
Expand Down
2 changes: 2 additions & 0 deletions integrations/gitlab/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import type { GitSyncOperationState, IntegrationSpaceInstallation } from '@gitbo

import type { GitLabSpaceConfiguration } from './types';

export const BRANCH_REF_PREFIX = 'refs/heads/';

/**
* The default commit message to use when a change request is merged in GitBook
*/
Expand Down

0 comments on commit c299134

Please sign in to comment.