Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix branch names not displaying when typed explicitly for github & gitlab #300

Merged
merged 1 commit into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion integrations/github/src/components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ export const configBlock = createComponent<
...element,
state: {
...element.state,
branch: action.branch,
branch: action.branch.includes('refs/heads/')
? action.branch
: `refs/heads/${action.branch}`,
},
};
case 'toggle.customTemplate':
Expand Down Expand Up @@ -261,6 +263,8 @@ export const configBlock = createComponent<
query: {
repository:
element.dynamicState('repository'),
selectedBranch:
element.dynamicState('branch'),
v: versionHash,
},
},
Expand Down
19 changes: 18 additions & 1 deletion integrations/github/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,12 +267,14 @@ const handleFetchEvent: FetchEventCallback<GithubRuntimeContext> = async (reques
* API to fetch all branches of an account's repository
*/
router.get('/branches', async (req) => {
const { repository: queryRepository } = req.query;
const { repository: queryRepository, selectedBranch } = req.query;

const repositoryId =
queryRepository && typeof queryRepository === 'string'
? parseInt(queryRepository, 10)
: undefined;
const querySelectedBranch =
selectedBranch && typeof selectedBranch === 'string' ? selectedBranch : undefined;

const branches = repositoryId ? await fetchRepositoryBranches(context, repositoryId) : [];

Expand All @@ -284,6 +286,21 @@ const handleFetchEvent: FetchEventCallback<GithubRuntimeContext> = async (reques
})
);

/**
* When a branch is selected by typing its name, it might not be in the list of branches
* returned by the API. In this case, we add it to the list of branches so that it can be
* selected in the UI.
*/
if (querySelectedBranch) {
const hasSelectedBranch = data.some((branch) => branch.id === querySelectedBranch);
if (!hasSelectedBranch) {
data.push({
id: querySelectedBranch,
label: querySelectedBranch.replace('refs/heads/', ''),
});
}
}

return new Response(JSON.stringify(data), {
headers: {
'Content-Type': 'application/json',
Expand Down
6 changes: 5 additions & 1 deletion integrations/gitlab/src/components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ export const configBlock = createComponent<
...element,
state: {
...element.state,
branch: action.branch,
branch: action.branch.includes('refs/heads/')
? action.branch
: `refs/heads/${action.branch}`,
},
};
}
Expand Down Expand Up @@ -317,6 +319,8 @@ export const configBlock = createComponent<
query: {
project:
element.dynamicState('project'),
selectedBranch:
element.dynamicState('branch'),
v: versionHash,
},
},
Expand Down
19 changes: 18 additions & 1 deletion integrations/gitlab/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ const handleFetchEvent: FetchEventCallback<GitLabRuntimeContext> = async (reques
* API to fetch all branches of a project's repository
*/
router.get('/branches', async (req) => {
const { project: queryProject } = req.query;
const { project: queryProject, selectedBranch } = req.query;

const spaceInstallation = environment.spaceInstallation;
assertIsDefined(spaceInstallation, { label: 'spaceInstallation' });
Expand All @@ -180,6 +180,8 @@ const handleFetchEvent: FetchEventCallback<GitLabRuntimeContext> = async (reques
queryProject && typeof queryProject === 'string'
? parseInt(queryProject, 10)
: undefined;
const querySelectedBranch =
selectedBranch && typeof selectedBranch === 'string' ? selectedBranch : undefined;

const branches = projectId ? await fetchProjectBranches(config, projectId) : [];

Expand All @@ -191,6 +193,21 @@ const handleFetchEvent: FetchEventCallback<GitLabRuntimeContext> = async (reques
})
);

/**
* When a branch is selected by typing its name, it might not be in the list of branches
* returned by the API. In this case, we add it to the list of branches so that it can be
* selected in the UI.
*/
if (querySelectedBranch) {
const hasSelectedBranch = data.some((branch) => branch.id === querySelectedBranch);
if (!hasSelectedBranch) {
data.push({
id: querySelectedBranch,
label: querySelectedBranch.replace('refs/heads/', ''),
});
}
}

return new Response(JSON.stringify(data), {
headers: {
'Content-Type': 'application/json',
Expand Down