This repository was archived by the owner on Apr 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathspreadsheet.js
More file actions
272 lines (240 loc) · 7.69 KB
/
spreadsheet.js
File metadata and controls
272 lines (240 loc) · 7.69 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
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
264
265
266
267
268
269
270
271
272
const gs = require('google-spreadsheet');
const _ = require('underscore');
const async = require('async');
_.str = require('underscore.string');
const moment = require('moment');
const repos = require('./repos');
const Promise = require('bluebird');
const email = require('./email');
const INVITATION_VALID_FOR_DAYS = 30;
const LIMIT = 500;
function getWorksheet() {
return new Promise((resolve, reject) => {
const doc = new gs(process.env.SPREADSHEET_KEY);
doc.useServiceAccountAuth({
client_email: process.env.GOOGLE_CLIENT_EMAIL,
private_key: process.env.GOOGLE_PRIVATE_KEY.replace(/\\n/g, '\n') // Do magic to replace \n with actual newlines
}, (err) => {
if (err) {
return reject(err);
}
doc.getInfo((err, info) => {
if (err) {
return reject(err);
}
resolve(info.worksheets[0]);
});
});
});
}
function addCandidate(candidatename, assignment, windowDuration) {
return addRow({
candidatename,
assignment,
'window': windowDuration,
authcode: Math.floor(Math.random() * 10000000000),
created: moment().format()
})
}
function addRow(row) {
return getWorksheet()
.then((sheet) => {
return new Promise((resolve, reject) => {
sheet.addRow(row, (err, row) => {
if (err) {
return reject(err);
}
resolve(row);
})
});
});
}
function readSpreadsheet() {
getWorksheet()
.then(getRowsFromSheet);
}
function startAssignment(authcode, githubUsername) {
return getWorksheet()
.then((sheet) => {
return new Promise((resolve, reject) => {
// Get rows from the sheet. We order by assigned and reverse the returned rows
// so that we get candidates who have not been assigned yet.
sheet.getRows({
offset: 0,
limit: LIMIT,
orderby: 'assigned',
reverse: false
}, (err, rows) => {
if (err) {
return reject(err);
}
const candidate = _.find(rows, (row) => {
return row.authcode === authcode;
});
if (!candidate) {
console.log(`Did not find a candidate matching authcode ${authcode}`);
return reject(new Error('Invalid Authentication Code'));
}
if (candidate.assigned) {
console.log(`Attempt to re-use auth code ${authcode}`);
return reject(new Error('Used Authentication Code'));
}
if (moment(candidate.created).isBefore(moment().subtract(INVITATION_VALID_FOR_DAYS, 'days'))) {
console.log(`Attempt to use expired auth code ${authcode}`);
return reject(new Error('Expired Authentication Code'));
}
repos.initializeCandidate({
templateRepo: candidate.assignment,
candidateName: candidate.candidatename,
candidateGitHubUsername: githubUsername
}).then((repo) => {
candidate.gitHub = githubUsername;
candidate.assigned = moment().format();
candidate.save();
resolve(repo);
});
});
});
})
.catch((err) => {
console.log('Caught error in spreadsheet startAssignment: ', err);
});
}
function scanForExpiringInvitations() {
return getWorksheet()
.then((sheet) => {
return new Promise((resolve, reject) => {
// Get rows from the sheet. We order by assigned and reverse the returned rows
// so that we get candidates who have not been assigned yet.
sheet.getRows({
offset: 0,
limit: LIMIT,
orderby: 'assigned',
reverse: false
}, (err, rows) => {
if (err) {
return reject(err);
}
expiringInvitations = _.filter(rows, (row) => {
console.log(`Assigned: ${row.assigned}`);
console.log(`now: ${moment()}`);
return !row.revoked && moment().isSame(moment(row.created).add(INVITATION_VALID_FOR_DAYS + 1, 'days'), 'day');
});
console.log(`Scanned ${rows.length} rows and found ${expiringInvitations.length} invitations expired.`);
if (expiringInvitations.length > 0) {
email.sendInvitationExpiringNotification(_.str.join(', ', _.pluck(expiringInvitations, 'candidatename')))
.then(resolve)
.catch(reject);
} else {
resolve();
}
});
});
});
}
function scanForExpiredWindows() {
return getWorksheet()
.then((sheet) => {
return new Promise((resolve, reject) => {
// Get rows from the sheet. We order by assigned and reverse the returned rows
// so that we get candidates who have not been assigned yet.
sheet.getRows({
offset: 0,
limit: LIMIT,
orderby: 'assigned',
reverse: false
}, (err, rows) => {
if (err) {
return reject(err);
}
expiredRows = _.filter(rows, (row) => {
return !row.revoked && moment().isAfter(moment(row.assigned).add(row.window, 'hours'));
});
console.log(`Found ${expiredRows.length} expired rows.`);
const revocations = [];
_.each(expiredRows, (row) => {
row.revoked = moment().format();
row.save();
revocations.push(new Promise((resolve, reject) => {
// Google has ugly cruft.
delete row._xml;
console.log('Removing access for expired row ', row);
return repos.removeCollaboratorAccess({
candidateGitHubUsername: row.github,
templateRepo: row.assignment
})
.then(email.sendRevocationNotification(row.candidatename))
.then(resolve)
.catch(reject);
}));
});
console.log('Waiting for ' + revocations.length + ' revocations...');
Promise.all(revocations)
.then(resolve)
.catch(reject);
});
});
});
}
function archiveRepos() {
return getWorksheet()
.then((sheet) => {
return new Promise((resolve, reject) => {
// Get rows from the sheet. We order by assigned and reverse the returned rows
// so that we get candidates who have not been archived yet.
sheet.getRows({
offset: 0,
limit: LIMIT,
orderby: 'archived',
reverse: false
}, (err, rows) => {
if (err) {
return reject(err);
}
rowsToArchive = _.filter(rows, (row) => {
// Allow 14 days before archiving a repo
return !row.archived && row.revoked && moment().isAfter(moment(row.revoked).add(30, 'days'));
});
console.log(`Found ${rowsToArchive.length} repos to archive.`);
async.eachSeries(rowsToArchive, (row, callback) => {
row.archived = moment().format();
row.save();
// Google has ugly cruft.
delete row._xml;
console.log(`Archiving ${row.candidatename}'s project...'`);
repos.archiveRepo({
candidateGitHubUsername: row.github,
templateRepo: row.assignment,
candidateName: row.candidatename
})
.then(email.sendArchiveNotification(row.candidatename))
.then(() => {
console.log(`Success archiving ${row.candidatename}'s project.'`);
callback();
})
.catch((err) => {
// Mark the row as not archived.
row.archived = undefined;
row.save();
callback(err);
});
}, (err) => {
if (err) {
return reject(err);
}
resolve();
});
});
});
});
}
module.exports = {
archiveRepos,
startAssignment,
addCandidate,
scanForExpiredWindows,
scanForExpiringInvitations
}
//scanForExpiredWindows();
//startAssignment('3009270414', 'Furchin');
//addCandidate('Michal Bryc', 'battleship', 72);