forked from angular/google-cla-verifier-for-github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcla-verifier.gs
360 lines (277 loc) · 11.2 KB
/
cla-verifier.gs
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/**
* @fileoverview Google CLA Verifier for GitHub
* @author Igor Minar (igor@angularjs.org)
* @copyright (c) 2013 Google, Inc
* @license MIT
* @version 1.5.0
* @description
*
* This Google App Script app that automatically verifies whether PRs in a given project where authored by developers who signed
* Google's CLA via the online form (https://developers.google.com/open-source/cla/individual).
*
* The association between the developer and CLA signer is done by matching email in the PR commit against email in the CLA spreadsheet.
*
* Features
* --------
* - retrieves all open PRs from a github repo and checks their CLA status
* - adds "cla: yes" or "cla: no" label to all open PRs
* - supports cron-like scheduling via App Script triggers
* - emails log output to the person who installs this script after each run
*
*
* Instalation
* ------------
*
* 1/ App Script Project
*
* Start a new "blank project" at http://script.google.com under your @google.com account.
*
* Create a new file "cla-verifier.gs" in there and copy the conents of this file there.
*
*
* 2/ ENV Variables
*
* This script assumes that there is another .gs file in the same project which defines GITHUB_REPO and GITHUB_ACCESS_TOKEN variables.
* The filename is not significant. Example:
var GITHUB_ACCESS_TOKEN = '12345'; //"Personal Token" string generated via https://github.com/settings/applications
var GITHUB_REPO = 'angular/angular.js';
var CLA_NOT_FOUND_COMMENT = 'Please sign CLA at https://developers.google.com/open-source/cla/individual';
var THANKS_FOR_SIGNING_CLA_COMMENT = 'Achievement unlocked: CLA signature found!';
*
* 3/ GitHub Labels
*
* In the project to be monitored create two labels:
* - "cla: yes"
* - "cla: no"
*
*
* 4/ App Script Project Trigger
*
* Set up a project trigger that will run the "checkCla" function once an hour (or once a day).
*
*
* 5/ GitHub PR Helper Chrome Extension
*
* Since GitHub's UI doesn't show labels for PRs, install [GitHub PR Helper](https://github.com/petebacondarwin/github-pr-helper)
* Chrome Extension to make the labels visible in the PR/Issues list and detail views.
*
*
* Limitations
* -----------
* - doesn't check corporate signers
* - retrieves email only from the first commit in the PR
* - there are no unit or end-to-end tests for this code (I have yet to figure out a way to test app script code)
*
*
* Development / Improvements
* --------------------------
*
* The code is hosted at https://github.com/angular/google-cla-verifier-for-github
*
* Changelog: https://github.com/angular/google-cla-verifier-for-github/commits/master
*/
function checkCla() {
if (!CLA_NOT_FOUND_COMMENT) throw new Error('CLA_NOT_FOUND_COMMENT env variable not found');
if (!THANKS_FOR_SIGNING_CLA_COMMENT) throw new Error('THANKS_FOR_SIGNING_CLA_COMMENT env variable not found');
log('Starting CLA check');
var start = Date.now();
var newClaPrs = [];
log('Initiating GitHub Client');
var github = new GitHub();
var prsToVerify = github.getPrsWithNoCla();
if (prsToVerify.length) {
log('Initiating ClaRepo');
var claRepo = new ClaRepo();
var emailsWithoutCla = [];
log('Verifying CLA for PRs');
prsToVerify.forEach(function (prNumber) {
var email = github.getEmailForPr(prNumber);
log(" -> PR #%s - email: %s", prNumber, email);
if (claRepo.containsEmail(email)) {
log(" ->> CLA found!");
if (github.isLabeledAsClaNo(prNumber)) {
log(" ->> This PR was previously checked and didn't have CLA, posting a 'thank you' comment");
github.postComment(prNumber, THANKS_FOR_SIGNING_CLA_COMMENT);
}
log(" ->> Applying CLA label to the PR");
github.labelPrAsClaYes(prNumber);
newClaPrs.push(prNumber);
} else {
emailsWithoutCla.push(email);
if (!github.isLabeledAsClaNo(prNumber)) {
log(" ->> CLA not found, posting CLA request comment", prNumber);
github.postComment(prNumber, CLA_NOT_FOUND_COMMENT);
github.labelPrAsClaNo(prNumber);
}
}
});
}
var end = Date.now();
log("Finished CLA Check (took: %sms | verified %s PRs | found %s new CLAs)", (end - start), prsToVerify.length, newClaPrs.length);
emailLog(newClaPrs.length, (prsToVerify.length - newClaPrs.length));
}
function GitHub() {
if (!GITHUB_REPO) throw new Error('GITHUB_REPO env variable not found');
if (!GITHUB_ACCESS_TOKEN) throw new Error('GITHUB_ACCESS_TOKEN env variable not found');
var PRS_URL = 'https://api.github.com/repos/' + GITHUB_REPO + '/pulls?access_token=' + GITHUB_ACCESS_TOKEN + '&state=open\&page=';
var PR_LABEL_URL = 'https://api.github.com/repos/' + GITHUB_REPO + '/issues/ISSUE_NUMBER/labels?access_token=' + GITHUB_ACCESS_TOKEN;
var ISSUE_COMMENT_URL = 'https://api.github.com/repos/' + GITHUB_REPO + '/issues/ISSUE_NUMBER/comments?access_token=' + GITHUB_ACCESS_TOKEN;
var CLA_NO_LABEL = 'cla: no';
var CLA_YES_LABEL = 'cla: yes';
var openPrs = fetchOpenPrs();
var openPrsMap = prArrayToMap(openPrs);
var labelsCache = {};
this.getPrsWithNoCla = getPrsWithNoCla;
this.getEmailForPr = getEmailForPr;
this.getLabelsForPr = getLabelsForPr;
this.labelPrAsClaYes = labelPrAsClaYes;
this.labelPrAsClaNo = labelPrAsClaNo;
this.isLabeledAsClaNo = isLabeledAsClaNo;
this.postComment = postComment;
function fetchOpenPrs() {
var response = UrlFetchApp.fetch(prPageUrl(1));
var linkHeader = response.getHeaders()['Link'];
var hasMultiplePages = !!linkHeader;
var numberOfPages = hasMultiplePages
? parseInt(linkHeader.match(/page=(\d+)>; rel="last"/)[1], 10)
: 1;
log(' -> Determined that there are %s pages of open PRs', numberOfPages);
var allPrs = [];
var allPrsMap = {};
var page = Utilities.jsonParse(response.getContentText());
allPrs = allPrs.concat(page);
for (var i = 2; i <= numberOfPages; i++) {
response = UrlFetchApp.fetch(prPageUrl(i));
page = Utilities.jsonParse(response.getContentText());
allPrs = allPrs.concat(page);
}
log(' -> Fetched PR info for %s PRs', allPrs.length);
return allPrs;
}
function prArrayToMap(prs) {
return prs.reduce(function(map, pr) { map[pr['number']] = pr; return map}, {});
}
function getPrsWithNoCla() {
var noClaPrs = openPrs.reduce(function(noClaPrs, pr) {
var prNumber = pr['number'];
var labels = getLabelsForPr(prNumber);
// if PR state is "No CLA"
if (labels.indexOf(CLA_NO_LABEL) >= 0 || labels.indexOf(CLA_YES_LABEL) === -1) {
log(' -> PR #%s is missing CLA', prNumber);
noClaPrs.push(prNumber);
}
return noClaPrs;
}, []);
noClaPrs.sort(function(pr1, pr2) {
return pr1['number'] > pr2['number'];
});
return noClaPrs;
}
function getEmailForPr(prNumber) {
var patchUrl = openPrsMap[prNumber]['patch_url'];
var response = UrlFetchApp.fetch(patchUrl).getContentText();
var email = response.match(/^From: [^<]* <(\S+@\S+)>$/m)[1];
return email;
}
function getLabelsForPr(prNumber) {
var labels = labelsCache[prNumber];
if (labels) return labels;
var response = UrlFetchApp.fetch(prLabelsUrl(prNumber));
labels = Utilities.jsonParse(response.getContentText()).
map(function(labelInfo) {return labelInfo.name; });
labelsCache[prNumber] = labels;
return labels;
}
function labelPrAsClaYes(prNumber) {
UrlFetchApp.fetch(prLabelsUrl(prNumber), {method: 'POST', payload: Utilities.jsonStringify([CLA_YES_LABEL])});
UrlFetchApp.fetch(prLabelUrl(prNumber, CLA_NO_LABEL), {method: 'DELETE'});
}
function labelPrAsClaNo(prNumber) {
UrlFetchApp.fetch(prLabelsUrl(prNumber), {method: 'POST', payload: Utilities.jsonStringify([CLA_NO_LABEL])});
UrlFetchApp.fetch(prLabelUrl(prNumber, CLA_YES_LABEL), {method: 'DELETE'});
}
function isLabeledAsClaNo(prNumber) {
return getLabelsForPr(prNumber).indexOf(CLA_NO_LABEL) >= 0;
}
function postComment(issueNumber, text) {
UrlFetchApp.fetch(issueCommentUrl(issueNumber), {method: 'POST', payload: Utilities.jsonStringify({body: text})});
}
function prPageUrl(page) {
return PRS_URL + page;
}
function prLabelsUrl(prNumber) {
return PR_LABEL_URL.replace('ISSUE_NUMBER', prNumber);
}
function prLabelUrl(prNumber, label) {
var escapedLabel = label.replace(':', '%3A').replace(/\s/, '+');
return PR_LABEL_URL.replace('ISSUE_NUMBER', prNumber).replace('?', '/' + escapedLabel + '?');
}
function issueCommentUrl(issueNumber) {
return ISSUE_COMMENT_URL.replace('ISSUE_NUMBER', issueNumber);
}
}
function ClaRepo() {
var emails = getEmailsFromSpreadsheet('https://docs.google.com/a/google.com/spreadsheet/ccc?key=1N4NW6EMv-j_VEZaX_clH4Hjj8fpnY3fNHeKYK1GIGvM');
var signClaEmails = getEmailsFromSpreadsheet('https://docs.google.com/a/google.com/spreadsheets/d/1yBqaKEk9BEjLeJ_aQMGSt3RnTd6f7PYRioyMSzaZAfo/edit');
this.containsEmail = function(email) {
email = normalizeEmail(email);
var spreadsheetRowIndex = emails.indexOf(email) + 2; // +1 because first row is header and +1 because the rows are 1-based
if (spreadsheetRowIndex >= 2) {
log(' ->> Found signature: %s (legacy spreadsheet index: %s)', email, spreadsheetRowIndex);
return true;
}
var spreadsheetRowIndex = signClaEmails.indexOf(email) + 2; // +1 because first row is header and +1 because the rows are 1-based
if (spreadsheetRowIndex >= 2) {
log(' ->> Found signature: %s (signCla spreadsheet index: %s)', email, spreadsheetRowIndex);
return true;
}
}
}
function getEmailsFromSpreadsheet(spreadsheetUrl) {
var claSpreadSheet = SpreadsheetApp.openByUrl(spreadsheetUrl);
var sheet = claSpreadSheet.getSheetByName("Individual Signers");
var rangeWithEmails = sheet.getRange(2, 3, sheet.getLastRow());
var valuesWithEmails = rangeWithEmails.getValues();
var emails = [];
for (var i = 0; i < valuesWithEmails.length; i++) {
var email = valuesWithEmails[i][0];
email = normalizeEmail(email);
if (email) {
emails.push(normalizeEmail(email));
}
}
return emails;
}
/**
* serializes all msg variables into a pretty string and passes them onto the default Logger.log(
*
* @param {string} message Message format with %s as placeholders
* @param {*...} msgVariables
*/
function log(message) {
var args = [message];
var arg;
for (var i = 1, l = arguments.length; i <= l; i++) {
arg = arguments[i];
switch (typeof arg) {
case 'string': break;
case 'number': arg = arg.toString();
break;
default: arg = Utilities.jsonStringify(arg);
}
args.push(arg);
}
Logger.log.apply(Logger, args);
}
function emailLog(newClaCount, claMissingCount) {
var recipient = Session.getActiveUser().getEmail();
var subject = 'Google CLA Verifier Log for ' + GITHUB_REPO + '(newly signed: ' + newClaCount + ', still missing: ' + claMissingCount + ')';
var body = Logger.getLog();
MailApp.sendEmail(recipient, subject, body);
}
function normalizeEmail(email) {
if (!email || (typeof email !== 'string')) return null;
email = email.toLowerCase();
email = email.replace(/(^\s+|\s+$)/g, '');
return email;
}