Skip to content

Commit

Permalink
fix branch names not displaying when typed explicitly for github & gi…
Browse files Browse the repository at this point in the history
…tlab (#300)

fix branch names not displaying when typed explicitly
  • Loading branch information
taranvohra authored Nov 20, 2023
1 parent e31fbfb commit a304397
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 4 deletions.
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

0 comments on commit a304397

Please sign in to comment.