All files / lib/cli util.js

100% Statements 84/84
100% Branches 15/15
100% Functions 2/2
100% Lines 84/84

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 851x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 28x 28x 28x 28x 28x 21x 21x 21x 21x 21x 28x 7x 7x 35x 63x 5x 5x 35x 7x 7x 28x 28x 28x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 27x 27x 27x 27x 26x 26x 26x 26x 26x 26x 26x 27x 1x 1x 27x 27x 27x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
"use strict";
import _ from 'lodash';
 
const COLOR_STATUS = {
  blue  : [ 'ok' ],
  green : [ 'ok', 'success' ],
  grey  : [ 'aborted', 'unknown' ],
  red   : [ 'fail', 'failure' ],
  yellow: [ 'warn', 'building' ]
};
 
/**
 * Get color based on status and Jenkins status color.
 * Jenkins status color will take precedence over status because
 * Jenkins uses either blue or green as the color for ok status.
 *
 * Color property can contain either color or status text.
 * Hence the need for mapping color to status text in the case where value is really color.
 * This is used in conjunction with statusByColor function.
 *
 * @param {String} status: Jenkins status
 * @param {String} jenkinsColor: Jenkins status color
 * @return the status text
 */
function colorByStatus(status, jenkinsColor) {
 
  let color;
 
  if (jenkinsColor) {
    // Jenkins color value can contain either a color, color_anime, or status in job.color field,
    // hence to get color/status value out of the mix we need to remove the postfix _anime,
    // _anime postfix only exists on a job currently being built
    jenkinsColor = jenkinsColor.replace(/_anime$/, '');
    color = (COLOR_STATUS[jenkinsColor] && COLOR_STATUS[jenkinsColor][0]) ? jenkinsColor : 'grey';
  } else {
    color = 'grey';
    _.keys(COLOR_STATUS).forEach(function (_color) {
      COLOR_STATUS[_color].forEach(function (_status) {
        if (_status === status) {
          color = _color;
        }
      });
    });
  }
 
  return color;
}
 
/**
 * Get status based on the value of color property.
 *
 * Color property can contain either color or status text.
 * Hence the need for mapping color to status text in the case where value is really color.
 * This is used in conjunction with colorByStatus function.
 *
 * @param {String} jenkinsColor: Jenkins status color
 * @return the status text
 */
function statusByColor(jenkinsColor) {
 
  let status;
  if (jenkinsColor) {
 
    // Jenkins color value can contain either a color, color_anime, or status in job.color field,
    // hence to get color/status value out of the mix we need to remove the postfix _anime,
    // _anime postfix only exists on a job currently being built
    jenkinsColor = jenkinsColor.replace(/_anime$/, '');
 
    status = (COLOR_STATUS[jenkinsColor]) ? COLOR_STATUS[jenkinsColor][0] : jenkinsColor;
  } else {
    status = 'unknown';
  }
 
  return status;
}
 
const exports = {
  colorByStatus: colorByStatus,
  statusByColor: statusByColor
};
 
export {
  exports as default
};