Skip to content

Commit

Permalink
adding support for multiple branches
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Mervine committed Jun 28, 2014
1 parent 3006566 commit 3837b79
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 22 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,25 +80,27 @@
"prod": {
"script": "/home/me/update_prod.sh",
"branch": "master" // optional branch matcher
// "branch" can also be an array of branches:
// e.g. `[ "master", "develop" ]`
},
"dev": {
"script": "/home/me/update_dev.sh",
"branch": "develop" // optional branch matcher
"branch": [ "release", "develop" ] // optional branch matcher
}
}

Where `/home/me/update_prod.sh` is something like:

#!/usr/bin/env bash
cd /path/to/mysite

# For safty, you can stash any changes, although best practice says
# there shouldn't be any here.
# git stash

git checkout master
git pull

make restart


Expand Down
2 changes: 1 addition & 1 deletion example.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
},
"gofish" : {
"script": "./test/script.js",
"branch" : "master"
"branch" : ["master"]
}
}
21 changes: 14 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,20 @@ var server = http.createServer(function (request, response) {
return;
}

if (config[endpoint].branch &&
!ref.match(config[endpoint].branch)) {
console.warn('Skipping request:\n -> %s doesn\'t match %s',
ref, config[endpoint].branch);
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end();
return;
var branch = config[endpoint].branch;
if (branch) {
var skip = function() {
console.warn('Skipping request:\n -> %s doesn\'t match %s', ref, branch);
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end();
};

if (Array.isArray(branch)) {
if (!branch.some(function(i) { return ref.match(i) })) return skip();
} else {
if (!ref.match(branch)) return skip();
}

}

if (config[endpoint].script) {
Expand Down
54 changes: 44 additions & 10 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var fs = require('fs');
var qs = require('querystring');
var format = require('util').format;
var request = require('request-lite');
var async = require('async');

// supress output

Expand All @@ -31,16 +32,49 @@ var server = require('../');
tape('git fish', function (group) {

group.test('test valid json', function (test) {
request.post('http://localhost:10888/test1?token=go-fish', { json: post_data }, function(err, res, body) {
test.error(err, 'should not error')
test.ok(res, 'should return response');
test.equal(res.statusCode, 200, 'should return 200');
setTimeout(function () {
// give it a second
test.ok(fs.existsSync('/tmp/.git.fish.test.out'), 'should run script');
cleanup();
test.end();
}, 200);
async.series([
function(callback) {
request.post('http://localhost:10888/test1?token=go-fish', { json: post_data }, function(err, res, body) {
test.error(err, 'should not error')
test.ok(res, 'should return response');
test.equal(res.statusCode, 200, 'should return 200');
setTimeout(function () {
// give it a bit
test.ok(fs.existsSync('/tmp/.git.fish.test.out'), 'should run script');
cleanup();
callback();
}, 200);
});
},
function(callback) {
request.post('http://localhost:10888/test2?token=go-fish', { json: post_data }, function(err, res, body) {
test.error(err, 'should not error')
test.ok(res, 'should return response');
test.equal(res.statusCode, 200, 'should return 200');
setTimeout(function () {
// give it a bit
test.ok(fs.existsSync('/tmp/.git.fish.test.out'), 'should run script');
cleanup();
callback();
}, 200);
});
},
function(callback) {
request.post('http://localhost:10888/test3?token=go-fish', { json: post_data }, function(err, res, body) {
test.error(err, 'should not error')
test.ok(res, 'should return response');
test.equal(res.statusCode, 200, 'should return 200');
setTimeout(function () {
// give it a bit
test.ok(fs.existsSync('/tmp/.git.fish.test.out'), 'should run script');
cleanup();
callback();
}, 200);
});
},
],
function() {
test.end();
});
});

Expand Down
4 changes: 4 additions & 0 deletions test/test.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
"test2" : {
"script": "test/script.js",
"branch" : "master"
},
"test3" : {
"script": "test/script.js",
"branch" : ["master", "develop"]
}
}

0 comments on commit 3837b79

Please sign in to comment.