lib/jenkins.js
function monitor(opts, cb) {
  var self = this;

  function singleJobResultCb(err, result) {
    if (!err) {
      result = util.statusByColor(result.color);
    }
    cb(err, result);
  }

  // when there are multiple jobs, status is derived following these rules:
  // - fail if any job has Jenkins color red
  // - warn if any job has Jenkins color yellow but no red
  // - non-success (e.g. notbuilt) if any job has Jenkins color status but no red and yellow
  // - success only if all jobs are either blue or green
  function multiJobsResultCb(err, result) {
    if (!err) {

      var hasRed        = false;
      var hasYellow     = false;
      var hasNonSuccess = false;
      var hasSuccess    = false;

      var successColor;
      var nonSuccessColor;

      result.jobs.forEach(function (job) {
        if (job.color === 'red') {
          hasRed = true;
        }
        if (job.color === 'yellow') {
          hasYellow = true;
        }
        if (['red', 'yellow', 'blue', 'green'].indexOf(job.color) === -1) {
          hasNonSuccess   = true;
          nonSuccessColor = job.color;
        }
        if (job.color === 'blue' || job.color === 'green') {
          hasSuccess   = true;
          successColor = job.color;
        }
      });

      var resultColor;
      if (hasRed) {
        resultColor = 'red';
      } else if (hasYellow) {
        resultColor = 'yellow';
      } else if (hasNonSuccess) {
        resultColor = nonSuccessColor;
      } else {
        resultColor = successColor;
      }

      result = util.statusByColor(resultColor);
    }
    cb(err, result);
  }

  function _notify() {
    if (opts.job) {
      self.readJob(opts.job, singleJobResultCb);
    } else if (opts.view) {
      self.readView(opts.view, multiJobsResultCb);
    } else {
      self.info(multiJobsResultCb);
    }
  }

  _notify();
  new cron.CronJob(opts.schedule || '0 * * * * *', _notify).start();
}
function monitor(opts, cb) {
  var self = this;

  function singleJobResultCb(err, result) {
    if (!err) {
      result = util.statusByColor(result.color);
    }
    cb(err, result);
  }

  // when there are multiple jobs, status is derived following these rules:
  // - fail if any job has Jenkins color red
  // - warn if any job has Jenkins color yellow but no red
  // - non-success (e.g. notbuilt) if any job has Jenkins color status but no red and yellow
  // - success only if all jobs are either blue or green
  function multiJobsResultCb(err, result) {
    if (!err) {

      var hasRed        = false;
      var hasYellow     = false;
      var hasNonSuccess = false;
      var hasSuccess    = false;

      var successColor;
      var nonSuccessColor;

      result.jobs.forEach(function (job) {
        if (job.color === 'red') {
          hasRed = true;
        }
        if (job.color === 'yellow') {
          hasYellow = true;
        }
        if (['red', 'yellow', 'blue', 'green'].indexOf(job.color) === -1) {
          hasNonSuccess   = true;
          nonSuccessColor = job.color;
        }
        if (job.color === 'blue' || job.color === 'green') {
          hasSuccess   = true;
          successColor = job.color;
        }
      });

      var resultColor;
      if (hasRed) {
        resultColor = 'red';
      } else if (hasYellow) {
        resultColor = 'yellow';
      } else if (hasNonSuccess) {
        resultColor = nonSuccessColor;
      } else {
        resultColor = successColor;
      }

      result = util.statusByColor(resultColor);
    }
    cb(err, result);
  }

  function _notify() {
    if (opts.job) {
      self.readJob(opts.job, singleJobResultCb);
    } else if (opts.view) {
      self.readView(opts.view, multiJobsResultCb);
    } else {
      self.info(multiJobsResultCb);
    }
  }

  _notify();
  new cron.CronJob(opts.schedule || '0 * * * * *', _notify).start();
}
function monitor ( opts , cb ) { var self = this ; function singleJobResultCb ( err , result ) { if ( ! err ) { result = util . statusByColor ( result . color ) ; } cb ( err , result ) ; } // when there are multiple jobs, status is derived following these rules: // - fail if any job has Jenkins color red // - warn if any job has Jenkins color yellow but no red // - non-success (e.g. notbuilt) if any job has Jenkins color status but no red and yellow // - success only if all jobs are either blue or green function multiJobsResultCb ( err , result ) { if ( ! err ) { var hasRed = false ; var hasYellow = false ; var hasNonSuccess = false ; var hasSuccess = false ; var successColor ; var nonSuccessColor ; result . jobs . forEach ( function ( job ) { if ( job . color === 'red' ) { hasRed = true ; } if ( job . color === 'yellow' ) { hasYellow = true ; } if ( [ 'red' , 'yellow' , 'blue' , 'green' ] . indexOf ( job . color ) === - 1 ) { hasNonSuccess = true ; nonSuccessColor = job . color ; } if ( job . color === 'blue' || job . color === 'green' ) { hasSuccess = true ; successColor = job . color ; } } ) ; var resultColor ; if ( hasRed ) { resultColor = 'red' ; } else if ( hasYellow ) { resultColor = 'yellow' ; } else if ( hasNonSuccess ) { resultColor = nonSuccessColor ; } else { resultColor = successColor ; } result = util . statusByColor ( resultColor ) ; } cb ( err , result ) ; } function _notify ( ) { if ( opts . job ) { self . readJob ( opts . job , singleJobResultCb ) ; } else if ( opts . view ) { self . readView ( opts . view , multiJobsResultCb ) ; } else { self . info ( multiJobsResultCb ) ; } } _notify ( ) ; new cron . CronJob ( opts . schedule || '0 * * * * *' , _notify ) . start ( ) ; }
Monitor Jenkins latest build status on a set interval.

@param {Object} opts: optional
- job: Jenkins job name
- view: Jenkins view name
- schedule: cron scheduling definition in standard * * * * * * format, default: 0 * * * * * (every minute)
@param {Function} cb: standard cb(err, result) callback
Monitor Jenkins latest build status on a set interval .