Jenkins.prototype.dashboard = function (opts, cb) { // backward compatibility v0.1.9 and older if (!cb) { cb = opts; opts = {}; } var self = this, url = this.url + ((opts.viewName) ? '/view/' + opts.viewName: '') + '/api/json'; function _success(result, cb) { var jobs = JSON.parse(result.body).jobs, data = []; if (!_.isEmpty(jobs)) { jobs.forEach(function (job) { data.push({ status: self._statusByColor(job.color), name: job.name }); }); } cb(null, data); } this.opts.handlers[200] = _success; req.request('get', url, this.opts, cb); };
/** * Fetch a job configuration. * * @param {String} name: Jenkins job name * @param {Function} cb: standard cb(err, result) callback */ function fetchConfig(name, cb) { this.opts.handlers[200] = util.passThroughSuccess; this.opts.handlers[404] = util.jobNotFoundError(name); req.request('get', this.url + '/job/' + name + '/config.xml', this.opts, cb); }
/** * Delete a job. * * @param {String} name: Jenkins job name * @param {Function} cb: standard cb(err, result) callback */ function _delete(name, cb) { this.opts.handlers[200] = util.passThroughSuccess; this.opts.handlers[404] = util.jobNotFoundError(name); req.request('post', this.url + '/job/' + name + '/doDelete', this.opts, cb); }
/** * Retrieve information about the latest build of a job. * * @param {String} name: Jenkins job name * @param {Function} cb: standard cb(err, result) callback */ function readLatest(name, cb) { this.opts.handlers[200] = util.passThroughSuccessJson; this.opts.handlers[404] = util.jobNotFoundError(name); req.request('get', this.url + '/job/' + name + '/lastBuild/api/json', this.opts, cb); }
Jenkins.prototype.getBuild = function(name, number, cb) { function _success(result, cb) { var _build = JSON.parse(result.body), buildMoment; if (_build.building > 0) { buildMoment = moment(_build.timestamp); _build.buildDateDistance = "Started " + buildMoment.fromNow(); } else { buildMoment = moment(_build.timestamp + _build.duration); _build.buildDateDistance = "Ended " + buildMoment.fromNow(); } _build.buildDate = buildMoment.toISOString(); cb(null, _build); } function _notFound(result, cb) { cb(new Error(text.__('No build could be found for job %s', name))); } this.opts.handlers[200] = _success; this.opts.handlers[404] = _notFound; req.request('get', this._buildUrl(name, number) + '/api/json', this.opts, cb); };
/** * Stop a running/building job. * * @param {String} name: Jenkins job name * @param {Function} cb: standard cb(err, result) callback */ function stop(name, cb) { this.opts.handlers[200] = util.passThroughSuccess; this.opts.handlers[404] = util.jobNotFoundError(name); req.request('post', this.url + '/job/' + name + '/lastBuild/stop', this.opts, cb); }
Jenkins.prototype.build = function (jobName, params, cb) { var json = { parameter: [] }, method = 'post'; if (params) { params.split('&').forEach(function (param) { var keyVal = param.split('='); json.parameter.push({ name: keyVal[0], value: keyVal[1] }); }); } this.opts.queryStrings = { token: 'nestor', json: JSON.stringify(json) }; function _success(result, cb) { cb(); } function _notFound(result, cb) { cb(new Error(text.__('Job %s does not exist', jobName))); } function _paramsRequire(result, cb) { cb(new Error(text.__('Job %s requires build parameters', jobName))); } this.opts.handlers[200] = _success; // backward compatibility (< v1.5xx) this.opts.handlers[201] = _success; this.opts.handlers[404] = _notFound; this.opts.handlers[405] = _paramsRequire; // backward compatibility (< v1.5xx) req.request(method, this._jobUrl(jobName) + '/build', this.opts, cb); };
/** * Update a job with specified configuration * * @param {String} name: Jenkins job name * @param {String} config: Jenkins job config.xml * @param {Function} cb: standard cb(err, result) callback */ function update(name, config, cb) { this.opts.body = config; this.opts.handlers[200] = util.passThroughSuccess; this.opts.handlers[404] = util.jobNotFoundError(name); req.request('post', this.url + '/job/' + name + '/config.xml', this.opts, cb); }
/** * Copy an existing job into a new job. * * @param {String} existingName: existing Jenkins job name to copy from * @param {String} newName: new Jenkins job name to copy to * @param {Function} cb: standard cb(err, result) callback */ function copy(existingName, newName, cb) { this.opts.queryStrings = { name: newName, mode: 'copy', from: existingName }; this.opts.headers = { 'content-type': 'text/plain' }; this.opts.handlers[200] = util.passThroughSuccess; this.opts.handlers[400] = util.htmlError; req.request('post', this.url + '/createItem', this.opts, cb); }
/** * Create a job with specified configuration. * * @param {String} name: Jenkins job name * @param {String} config: Jenkins job config.xml * @param {Function} cb: standard cb(err, result) callback */ function create(name, config, cb) { this.opts.queryStrings = { name: name }; this.opts.headers = { 'content-type': 'application/xml' }; this.opts.body = config; this.opts.handlers[200] = util.passThroughSuccess; this.opts.handlers[400] = util.htmlError; req.request('post', this.url + '/createItem/api/json', this.opts, cb); }
/** * Retrieve Jenkins version number from x-jenkins header. * If x-jenkins header does not exist, then it's assumed that the server is not a Jenkins instance. * * @param {Function} cb: standard cb(err, result) callback */ function version(cb) { function _success(result, cb) { if (result.headers['x-jenkins']) { cb(null, result.headers['x-jenkins']); } else { cb(new Error(text.__('Not a Jenkins server'))); } } this.opts.handlers[200] = _success; req.request('head', this.url, this.opts, cb); }
Jenkins.prototype.stop = function (jobName, cb) { function _success(result, cb) { cb(null); } function _notFound(result, cb) { cb(new Error(text.__('Job %s does not exist', jobName))); } this.opts.handlers[200] = _success; this.opts.handlers[404] = _notFound; req.request('post', this._buildUrl(jobName, 'lastBuild') + '/stop', this.opts, cb); };
/** * Build a job. * * @param {String} name: Jenkins job name * @param {Object} params: build parameters key-value pairs * @param {Function} cb: standard cb(err, result) callback */ function build(name, params, cb) { var body = { parameter: [] } ; _.keys(params).forEach(function (key) { body.parameter.push({ name: key, value: params[key] }); }); this.opts.queryStrings = { token: 'nestor', json: JSON.stringify(body) }; this.opts.handlers[200] = util.passThroughSuccess; // backward compatibility for old Jenkins versions this.opts.handlers[201] = util.passThroughSuccess; this.opts.handlers[404] = util.jobNotFoundError(name); this.opts.handlers[405] = util.jobRequireParamsError(name); // backward compatibility for old Jenkins versions req.request('post', this.url + '/job/' + name + '/build', this.opts, cb); }
Jenkins.prototype.queue = function (cb) { function _success(result, cb) { var items = JSON.parse(result.body).items, data = []; if (!_.isEmpty(items)) { items.forEach(function (item) { data.push(item.task.name); }); } cb(null, data); } this.opts.handlers[200] = _success; req.request('get', this.url + '/queue/api/json', this.opts, cb); };
Jenkins.prototype.executor = function (cb) { this.opts.queryStrings = { depth: 1 }; function _success(result, cb) { var computers = JSON.parse(result.body).computer, data = {}; computers.forEach(function (computer) { data[computer.displayName] = { executors: [] }; var idleCount = 0, activeCount = 0; computer.executors.forEach(function (executor) { data[computer.displayName].executors.push({ idle: executor.idle, stuck: executor.likelyStuck, progress: executor.progress, name: (!executor.idle) ? executor.currentExecutable.url.replace(/.*\/job\//, '').replace(/\/.*/, '') : undefined }); if (executor.idle) { idleCount += 1; } else { activeCount += 1; } }); var summary = []; if (activeCount > 0) { summary.push(text.__('%d active', activeCount)); } if (idleCount > 0) { summary.push(text.__('%d idle', idleCount)); } data[computer.displayName].summary = summary.join(', '); }); cb(null, data); } this.opts.handlers[200] = _success; req.request('get', this.url + '/computer/api/json', this.opts, cb); };
function (cb) { var params = { url: url, queryStrings: { start: parseInt(result.headers['x-text-size'], 10) }, agentOptions : self.opts.agentOptions, handlers: {} }; var envProxy = req.proxy(url); if (envProxy) { params.proxy = envProxy; } function innerSuccess (_result,cb) { result = _result; if (_result.body) { stream.emit('data', _result.body); } setTimeout(cb, interval); } params.handlers[200] = innerSuccess; req.request('get', url, params, cb); },
Jenkins.prototype.job = function (name, cb) { var self = this; function _success(result, cb) { var _job = JSON.parse(result.body), data = {}; data.status = self._statusByColor(_job.color); data.reports = []; _job.healthReport.forEach(function (report) { data.reports.push(report.description); }); cb(null, data); } function _notFound(result, cb) { cb(new Error(text.__('Job %s does not exist', name))); } this.opts.handlers[200] = _success; this.opts.handlers[404] = _notFound; req.request('get', this._jobUrl(name) + '/api/json', this.opts, cb); };
/** * Retrieve a list of jobs in the queue waiting for available * executor or for a previously running build of the same job * to finish. * * @param {Function} cb: standard cb(err, result) callback */ function queue(cb) { this.opts.handlers[200] = util.passThroughSuccess; req.request('get', this.url + '/queue/api/json', this.opts, cb); }
process.nextTick(function () { req.request('get', url, self.opts, cb); });
/** * Retrieve information about a view. * * @param {String} name: Jenkins view name * @param {Function} cb: standard cb(err, result) callback */ function read(name, cb) { this.opts.handlers[200] = util.passThroughSuccessJson; req.request('get', this.url + '/view/' + name + '/api/json', this.opts, cb); }