Merge pull request #2966 from Infisical/misc/add-pagination-handling-for-gitlab-groups-fetch

misc: add pagination handling for gitlab groups fetch
This commit is contained in:
Sheen
2025-01-10 19:15:38 +08:00
committed by GitHub
@@ -1,3 +1,5 @@
import { AxiosResponse } from "axios";
import { request } from "@app/lib/config/request"; import { request } from "@app/lib/config/request";
import { BadRequestError } from "@app/lib/errors"; import { BadRequestError } from "@app/lib/errors";
@@ -11,19 +13,27 @@ const getTeamsGitLab = async ({ url, accessToken }: { url: string; accessToken:
const gitLabApiUrl = url ? `${url}/api` : IntegrationUrls.GITLAB_API_URL; const gitLabApiUrl = url ? `${url}/api` : IntegrationUrls.GITLAB_API_URL;
let teams: Team[] = []; let teams: Team[] = [];
const res = ( let page: number = 1;
await request.get<{ name: string; id: string }[]>(`${gitLabApiUrl}/v4/groups`, { while (page > 0) {
headers: { // eslint-disable-next-line no-await-in-loop
Authorization: `Bearer ${accessToken}`, const { data, headers }: AxiosResponse<{ name: string; id: string }[]> = await request.get(
"Accept-Encoding": "application/json" `${gitLabApiUrl}/v4/groups?page=${page}`,
{
headers: {
Authorization: `Bearer ${accessToken}`,
"Accept-Encoding": "application/json"
}
} }
}) );
).data;
teams = res.map((t) => ({ page = Number(headers["x-next-page"] ?? "");
name: t.name, teams = teams.concat(
id: t.id.toString() data.map((t) => ({
})); name: t.name,
id: t.id.toString()
}))
);
}
return teams; return teams;
}; };