All files / lib/api util.js

100% Statements 110/110
100% Branches 13/13
100% Functions 8/8
100% Lines 110/110

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 1101x 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 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 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
"use strict";
 
/**
 * Handle success simply by passing the entire response through to callback
 *
 * @param {Object} result: result of the sent request as a string
 * @param {Function} cb: standard cb(err, result) callback
 */
function passThroughResponse(result, cb) {
  cb(null, result);
}
 
/**
 * Handle success simply by passing result's (response) body through to callback.
 *
 * @param {Object} result: result of the sent request as a string
 * @param {Function} cb: standard cb(err, result) callback
 */
function passThroughSuccess(result, cb) {
  cb(null, result.body);
}
 
/**
 * Handle success simply by passing result's (response) JSON string body parsed as an object through to callback.
 *
 * @param {Object} result: result of the sent request as object
 * @param {Function} cb: standard cb(err, result) callback
 */
function passThroughSuccessJson(result, cb) {
  cb(null, JSON.parse(result.body));
}
 
/**
 * Parse HTML error page from Jenkins, pass the error message to the callback.
 * This error is usually the response body of error 400.
 *
 * @param {Object} result: result of the sent request
 * @param {Function} cb: standard cb(err, result) callback
 */
function htmlError(result, cb) {
  const message = result.body
    .match(/<h1>Error<\/h1>.+<\/p>/).toString()
    .replace(/<h1>Error<\/h1>/, '')
    .replace(/<\/?p>/g, '');
  cb(new Error(message));
}
 
/**
 * Create a 'job not found' error handler function.
 *
 * @param {String} name: the job name
 * @return a handler function
 */
function jobNotFoundError(name) {
  return function (result, cb) {
    cb(new Error(`Job ${name} does not exist`));
  };
}
 
/**
 * Create a 'job or build not found' error handler function.
 *
 * @param {String} name: the job name
 * @param {Number} buildNumber: the job's build number
 * @return a handler function
 */
function jobBuildNotFoundError(name, buildNumber) {
  return function (result, cb) {
    cb(new Error(`Job ${name} build ${buildNumber} does not exist`));
  };
}
 
/**
 * Create a 'job require params' error handler function.
 *
 * @param {String} name: the job name
 * @return a handler function
 */
function jobRequireParamsError(name) {
  return function (result, cb) {
    cb(new Error(`Job ${name} requires build parameters`));
  };
}
 
/**
 * Create a 'view not found' error handler function.
 *
 * @param {String} name: the view name
 * @return a handler function
 */
function viewNotFoundError(name) {
  return function (result, cb) {
    cb(new Error(`View ${name} does not exist`));
  };
}
 
const exports = {
  passThroughResponse: passThroughResponse,
  passThroughSuccess: passThroughSuccess,
  passThroughSuccessJson: passThroughSuccessJson,
  htmlError: htmlError,
  jobNotFoundError: jobNotFoundError,
  jobBuildNotFoundError: jobBuildNotFoundError,
  jobRequireParamsError: jobRequireParamsError,
  viewNotFoundError: viewNotFoundError
};
 
export {
  exports as default
};