-
Notifications
You must be signed in to change notification settings - Fork 0
/
webhook.js
executable file
·69 lines (57 loc) · 1.68 KB
/
webhook.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
#!env node
// All scripts should do the pull themselves
var http = require('http');
var url = require('url');
var exec = require('child_process').exec;
var gitPull = 'git pull';
var port = 2047;
var hmac = function (str) {
var secret = process.env['secret'];
var crypto = require('crypto');
var hmac = crypto.createHmac('sha1', secret);
hmac.update('some data to hash');
return hmac.digest('hex');
};
var res200 = function(req, res) {
res.writeHead(200, {'Content-Type': 'text/plain' });
res.end();
};
var resError = function(code, msg, req, res) {
res.writeHead(code, {"Content-Type": "text/plain"});
res.end(msg);
};
var pull = function (req, res) {
runTask('./scripts/pull.sh', req, res);
};
var docs = function (req, res) {
runTask('./doc/make.sh', req, res);
};
var rels = function (req, res) {
runTask('./scripts/release.sh', req, res);
};
var runTask = function (script, req, res) {
exec(script, function(error, stdout, stderr) {
if (!error) {
console.log(script + "\n" + stdout);
res200(req, res);
} else {
resError(500, 'Internal Server Error\n', req, res);
console.log("There was an error running pull\n" + error.stack);
console.log(stderr);
}
});
};
var routes = {
'/pull': pull,
'/docs': docs,
'/rels': rels
};
function tryPull (req, res) {
//fixme -- verify by calculating hmac
if (routes[url.parse(req.url).pathname]) {
routes[url.parse(req.url).pathname](req, res);
} else {
resError(404, "404 Not Found\n", req, res);
}
}
http.createServer(tryPull).listen(port, console.log("server listening at " + port));