-
Notifications
You must be signed in to change notification settings - Fork 148
/
groups.ts
76 lines (65 loc) · 2.17 KB
/
groups.ts
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
import * as R from 'ramda';
import * as gitlabApi from '@lagoon/commons/dist/gitlab/api';
import {
sanitizeGroupName,
addGroupWithParent,
addGroup,
addUserToGroup
} from '@lagoon/commons/dist/api';
import { logger } from '@lagoon/commons/dist/logs/local-logger';
interface GitlabGroup {
id: number,
name: string,
full_path: string,
parent_id: number,
};
const groupExistsRegex = /Group.*?exists/;
const sortGroupsByHierarchy = R.sortBy(R.path(['full_path']));
const convertRoleNumberToString = R.cond([
[R.equals(10), R.always('GUEST')],
[R.equals(20), R.always('REPORTER')],
[R.equals(30), R.always('DEVELOPER')],
[R.equals(40), R.always('MAINTAINER')],
[R.equals(50), R.always('OWNER')],
]);
const syncGroup = async group => {
const groupName = sanitizeGroupName(group.full_path);
logger.debug(`Processing ${group.name} (${groupName})`);
try {
if (group.parent_id) {
const parentGroup = await gitlabApi.getGroup(group.parent_id);
await addGroupWithParent(groupName, sanitizeGroupName(parentGroup.full_path));
} else {
await addGroup(groupName);
}
} catch (err) {
if (!R.test(groupExistsRegex, err.message)) {
throw new Error(`Could not sync (add) gitlab group ${group.name} id ${group.id}: ${err.message}`);
}
}
const groupMembers = await gitlabApi.getGroupMembers(group.id);
for (const member of groupMembers) {
const user = await gitlabApi.getUser(member.id);
await addUserToGroup(user.email, groupName, convertRoleNumberToString(member.access_level));
}
};
(async () => {
const allGroups = await gitlabApi.getAllGroups();
let groupsQueue = sortGroupsByHierarchy(allGroups).map(group => ({ group, retries: 0}));
logger.info(`Syncing ${allGroups.length} groups`);
while (groupsQueue.length > 0) {
const { group, retries } = groupsQueue.shift();
try {
await syncGroup(group);
} catch (err) {
if (retries < 3) {
logger.warn(`Error syncing, adding to end of queue: ${err.message}`);
groupsQueue.push({ group, retries: retries + 1 });
}
else {
logger.error(`Sync failed: ${err.message}`);
}
}
}
logger.info('Sync completed');
})()