Code coverage report for lib/cli/util.js

Statements: 100% (19 / 19)      Branches: 100% (10 / 10)      Functions: 100% (4 / 4)      Lines: 100% (19 / 19)      Ignored: none     

All files » lib/cli/ » util.js
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 701   1                                         1   27   27       21 21   6 6 30 48 5           27                         1         26   26 26     1 1
var _ = require('lodash');
 
const COLOR_STATUS = {  
  blue  : [ 'ok' ],
  green : [ 'ok', 'success' ],
  grey  : [ 'aborted' ],
  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) {
 
  var 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) {
 
  // 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$/, '');
 
  var status = (COLOR_STATUS[jenkinsColor]) ? COLOR_STATUS[jenkinsColor][0] : jenkinsColor;
  return status;
}
 
exports.colorByStatus = colorByStatus;
exports.statusByColor = statusByColor;