All files / lib/api jenkins.js

100% Statements 127/127
100% Branches 18/18
100% Functions 8/8
100% Lines 127/127

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 1271x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 4x 4x 4x 1x 1x 1x 1x 1x 4x 4x 4x 2x 1x 1x 1x 4x 4x 4x 1x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 1x 3x 1x 1x 1x 1x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x
"use strict";
import dgram from 'dgram';
import RssParser from 'rss-parser';
import xml2js from 'xml2js';
 
/**
 * Retrieve Jenkins instance computer information.
 *
 * @param {Function} cb: standard cb(err, result) callback
 */
function computer(cb) {
  const depth = 1;
  this.remoteAccessApi.getComputer(depth, cb);
}
 
/**
 * Retrieve Jenkins crumb, this is needed when sending POST requests to a Jenkins
 * instance with CSRF protection.
 *
 * @param {Function} cb: standard cb(err, result) callback
 */
function crumb(cb) {
  this.baseApi.getCrumb(cb);
}
 
/**
 * Discover whether there's a Jenkins instance running on the specified host.
 *
 * @param {String} host: hostname
 * @param {Function} cb: standard cb(err, result) callback
 */
function discover(host, cb) {
 
  const TIMEOUT = 5000;
 
  const socket = dgram.createSocket('udp4');
  const buffer = new Buffer('Long live Jenkins!');
  const parser = new xml2js.Parser();
 
  socket.on('error', function (err) {
    socket.close();
    cb(err);
  });
 
  socket.on('message', function (result) {
    socket.close();
    parser.addListener('end', function (result) {
      cb(null, result);
    });
    parser.parseString(result);
  });
 
  socket.send(buffer, 0, buffer.length, 33848, host, function (err, result) {
    if (err) {
      socket.close();
      cb(err);
    }
  });
 
  setTimeout(function () {
    cb(new Error(`Unable to find any Jenkins instance on ${host}`));
  }, TIMEOUT);
}
 
/**
 * Retrieve Jenkins instance information.
 *
 * @param {Function} cb: standard cb(err, result) callback
 */
function info(cb) {
  this.remoteAccessApi.getJenkins(cb);
}
 
/**
 * Parse Jenkins instance feed (all jobs).
 *
 * @param {Function} cb: standard cb(err, result) callback
 */
function parseFeed(cb) {
  const url = this.url + '/rssAll';
  new RssParser().parseURL(url, 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.remoteAccessApi.getQueue(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 _cb(err, result, response) {
    if (err) {
      cb(err);
    } else if (response.headers['x-jenkins']) {
      cb(null, response.headers['x-jenkins']);
    } else {
      cb(new Error('Not a Jenkins server'));
    }
  }
  this.remoteAccessApi.headJenkins(_cb);
}
 
const exports = {
  computer: computer,
  crumb: crumb,
  discover: discover,
  info: info,
  parseFeed: parseFeed,
  queue: queue,
  version: version
};
 
export {
  exports as default
};