forked from Spandamn/DH
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.js
More file actions
100 lines (93 loc) · 3.36 KB
/
github.js
File metadata and controls
100 lines (93 loc) · 3.36 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
/**
* GitHub Alert Notifications
* Pokemon Showdown - http://pokemonshowdown.com/
*
* This notifies a set of rooms of changes to specific repositor(y/ies).
*
* Much of this comes from Ecuacion's bot, which is based on xfix's bot,
* and jd who originally converted this to PS server-side.
*
* @license MIT license
*/
'use strict';
let gitConfig = {};
const git = exports.github = require('githubhook')(gitConfig);
if (!Config.github) {
return;
} else if (Config.github === {}) {
gitConfig.port = 3420;
gitConfig.secret = "";
} else {
gitConfig = {
port: Config.github.port,
secret: Config.github.secret,
};
}
let updates = {};
let targetRooms = (Config.github.rooms && Config.github.rooms.length) ? Config.github.rooms : ['development'];
targetRooms = targetRooms.map(toId);
for (let i = 0; i < targetRooms.length; i++) {
targetRooms[i] = Rooms(targetRooms[i]);
}
let gitBans = {};
if (targetRooms[0].chatRoomData) targetRooms[0].chatRoomData.gitBans = gitBans;
let sendReport = function(html) {
for (let curRoom of targetRooms) {
if (curRoom) curRoom.add(`|html|<div class="infobox">${html}</div>`).update();
}
};
git.on('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 += `<font color='909090'>${Chat.escapeHTML(result.pusher.name)}</font> `;
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];
message = "";
message += `<font color='FF00FF'>${Chat.escapeHTML(repo)}</font>/`;
message += Chat.html `<font color='800080'>${branch}</font> `;
message += Chat.html `<a href="${commit.url}">`;
message += Chat.html `<font color='606060'>${commit.id.substring(0, 6)}</font></a> `;
message += Chat.html `<font color='909090'>${commit.author.name}</font>: ${shortCommit}`;
if (commitMessage !== shortCommit) message += '…';
messages.push(message);
});
sendReport(messages.join('<br />'));
});
git.on('pull_request', function pullRequest(repo, ref, result) {
let COOLDOWN = 10 * 60 * 1000;
let requestUsername = toId(result.sender.login);
if (requestUsername in gitBans) return;
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 === 'unlabeled') {
// 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;
sendReport(message);
});
git.listen();