Skip to content

Commit 0210c52

Browse files
authored
fix: fetch gitlab groups concurrently and improve logs (#4267)
Signed-off-by: Mouad BANI <mouad-mb@outlook.com>
1 parent c1ef5d3 commit 0210c52

2 files changed

Lines changed: 49 additions & 26 deletions

File tree

backend/src/serverless/integrations/usecases/gitlab/getProjects.ts

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,37 +23,48 @@ export async function fetchAllGitlabGroups(accessToken: string) {
2323
}))
2424
}
2525

26-
export async function fetchGitlabGroupProjects(accessToken: string, groups: any[]) {
27-
const groupProjects = {}
26+
async function fetchProjectsForGroup(accessToken: string, group: any) {
27+
const projects = []
28+
let page = 1
29+
let hasMorePages = true
2830

29-
for (const group of groups) {
30-
const projects = []
31-
let page = 1
32-
let hasMorePages = true
31+
while (hasMorePages) {
32+
const response = await axios.get(`https://gitlab.com/api/v4/groups/${group.id}/projects`, {
33+
headers: { Authorization: `Bearer ${accessToken}` },
34+
params: { page, per_page: 100, archived: false },
35+
})
36+
projects.push(...response.data)
37+
hasMorePages = response.headers['x-next-page'] !== ''
38+
page++
39+
}
3340

34-
while (hasMorePages) {
35-
const response = await axios.get(`https://gitlab.com/api/v4/groups/${group.id}/projects`, {
36-
headers: { Authorization: `Bearer ${accessToken}` },
37-
params: { page, per_page: 100, archived: false },
38-
})
39-
projects.push(...response.data)
40-
hasMorePages = response.headers['x-next-page'] !== ''
41-
page++
42-
}
41+
return projects.map((project) => ({
42+
groupId: group.id,
43+
groupName: group.name,
44+
groupPath: group.path,
45+
id: project.id,
46+
name: project.name,
47+
path_with_namespace: project.path_with_namespace,
48+
enabled: false,
49+
forkedFrom: project?.forked_from_project?.web_url || null,
50+
}))
51+
}
4352

44-
groupProjects[group.id] = projects.map((project) => ({
45-
groupId: group.id,
46-
groupName: group.name,
47-
groupPath: group.path,
48-
id: project.id,
49-
name: project.name,
50-
path_with_namespace: project.path_with_namespace,
51-
enabled: false,
52-
forkedFrom: project?.forked_from_project?.web_url || null,
53-
}))
53+
export async function fetchGitlabGroupProjects(accessToken: string, groups: any[]) {
54+
const CONCURRENCY = 10
55+
const groupProjects: Record<number, any[]> = {}
56+
57+
for (let i = 0; i < groups.length; i += CONCURRENCY) {
58+
const batch = groups.slice(i, i + CONCURRENCY)
59+
const results = await Promise.all(
60+
batch.map((group) => fetchProjectsForGroup(accessToken, group)),
61+
)
62+
batch.forEach((group, idx) => {
63+
groupProjects[group.id] = results[idx]
64+
})
5465
}
5566

56-
return groupProjects as Record<number, any[]>
67+
return groupProjects
5768
}
5869

5970
export async function fetchGitlabUserProjects(accessToken: string, userId: number) {

backend/src/services/integrationService.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2774,6 +2774,18 @@ export default class IntegrationService {
27742774

27752775
await SequelizeRepository.commitTransaction(transaction)
27762776
} catch (err) {
2777+
this.options.log.error(
2778+
{
2779+
errMessage: err?.message,
2780+
errName: err?.name,
2781+
errStack: err?.stack,
2782+
gitlabStatus: err?.response?.status,
2783+
gitlabError: err?.response?.data,
2784+
gitlabUrl: err?.config?.url,
2785+
gitlabMethod: err?.config?.method,
2786+
},
2787+
'gitlabConnect failed',
2788+
)
27772789
await SequelizeRepository.rollbackTransaction(transaction)
27782790
throw err
27792791
}

0 commit comments

Comments
 (0)