Skip to content

Commit

Permalink
Unanonymous handler functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
cliffano committed Jan 23, 2013
1 parent 9e3dbeb commit 8850615
Showing 1 changed file with 57 additions and 47 deletions.
104 changes: 57 additions & 47 deletions lib/jenkins.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
};
}
Expand All @@ -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);
};
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
};
Expand All @@ -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);
};
Expand All @@ -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)) {
Expand All @@ -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);
};
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
};
Expand All @@ -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);
Expand All @@ -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);
};
Expand All @@ -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)) {
Expand All @@ -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);
};
Expand All @@ -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);
};
Expand Down

0 comments on commit 8850615

Please sign in to comment.