git2consul
Advanced tools
Comparing version 0.3.3 to 0.4.0
@@ -30,2 +30,3 @@ var fs = require('fs'); | ||
git_commands.clone(repo_config.url, repo_config.name, branch, branch_parent, function(err, output) { | ||
/* istanbul ignore next */ | ||
if (err) return cb(err); | ||
@@ -43,2 +44,3 @@ consul_broker.handleRefChange(bm, cb); | ||
git_commands.pull(branch_directory, function(err, output) { | ||
/* istanbul ignore next */ | ||
if (err) return cb(err); | ||
@@ -132,2 +134,3 @@ consul_broker.handleRefChange(this_obj, cb); | ||
bm.pull(function(err, msg) { | ||
/* istanbul ignore next */ | ||
if (err) return branch_creation_callback(err); | ||
@@ -142,2 +145,3 @@ logger.info("Initialized branch manager: %s", msg); | ||
mkdirp(branch_parent, function(err) { | ||
/* istanbul ignore next */ | ||
if (err) return branch_creation_callback(err); | ||
@@ -147,2 +151,3 @@ | ||
clone(bm, function(err, msg) { | ||
/* istanbul ignore next */ | ||
if (err) return branch_creation_callback(err); | ||
@@ -149,0 +154,0 @@ logger.info("Initialized branch manager: %s", msg); |
@@ -19,2 +19,3 @@ var fs = require('fs'); | ||
consul.kv.set({'key': key_name, value: content, token: token}, function(err) { | ||
/* istanbul ignore if */ | ||
if (err) return cb('Failed to write key ' + key_name + ' due to ' + err); | ||
@@ -35,2 +36,3 @@ cb(); | ||
fs.readFile(fqf, {encoding:'utf8'}, function(err, body) { | ||
/* istanbul ignore if */ | ||
if (err) return cb('Failed to read key ' + fqf + ' due to ' + err); | ||
@@ -50,2 +52,3 @@ var body = body ? body.trim() : ''; | ||
consul.kv.del({'key': key_name, token: token}, function(err) { | ||
/* istanbul ignore if */ | ||
if (err) return cb('Failed to delete key ' + key_name + ' due to ' + err); | ||
@@ -103,3 +106,3 @@ cb(); | ||
break; | ||
// TODO: Test and handle copies and moves. | ||
/* istanbul ignore next */ | ||
default: | ||
@@ -117,2 +120,3 @@ logger.error('Unknown git status %s', record.type); | ||
consul.kv.get({'key': key_name, token: token}, function(err, item) { | ||
/* istanbul ignore if */ | ||
if (err) return cb(err); | ||
@@ -140,2 +144,3 @@ cb(null, item === undefined ? item : item.Value); | ||
branch_manager.getCurrentRef(function(err, ref) { | ||
/* istanbul ignore if */ | ||
if (err) return cb("Failed to get current ref for branch " + branch_manager.getBranchName() + " due to " + err); | ||
@@ -147,2 +152,3 @@ | ||
var handle_records = function(err, records) { | ||
/* istanbul ignore if */ | ||
if (err) return cb(err); | ||
@@ -156,3 +162,2 @@ process_records(branch_manager, records, function(errs) { | ||
return cb(err); | ||
cb(null, 'Most recent ref in branch ' + branch_manager.getBranchName() + ' is ' + ref); | ||
}); | ||
@@ -164,2 +169,3 @@ }); | ||
exports.getLastProcessedRef(branch_manager, function(err, last_processed_ref) { | ||
/* istanbul ignore if */ | ||
if (err) return cb(err); | ||
@@ -166,0 +172,0 @@ |
@@ -12,2 +12,3 @@ var _ = require('underscore'); | ||
var hook_providers = { | ||
'bitbucket' : require('./hooks/webhook.js').bitbucket, | ||
'github' : require('./hooks/webhook.js').github, | ||
@@ -14,0 +15,0 @@ 'stash' : require('./hooks/webhook.js').stash, |
@@ -43,2 +43,5 @@ var util = require('util'); | ||
// Parse form data (for bitbucket) | ||
app.use(bodyParser.urlencoded({extended:true})); | ||
app.listen(config.port); | ||
@@ -73,6 +76,7 @@ | ||
logger.debug(util.inspect(req.body)) | ||
/* istanbul ignore else */ | ||
if (implementation.isValid(req)) { | ||
logger.trace(util.inspect(req.body, {depth: 10})); | ||
// Only pull changed branches | ||
@@ -82,5 +86,4 @@ var changes = implementation.getHeadChanges(req); | ||
var change = changes[i]; | ||
var ref = change.ref; | ||
var to_hash = change.to_hash; | ||
logger.debug('Handling reference change to %s', util.inspect(ref)); | ||
logger.debug('Webhook noted change to branch %s with commit id %s', change.branch, change.to_hash); | ||
@@ -127,3 +130,2 @@ // Update consul git branch | ||
return [{ | ||
'ref': req.body.ref, | ||
'to_hash': req.body.head_commit.id, | ||
@@ -136,6 +138,2 @@ 'branch': req.body.ref.substring(11) | ||
return []; | ||
}, | ||
init: function(config, git_manager) { | ||
create_webhook(config, git_manager, this); | ||
} | ||
@@ -164,3 +162,2 @@ }; | ||
changes.push({ | ||
'ref': refChange.refId, | ||
'to_hash': refChange.toHash, | ||
@@ -172,7 +169,52 @@ 'branch': refChange.refId.substring(11) | ||
return changes; | ||
} | ||
}; | ||
/** | ||
* This object defines the properties unique to a bitbucket webhook. | ||
*/ | ||
exports.bitbucket = { | ||
type: 'bitbucket', | ||
isValid: function(req) { | ||
if (req && req.body && req.body.payload) { | ||
try { | ||
req.body = JSON.parse(req.body.payload); | ||
return req.body && req.body.commits && req.body.commits.length; | ||
/* istanbul ignore next */ | ||
} catch(e) { | ||
// We don't care about a busted message. | ||
} | ||
} | ||
return false; | ||
}, | ||
init: function(config, git_manager) { | ||
create_webhook(config, git_manager, this); | ||
getHeadChanges: function(req) { | ||
var changes = []; | ||
for (var i=0; i<req.body.commits.length; ++i) { | ||
var commit = req.body.commits[i]; | ||
// Only update if the head of a branch changed | ||
/* istanbul ignore else */ | ||
if (commit.branch && (commit.parents.length !== 0) && commit.node) { | ||
changes.push({ | ||
'to_hash': commit.node, | ||
'branch': commit.branch | ||
}); | ||
} | ||
} | ||
return changes; | ||
} | ||
}; | ||
/** | ||
* Register an init method for each support webhook implementation. | ||
*/ | ||
[exports.bitbucket, exports.github, exports.stash].forEach(function(hook_impl) { | ||
hook_impl.init = function(git_manager, config) { | ||
create_webhook(git_manager, config, hook_impl); | ||
}; | ||
}); |
@@ -61,2 +61,3 @@ var exec = require('child_process').exec; | ||
run_command('git diff --name-status ' + from_ref + ' ' + to_ref, cwd, function(err, output) { | ||
/* istanbul ignore if */ | ||
if (err) return cb(err); | ||
@@ -67,2 +68,3 @@ | ||
lines.forEach(function(line) { | ||
/* istanbul ignore if */ | ||
if (line.length < 2) return; | ||
@@ -82,2 +84,3 @@ var type = line.charAt(0); | ||
run_command('git ls-tree --name-status -r HEAD', cwd, function(err, output) { | ||
/* istanbul ignore if */ | ||
if (err) return cb(err); | ||
@@ -84,0 +87,0 @@ |
{ | ||
"name": "git2consul", | ||
"description": "System for moving data from git to consul", | ||
"version": "0.3.3", | ||
"version": "0.4.0", | ||
"contributors": [ | ||
@@ -6,0 +6,0 @@ { |
@@ -63,3 +63,3 @@ #### git2consul | ||
The above example illustrates a 2 repo git2consul setup: one repo lives in an on-premises Git solution and the other is hosted at github. The hooks array under each repository defines how git2consul will be notified of changes. git2consul supports [Atlassian Stash](https://confluence.atlassian.com/display/STASH/POST+service+webhook+for+Stash) and [GitHub](https://developer.github.com/v3/repos/hooks/) webhooks as well as a basic polling model. | ||
The above example illustrates a 2 repo git2consul setup: one repo lives in an on-premises Git solution and the other is hosted at github. The hooks array under each repository defines how git2consul will be notified of changes. git2consul supports [Atlassian Stash](https://confluence.atlassian.com/display/STASH/POST+service+webhook+for+Stash), [Atlassian Bitbucket](https://confluence.atlassian.com/display/BITBUCKET/POST+hook+management), [GitHub](https://developer.github.com/v3/repos/hooks/) webhooks as well as a basic polling model. | ||
@@ -66,0 +66,0 @@ Note that multiple webhooks can share the same port. The only constraint is that webhooks for different repos do not share the same port and path. |
@@ -64,30 +64,71 @@ var should = require('should'); | ||
// Test webhooks sharing a port | ||
[[{ | ||
'type': 'github', | ||
'url': '/githubpoke', | ||
'port': 5252, | ||
'body': { ref: "refs/heads/master", head_commit: {id: 12345} }, | ||
'fqurl': 'http://localhost:5252/githubpoke' | ||
},{ | ||
'type': 'stash', | ||
'url': '/stashpoke', | ||
'port': 5252, | ||
'body': { refChanges: [{refId: "refs/heads/master", toHash: "0"}]}, | ||
'fqurl': 'http://localhost:5252/stashpoke' | ||
}],[{ | ||
'type': 'github', | ||
'url': '/githubpoke_bogus_branch', | ||
'port': 5252, | ||
'body': { ref: "refs/heads/bogus_branch", head_commit: {id: 12345} }, | ||
'fqurl': 'http://localhost:5252/githubpoke_bogus_branch', | ||
'no_change_expected': true | ||
},{ | ||
'type': 'stash', | ||
'url': '/stashpoke_bogus_branch', | ||
'port': 5252, | ||
'body': { refChanges: [{refId: "refs/heads/bogus_branch", toHash: "0"}]}, | ||
'fqurl': 'http://localhost:5252/stashpoke_bogus_branch', | ||
'no_change_expected': true | ||
}]].forEach(function(hook_config) { | ||
[ | ||
// Test webhooks sharing a port | ||
[{ | ||
'type': 'github', | ||
'url': '/githubpoke', | ||
'port': 5252, | ||
'body': { ref: "refs/heads/master", head_commit: {id: 12345} }, | ||
'fqurl': 'http://localhost:5252/githubpoke' | ||
},{ | ||
'type': 'stash', | ||
'url': '/stashpoke', | ||
'port': 5252, | ||
'body': { refChanges: [{refId: "refs/heads/master", toHash: "0"}]}, | ||
'fqurl': 'http://localhost:5252/stashpoke' | ||
},{ | ||
'type': 'bitbucket', | ||
'url': '/bitbucketpoke', | ||
'port': 5252, | ||
// Seriously, their default POST hook is this ugly | ||
'body': '%7B%22repository%22%3A+%7B%22website%22%3A+null%2C+%22fork%22%3A+false%2C+%22name%22%3A+%22configuration%22%2C+%22scm%22%3A+%22git%22%2C+%22owner%22%3A+%22ryanbreen%22%2C+%22absolute_url%22%3A+%22%2Fryanbreen%2Fconfiguration%2F%22%2C+%22slug%22%3A+%22configuration%22%2C+%22is_private%22%3A+true%7D%2C+%22truncated%22%3A+false%2C+%22commits%22%3A+%5B%7B%22node%22%3A+%226f086f3d3de7%22%2C+%22files%22%3A+%5B%7B%22type%22%3A+%22modified%22%2C+%22file%22%3A+%22jobs_service%2Fauspice%2Fdemo-consumer-key%22%7D%5D%2C+%22raw_author%22%3A+%22Ryan+Breen+%3Crbreen%40vistaprint.com%3E%22%2C+%22utctimestamp%22%3A+%222014-10-18+23%3A20%3A47%2B00%3A00%22%2C+%22author%22%3A+%22Ryan+Breen%22%2C+%22timestamp%22%3A+%222014-10-19+01%3A20%3A47%22%2C+%22raw_node%22%3A+%226f086f3d3de724b9007934408e023b628a59ea15%22%2C+%22parents%22%3A+%5B%2230c88d68d029%22%5D%2C+%22branch%22%3A+%22master%22%2C+%22message%22%3A+%22Adding+bitbucket+test.%5Cn%22%2C+%22revision%22%3A+null%2C+%22size%22%3A+-1%7D%5D%2C+%22canon_url%22%3A+%22https%3A%2F%2Fbitbucket.org%22%2C+%22user%22%3A+%22ryanbreen%22%7D', | ||
'fqurl': 'http://localhost:5252/bitbucketpoke' | ||
}], | ||
// Test no-op changes to an incorrect branch | ||
[{ | ||
'type': 'github', | ||
'url': '/githubpoke_bogus_branch', | ||
'port': 5252, | ||
'body': { ref: "refs/heads/bogus_branch", head_commit: {id: 12345} }, | ||
'fqurl': 'http://localhost:5252/githubpoke_bogus_branch', | ||
'no_change_expected': true | ||
},{ | ||
'type': 'stash', | ||
'url': '/stashpoke_bogus_branch', | ||
'port': 5252, | ||
'body': { refChanges: [{refId: "refs/heads/bogus_branch", toHash: "0"}]}, | ||
'fqurl': 'http://localhost:5252/stashpoke_bogus_branch', | ||
'no_change_expected': true | ||
},{ | ||
'type': 'bitbucket', | ||
'url': '/bitbucket_bogus_branch', | ||
'port': 5252, | ||
'body': '%7B%22repository%22%3A+%7B%22website%22%3A+null%2C+%22fork%22%3A+false%2C+%22name%22%3A+%22configuration%22%2C+%22scm%22%3A+%22git%22%2C+%22owner%22%3A+%22ryanbreen%22%2C+%22absolute_url%22%3A+%22%2Fryanbreen%2Fconfiguration%2F%22%2C+%22slug%22%3A+%22configuration%22%2C+%22is_private%22%3A+true%7D%2C+%22truncated%22%3A+false%2C+%22commits%22%3A+%5B%7B%22node%22%3A+%226f086f3d3de7%22%2C+%22files%22%3A+%5B%7B%22type%22%3A+%22modified%22%2C+%22file%22%3A+%22jobs_service%2Fauspice%2Fdemo-consumer-key%22%7D%5D%2C+%22raw_author%22%3A+%22Ryan+Breen+%3Crbreen%40vistaprint.com%3E%22%2C+%22utctimestamp%22%3A+%222014-10-18+23%3A20%3A47%2B00%3A00%22%2C+%22author%22%3A+%22Ryan+Breen%22%2C+%22timestamp%22%3A+%222014-10-19+01%3A20%3A47%22%2C+%22raw_node%22%3A+%226f086f3d3de724b9007934408e023b628a59ea15%22%2C+%22parents%22%3A+%5B%2230c88d68d029%22%5D%2C+%22branch%22%3A+%22dev%22%2C+%22message%22%3A+%22Adding+bitbucket+test.%5Cn%22%2C+%22revision%22%3A+null%2C+%22size%22%3A+-1%7D%5D%2C+%22canon_url%22%3A+%22https%3A%2F%2Fbitbucket.org%22%2C+%22user%22%3A+%22ryanbreen%22%7D', | ||
'fqurl': 'http://localhost:5252/bitbucket_bogus_branch', | ||
'no_change_expected': true | ||
}], | ||
// Test no-op changes with non-HEAD refs | ||
[{ | ||
'type': 'github', | ||
'url': '/githubpoke_bogus_ref', | ||
'port': 5252, | ||
'body': { ref: "refs/remotes/origin/master", head_commit: {id: 12345} }, | ||
'fqurl': 'http://localhost:5252/githubpoke_bogus_branch', | ||
'no_change_expected': true | ||
},{ | ||
'type': 'stash', | ||
'url': '/stashpoke_bogus_ref', | ||
'port': 5252, | ||
'body': { refChanges: [{refId: "refs/remotes/origin/master", toHash: "0"}]}, | ||
'fqurl': 'http://localhost:5252/stashpoke_bogus_ref', | ||
'no_change_expected': true | ||
},{ | ||
'type': 'bitbucket', | ||
'url': '/bitbucket_bogus_ref', | ||
'port': 5252, | ||
'body': '', | ||
'fqurl': 'http://localhost:5252/bitbucket_bogus_ref', | ||
'no_change_expected': true | ||
}] | ||
].forEach(function(hook_config) { | ||
@@ -110,2 +151,4 @@ describe('webhook', function() { | ||
// This creates a function suitable as the predicate of a mocha test. The function will enclose | ||
// the config object and use it to send a request to the webhook and validate the response. | ||
var create_request_validator = function(config) { | ||
@@ -118,3 +161,8 @@ return function(done) { | ||
var req_conf = { url: config.fqurl, method: 'POST', json: config.body }; | ||
var req_conf = { url: config.fqurl, method: 'POST' }; | ||
if (config.type === 'bitbucket') { | ||
//req_conf.headers = {'content-type':'application/x-www-form-urlencoded'}; | ||
req_conf.form = { payload: decodeURIComponent(config.body).replace(/\+/g, ' ') }; | ||
} else req_conf.json = config.body | ||
if (config.type === 'stash') req_conf.headers = {'content-encoding':'UTF-8'}; | ||
@@ -121,0 +169,0 @@ request(req_conf, function(err) { |
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
79033
1718
21