Skip to content
Browse files

Initial commit.

  • Loading branch information...
0 parents commit 0cf9f10109d831d68488cfed53341fd05f1809ae @mythmon committed Apr 2, 2012
Showing with 59 additions and 0 deletions.
  1. +2 −0 README
  2. +57 −0 ears.js
2 README
@@ -0,0 +1,2 @@
+Node.js program to listen for incoming Github post-receive hooks, and act on
+them based on a config file.
57 ears.js
@@ -0,0 +1,57 @@
+var http = require('http');
+var fs = require('fs');
+var cp = require('child_process');
+
+var config = {}
+
+var configFile = fs.readFile('config.json', function (err, data) {
+ if (err) throw err;
+ config = JSON.parse(data);
+ console.log(config);
+});
+
+var server = http.createServer(function (req, res) {
+ console.log("Got %s request to %s.", req.method, req.url);
+ var data = '';
+
+ req.addListener('data', function (chunk) { data += chunk; });
+ req.addListener('end', function () {
+ data = data.replace(/^payload=/, '');
+ data = decodeURIComponent(data);
+ data = JSON.parse(data);
+
+ handleHit(data, function (err) {
+ if (err) {
+ res.writeHead(500);
+ console.log({"status": err});
+ res.end(JSON.stringify({"status": err}));
+ } else {
+ res.writeHead(200);
+ console.log({"status": "ok"});
+ res.end(JSON.stringify({"status": "ok"}));
+ }
+ });
+ });
+});
+
+function handleHit(data, callback) {
+ var repo = data.repository.name;
+ options = config.repositories[repo];
+ if (options === undefined) {
+ callback('no such repository "' + repo + '"');
+ return;
+ }
+ if (options.action === 'pull') {
+ cp.exec('git pull', {cwd: options.path}, function (error, stdout, stderr) {
+ if (error) {
+ callback(stderr);
+ } else {
+ callback();
+ }
+ });
+ }
+}
+
+server.listen(8080);
+
+// EOF vim:ts=2:sw=2

0 comments on commit 0cf9f10

Please sign in to comment.
Something went wrong with that request. Please try again.