Skip to content

Commit ccac053

Browse files
committed
feat: fetch group projects concurrently
Signed-off-by: Mouad BANI <mouad-mb@outlook.com>
1 parent e516309 commit ccac053

1 file changed

Lines changed: 36 additions & 26 deletions

File tree

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

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,37 +23,47 @@ 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 groupProjects: Record<number, any[]> = {}
55+
56+
for (let i = 0; i < groups.length; i += CONCURRENCY) {
57+
const batch = groups.slice(i, i + CONCURRENCY)
58+
const results = await Promise.all(
59+
batch.map((group) => fetchProjectsForGroup(accessToken, group)),
60+
)
61+
batch.forEach((group, idx) => {
62+
groupProjects[group.id] = results[idx]
63+
})
5464
}
5565

56-
return groupProjects as Record<number, any[]>
66+
return groupProjects
5767
}
5868

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

0 commit comments

Comments
 (0)