Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

test hooks work

  • Loading branch information...
commit 79c0144a51571d51a512270b71b7ddeea2070d9e 1 parent 2595782
@substack authored
Showing with 64 additions and 28 deletions.
  1. +64 −28 bin/cmd.js
View
92 bin/cmd.js
@@ -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)
});
});
-});
+}
Please sign in to comment.
Something went wrong with that request. Please try again.