Merge pull request #2279 from A-D-E/fix/gitlab-branches-pagination

The getGitlabBranches function was only returning the first 20 branches
This commit is contained in:
Mauricio Siu
2025-08-02 12:48:32 -06:00
committed by GitHub

View File

@@ -4,8 +4,8 @@ import { paths } from "@dokploy/server/constants";
import type { apiGitlabTestConnection } from "@dokploy/server/db/schema"; import type { apiGitlabTestConnection } from "@dokploy/server/db/schema";
import type { Compose } from "@dokploy/server/services/compose"; import type { Compose } from "@dokploy/server/services/compose";
import { import {
type Gitlab,
findGitlabById, findGitlabById,
type Gitlab,
updateGitlab, updateGitlab,
} from "@dokploy/server/services/gitlab"; } from "@dokploy/server/services/gitlab";
import type { InferResultType } from "@dokploy/server/types/with"; import type { InferResultType } from "@dokploy/server/types/with";
@@ -310,22 +310,43 @@ export const getGitlabBranches = async (input: {
const gitlabProvider = await findGitlabById(input.gitlabId); const gitlabProvider = await findGitlabById(input.gitlabId);
const branchesResponse = await fetch( const allBranches = [];
`${gitlabProvider.gitlabUrl}/api/v4/projects/${input.id}/repository/branches`, let page = 1;
{ const perPage = 100; // GitLab's max per page is 100
headers: {
Authorization: `Bearer ${gitlabProvider.accessToken}`,
},
},
);
if (!branchesResponse.ok) { while (true) {
throw new Error(`Failed to fetch branches: ${branchesResponse.statusText}`); 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 allBranches as {
return branches as {
id: string; id: string;
name: string; name: string;
commit: { commit: {