-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.sh
84 lines (69 loc) · 2.59 KB
/
server.sh
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
#!/usr/local/bin/node
var http = require('http'),
url = require('url'),
exec = require('child_process').exec,
qs = require('querystring'),
Trello = require('./lib/handlers/trello.js');
var host = process.env.NGHWH_HOST,
port = process.env.NGHWH_PORT,
thisServerUrl = "http://" + host + ":" + port,
secretKey = process.env.NGHWH_SECRET_KEY,
trelloKey = process.env.TRELLO_KEY,
trelloToken = process.env.TRELLO_TOKEN,
trello = new Trello(trelloKey, trelloToken);
process.on('uncaughtException', function (err) {
console.log('[exception] ' + err);
console.log(err.stack);
});
http.createServer(function (req, res) {
var data = "";
req.on("data", function(chunk) {
data += chunk;
});
req.on("end", function() {
try {
var parsedUrl = url.parse(req.url, true),
githubEvent = req.headers['x-github-event'],
params = {};
if(parsedUrl.query['secret_key'] != secretKey) {
console.log("[warning] Unauthorized request " + req.url);
res.writeHead(401, "Not Authorized", {'Content-Type': 'text/html'});
res.end('401 - Not Authorized');
return;
}
// debugging
console.log("[trace] data is '" + data + "'");
if(data && data.length > 0) {
params = JSON.parse(data);
}
// For details, see https://developer.github.com/v3/activity/events/types/
//
switch(githubEvent) {
case 'pull_request':
console.log('action: ' + params['action']);
console.log('user: ' + params['pull_request']['user']['login']);
console.log('title: ' + params['pull_request']['title']);
console.log('url: ' + params['pull_request']['html_url']);
if(['opened','closed'].indexOf(params['action']) >= 0) {
trello.onPullRequest( params['action'],
params['pull_request']['user']['login'],
params['pull_request']['title'],
params['pull_request']['html_url'],
params['pull_request']['merged']);
}
break;
default:
console.log('unhandled event" ' + githubEvent);
}
res.writeHead(200, "OK", {'Content-Type': 'text/html'});
res.end('200 - OK');
return;
} catch (e) {
console.log('[exception] ' + e);
console.log(e.stack);
res.writeHead(500, "Internal Server Error", {'Content-Type': 'text/html'});
res.end('500 Internal Server Error');
}
});
}).listen(port, host);
console.log('Server running at ' + thisServerUrl );