forked from creationix/creationix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostReceive.js
36 lines (34 loc) · 1.08 KB
/
postReceive.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
var Url = require('url');
var ChildProcess = require('child_process');
var QueryString = require('querystring');
// Handler for github post-receive hooks
module.exports = function setup(mount, script, hook) {
return function handle(req, res, next) {
if (req.method !== "POST") return next();
if (!req.hasOwnProperty("uri")) { req.uri = Url.parse(req.url); }
if (req.uri.pathname !== mount) return next();
var data = "";
req.setEncoding('utf8');
req.on('data', function (chunk) {
data += chunk;
});
req.on('end', function () {
var message;
try {
data = QueryString.parse(data);
message = JSON.parse(data.payload);
} catch (err) {
message = {};
}
ChildProcess.execFile(script, [], {}, function (err, stdout, stderr) {
if (err) return next(err);
var body = stdout + stderr;
process.stdout.write(stdout);
process.stderr.write(stderr);
if (hook) hook();
res.writeHead(200, {"Content-Length": Buffer.byteLength(body)});
res.end(body);
});
});
};
};