-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
69 lines (56 loc) · 2.09 KB
/
index.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
var express = require('express');
var bodyParser = require('body-parser');
var multer = require('multer');
var requests = require('request');
var mustache = require('mustache');
var glob = require("glob");
var templates = require('./templates.js');
var hipchat_host = (process.env.HIPCHAT_HOST || 'https://hipchat.zalando.net');
var app = express();
app.set('port', (process.env.PORT || 5000));
app.use(bodyParser.json({strict: false}));
app.use(bodyParser.text());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(multer());
function getType(type, payload) {
var action = payload.action || '';
var body = ((payload.comment && payload.comment.body) || '').trim();
if (type == 'issue_comment' && action == 'created' && (body == ':+1:' || body == '👍')) {
return 'issue_approve';
}
return type;
}
app.all('*', function(request, response) {
var type = request.get('X-GitHub-Event');
var token = request.body.auth_token;
var room = request.body.room_id;
if (!type || !token || !room || !request.body.payload) {
response.status(400).send("This is not a valid event.");
return;
}
var payload = JSON.parse(request.body.payload);
console.log(type + ' @ ' + room);
var template = templates.getTemplate(getType(type, payload));
if (!template) {
console.log("Unknown event type " + type);
response.status(400).send("Unknown event type " + type);
return;
}
var body = mustache.render(template, payload);
requests.post(hipchat_host + '/v2/room/' + room + '/notification',
{
json: JSON.parse(body),
auth: { bearer: token }
},
function (error, res, b) {
if (error) {
console.log("Error forwarding event: " + error);
}
response.status((res && res.statusCode) || 500).send(b);
}
);
});
app.listen(app.get('port'), function() {
console.log('Running on port', app.get('port'));
console.log('Proxying to', hipchat_host);
});