-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.js
More file actions
116 lines (100 loc) · 3.33 KB
/
github.js
File metadata and controls
116 lines (100 loc) · 3.33 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
/**
* GitHub Alerts
*
* Primary code from https://github.com/Ecuacion/Pokemon-Showdown-Node-Bot/tree/master/features/github
* Also, code from https://github.com/smogon/GitHub-Bot
* This handles alerting our Development room of GitHub updates/changes.
* This was converted to work on PS servers by jd, and then converted to work on GitHub by panpawn.
*
* @license MIT license
*/
'use strict';
const request = require('request');
const usernames = require('./github-usernames');
if (!Config.github) return;
let updates = {};
const github = exports.github = require('githubhook')({
port: Config.github.port,
secret: Config.github.secret,
});
function sendMessage(message) {
for (let room of Config.github.rooms) {
Chat.basicSend(bot.channels.get(room), message);
}
}
function shorten (url) {
return new Promise(function (resolve, reject) {
function shortenCallback (error, response, body) {
let shortenedUrl = url
if (!error && response.headers.location) {
shortenedUrl = response.headers.location
}
return resolve(shortenedUrl);
}
request.post('https://git.io/', {form: {url: url}}, shortenCallback)
});
}
function getRepo(repo) {
switch (repo) {
case 'Pokemon-Showdown':
repo = 'server';
break;
case 'Pokemon-Showdown-Client':
repo = 'client';
break;
case 'Pokemon-Showdown-Dex':
repo = 'dex';
break;
default:
repo = repo.toLowerCase();
}
return repo;
}
github.on('push', async function push(repo, ref, result) {
let url = result.compare;
let branch = /[^/]+$/.exec(ref)[0];
let message = [];
repo = getRepo(repo);
const userid = result.pusher.name.toLowerCase().replace(/[^a-z0-9]+/g, '');
const username = usernames[userid] || userid;
const action = result.forced ? 'force-pushed' : 'pushed';
const number = result.commits.length === 1 ? `1 new commit` : `${result.commits.length} new commits`;
message.push(`[${repo}] ${username} ${action} ${number} to ${branch}:`);
await Tools.asyncForEach(result.commits, async function (commit) {
const commitMessage = commit.message;
const commitUsername = usernames[commit.author.name] || commit.author.name;
const commitHash = commit.id.substring(0, 6);
const commitUrl = await shorten(commit.url);
let shortCommit = /.+/.exec(commitMessage)[0];
if (commitMessage !== shortCommit) {
shortCommit += '...';
}
message.push(`${commitHash} ${commitUsername}: ${shortCommit} (${commitUrl})`);
});
sendMessage(message.join('\n'));
});
github.on('pull_request', async function pullRequest(repo, ref, result) {
const COOLDOWN = 10 * 60 * 1000;
const requestNumber = result.pull_request.number;
const url = await shorten(result.pull_request.html_url);
let action = result.action;
const userid = result.sender.login.toLowerCase().replace(/[^a-z0-9]+/g, '');
const username = usernames[userid] || userid;
repo = getRepo(repo);
if (!updates[repo]) updates[repo] = {};
if (action === 'synchronize') {
action = 'updated';
}
if (action === 'labeled' || action === 'unlabeled') {
// Nobody cares about labels
return;
}
const now = Date.now();
if (updates[repo][requestNumber] && updates[repo][requestNumber] + COOLDOWN > now) {
return;
}
updates[repo][requestNumber] = now;
let message = `[${repo}] ${username} ${action} pull request #${requestNumber}: ${result.pull_request.title} (${url})`;
sendMessage(message);
});
github.listen();