-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub.js
80 lines (72 loc) · 2.82 KB
/
github.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
'use strict';
/*
* from https://github.com/Ecuacion/Pokemon-Showdown-Node-Bot/tree/master/features/github
* modified to work as a chat plugin.
*/
if (!Config.github) return;
let updates = {};
const github = exports.github = require('githubhook')({
port: Config.github.port,
secret: Config.github.secret,
});
function sendMessages(str) {
for (let room in Config.github.rooms) {
if (!Rooms(Config.github.rooms[room])) continue;
Rooms(Config.github.rooms[room]).add("|raw|<div class=\"infobox\">" + str + "</div>").update();
}
}
github.on('push', function push(repo, ref, result) {
let url = result.compare;
let branch = /[^/]+$/.exec(ref)[0];
let messages = [];
let message = "";
message += "[<font color='FF00FF'>" + Tools.escapeHTML(repo) + '</font>] ';
message += "<font color='909090'>" + Tools.escapeHTML(result.pusher.name) + "</font> ";
message += (result.forced ? '<font color="red">force-pushed</font>' : 'pushed') + " ";
message += "<b>" + Tools.escapeHTML(result.commits.length) + "</b> ";
message += "new commit" + (result.commits.length === 1 ? '' : 's') + " to ";
message += "<font color='800080'>" + Tools.escapeHTML(branch) + "</font>: ";
message += "<a href=\"" + Tools.escapeHTML(url) + "\">View & compare</a>";
messages.push(message);
result.commits.forEach(function (commit) {
let commitMessage = commit.message;
let shortCommit = /.+/.exec(commitMessage)[0];
if (commitMessage !== shortCommit) {
shortCommit += '...';
}
message = "";
message += "<font color='FF00FF'>" + Tools.escapeHTML(repo) + "</font>/";
message += "<font color='800080'>" + Tools.escapeHTML(branch) + "</font> ";
message += "<a href=\"" + Tools.escapeHTML(commit.url) + "\">";
message += "<font color='606060'>" + Tools.escapeHTML(commit.id.substring(0, 6)) + "</font></a> ";
message += "<font color='909090'>" + Tools.escapeHTML(commit.author.name) + "</font>: " + Tools.escapeHTML(shortCommit);
messages.push(message);
});
sendMessages(messages.join("<br>"));
});
github.on('pull_request', function pullRequest(repo, ref, result) {
let COOLDOWN = 10 * 60 * 1000;
let requestNumber = result.pull_request.number;
let url = result.pull_request.html_url;
let action = result.action;
if (!updates[repo]) updates[repo] = {};
if (action === 'synchronize') {
action = 'updated';
}
if (action === 'labeled') {
// Nobody cares about labels
return;
}
let now = Date.now();
if (updates[repo][requestNumber] && updates[repo][requestNumber] + COOLDOWN > now) {
return;
}
updates[repo][requestNumber] = now;
let message = "";
message += "[<font color='FF00FF'>" + repo + "</font>] ";
message += "<font color='909090'>" + result.sender.login + "</font> ";
message += action + " pull request <a href=\"" + url + "\">#" + requestNumber + "</a>: ";
message += result.pull_request.title;
sendMessages(message);
});
github.listen();