Permalink
Browse files

Add github postReceive hook

  • Loading branch information...
1 parent 33068f9 commit ba516e569beb38a81e360fb2e44e8932801685c9 @creationix committed Dec 20, 2011
Showing with 35 additions and 1 deletion.
  1. +1 −1 package.json
  2. +34 −0 postReceive.js
View
@@ -2,7 +2,7 @@
"name": "creationix",
"description": "Creationix is a meta package for my personal packages",
"tags": ["stack", "creationix", "meta", "sandbox"],
- "version": "0.2.4",
+ "version": "0.2.5",
"author": "Tim Caswell <tim@creationix.com>",
"repository": {
"type": "git",
View
@@ -0,0 +1,34 @@
+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) {
+ 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);
+ console.log(message)
+ } catch (err) {
+ return next(err);
+ }
+ ChildProcess.execFile(script, [], {}, function (err, stdout, stderr) {
+ if (err) return next(err);
+ var body = stdout + stderr;
+ res.writeHead(200, {"Content-Length": Buffer.byteLength(body)});
+ res.end(body);
+ });
+ });
+ };
+};
+

0 comments on commit ba516e5

Please sign in to comment.