-
Notifications
You must be signed in to change notification settings - Fork 2
/
gitlab.js
58 lines (51 loc) · 1.4 KB
/
gitlab.js
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
const fetch = require('node-fetch');
async function gitlab(method, path, body = null) {
const response = await fetch(`${gitlab.path}/api/v4${path}?per_page=99999`, {
method,
headers: {
'PRIVATE-TOKEN': gitlab.key,
"Content-Type": 'application/json'
},
body: body ? JSON.stringify(body) : undefined,
});
const json = await response.json();
if (!response.ok) {
throw json;
}
return json;
}
gitlab.gitlab = gitlab;
gitlab.init = async function() {
[ n, p ] = await Promise.all([
gitlab('GET', '/namespaces'),
(async ()=>{
const { id } = await gitlab('GET', '/user');
return await gitlab("GET", `/users/${id}/projects`);
})()
]);
gitlab.namespaces = n;
gitlab.projects = p;
}
gitlab.getNamespaceID = async function (groupName) {
for (let n of gitlab.namespaces) {
if (n.path === groupName) {
return n.id;
}
}
const group = await gitlab('POST', '/groups', {
name: groupName,
path: groupName,
});
console.log("Created group", group);
gitlab.namespaces.push(group);
return group.id;
}
gitlab.getProjectExists = function (projectName) {
for (let p of gitlab.projects) {
if (p.path_with_namespace === projectName) {
return true;
}
}
return false;
}
module.exports = gitlab;