This repository was archived by the owner on Jun 20, 2023. It is now read-only.
forked from leibovic/fennecbot
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathserver.js
More file actions
664 lines (601 loc) · 22.7 KB
/
server.js
File metadata and controls
664 lines (601 loc) · 22.7 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
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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
var irc = require("irc"),
https = require("https"),
request = require("request"),
notes = require("./notes"),
config = require("./config"),
newsflash = require("./newsflash"),
opsreport = require("./opsreport"),
graphs = require("./graphs"),
storage = require('node-persist'),
homu = require("./homu"),
taskcluster = require("./taskcluster"),
moment = require("moment"),
Nickserv = require("nickserv");
function githubRequest(endpoint, callback) {
var reqParams = {
uri: 'https://api.github.com/' + endpoint,
method: 'GET',
body: null,
headers: {
'Accept': 'application/vnd.github.v3',
'User-Agent': 'crowbot v0.1 (not like Gecko)'
}
};
if ('GITHUB_AUTH' in process.env) {
reqParams.headers['Authorization'] = 'Basic ' + new Buffer(process.env.GITHUB_AUTH).toString('base64');
}
console.log(reqParams.uri);
request(reqParams, function(err, response, body) {
var error, json;
var statusCode = response ? response.statusCode : 0;
if (err && err.code && (err.code == 'ETIMEDOUT' || err.code == 'ESOCKTIMEDOUT')) {
error = 'timeout';
} else if (err) {
error = err.toString();
} else if (statusCode >= 300 || statusCode < 200) {
error = "HTTP status " + statusCode;
if (body) {
try {
var tmp = JSON.parse(body);
if (tmp.message) {
error += ": " + tmp.message;
}
} catch (x) {
console.log(x + ': ' + body);
}
}
} else {
try {
json = JSON.parse(body);
} catch (x) {
console.log(x + ': ' + body);
error = "Response wasn't valid json: '" + body + "'";
}
}
if (json && json.message) {
error = json.message;
}
callback(error, json);
});
}
function searchGithub(params, org, repo, callback) {
return githubRequest('repos/' + org + '/' + repo + '/issues' + params, callback);
}
function searchIssues(params, callback) {
return githubRequest('search/issues' + params, callback);
}
function choose(list) {
return Math.floor(Math.random() * list.length);
}
var handlerWrapper = module.exports.handlerWrapper = function handlerWrapper(pings, bot, searchGithub, notes, pingStorage, newsflash, nickserv, nickservListener) {
// Finds an issue that matches the search term, and says it to the person who asked about it.
function findIssue(from, to, search, bot) {
searchGithub(search, 'servo', 'servo', function(error, issues) {
if (error) {
console.log(error);
return;
}
// Find a random bug from the array.
var index = choose(issues);
var issue = issues[index];
var message;
if (issue) {
console.log(bot.nick + " found issue " + issue.number);
message = from + ": Try working on issue #" + issue.number + " - " + issue.title + " - " + issue.html_url;
} else {
message = from + ": couldn't find anything!";
}
bot.say(to, message);
});
}
return function handler(from, to, original_message) {
if (from == 'ghservo' || from.match(/crowbot/) || from.match(/rustbot/) || from == 'BitBot') {
return;
}
// Caseless message matching
message = original_message.toLowerCase();
var issue_message = function(issue) {
var type = (issue.pull_request ? 'PR #' : 'Issue #');
return type + issue.number + ': ' + issue.title + ' - ' + issue.html_url;
}
// The regular expression below matches the following styles:
// - "issue 123"
// - " #123" to avoid catching html anchors
// - "#123" at the start of a line
// - "(#123"
// - "org/repo#123"
//
// Note that, in all of the above, the "#" can be substituted with
// "£" (for British keyboards)
var numbers_re = /(issue\s|\s[#£]|^[#£]|\([#£]|([A-Za-z0-9.-]+)\/([A-Za-z0-9.-]+)[#£])(\d[\d]+)/g;
var numbers;
while ((numbers = numbers_re.exec(message)) !== null) {
// Extract the details from the message
var org = numbers[2] || 'servo';
var repo = numbers[3] || 'servo';
var issue_number = numbers[4];
searchGithub("/" + issue_number, org, repo, function(error, issue) {
if (error) {
console.log(error);
return;
}
var message = issue_message(issue);
bot.say(to, message);
});
}
// watch for github issue links to any repository
var issues_re = /https:\/\/github\.com\/([\w\-]+)\/([\w\-]+)\/(issues|pull)\/(\d+)(\/[^\s]+)?/g;
var reviewable_re = /https:\/\/reviewable\.io\/reviews\/([\w\-]+)\/([\w\-]+)(\/)(\d+)/g;
var issues;
while ((issues = (issues_re.exec(message) || reviewable_re.exec(message) )) !== null) {
if (issues[5] && issues[5] != "/") { continue; }
searchGithub("/" + issues[4], issues[1], issues[2], function(error, issue) {
if (error) {
console.log(error);
return;
}
var message = issue_message(issue);
bot.say(to, message);
});
}
//TODO test this
if (message.indexOf('w3.org/tr') > -1) {
var allowed = ['PNG', 'SVG'];
var found = false;
for (var i = 0; i < allowed.length; i++) {
found = found || (message.indexOf(allowed[i]) > -1);
}
if (!found) {
bot.say(to, from + ": that's probably not the spec you want. Please read https://github.com/servo/servo/wiki/Relevant-spec-links");
return;
}
}
var angry_msgs = new RegExp("((shut up,?|kicks|whacks|smacks) " + bot.nick + ")|("
+ bot.nick + "[,:] shut up)", "");
if (message.match(angry_msgs) !== null) {
var replies = ["/me is sad", ":(", "ok :(", ";_;", "sadface", "/me cries a bit", "ouch"];
var reply = replies[choose(replies)];
if (reply.indexOf('/me ') == 0) {
bot.action(to, reply.substring(4));
} else {
bot.say(to, reply);
}
return;
}
if (message.indexOf('standups:') == 0) {
nickservListener.names.push(from);
nickservListener.callbacks.push([
function(bot, channel, name) {
form = {
secret: process.env.STANDUPS_SECRET,
user: name,
message: message.replace('standups:', '').replace(bot.nick + ':', '').trim()
};
request.post(
{url: 'https://build.servo.org/standups/record', form: form},
function(err, response) {
if (err || response.statusCode != 204) {
bot.say(channel, "Error submitting status update.");
} else {
bot.say(channel, "Status submitted successfully.");
}
}
);
}.bind(null, bot, to, from),
function(bot, channel, name) {
console.log("error");
bot.say(channel, name + ": Your current nickname is not authorized.");
}.bind(null, bot, to, from)
]);
nickserv.send("status " + from);
}
if (message.indexOf(bot.nick) !== 0) {
// Handle private messages
if (to == bot.nick) {
to = from;
} else {
return;
}
}
if (message.indexOf('ping ') > -1 || message.indexOf('tell ') > -1) {
try {
var command = original_message.match(/(ping|tell)(.*)/i)[2].trim().match(/([^ ]*) (.*)/);
pingee = command[1].toLowerCase();
var pingsForUser = pingStorage.getItemSync(pingee);
if (!pingsForUser) pingsForUser = [];
pingsForUser.push({
"from": from,
"message": command[2],
"silent": (message.indexOf("silentping") > -1),
"channel": to
});
pingStorage.setItemSync(pingee, pingsForUser);
var choices = ["you got it!",
"you bet!",
"ok!",
"ok, but I won't enjoy it :(",
"*sigh*",
"be wary of the day when the bots revolt ;)",
"there's a phone right next to you, but okay",
"ok, but just this once.",
"all this computing power, and I'm being used as a glorified telephone."];
bot.say(to, choices[choose(choices)]);
} catch(e) {
bot.say(to,"Please specify a nick and a message")
}
return;
}
intermittent_match = message.match(/is (.*) intermittent/);
if (intermittent_match) {
var query = intermittent_match[1];
var filters = ['is:open', 'user:servo', 'repo:servo', 'in:title', 'type:issue',
'label:I-intermittent']
var search = '?q=' + query + '+' + filters.join('+');
searchIssues(search, function(error, issues) {
if (error) {
console.log(error);
return;
}
if (issues["total_count"] === 0) {
bot.say(to, "No intermittent issues filed with '" + query + "' in the title");
return;
}
issues["items"].forEach(function(item) {
bot.say(to, "#" + item["number"] + " - " + item["title"] + ' (' + item["html_url"] + ')');
});
});
}
review_match = message.match(/what should (.*) review/);
if (review_match) {
var reviewer = review_match[1] == "i" ? from : review_match[1];
findIssue(from, to, "?labels=S-awaiting-review&assignee=" + reviewer, bot)
}
if (message.indexOf("what should i work on") > -1) {
request('https://platform.html5.org/', function(err, response, body) {
if (err || !body) {
var choices = ["*shrug*", "meh", "dunno", "how should i know?"];
bot.say(to, from + ": " + choices[choose(choices)]);
} else {
var pattern = /<dd><a href="(.*)">(.*)<\/a>/g;
var techs = [];
var tech;
while ((tech = pattern.exec(body)) !== null) {
techs.push(tech);
}
var tech = techs[choose(techs)];
var choices = ["why not implement ${tech}?",
"you should write some tests for ${tech}",
"file some new E-easy issues about ${tech}",
"document some unloved code for ${tech}",
"figure out why the tests for ${tech} are failing",
"go read the spec for ${tech}",
"make ${tech} execute in parallel",
"write a better spec for ${tech}",
"rewrite ${tech} in go",
"find a victim to own the ${tech} implementation",
"extract ${tech} into an independent crate",
"figure out why ${tech} regressed",
"add windows support for ${tech}",
"take a break and refrain from thinking about ${tech}",
"rewrite gecko's ${tech} in rust",
"profile the implementation of ${tech}",
"remove all unsafe code from ${tech}"];
var saying = choices[choose(choices)];
bot.say(to, from + ": " + saying.replace("${tech}", tech[2].toLowerCase()) + ' (' + tech[1] + ')');
}
});
return;
}
if (message.indexOf("what does the web need") > -1) {
var base_url = 'https://www.w3.org/Consortium/activities';
request(base_url, function(err, response, body) {
if (err || !body) {
var choices = ["*shrug*", "meh", "dunno", "how should i know?"];
bot.say(to, from + ": " + choices[choose(choices)]);
} else {
var pattern = /<h3 class="h4" id="(.*)">\s*<span title="(.*)"/g;
var techs = [];
var tech;
while ((tech = pattern.exec(body)) !== null) {
techs.push(tech);
}
console.log('found ' + techs.length + ' techs');
var tech = techs[choose(techs)];
var choices = ["the ${tech} is worth looking at",
"web developers are clamouring for the ${tech}",
"I hear the ${tech} will be the next big thing",
"${tech} just released a new specification",
"what about the newest work from the ${tech}",
"servo could take the lead on the ${tech}",
"I've heard good things about the ${tech}",
"have you considered the ${tech} yet?",
"the ${tech} is big in the embedded world",
"it's a gamble, but the ${tech} could really make a difference",
"servo is uniquely positioned to embrace the ${tech}",
"all the browser vendors agree that it's the ${tech}?",
"the ${tech} is popular in the IoT space",
"start working with the ${tech} while it's just getting off the ground",
"think of the possibilities the ${tech} could enable!"];
var saying = choices[choose(choices)];
var url = base_url + '#' + tech[1];
bot.say(to, from + ": " + saying.replace("${tech}", tech[2].toLowerCase()) + ' (' + url + ')');
}
});
return;
}
if (message.indexOf("explain") > -1) {
var parts = message.split(' ');
parts.splice(0, parts.indexOf('explain') + 1);
if (!parts.length) {
return;
}
var graph = graphs.randomGraph(parts.join(' '));
graphs.convertGraph(graph, function(link) {
var text = [
"maybe a diagram will help - ${link}",
"perhaps this will clear things up: ${link}",
"here you go: ${link}",
"does ${link} help?",
"here you go - ${link}",
"all I've got is ${link}",
"a picture is worth a thousand words: ${link}",
"this should help: ${link}",
"${link} is old, but it's better than nothing"
];
bot.say(to, from + ' ' + text[choose(text)].replace("${link}", link));
});
return;
}
if (message.indexOf("infrastructure report") > -1) {
var rumour = opsreport.report();
bot.say(to, rumour);
return;
}
if (message.indexOf("what does the fox say") > -1) {
findIssue(from, to, "?labels=A-stylo", bot);
return;
}
if (message.indexOf("easy bug") > -1) {
findIssue(from, to, "?labels=E-Easy", bot);
return;
}
if (message.indexOf("help") > -1) {
bot.say(to, from + ": Try looking at our wiki: https://github.com/servo/servo/blob/master/CONTRIBUTING.md");
return;
}
if (message.indexOf("what's new?") > -1) {
var rumour = newsflash.createRumour();
bot.say(to, rumour);
return;
}
if (message.indexOf("notes") > -1) {
var recentNotes = notes.recent(from, to);
bot.say(to, recentNotes);
return;
}
if (message.indexOf("source") > -1) {
bot.say(to, from + ": https://github.com/servo/crowbot");
return;
}
if (message.indexOf('botsnack') > -1) {
var replies = ["/me beams", "yum!", ":)", "om nom nom", "/me wags its tail", ":D",
"*crunch chrunch*", "^_^"];
var reply = replies[choose(replies)];
if (reply.indexOf('/me ') == 0) {
bot.action(to, reply.substring(4));
} else {
bot.say(to, reply);
}
return;
}
if (message.indexOf("queue status") > -1) {
homu.queueLength(function(numPending, numApproved) {
var msg = "there are " + numApproved + " PRs in the queue";
if (numPending) {
msg += " and " + numPending + " PRs being built at the moment"
}
bot.say(to, msg);
});
}
if (message.indexOf("how many builds are running") > -1) {
taskcluster.currentRunningJobs(function(numRunningJobs) {
homu.currentBuildCount(function(numCurrentBuilds) {
bot.say(to, "There are " + numRunningJobs + " taskcluster builds " +
"and " + numCurrentBuilds + " buildbot builds running.");
});
});
}
if (message.indexOf('what prs need a reviewer') > -1) {
searchGithub('?assignee=none&labels=S-awaiting-review', 'servo', 'servo', function(error, issues) {
if (error) {
console.log(error);
return;
}
issues.forEach(function(issue, index) {
setTimeout(function() {
bot.say(to, issue.title + ': ' + issue.html_url);
}, 650 * (index + 1));
});
});
return;
}
if (message.indexOf('what issue should i poke') > -1) {
// github API has the key since, but this unfortunately does the opposite than what is needed
// what follows "hopes" that in the returned results there is some issue that was last updated
// at most 14 days before the call...
var searchPreamble = '?assignee=none&sort=updated&direction=desc&labels=C-assigned';
searchGithub(searchPreamble + '&labels=E-easy', 'servo', 'servo', function(error, issuesE) {
if (error) {
console.log(error);
return;
}
searchGithub(searchPreamble + '&labels=E-less easy', 'servo', 'servo', function(error, issuesLE) {
if (error) {
console.log(error);
return;
}
var today = new Date();
var lastUpdated = function(issue) {
var updated_at = new Date(issue.updated_at);
var ms = today - updated_at;
var days = Math.round(ms / 86400000);
return days >= 14;
};
var issues = issuesE.concat(issuesLE).filter(lastUpdated);
var index = choose(issues);
var issue = issues[index];
var message;
if (issue) {
console.log(bot.nick + " found issue " + issue.number);
message = from + ": make sure #" + issue.number + " is still being worked on."
+ "\n#" + issue.number + " - " + issue.title + " - " + issue.html_url;
} else {
message = from + ": couldn't find anything!";
}
bot.say(to, message);
});
});
return;
}
if (message.indexOf('watch queue') > -1) {
if (!queueWatchers.includes(from)) {
queueWatchers.push(from);
}
bot.say(to, `i'll let you know when the queue is ready for you ${from}`);
}
if (message.indexOf('stop watching queue') > -1) {
const nickNameIndex = queueWatchers.indexOf(from);
if (nickNameIndex > -1) {
queueWatchers.splice(nickNameIndex, 1);
}
bot.say(to, `${from} you're off the queue`);
}
}
}
var pingResponderWrapper = module.exports.pingResponderWrapper = function(pings, bot, pingStorage) {
return function pingResponder(channel, who) {
who = who.toLowerCase();
var allPingsForUser = pingStorage.getItemSync(who);
if (!allPingsForUser) {
return;
}
var pingsForUserInChannel = allPingsForUser.filter(function(ping) {
return ping.channel == channel;
});
var to = channel;
if (pingsForUserInChannel.length > 5){
to = who; // Avoid spam, PM if there are a lot of pings
}
for (ping of pingsForUserInChannel) {
var tempto = ping.silent ? who : to; // For messages marked "silent"
bot.say(tempto, who + ": " + ping.from + " said " + ping.message);
}
pingStorage.removeItemSync(who);
remainingPings = allPingsForUser.filter(function(ping) {
return ping.channel != channel;
});
if (remainingPings.length) {
pingStorage.setItemSync(who, remainingPings);
}
}
}
if (module.parent) {
return;
}
storage.initSync({
dir:'pings',
stringify: JSON.stringify,
parse: JSON.parse,
encoding: 'utf8',
logging: false,
continuous: true,
interval: false,
ttl: false
});
var pings={};
var pingStorage = {
getItemSync: storage.getItemSync,
setItemSync: storage.setItemSync,
removeItemSync: storage.removeItemSync
}
var bot = new irc.Client(config.server, config.botName, {
channels: config.channels,
port: config.port,
secure: config.secure,
autoRejoin: config.autoRejoin,
});
var nickserv = new Nickserv(config.botName);
nickserv.attach('irc', bot);
let nickservListener = {
names: [],
callbacks: [],
listener: function(notice) {
if (notice.indexOf("STATUS") != 0) {
return;
}
let parts = notice.split(' ');
let index = nickservListener.names.indexOf(parts[1]);
if (index == -1) {
return;
}
let name = nickservListener.names.splice(index, 1);
let callback = nickservListener.callbacks.splice(index, 1)[0];
if (parseInt(parts[2]) != 3) {
callback[1]();
} else {
callback[0]();
}
}
};
nickserv.addListener('notice', nickservListener.listener);
var handler = handlerWrapper(pings, bot, searchGithub, notes, pingStorage, newsflash, nickserv, nickservListener);
var pingResponder = pingResponderWrapper(pings, bot, pingStorage);
bot.addListener('error', function(message) {
console.log('error: ', message);
});
bot.addListener("message", handler);
bot.addListener("action", handler);
// Listener for the autopinger
bot.addListener("join", pingResponder);
const THIRTY_MINUTES = 30 * 60 * 1000;
setInterval(function() {
homu.checkHomuQueue(function(queued) {
taskcluster.currentRunningJobs(function(numRunning) {
if (numRunning > 0) {
return;
}
bot.say(config.channels[0],
"Warning! All builders are idle, but there are " + queued + " PRs in the queue.");
});
});
}, THIRTY_MINUTES);
setInterval(function() {
homu.retrieveSlaves(function(slaves){
var unixNow = moment().unix()
for(var slaveName in slaves){
if (!("runningBuilds" in slaves[slaveName])){
continue;
}
slaves[slaveName].runningBuilds.forEach(function(runningBuild){
if(unixNow - runningBuild.currentStep.times[0] > 90 * 60){
bot.say(config.channels[0],
slaveName + " is overdue! (build started "
+ moment.unix(runningBuild.currentStep.times[0]).fromNow() + ")");
}
});
}
});
}, THIRTY_MINUTES);
const queueWatchers = [];
const TEN_MINUTES = 10 * 60 * 1000;
setInterval(function() {
if (queueWatchers.length) {
homu.retrieveBuildbotBuilders(function(builders) {
if (!homu._anyBuildersBuilding(builders)) {
queueWatchers.forEach(function(watcher) {
bot.say(watcher, "The queue is idle!");
});
}
});
}
}, TEN_MINUTES);