All files / lib/cli jenkins.js

100% Statements 178/178
97.61% Branches 41/42
100% Functions 18/18
100% Lines 178/178

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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 1781x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 4x 4x 4x 1x 4x 3x 8x 8x 8x 3x 3x 4x 4x 4x 4x 4x 5x 5x 1x 1x 1x 1x 1x 1x 1x 4x 4x 3x 3x 3x 2x 2x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 4x 4x 1x 1x 1x 1x 1x 1x 1x 3x 3x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 3x 3x 4x 2x 2x 3x 1x 1x 1x 2x 2x 2x 2x 2x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 3x 3x 6x 3x 3x 3x 3x 1x 3x 1x 1x 1x 1x 3x 3x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 2x 2x 2x 1x 1x 1x 2x 1x 1x 2x 2x 2x 2x 2x 3x 3x 1x 1x 1x 1x 1x 1x 1x 2x 2x 1x 1x 1x 1x 1x 1x 1x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x
"use strict";
import _ from 'lodash';
import cli from 'bagofcli';
import Jenkins from '../jenkins.js';
import util from './util.js';
 
/**
 * Get a handler that calls Jenkins API to get a list of all jobs on the Jenkins instance.
 *
 * @param {Function} cb: callback for argument handling
 * @return Jenkins API handler function
 */
function dashboard(cb) {
  return function (command) {
    function resultCb(result) {
      const jobs = result.jobs;
      if (_.isEmpty(jobs)) {
        console.log('Jobless Jenkins');
      } else {
        jobs.forEach(function (job) {
          const status = util.statusByColor(job.color);
          const color  = util.colorByStatus(status, job.color);
          console.log(`${status[color]} - ${job.name}`);
        });
      }
    }
    function jenkinsCb(jenkins) {
      jenkins.info(cli.exitCb(null, resultCb));
    }
    cb(command, jenkinsCb);
  };
}
 
/**
 * Get a handler that calls Jenkins API to discover a Jenkins instance on specified host.
 *
 * @param {Function} cb: callback for argument handling
 * @return Jenkins API handler function
 */
function discover(cb) {
  return function (command, args) {
    let host = _.get(args, '[0]');
 
    if (!host) {
      host = 'localhost';
    }
 
    function resultCb(result) {
      const version = result.hudson.version[0];
      const host = result.hudson.url && result.hudson.url[0] ? result.hudson.url[0] : host;
      console.log(`Jenkins ver. ${version} is running on ${host}`);
    }
    function jenkinsCb(jenkins) {
      jenkins.discover(host, cli.exitCb(null, resultCb));
    }
    cb(command, jenkinsCb);
  };
}
 
/**
 * Get a handler that calls Jenkins API to display executors information per computer
 *
 * @param {Function} cb: callback for argument handling
 * @return Jenkins API handler function
 */
function executor(cb) {
  return function (command) {
    function resultCb(result) {
 
      const computers = result.computer;
 
      if (_.isEmpty(computers)) {
        console.log('No executor found');
      } else {
 
        const data = Jenkins.executorSummary(computers);
 
        _.keys(data).forEach(function (computer) {
          console.log('+ %s | %s', computer, data[computer].summary);
          data[computer].executors.forEach(function (executor) {
            if (!executor.idle) {
              console.log('  - %s | %s%%s', executor.name, executor.progress, (executor.stuck) ? ' stuck!' : '');
            }
          });
        });
 
      }
    }
    function jenkinsCb(jenkins) {
      jenkins.computer(cli.exitCb(null, resultCb));
    }
    cb(command, jenkinsCb);
  };
}
 
/**
 * Get a handler that calls Jenkins API to retrieve feed.
 * Feed contains articles.
 *
 * @param {Function} cb: callback for argument handling
 * @return Jenkins API handler function
 */
function feed(cb) {
  return function (command) {
    function resultCb(result) {
      result.items.forEach(function (article) {
        console.log(article.title);
      });
    }
    function jenkinsCb(jenkins) {
      if (command.job) {
        jenkins.parseJobFeed(command.job, cli.exitCb(null, resultCb));
      } else if (command.view) {
        jenkins.parseViewFeed(command.view, cli.exitCb(null, resultCb));
      } else {
        jenkins.parseFeed(cli.exitCb(null, resultCb));
      }
    }
    cb(command, jenkinsCb);
  };
}
 
/**
 * Get a handler that calls Jenkins API to read the queue.
 * Queue contains a list of jobs that are queued waiting for a free executor.
 *
 * @param {Function} cb: callback for argument handling
 * @return Jenkins API handler function
 */
function queue(cb) {
  return function (command) {
    function resultCb(result) {
      const items = result.items;
      if (_.isEmpty(items)) {
        console.log('Queue is empty');
      } else {
        items.forEach(function (item) {
            console.log('- %s', item.task.name);
        });
      }
    }
    function jenkinsCb(jenkins) {
      jenkins.queue(cli.exitCb(null, resultCb));
    }
    cb(command, jenkinsCb);
  };
}
 
/**
 * Get a handler that calls Jenkins API to read the Jenkins version.
 *
 * @param {Function} cb: callback for argument handling
 * @return Jenkins API handler function
 */
function version(cb) {
  return function (command) {
    function resultCb(result) {
      console.log(`Jenkins ver. ${result}`);
    }
    function jenkinsCb(jenkins) {
      jenkins.version(cli.exitCb(null, resultCb));
    }
    cb(command, jenkinsCb);
  };
}
 
const exports = {
  dashboard: dashboard,
  discover: discover,
  executor: executor,
  feed: feed,
  queue: queue,
  version: version
};
 
export {
  exports as default
};