This repository has been archived by the owner on Nov 28, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 270
/
server.js
463 lines (411 loc) · 12.9 KB
/
server.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
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/**
* Copyright (c) 2016-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @flow
*/
var bl = require('bl');
var config = require('./config');
var express = require('express');
var fs = require('fs');
var mentionBot = require('./mention-bot.js');
var messageGenerator = require('./message.js');
var util = require('util');
var schedule = require('./schedule.js');
var GitHubApi = require('github');
var jsonlint = require('jsonlint');
var CONFIG_PATH = '.mention-bot';
if (!process.env.GITHUB_TOKEN) {
console.error('The bot was started without a GitHub account to post with.');
console.error('To get started:');
console.error('1) Create a new account for the bot');
console.error('2) Settings > Personal access tokens > Generate new token');
console.error('3) Only check `public_repo` and click Generate token');
console.error('4) Run the following command:');
console.error('GITHUB_TOKEN=insert_token_here npm start');
console.error('5) Run the following command in another tab:');
console.error('curl -X POST -d @__tests__/data/23.webhook http://localhost:5000/');
console.error('6) Check if it has commented here: https://github.com/fbsamples/bot-testing/pull/23');
process.exit(1);
}
if (!process.env.GITHUB_USER) {
console.warn(
'There was no GitHub user detected.',
'This is fine, but mention-bot won\'t work with private repos.'
);
console.warn(
'To make mention-bot work with private repos, please expose',
'GITHUB_USER and GITHUB_PASSWORD as environment variables.',
'The username and password must have access to the private repo',
'you want to use.'
);
}
var github = new GitHubApi({
host: config.github.apiHost,
pathPrefix: config.github.pathPrefix,
protocol: config.github.protocol,
port: config.github.port
});
github.authenticate({
type: 'oauth',
token: process.env.GITHUB_TOKEN
});
var app = express();
function buildMentionSentence(reviewers) {
var atReviewers = reviewers.map(function(owner) { return '@' + owner; });
if (reviewers.length === 1) {
return atReviewers[0];
}
return (
atReviewers.slice(0, atReviewers.length - 1).join(', ') +
' and ' + atReviewers[atReviewers.length - 1]
);
}
function getDefaultMessageGenerator(findPotentialReviewers) {
return function(reviewers, pullRequester) {
return util.format(
'%s, thanks for your PR! ' +
'%se identified %s to be%s potential reviewer%s.',
pullRequester,
findPotentialReviewers ? 'By analyzing the history of the files in this pull request, w' : 'W',
buildMentionSentence(reviewers),
reviewers.length > 1 ? '' : ' a',
reviewers.length > 1 ? 's' : ''
);
}
}
function configMessageGenerator(message, reviewers, pullRequester) {
var withReviewers = message.replace(/@reviewers/g, buildMentionSentence(reviewers));
return withReviewers.replace(/@pullRequester/g, pullRequester);
}
function getRepoConfig(request) {
return new Promise(function(resolve, reject) {
github.repos.getContent(request, function(err, result) {
if (err) {
reject(err);
return;
}
try {
var data = JSON.parse(result.data);
resolve(data);
} catch (e) {
try {
e.repoConfig = result.data;
} catch (e) {}
reject(e);
}
});
});
}
async function work(body) {
var data = {};
try {
data = JSON.parse(body.toString());
console.log(data.pull_request.html_url);
} catch (e) {
console.error(e);
}
// default config
var repoConfig = {
maxReviewers: 3,
numFilesToCheck: 5,
userBlacklist: [],
userBlacklistForPR: [],
userWhitelist: [],
fileBlacklist: [],
requiredOrgs: [],
findPotentialReviewers: true,
actions: ['opened'],
branches:[],
skipAlreadyAssignedPR: false,
skipAlreadyMentionedPR: false,
delayed: false,
delayedUntil: '3d',
assignToReviewer: false,
createReviewRequest: false,
createComment: true,
skipTitle: '',
withLabel: '',
skipCollaboratorPR: false,
};
if (process.env.MENTION_BOT_CONFIG) {
try {
repoConfig = {
...repoConfig,
...JSON.parse(process.env.MENTION_BOT_CONFIG)
};
} catch(e) {
console.error(
'Error attempting to read the config from the environment variable ' +
' MENTION_BOT_CONFIG'
);
console.error(e);
}
}
try {
// request config from repo
var configRes = await getRepoConfig({
owner: data.repository.owner.login,
repo: data.repository.name,
ref: data.pull_request.base.ref,
path: CONFIG_PATH,
headers: {
Accept: 'application/vnd.github.v3.raw+json'
}
}).catch(function(e) {
if (e instanceof SyntaxError && repoConfig.actions.indexOf(data.action) !== -1) {
// Syntax error while reading custom configuration file
var errorLog = '';
try {
jsonlint.parse(e.repoConfig)
} catch(err) {
errorLog = err;
}
var message =
'Unable to parse mention-bot custom configuration file due to a syntax error.\n' +
'Please check the potential root causes below:\n\n' +
'1. Having comments\n' +
'2. Invalid JSON type\n' +
'3. Having extra "," in the last JSON attribute\n\n' +
'Error message:\n' +
'```\n' + errorLog + '\n```';
createComment(data, message);
}
});
repoConfig = {...repoConfig, ...configRes};
} catch (e) {
if (e.code === 404 &&
e.message.match(/message.*Not Found.*documentation_url.*developer.github.com/)) {
console.log('Couldn\'t find ' + CONFIG_PATH + ' in repo. Continuing with default configuration.');
} else {
console.error(e);
}
}
function isValid(repoConfig, data) {
if (repoConfig.branches && repoConfig.branches.length > 0 && repoConfig.branches.indexOf(data.pull_request.base.ref) === -1) {
console.log(
'Skipping because base ref is "' + data.pull_request.base.ref + '".',
'We only care about: "' + repoConfig.branches.join("', '") + '"'
);
return false;
}
if (repoConfig.actions.indexOf(data.action) === -1) {
console.log(
'Skipping because action is "' + data.action + '".',
'We only care about: "' + repoConfig.actions.join("', '") + '"'
);
return false;
}
if (repoConfig.withLabel && data.label &&
data.label.name != repoConfig.withLabel) {
console.log('Skipping because pull request does not have label: "' + repoConfig.withLabel + '".');
return false;
}
if (repoConfig.skipTitle &&
data.pull_request.title.indexOf(repoConfig.skipTitle) > -1) {
console.log('Skipping because pull request title contains: "' + repoConfig.skipTitle + '".');
return false;
}
if (repoConfig.skipCollaboratorPR) {
github.repos.checkCollaborator({
owner: data.repository.owner.login, // 'fbsamples'
repo: data.repository.name, // 'bot-testing'
username: data.pull_request.user.login
}, function(err, res){
if (res && res.meta.status === '204 No Content') {
console.log('Skipping because pull request is made by collaborator.');
return false;
}
});
}
if (repoConfig.skipAlreadyAssignedPR &&
data.pull_request.assignee &&
data.pull_request.assignee.login) {
console.log('Skipping because pull request is already assigned.');
return false;
}
if (process.env.REQUIRED_ORG) {
if (repoConfig.requiredOrgs.indexOf(process.env.REQUIRED_ORG) === -1) {
repoConfig.requiredOrgs.push(process.env.REQUIRED_ORG);
}
}
if (repoConfig.userBlacklistForPR.indexOf(data.pull_request.user.login) >= 0) {
console.log('Skipping because blacklisted user created Pull Request.');
return false;
}
if (repoConfig.skipTitle &&
data.pull_request.title.indexOf(repoConfig.skipTitle) > -1) {
console.log('Skipping because pull request title contains: "' + repoConfig.skipTitle + '".');
return false;
}
return true;
}
if (!isValid(repoConfig, data)) {
return;
}
var org = null;
if (data.organization) {
org = data.organization.login;
}
var reviewers = await mentionBot.guessOwnersForPullRequest(
data.repository.html_url, // 'https://github.com/fbsamples/bot-testing'
data.pull_request.number, // 23
data.pull_request.user.login, // 'mention-bot'
data.pull_request.base.ref, // 'master'
data.repository.private, //true or false
org, //the org name of the repo
repoConfig,
github
);
console.log('Reviewers:', reviewers);
if (reviewers.length === 0) {
console.log('Skipping because there are no reviewers found.');
return;
}
var message = null;
if (repoConfig.message) {
message = configMessageGenerator(
repoConfig.message,
reviewers,
'@' + data.pull_request.user.login
);
} else {
message = messageGenerator(
reviewers,
'@' + data.pull_request.user.login, // pull-requester
buildMentionSentence,
getDefaultMessageGenerator(repoConfig.findPotentialReviewers)
);
}
function createComment(data, message, reject) {
if (!repoConfig.createComment) {
return;
}
github.issues.createComment({
owner: data.repository.owner.login, // 'fbsamples'
repo: data.repository.name, // 'bot-testing'
number: data.pull_request.number, // 23
body: message
}, function(err) {
if (err) {
if (typeof reject === 'function') {
return reject(err);
}
}
})
}
function assignReviewer(data, reviewers, reject) {
if (!repoConfig.assignToReviewer) {
return;
}
github.issues.edit({
owner: data.repository.owner.login, // 'fbsamples'
repo: data.repository.name, // 'bot-testing'
number: data.pull_request.number, // 23
assignees: reviewers
}, function(err) {
if (err) {
if (typeof reject === 'function') {
return reject(err);
}
}
});
}
function requestReview(data, reviewers, reject) {
if (!repoConfig.createReviewRequest) {
return;
}
github.pullRequests.createReviewRequest({
owner: data.repository.owner.login, // 'fbsamples'
repo: data.repository.name, // 'bot-testing'
number: data.pull_request.number, // 23
reviewers: reviewers
}, function(err) {
if (err) {
if (typeof reject === 'function') {
return reject(err);
}
}
});
}
function getComments(data, page) {
return new Promise(function(resolve, reject) {
github.issues.getComments({
owner: data.repository.owner.login, // 'fbsamples'
repo: data.repository.name, // 'bot-testing'
number: data.pull_request.number, // 23
page: page, // 1
per_page: 100 // maximum supported
}, function(err, result) {
if (err) {
reject(err);
}
resolve(result);
});
});
}
if (repoConfig.skipAlreadyMentionedPR) {
var page;
var comments = [[]];
for (page = 1; comments.length != 0; ++page) {
comments = await getComments(data, page);
if (comments.find(function(comment) {
return comment.body == message;
})) {
console.log('Skipping because there is already existing an exact mention.');
return;
}
}
}
if (repoConfig.delayed) {
schedule.performAt(schedule.parse(repoConfig.delayedUntil), function(resolve, reject) {
github.pullRequests.get({
owner: data.repository.owner.login,
repo: data.repository.name,
number: data.pull_request.number
}, function(err, currentData) {
if (err) {
reject(err);
return;
}
if (!isValid(repoConfig, currentData)) {
reject('PR validation failed');
return;
}
createComment(currentData, message, reject);
assignReviewer(currentData, reviewers, reject);
requestReview(currentData, reviewers, reject);
});
});
} else {
createComment(data, message);
assignReviewer(data, reviewers);
requestReview(data, reviewers);
}
return;
};
app.post('/', function(req, res) {
req.pipe(bl(function(err, body) {
work(body)
.then(function() { res.end(); })
.catch(function(e) {
console.error(e);
console.error(e.stack);
res.status(500).send('Internal Server Error');
});
}));
});
app.get('/', function(req, res) {
res.send(
'GitHub Mention Bot Active. ' +
'Go to https://github.com/facebook/mention-bot for more information.'
);
});
app.set('port', process.env.PORT || 5000);
app.listen(app.get('port'), function() {
console.log('Listening on port', app.get('port'));
});