import async from 'async';
import ConsoleStream from './consolestream.js';
import RssParser from 'rss-parser';
function create(name, config, cb) {
contentType: 'application/xml',
this.remoteAccessApi.postCreateItem(name, _.merge(opts, this.opts.headers), cb);
function read(name, cb) {
this.remoteAccessApi.getJob(name, cb);
function readLatest(name, cb) {
this.remoteAccessApi.getJobLastBuild(name, cb);
function update(name, config, cb) {
this.remoteAccessApi.postJobConfig(name, config, this.opts.headers, cb);
function _delete(name, cb) {
this.remoteAccessApi.postJobDelete(name, this.opts.headers, cb);
function build(name, params, cb) {
const body = { parameter: [] } ;
_.keys(params).forEach(function (key) {
body.parameter.push({ name: key, value: params[key] });
this.remoteAccessApi.postJobBuild(name, JSON.stringify(body), _.merge(opts, this.opts.headers), cb);
function checkStarted(buildUrl, cb) {
function _cb(err, result) {
if (result && result._class === 'hudson.model.Queue$LeftItem') {
function parseQueueItemNumber(buildUrl) {
const elems = buildUrl.split('/');
return elems[elems.length - 2];
this.remoteAccessApi.getQueueItem(parseQueueItemNumber(buildUrl), _cb);
function stop(name, cb) {
this.remoteAccessApi.postJobLastBuildStop(name, this.opts.headers, cb);
function streamConsole(name, buildNumber, interval, cb) {
buildNumber = buildNumber || 'lastBuild';
const stream = new ConsoleStream();
function _processMoreChunks(result, response, cb) {
stream.emit('data', response.text);
cb(null, response.headers['x-more-data'] === 'true');
function _moreChunkCb(err, _result, _response) {
stream.emit('data', _response.text);
setTimeout(cb, interval);
const start = parseInt(response.headers['x-text-size'], 10);
self.remoteAccessApi.getJobProgressiveText(name, buildNumber, start, _moreChunkCb);
process.nextTick(function () {
function _firstChunkCb(err, result, response) {
if (!err && response.statusCode === 200) {
_processMoreChunks(result, response, cb);
self.remoteAccessApi.getJobProgressiveText(name, buildNumber, start, _firstChunkCb);
function enable(name, cb) {
this.remoteAccessApi.postJobEnable(name, this.opts.headers, cb);
function disable(name, cb) {
this.remoteAccessApi.postJobDisable(name, this.opts.headers, cb);
function copy(existingName, newName, cb) {
contentType: 'text/plain',
this.remoteAccessApi.postCreateItem(newName, _.merge(opts, this.opts.headers), cb);
function fetchConfig(name, cb) {
this.remoteAccessApi.getJobConfig(name, cb);
function parseFeed(name, cb) {
const url = this.url + '/job/' + name + '/rssAll';
new RssParser().parseURL(url, cb);
checkStarted: checkStarted,
streamConsole: streamConsole,
fetchConfig: fetchConfig,