fix: Load all branches, not just the first 30

This commit is contained in:
Andras Bacsai 2022-04-12 21:48:50 +02:00
parent 0e6abf172b
commit 58763ef84c
2 changed files with 39 additions and 27 deletions

View File

@ -36,8 +36,15 @@
}); });
} }
async function loadBranchesByPage(page = 0) {
return await get(`${apiUrl}/repos/${selected.repository}/branches?per_page=100&page=${page}`, {
Authorization: `token ${$gitTokens.githubToken}`
});
}
let reposSelectOptions; let reposSelectOptions;
let branchSelectOptions; let branchSelectOptions;
async function loadRepositories() { async function loadRepositories() {
let page = 1; let page = 1;
let reposCount = 0; let reposCount = 0;
@ -58,24 +65,27 @@
})); }));
} }
async function loadBranches(event) { async function loadBranches(event) {
branches = [];
selected.repository = event.detail.value; selected.repository = event.detail.value;
let page = 1;
let branchCount = 0;
loading.branches = true; loading.branches = true;
selected.branch = undefined; const loadedBranches = await loadBranchesByPage();
selected.projectId = repositories.find((repo) => repo.full_name === selected.repository).id; branches = branches.concat(loadedBranches);
try { branchCount = branches.length;
branches = await get(`${apiUrl}/repos/${selected.repository}/branches`, { if (branchCount === 100) {
Authorization: `token ${$gitTokens.githubToken}` while (branchCount === 100) {
}); page = page + 1;
branchSelectOptions = branches.map((branch) => ({ const nextBranches = await loadBranchesByPage(page);
value: branch.name, branches = branches.concat(nextBranches);
label: branch.name branchCount = nextBranches.length;
})); }
return;
} catch ({ error }) {
return errorNotification(error);
} finally {
loading.branches = false;
} }
loading.branches = false;
branchSelectOptions = branches.map((branch) => ({
value: branch.name,
label: branch.name
}));
} }
async function isBranchAlreadyUsed(event) { async function isBranchAlreadyUsed(event) {
selected.branch = event.detail.value; selected.branch = event.detail.value;
@ -166,30 +176,36 @@
{:else} {:else}
<form on:submit|preventDefault={handleSubmit} class="flex flex-col justify-center text-center"> <form on:submit|preventDefault={handleSubmit} class="flex flex-col justify-center text-center">
<div class="flex-col space-y-3 md:space-y-0 space-x-1"> <div class="flex-col space-y-3 md:space-y-0 space-x-1">
<div class="flex gap-4"> <div class="flex-col md:flex gap-4">
<div class="custom-select-wrapper"> <div class="custom-select-wrapper">
<Select <Select
placeholder={loading.repositories placeholder={loading.repositories
? 'Loading repositories ...' ? 'Loading repositories...'
: 'Please select a repository'} : 'Please select a repository'}
id="repository" id="repository"
showIndicator={true}
isWaiting={loading.repositories}
on:select={loadBranches} on:select={loadBranches}
items={reposSelectOptions} items={reposSelectOptions}
isDisabled={loading.repositories} isDisabled={loading.repositories}
isClearable={false}
/> />
</div> </div>
<input class="hidden" bind:value={selected.projectId} name="projectId" /> <input class="hidden" bind:value={selected.projectId} name="projectId" />
<div class="custom-select-wrapper"> <div class="custom-select-wrapper">
<Select <Select
placeholder={loading.branches placeholder={loading.branches
? 'Loading branches ...' ? 'Loading branches...'
: !selected.repository : !selected.repository
? 'Please select a repository first' ? 'Please select a repository first'
: 'Please select a branch'} : 'Please select a branch'}
id="repository" isWaiting={loading.branches}
showIndicator={selected.repository}
id="branches"
on:select={isBranchAlreadyUsed} on:select={isBranchAlreadyUsed}
items={branchSelectOptions} items={branchSelectOptions}
isDisabled={loading.branches || !selected.repository} isDisabled={loading.branches || !selected.repository}
isClearable={false}
/> />
</div> </div>
</div> </div>
@ -202,13 +218,6 @@
class:bg-orange-600={showSave} class:bg-orange-600={showSave}
class:hover:bg-orange-500={showSave}>Save</button class:hover:bg-orange-500={showSave}>Save</button
> >
<!-- <button class="w-40"
><a
class="no-underline"
href="{apiUrl}/apps/{application.gitSource.githubApp.name}/installations/new"
>Modify Repositories</a
></button
> -->
</div> </div>
</form> </form>
{/if} {/if}

View File

@ -50,7 +50,10 @@ textarea {
} }
#svelte .listContainer { #svelte .listContainer {
@apply bg-coolgray-400 text-white scrollbar-w-2 scrollbar-thumb-coollabs scrollbar-track-coolgray-200; @apply bg-coolgray-400 text-white scrollbar-w-2 scrollbar-thumb-green-500 scrollbar-track-coolgray-200;
}
#svelte .selectedItem {
@apply pl-3;
} }
#svelte .item.hover { #svelte .item.hover {