forked from facebookarchive/mention-bot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
529 lines (478 loc) · 13.7 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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
/**
* 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 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();
var dlangReviewers = {
"dlang/dmd": [
"9rnsr",
"andralex",
"AndrejMitrovic",
//"AndrewEdwards",
"braddr",
"CyberShadow",
"DmitryOlshansky",
//"donc",
"ibuclaw",
"klickverbot",
"MartinNowak",
"rainers",
"WalterBright",
"yebblies"
],
"dlang/druntime": [
"9rnsr",
"alexrp",
"andralex",
//"AndrewEdwards",
"braddr",
"complexmath",
"CyberShadow",
"DmitryOlshansky",
//"donc",
"ibuclaw",
"jmdavis",
"klickverbot",
"MartinNowak",
"monarchdodra",
"rainers",
"schveiguy",
"WalterBright",
"yebblies",
],
"dlang/phobos": [
"9il",
"9rnsr",
"alexrp",
"andralex",
"AndrejMitrovic",
//"AndrewEdwards",
"braddr",
"burner",
"complexmath",
"CyberShadow",
"DmitryOlshansky",
//"donc",
//"dsimcha",
"Hackerpilot",
"ibuclaw",
//"JakobOvrum",
"jmdavis",
"klickverbot",
"kyllingstad",
"MartinNowak",
"monarchdodra",
"quickfur",
"rainers",
"repeatedly",
"schveiguy",
"shoo",
"WalterBright",
"wilzbach",
"yebblies"
],
"dlang/dlang.org": [
"9rnsr",
"adamdruppe",
"alexrp",
"andralex",
"AndrejMitrovic",
//"AndrewEdwards",
"braddr",
"complexmath",
"CyberShadow",
"DmitryOlshansky",
//"donc",
"Hackerpilot",
"ibuclaw",
"jmdavis",
"klickverbot",
"MartinNowak",
"rainers",
"schuetzm",
"WalterBright"
],
"dlang/tools": [
"9rnsr",
"andralex",
"AndrejMitrovic",
"burner",
"CyberShadow",
"Dicebot",
"DmitryOlshansky",
"eco",
"Hackerpilot",
"jmdavis",
"klickverbot",
"MartinNowak",
"quickfur",
"schveiguy",
"WalterBright",
"wilzbach",
"yebblies"
],
"dlang/installer": [
"andralex",
//"AndrewEdwards",
"CyberShadow",
"DmitryOlshansky",
"eco",
"jordisayol",
"MartinNowak",
"WalterBright"
]
}
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 defaultMessageGenerator(reviewers, pullRequester, assignee) {
return util.format(
'%s, thanks for your PR! ' +
'By analyzing the annotation information on this pull request' +
', we identified %s to be%s potential reviewer%s. ' +
'@%s: The PR was automatically assigned to you, please reassign it ' +
'if you were identified mistakenly.' +
'<br><br>(The DLang Bot is under development. If you experience any issues, please ' +
'open an issue at [its repo](https://github.com/dlang-bots/mention-bot).)',
pullRequester,
buildMentionSentence(reviewers),
reviewers.length > 1 ? '' : ' a',
reviewers.length > 1 ? 's' : '',
assignee
);
}
function configMessageGenerator(message, reviewers, pullRequester, assignee) {
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);
}
resolve(result);
});
});
}
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: 2,
numFilesToCheck: 10,
userBlacklist: ["braddr"],
userBlacklistForPR: [],
userWhitelist: [],
fileBlacklist: [],
requiredOrgs: [],
findPotentialReviewers: true,
actions: ['opened', 'synchronize'],
skipAlreadyAssignedPR: true,
skipAlreadyMentionedPR: true,
delayed: true,
delayedUntil: '3d',
assignToReviewer: true,
skipTitle: '',
withLabel: '',
skipCollaboratorPR: false,
};
try {
// request config from repo
var configRes = await getRepoConfig({
user: data.repository.owner.login,
repo: data.repository.name,
path: CONFIG_PATH,
headers: {
Accept: 'application/vnd.github.v3.raw+json'
}
});
repoConfig = {...repoConfig, ...configRes};
} catch (e) {
if (e.code === 404 &&
e.message === '{"message":"Not Found","documentation_url":"https://developer.github.com/v3"}') {
console.log('Couldn\'t find ' + CONFIG_PATH + ' in repo. Continuing with default configuration.');
} else {
console.error(e);
}
}
function isValid(repoConfig, data) {
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.getCollaborator({
user: data.repository.owner.login, // 'fbsamples'
repo: data.repository.name, // 'bot-testing'
collabuser: 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;
}
// use hard-coded reviewers for Phobos
var assignee;
if (dlangReviewers[data.repository.full_name])
{
var potentialReviewers = dlangReviewers[data.repository.full_name];
for (var i = 0; i < reviewers.length; i++)
{
if (potentialReviewers.indexOf(reviewers[i]) >= 0)
{
assignee = reviewers[i];
break;
}
};
}
// otherwise apply good luck and go with the first one
if (typeof(assignee) == "undefined")
{
assignee = reviewers[0];
}
console.log('Assignee: ', assignee);
var message = null;
if (repoConfig.message) {
message = configMessageGenerator(
repoConfig.message,
reviewers,
'@' + data.pull_request.user.login,
assignee
);
} else {
message = messageGenerator(
reviewers,
'@' + data.pull_request.user.login, // pull-requester
buildMentionSentence,
defaultMessageGenerator,
assignee
);
}
function createComment(data, message, reject) {
github.issues.createComment({
user: 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({
user: 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 getComments(data, page) {
return new Promise(function(resolve, reject) {
github.issues.getComments({
user: 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({
user: 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)) {
return;
}
createComment(currentData, message, reject);
assignReviewer(currentData, reviewers, reject);
});
});
} else {
createComment(data, message);
// for now we only pick the best matching reviewer
assignReviewer(data, [assignee]);
}
return;
};
app.post('/', function(req, res) {
req.pipe(bl(function(err, body) {
work(body)
.then(function() { res.end(); })
.catch(function(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'));
});