-
Notifications
You must be signed in to change notification settings - Fork 46
/
github.js
84 lines (77 loc) · 2.9 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
81
82
83
84
/**
* GitHub Alerts
*
* Primary code from https://github.com/Ecuacion/Pokemon-Showdown-Node-Bot/tree/master/features/github
* This handles alerting our Development room of GitHub updates/changes.
* This was converted to work on PS servers by jd.
*
* @license MIT license
*/
'use strict';
if (!Config.github || !Config.github.port) 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'>" + Chat.escapeHTML(repo) + '</font>] ';
message += WL.nameColor(result.pusher.name, true) + " ";
message += (result.forced ? '<font color="red">force-pushed</font>' : 'pushed') + " ";
message += "<b>" + Chat.escapeHTML(result.commits.length) + "</b> ";
message += "new commit" + (result.commits.length === 1 ? '' : 's') + " to ";
message += "<font color='800080'>" + Chat.escapeHTML(branch) + "</font>: ";
message += "<a href=\"" + Chat.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'>" + Chat.escapeHTML(repo) + "</font>/";
message += "<font color='800080'>" + Chat.escapeHTML(branch) + "</font> ";
message += "<a href=\"" + Chat.escapeHTML(commit.url) + "\">";
message += "<font color='606060'>" + Chat.escapeHTML(commit.id.substring(0, 6)) + "</font></a> ";
message += WL.nameColor(commit.author.name, true) + ": " + Chat.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') {
action = 'labled';
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 += WL.nameColor(result.sender.login, true) + " ";
message += action + " pull request <a href=\"" + url + "\">#" + requestNumber + "</a>: ";
message += result.pull_request.title;
sendMessages(message);
});
github.listen();