From e8f36f8ba5c273b41829bbb49cbd3be7ba68f9cd Mon Sep 17 00:00:00 2001 From: A-D-E Date: Wed, 30 Jul 2025 14:52:25 +0200 Subject: [PATCH] The getGitlabBranches function was only returning the first 20 branches due to GitLab's default API pagination limit. This prevented users from accessing branches in repositories with more than 20 branches. Changes: - Add pagination loop to fetch all branches across multiple pages - Set per_page to 100 (GitLab's maximum) for efficiency - Add safety check using x-total header to prevent unnecessary requests - Follow the same pagination pattern as validateGitlabProvider function Fixes issue where branch selection was limited to first 20 branches in repositories with many branches. --- packages/server/src/utils/providers/gitlab.ts | 45 +++++++++++++------ 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/packages/server/src/utils/providers/gitlab.ts b/packages/server/src/utils/providers/gitlab.ts index 4f9e011a..65c280e0 100644 --- a/packages/server/src/utils/providers/gitlab.ts +++ b/packages/server/src/utils/providers/gitlab.ts @@ -303,22 +303,41 @@ export const getGitlabBranches = async (input: { const gitlabProvider = await findGitlabById(input.gitlabId); - const branchesResponse = await fetch( - `https://gitlab.com/api/v4/projects/${input.id}/repository/branches`, - { - headers: { - Authorization: `Bearer ${gitlabProvider.accessToken}`, - }, - }, - ); + const allBranches = []; + let page = 1; + const perPage = 100; // GitLab's max per page is 100 - if (!branchesResponse.ok) { - throw new Error(`Failed to fetch branches: ${branchesResponse.statusText}`); + while (true) { + const branchesResponse = await fetch( + `https://gitlab.com/api/v4/projects/${input.id}/repository/branches?page=${page}&per_page=${perPage}`, + { + headers: { + Authorization: `Bearer ${gitlabProvider.accessToken}`, + }, + }, + ); + + if (!branchesResponse.ok) { + throw new Error(`Failed to fetch branches: ${branchesResponse.statusText}`); + } + + const branches = await branchesResponse.json(); + + if (branches.length === 0) { + break; + } + + allBranches.push(...branches); + page++; + + // Check if we've reached the total using headers (optional optimization) + const total = branchesResponse.headers.get("x-total"); + if (total && allBranches.length >= Number.parseInt(total)) { + break; + } } - const branches = await branchesResponse.json(); - - return branches as { + return allBranches as { id: string; name: string; commit: {