Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Unanonymous handler functions.

  • Loading branch information...
commit 88506154986974096eb9f7dfdd400d0989828b4c 1 parent 9e3dbeb
@cliffano authored
Showing with 57 additions and 47 deletions.
  1. +57 −47 lib/jenkins.js
View
104 lib/jenkins.js
@@ -16,18 +16,20 @@ var _ = require('underscore'),
function Jenkins(url, opts) {
opts = opts || {};
+ function _authFail(result, cb) {
+ cb(new Error('Authentication failed - incorrect username and/or password in JENKINS_URL'));
+ }
+
+ function _authRequire(result, cb) {
+ cb(new Error('Jenkins requires authentication - set username and password in JENKINS_URL'));
+ }
+
this.url = url || process.env.JENKINS_URL || 'http://localhost:8080';
this.opts = {
proxy: opts.proxy || process.env.http_proxy,
handlers: {
- // authentication failed error handler
- 401: function (result, cb) {
- cb(new Error('Authentication failed - incorrect username and/or password in JENKINS_URL'));
- },
- // authentication required error handler
- 403: function (result, cb) {
- cb(new Error('Jenkins requires authentication - set username and password in JENKINS_URL'));
- }
+ 401: _authFail,
+ 403: _authRequire
}
};
}
@@ -53,20 +55,21 @@ Jenkins.prototype.build = function (jobName, params, cb) {
}
this.opts.queryStrings = { token: 'nestor', json: JSON.stringify(json) };
- // success handler
- this.opts.handlers['200'] = function (result, cb) {
+ function _success(result, cb) {
cb();
- };
+ }
- // not found error handler
- this.opts.handlers['404'] = function _notFound(result, cb) {
+ function _notFound(result, cb) {
cb(new Error('Job ' + jobName + ' does not exist'));
- };
+ }
- // parameters required error handler
- this.opts.handlers['405'] = function (result, cb) {
+ function _paramsRequire(result, cb) {
cb(new Error('Job ' + jobName + ' requires build parameters'));
- };
+ }
+
+ this.opts.handlers[200] = _success;
+ this.opts.handlers[404] = _notFound;
+ this.opts.handlers[405] = _paramsRequire;
bag.http.request(method, this.url + '/job/' + jobName + '/build', this.opts, cb);
};
@@ -93,8 +96,7 @@ Jenkins.prototype.console = function (jobName, opts, cb) {
this.opts.queryStrings = { start: 0 }; // the first chunk
- // success handler
- this.opts.handlers['200'] = function (result, cb) {
+ function _success(result, cb) {
if (result.body) {
console.log(result.body);
}
@@ -129,12 +131,14 @@ Jenkins.prototype.console = function (jobName, opts, cb) {
cb(err);
}
);
- };
+ }
- // not found error handler
- this.opts.handlers['404'] = function (result, cb) {
+ function _notFound(result, cb) {
cb(new Error('Job ' + jobName + ' does not exist'));
- };
+ }
+
+ this.opts.handlers[200] = _success;
+ this.opts.handlers[404] = _notFound;
bag.http.request('get', url, this.opts, cb);
};
@@ -147,15 +151,16 @@ Jenkins.prototype.console = function (jobName, opts, cb) {
*/
Jenkins.prototype.stop = function (jobName, cb) {
- // success handler
- this.opts.handlers['200'] = function (result, cb) {
+ function _success(result, cb) {
cb(null);
- };
+ }
- // not found error handler
- this.opts.handlers['404'] = function (result, cb) {
+ function _notFound(result, cb) {
cb(new Error('Job ' + jobName + ' does not exist'));
- };
+ }
+
+ this.opts.handlers[200] = _success;
+ this.opts.handlers[404] = _notFound;
bag.http.request('get', this.url + '/job/' + jobName + '/lastBuild/stop', this.opts, cb);
};
@@ -170,8 +175,7 @@ Jenkins.prototype.dashboard = function (cb) {
var self = this;
- // success handler
- this.opts.handlers['200'] = function (result, cb) {
+ function _success(result, cb) {
var jobs = JSON.parse(result.body).jobs,
data = [];
if (!_.isEmpty(jobs)) {
@@ -180,7 +184,9 @@ Jenkins.prototype.dashboard = function (cb) {
});
}
cb(null, data);
- };
+ }
+
+ this.opts.handlers[200] = _success;
bag.http.request('get', this.url + '/api/json', this.opts, cb);
};
@@ -227,8 +233,7 @@ Jenkins.prototype.executor = function (cb) {
this.opts.queryStrings = { depth: 1 };
- // success handler
- this.opts.handlers['200'] = function (result, cb) {
+ function _success(result, cb) {
var computers = JSON.parse(result.body).computer,
data = {};
computers.forEach(function (computer) {
@@ -245,7 +250,9 @@ Jenkins.prototype.executor = function (cb) {
});
});
cb(null, data);
- };
+ }
+
+ this.opts.handlers[200] = _success;
bag.http.request('get', this.url + '/computer/api/json', this.opts, cb);
};
@@ -261,8 +268,7 @@ Jenkins.prototype.job = function (name, cb) {
var self = this;
- // success handler
- this.opts.handlers['200'] = function (result, cb) {
+ function _success(result, cb) {
var _job = JSON.parse(result.body),
data = {};
data.status = self._status(_job.color);
@@ -271,12 +277,14 @@ Jenkins.prototype.job = function (name, cb) {
data.reports.push(report.description);
});
cb(null, data);
- };
+ }
- // not found error handler
- this.opts.handlers['404'] = function (result, cb) {
+ function _notFound(result, cb) {
cb(new Error('Job ' + name + ' does not exist'));
- };
+ }
+
+ this.opts.handlers[200] = _success;
+ this.opts.handlers[404] = _notFound;
bag.http.request('get', this.url + '/job/' + name + '/api/json', this.opts, cb);
};
@@ -289,8 +297,7 @@ Jenkins.prototype.job = function (name, cb) {
*/
Jenkins.prototype.queue = function (cb) {
- // success handler
- this.opts.handlers['200'] = function (result, cb) {
+ function _success(result, cb) {
var items = JSON.parse(result.body).items,
data = [];
if (!_.isEmpty(items)) {
@@ -299,7 +306,9 @@ Jenkins.prototype.queue = function (cb) {
});
}
cb(null, data);
- };
+ }
+
+ this.opts.handlers[200] = _success;
bag.http.request('get', this.url + '/queue/api/json', this.opts, cb);
};
@@ -312,14 +321,15 @@ Jenkins.prototype.queue = function (cb) {
*/
Jenkins.prototype.version = function (cb) {
- // success handler
- this.opts.handlers['200'] = function (result, cb) {
+ function _success(result, cb) {
if (result.headers['x-jenkins']) {
cb(null, result.headers['x-jenkins']);
} else {
cb(new Error('Not a Jenkins server'));
}
- };
+ }
+
+ this.opts.handlers[200] = _success;
bag.http.request('head', this.url, this.opts, cb);
};
Please sign in to comment.
Something went wrong with that request. Please try again.