-
Notifications
You must be signed in to change notification settings - Fork 731
Expand file tree
/
Copy pathgetProjects.ts
More file actions
92 lines (80 loc) · 2.6 KB
/
Copy pathgetProjects.ts
File metadata and controls
92 lines (80 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import axios from 'axios'
export async function fetchAllGitlabGroups(accessToken: string) {
const groups = []
let page = 1
let hasMorePages = true
while (hasMorePages) {
const response = await axios.get('https://gitlab.com/api/v4/groups', {
headers: { Authorization: `Bearer ${accessToken}` },
params: { page, per_page: 100 },
})
groups.push(...response.data)
hasMorePages = response.headers['x-next-page'] !== ''
page++
}
return groups.map((group) => ({
id: group.id,
name: group.name as string,
path: group.path as string,
avatarUrl: group.avatar_url as string,
}))
}
async function fetchProjectsForGroup(accessToken: string, group: any) {
const projects = []
let page = 1
let hasMorePages = true
while (hasMorePages) {
const response = await axios.get(`https://gitlab.com/api/v4/groups/${group.id}/projects`, {
headers: { Authorization: `Bearer ${accessToken}` },
params: { page, per_page: 100, archived: false },
})
projects.push(...response.data)
hasMorePages = response.headers['x-next-page'] !== ''
page++
}
return projects.map((project) => ({
groupId: group.id,
groupName: group.name,
groupPath: group.path,
id: project.id,
name: project.name,
path_with_namespace: project.path_with_namespace,
enabled: false,
forkedFrom: project?.forked_from_project?.web_url || null,
}))
}
export async function fetchGitlabGroupProjects(accessToken: string, groups: any[]) {
const CONCURRENCY = 10
const groupProjects: Record<number, any[]> = {}
for (let i = 0; i < groups.length; i += CONCURRENCY) {
const batch = groups.slice(i, i + CONCURRENCY)
const results = await Promise.all(
batch.map((group) => fetchProjectsForGroup(accessToken, group)),
)
batch.forEach((group, idx) => {
groupProjects[group.id] = results[idx]
})
}
return groupProjects
}
export async function fetchGitlabUserProjects(accessToken: string, userId: number) {
const projects = []
let page = 1
let hasMorePages = true
while (hasMorePages) {
const response = await axios.get(`https://gitlab.com/api/v4/users/${userId}/projects`, {
headers: { Authorization: `Bearer ${accessToken}` },
params: { page, per_page: 100, archived: false },
})
projects.push(...response.data)
hasMorePages = response.headers['x-next-page'] !== ''
page++
}
return projects.map((project) => ({
id: project.id,
name: project.name,
path_with_namespace: project.path_with_namespace,
enabled: false,
forkedFrom: project?.forked_from_project?.web_url || null,
}))
}