Skip to content

Commit

Permalink
test hooks work
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halliday committed Apr 10, 2012
1 parent 2595782 commit 79c0144
Showing 1 changed file with 64 additions and 28 deletions.
92 changes: 64 additions & 28 deletions bin/cmd.js
Expand Up @@ -25,38 +25,74 @@ withConfig(function (config) {
};

remote(function (err, repo) {
if (err) return console.error(err);
if (err) console.error(err)
else if (process.argv[2] === 'test') {
testHook(config, repo);
}
else addHook(config, repo)
});
});

function hookUri (config, repo) {
return 'https://'
+ [ config.user, config.pass ].map(encodeURIComponent).join(':')
+ '@api.github.com/repos/' + repo + '/hooks'
;
}

function getHook (uri, cb) {
request.get({ uri : uri, json : true }, function (err, res, body) {
if (err) return cb(err);
if (res.statusCode !== 200) return cb(body);
if (!Array.isArray(body)) {
return cb('non-array response: ' + JSON.stringify(body));
}

var uri = 'https://'
+ [ config.user, config.pass ].map(encodeURIComponent).join(':')
+ '@api.github.com/repos/' + repo + '/hooks'
;
cb(null, body.filter(function (rec) {
return rec && rec.name === 'travis'
})[0]);
});
}

function testHook (config, repo) {
var uri = hookUri(config, repo);
getHook(uri, function (err, hook) {
if (err) return console.error(err);
if (!hook) return console.error('no hook for this project');

request.get({ uri : uri, json : true }, function (err, res, body) {
if (err) return console.error(err);
if (res.statusCode !== 200) return console.error(body);
if (!Array.isArray(body)) {
console.error('non-array response: ');
var opts = {
uri : uri + '/' + hook.id + '/test',
body : '',
};
request.post(opts, function (err, res, body) {
if (err) console.error(err)
else if (!res.statusCode.toString().match(/^2/)) {
console.error('response code ' + res.statusCode);
console.error(body);
return;
}

var hasTravis = body.some(function (rec) {
return rec && rec.name === 'travis'
});
if (hasTravis) {
return console.log('this repo already has a travis hook');
else console.log('test hook sent for ' + repo + '/' + hook.id)
});
});
}

function addHook (config, repo) {
var uri = hookUri(config, repo);
getHook(uri, function (err, hook) {
if (err) return console.error(err);
if (hook) return console.log('this repo already has a travis hook');

var opts = {
uri : uri,
body : JSON.stringify(doc),
};
request.post(opts, function (err, res, body) {
if (err) console.error(err);
else if (res.statusCode !== 200) console.log(body)
else if (body.id) {
console.log('travis hook added for ' + repo
+ ' with id ' + body.id);
}

var opts = {
uri : uri,
body : JSON.stringify(doc),
};
request.post(opts, function (err, res, body) {
if (err) console.error(err);
else if (res.statusCode !== 200) console.log(body)
else console.log('travis hook added for ' + repo)
});
else console.log(body)
});
});
});
}

0 comments on commit 79c0144

Please sign in to comment.