-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
82 lines (68 loc) · 2.11 KB
/
server.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
const fastify = require('fastify')();
const irc = require('irc-upd');
const settings = require('./settings');
const client = new irc.Client('irc.orpheus.network', settings.username, {
channels: []
});
client.addListener('registered', () => {
if (settings.nickserv) {
client.say('nickserv', `IDENTIFY ${settings.nickserv}`);
client.join(settings.channels.join(','));
}
});
function handle_issue(body, messages) {
let message = `${body.repository.name} - ${body.sender.login} ${body.action} issue #${body.issue.number}`;
message += ` - ${body.issue.title} | ${body.issue.html_url}`;
// TODO: handle comments
messages.push(message);
}
function handle_commits(body, messages) {
let ref = body.ref.split('/');
let branch = ref[ref.length - 1];
if (branch !== body.repository.master_branch) {
return;
}
for (let commit of body.commits) {
let id = commit.id.substr(0, 7);
let message = `${body.repository.name} - ${commit.author.name} just pushed commit ${id} to ${branch}`;
message += ` - ${commit.message.split(/\r?\n/)[0].substr(0, 50).trim()} | ${commit.url.replace(commit.id, id)}`;
messages.push(message);
}
}
fastify.get('/', async () => {
return { hello: 'world' };
});
fastify.post('/', async (req) => {
let body = req.body;
let event = req.headers['x-github-event'];
let messages = [];
if (event === 'issues') {
handle_issue(body, messages);
}
else if (event === 'push') {
handle_commits(body, messages);
}
else {
// View these events within GitHub to see headers and body
// console.log(JSON.stringify(body, null, 2));
// console.log(req.headers);
// let message = `New Github Event ${event} by ${body.sender.login}`;
// messages.push(message);
}
for (let channel of settings.channels) {
for (let message of messages) {
client.say(channel, message);
}
}
return { message: 'posted' };
});
const start = async () => {
fastify.listen(13452, '0.0.0.0', (err, address) => {
if (err) {
fastify.log.error(err);
process.exit(1);
}
fastify.log.info(`server listening on ${address}`);
});
};
start();