Skip to content

Commit a304397

Browse files
authored
fix branch names not displaying when typed explicitly for github & gitlab (#300)
fix branch names not displaying when typed explicitly
1 parent e31fbfb commit a304397

File tree

4 files changed

+46
-4
lines changed

4 files changed

+46
-4
lines changed

integrations/github/src/components.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ export const configBlock = createComponent<
6666
...element,
6767
state: {
6868
...element.state,
69-
branch: action.branch,
69+
branch: action.branch.includes('refs/heads/')
70+
? action.branch
71+
: `refs/heads/${action.branch}`,
7072
},
7173
};
7274
case 'toggle.customTemplate':
@@ -261,6 +263,8 @@ export const configBlock = createComponent<
261263
query: {
262264
repository:
263265
element.dynamicState('repository'),
266+
selectedBranch:
267+
element.dynamicState('branch'),
264268
v: versionHash,
265269
},
266270
},

integrations/github/src/index.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,12 +267,14 @@ const handleFetchEvent: FetchEventCallback<GithubRuntimeContext> = async (reques
267267
* API to fetch all branches of an account's repository
268268
*/
269269
router.get('/branches', async (req) => {
270-
const { repository: queryRepository } = req.query;
270+
const { repository: queryRepository, selectedBranch } = req.query;
271271

272272
const repositoryId =
273273
queryRepository && typeof queryRepository === 'string'
274274
? parseInt(queryRepository, 10)
275275
: undefined;
276+
const querySelectedBranch =
277+
selectedBranch && typeof selectedBranch === 'string' ? selectedBranch : undefined;
276278

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

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

289+
/**
290+
* When a branch is selected by typing its name, it might not be in the list of branches
291+
* returned by the API. In this case, we add it to the list of branches so that it can be
292+
* selected in the UI.
293+
*/
294+
if (querySelectedBranch) {
295+
const hasSelectedBranch = data.some((branch) => branch.id === querySelectedBranch);
296+
if (!hasSelectedBranch) {
297+
data.push({
298+
id: querySelectedBranch,
299+
label: querySelectedBranch.replace('refs/heads/', ''),
300+
});
301+
}
302+
}
303+
287304
return new Response(JSON.stringify(data), {
288305
headers: {
289306
'Content-Type': 'application/json',

integrations/gitlab/src/components.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ export const configBlock = createComponent<
106106
...element,
107107
state: {
108108
...element.state,
109-
branch: action.branch,
109+
branch: action.branch.includes('refs/heads/')
110+
? action.branch
111+
: `refs/heads/${action.branch}`,
110112
},
111113
};
112114
}
@@ -317,6 +319,8 @@ export const configBlock = createComponent<
317319
query: {
318320
project:
319321
element.dynamicState('project'),
322+
selectedBranch:
323+
element.dynamicState('branch'),
320324
v: versionHash,
321325
},
322326
},

integrations/gitlab/src/index.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ const handleFetchEvent: FetchEventCallback<GitLabRuntimeContext> = async (reques
169169
* API to fetch all branches of a project's repository
170170
*/
171171
router.get('/branches', async (req) => {
172-
const { project: queryProject } = req.query;
172+
const { project: queryProject, selectedBranch } = req.query;
173173

174174
const spaceInstallation = environment.spaceInstallation;
175175
assertIsDefined(spaceInstallation, { label: 'spaceInstallation' });
@@ -180,6 +180,8 @@ const handleFetchEvent: FetchEventCallback<GitLabRuntimeContext> = async (reques
180180
queryProject && typeof queryProject === 'string'
181181
? parseInt(queryProject, 10)
182182
: undefined;
183+
const querySelectedBranch =
184+
selectedBranch && typeof selectedBranch === 'string' ? selectedBranch : undefined;
183185

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

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

196+
/**
197+
* When a branch is selected by typing its name, it might not be in the list of branches
198+
* returned by the API. In this case, we add it to the list of branches so that it can be
199+
* selected in the UI.
200+
*/
201+
if (querySelectedBranch) {
202+
const hasSelectedBranch = data.some((branch) => branch.id === querySelectedBranch);
203+
if (!hasSelectedBranch) {
204+
data.push({
205+
id: querySelectedBranch,
206+
label: querySelectedBranch.replace('refs/heads/', ''),
207+
});
208+
}
209+
}
210+
194211
return new Response(JSON.stringify(data), {
195212
headers: {
196213
'Content-Type': 'application/json',

0 commit comments

Comments
 (0)