-
Notifications
You must be signed in to change notification settings - Fork 1
/
github.js
263 lines (247 loc) · 8.38 KB
/
github.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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import {
Octokit
} from '@octokit/core';
import fs from 'fs';
const octokit = new Octokit({
auth: process.argv[2]
});
const orgName = 'company'
async function getOrgUsers() {
const org = await octokit.request('GET /orgs/{org}', {
org: orgName
});
let githubUsersList = [];
console.log('Seats: ' + org.data.plan.filled_seats);
const ghpages = Math.ceil(org.data.plan.filled_seats / 100);
for (let i = 0; i < ghpages; i++) {
const githubUsers = await octokit.request('GET /orgs/{org}/members', {
org: orgName,
per_page: 100,
page: i + 1
});
githubUsersList = githubUsersList.concat(githubUsers.data);
}
console.log('Employees: ' + githubUsersList.length);
return githubUsersList;
}
async function getOrgPendingInvitations() {
const githubUsers = await octokit.request('GET /orgs/{org}/invitations', {
org: orgName,
per_page: 100
})
return githubUsers.data;
}
async function sendOrgInvitation(id) {
const invite = await octokit.request('POST /orgs/{org}/invitations', {
org: orgName,
invitee_id: id
})
return invite.data;
}
async function removeOrgUser(username) {
const response = await octokit.request('DELETE /orgs/{org}/members/{username}', {
org: orgName,
username: username
})
return response.data;
}
async function getUser(username) {
const user = await octokit.request('GET /users/{username}', {
username: username
})
return user.data;
}
async function getOrgTeams() {
const teams = await octokit.request('GET /orgs/{org}/teams', {
org: orgName,
per_page: 100
})
return teams.data;
}
async function getOrgTeam(name) {
const team = await octokit.request('GET /orgs/{org}/teams/{team_slug}', {
org: orgName,
team_slug: name
})
return team.data;
}
async function createOrgTeam(name, description, parent_team_id) {
const team = await octokit.request('POST /orgs/{org}/teams', {
org: orgName,
name,
privacy: 'closed',
description,
parent_team_id
})
return team.data;
}
async function removeOrgTeam(team_slug) {
const response = await octokit.request('DELETE /orgs/{org}/teams/{team_slug}', {
org: orgName,
team_slug
})
return response.data;
}
async function updateOrgTeam(name, description, parent_team_id) {
const team = await octokit.request('PATCH /orgs/{org}/teams/{team_slug}', {
org: orgName,
name,
team_slug: name,
privacy: 'closed',
description,
parent_team_id
})
return team.data;
}
async function addMemberToTeam(username, team_slug) {
const response = await octokit.request('PUT /orgs/{org}/teams/{team_slug}/memberships/{username}', {
org: orgName,
team_slug,
username,
role: 'member'
});
return response.data;
}
async function listTeamMembers(team_slug) {
const response = await octokit.request('GET /orgs/{org}/teams/{team_slug}/members', {
org: orgName,
team_slug
});
return response.data;
}
async function removeTeamMember(username, team_slug) {
const response = await octokit.request('DELETE /orgs/{org}/teams/{team_slug}/memberships/{username}', {
org: orgName,
team_slug,
username
});
return response.data;
}
try {
// USERS
const employees = JSON.parse(fs.readFileSync('active.json', 'utf8'));
let gitHubUsersList = await getOrgUsers();
const gitHubPendingInvitationList = await getOrgPendingInvitations();
gitHubUsersList = gitHubUsersList.concat(gitHubPendingInvitationList);
const gitHubUsersToAdd = [];
employees.active.map(employee => {
if (employee.github_username !== '') {
// Adding members to each team excluding employees team
const employeeTeams = employee.permissions.split(',');
employeeTeams.map(team => {
if (team !== '' && team.toLowerCase() !== 'employees') {
addMemberToTeam(employee.github_username, team.toLowerCase()).then(() => {
console.log(`User added to ${team.toLowerCase()} team: ${employee.github_username}`);
});
}
});
let isAdded = false;
gitHubUsersList.map(user => {
if (employee.github_username === user.login) {
isAdded = true;
}
});
if (!isAdded) {
gitHubUsersToAdd.push(employee);
}
}
});
gitHubUsersToAdd.map(employee => {
if (employee.github_username !== '') {
console.log('User to add - Name: ' + employee.first_name + ' ' + employee.last_name);
getUser(employee.github_username).then(user => {
sendOrgInvitation(user.id).then(() => {
console.log(`User invited: ${employee.github_username}`);
});
});
}
});
let gitHubUsersToRemove = [];
gitHubUsersList.map(user => {
let removeUser = true;
employees.active.map(employee => {
if (employee.github_username === user.login) {
removeUser = false;
// Adding member to employees team
addMemberToTeam(user.login, 'employees').then(() => {
console.log(`User added to employees team: ${employee.github_username}`);
});
}
});
if (removeUser) {
gitHubUsersToRemove.push(user);
}
});
// Don't remove invited members
gitHubUsersToRemove = gitHubUsersToRemove.filter((username) => {
return !gitHubPendingInvitationList.includes(username);
});
gitHubUsersToRemove.map(user => {
removeOrgUser(user.login).then(() => {
console.log('Remove: ' + user.login);
})
});
// TEAMS
const permissions = JSON.parse(fs.readFileSync('permissions.json', 'utf8'));
const gitHubTeams = await getOrgTeams();
gitHubTeams.map(gitHubTeam => {
let removeTeam = true;
permissions.teams.map(team => {
if (gitHubTeam.name === team.name) {
removeTeam = false;
}
});
if (removeTeam) {
removeOrgTeam(gitHubTeam.name).then(() => {
console.log(`Team removed: ${gitHubTeam.name}`);
})
}
});
permissions.teams.map(team => {
let createTeam = true;
gitHubTeams.map(gitHubTeam => {
if (gitHubTeam.name === team.name) {
createTeam = false;
// Check parent & update team info like name and description
if (team.parent) {
getOrgTeam(team.parent).then(parent => {
updateOrgTeam(team.name, team.description, parent.id).then(() => {
console.log(`Team updated: ${team.name}`);
});
});
} else {
updateOrgTeam(team.name, team.description, null).then(() => {
console.log(`Team updated: ${team.name}`);
});
}
}
});
if (createTeam && team.parent) {
getOrgTeam(team.parent).then(parent => {
createOrgTeam(team.name, team.description, parent.id).then(() => {
console.log(`Team created: ${team.name}`);
});
})
}
// Removing members from teams
if (!createTeam && team.name !== 'employees') {
listTeamMembers(team.name).then(response => {
const members = [];
response.map(member => {
members.push(member.login);
});
employees.active.map(employee => {
if (!employee.permissions.toLowerCase().includes(team.name)) {
if (members.includes(employee.github_username)) {
removeTeamMember(employee.github_username, team.name).then(() => {
console.log(`User removed from ${team.name} team: ${employee.github_username}`);
});
}
}
});
});
}
});
} catch (err) {
console.error(err);
}