-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.js
54 lines (37 loc) · 1.43 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
var http = require('http');
var spawn = require('child_process').spawn;
var crypto = require('crypto');
var url = require('url');
var secret = 'amazingkey'; // secret key of the webhook
var port = 8081; // port
http.createServer(function(req, res){
console.log("request received");
res.writeHead(400, {"Content-Type": "application/json"});
var path = url.parse(req.url).pathname;
if(path!='/push' || req.method != 'POST'){
var data = JSON.stringify({"error": "invalid request"});
return res.end(data);
}
var jsonString = '';
req.on('data', function(data){
jsonString += data;
});
req.on('end', function(){
var hash = "sha1=" + crypto.createHmac('sha1', secret).update(jsonString).digest('hex');
if(hash != req.headers['x-hub-signature']){
console.log('invalid key');
var data = JSON.stringify({"error": "invalid key", key: hash});
return res.end(data);
}
console.log("running hook.sh");
var deploySh = spawn('sh', ['hook.sh']);
deploySh.stdout.on('data', function(data){
var buff = new Buffer(data);
console.log(buff.toString('utf-8'));
});
res.writeHead(400, {"Content-Type": "application/json"});
var data = JSON.stringify({"success": true});
return res.end(data);
});
}).listen(port);
console.log("Server listening at " + port);