Permalink
Browse files

Initial commit.

  • Loading branch information...
scottgonzalez committed Jul 13, 2012
0 parents commit 7ba68d9ebeb76ca5f1f2e8fd5ac2e80d31a22a5f
Showing with 92 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +71 −0 notifier.js
  3. +20 −0 package.json
@@ -0,0 +1 @@
+node_modules
@@ -0,0 +1,71 @@
+var http = require( "http" ),
+ querystring = require( "querystring" ),
+ util = require( "util" ),
+ EventEmitter2 = require( "eventemitter2" ).EventEmitter2;
+
+function Notifier() {
+ var notifier = this;
+ this.server = http.createServer(function( request, response ) {
+ var data = "";
+ request.setEncoding( "utf8" );
+ request.on( "data", function( chunk ) {
+ data += chunk;
+ });
+ request.on( "end", function() {
+ try {
+ data = querystring.parse( data );
+ data = JSON.parse( data.payload );
+ } catch( error ) {
+ // Invalid data, stop processing
+ response.writeHead( 400 );
+ response.end();
+ notifier.emit( "error", error );
+ return;
+ }
+
+ // Accept the request and close the connection
+ response.writeHead( 202 );
+ response.end();
+
+ notifier.process( data );
+ });
+ });
+
+ EventEmitter2.call( this, {
+ wildcard: true,
+ delimiter: "/"
+ });
+}
+util.inherits( Notifier, EventEmitter2 );
+
+Notifier.prototype.listen = function() {
+ this.server.listen.apply( this.server, arguments );
+};
+
+Notifier.prototype.process = function( raw ) {
+ var refParts = raw.ref.split( "/" ),
+ type = refParts[ 1 ],
+ owner = raw.repository.owner.name,
+ repo = raw.repository.name,
+ data = {
+ commit: raw.after,
+ owner: owner,
+ repo: repo,
+ raw: raw
+ },
+ eventName = owner + "/" + repo + "/" + raw.ref.substr( 5 );
+
+ if ( type === "heads" ) {
+ // Handle namespaced branches
+ data.branch = refParts.slice( 2 ).join( "/" );
+ } else if ( type === "tags" ) {
+ data.tag = refParts[ 2 ];
+ }
+
+ this.emit( eventName, data );
+};
+
+exports.Notifier = Notifier;
+exports.createServer = function() {
+ return new Notifier();
+};
@@ -0,0 +1,20 @@
+{
+ "author": "Scott González <scott.gonzalez@gmail.com> (http://scottgonzalez.com)",
+ "name": "git-notifier",
+ "description": "Listen for GitHub updates using an EventEmitter.",
+ "version": "0.0.0",
+ "homepage": "https://github.com/scottgonzalez/node-git-notifier",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/scottgonzalez/node-git-notifier.git"
+ },
+ "main": "notifier.js",
+ "engines": {
+ "node": "*"
+ },
+ "dependencies": {
+ "eventemitter2": "0.4.9"
+ },
+ "devDependencies": {},
+ "optionalDependencies": {}
+}

0 comments on commit 7ba68d9

Please sign in to comment.