diff --git a/lib/main.js b/lib/main.js
index a8455b0..0404f4c 100644
--- a/lib/main.js
+++ b/lib/main.js
@@ -285,6 +285,34 @@ exports.init = function (host, defaultOptions, defaultParams) {
return url;
}
+ var getCrumb = function getCrumb(requestOptions, callback) {
+ var urlPattern = ['/crumbIssuer/api/json'];
+ var url = formatUrl.apply(null, urlPattern);
+
+ if (!requestOptions.method === 'POST' && !requestOptions.method === 'DELETE') {
+ return callback();
+ }
+
+ var getCrumbOptions = Object.assign({
+ method: 'GET',
+ url: url,
+ headers: [],
+ followAllRedirects: true
+ }, { headers: { 'Content-Type': 'application/json' } });
+
+ request(getCrumbOptions, function (error, response, body) {
+ if (error || response.statusCode !== HTTP_CODE_200 || response.should !== HTTP_CODE_201 || response.should !== HTTP_CODE_302) {
+ return callback(null);
+ }
+
+ var getCrumbResponse = JSON.parse(body);
+ var crumb = {};
+
+ crumb[getCrumbResponse.crumbRequestField] = getCrumbResponse.crumb;
+ callback(null, crumb);
+ });
+ };
+
/**
* Run the actual HTTP request.
*
@@ -318,38 +346,51 @@ exports.init = function (host, defaultOptions, defaultParams) {
body: null
}, options.request);
- // Do the request
- request(requestOptions, function (error, response, body) {
- if (error) {
- callback(error, response);
+ // Get Crumb if POST ? DELETE?
+ getCrumb(requestOptions, function (err, crumb) {
+
+ if (err) {
+ callback(err);
return;
}
- if (Array.isArray(options.successStatusCodes) && !options.successStatusCodes.includes(response.statusCode) || Array.isArray(options.failureStatusCodes) && options.failureStatusCodes.includes(response.statusCode)) {
- callback('Server returned unexpected status code: ' + response.statusCode, response);
- return;
+ // Do the request
+ if (crumb) {
+ Object.assign(requestOptions.headers, crumb);
}
- if (options.noparse) {
- // Wrap body in the response object
- if (typeof body === 'string') {
- body = { body: body };
+ request(requestOptions, function (error, response, body) {
+ if (error) {
+ callback(error, response);
+ return;
}
- // Add location
- var location = response.headers.Location || response.headers.location;
-
- if (location) {
- body.location = location;
+ if (Array.isArray(options.successStatusCodes) && !options.successStatusCodes.includes(response.statusCode) || Array.isArray(options.failureStatusCodes) && options.failureStatusCodes.includes(response.statusCode)) {
+ callback('Server returned unexpected status code: ' + response.statusCode, response);
+ return;
}
- // Add status code
- body.statusCode = response.statusCode;
+ if (options.noparse) {
+ // Wrap body in the response object
+ if (typeof body === 'string') {
+ body = { body: body };
+ }
- callback(null, body);
- } else {
- tryParseJson(body, options.bodyProp, callback);
- }
+ // Add location
+ var location = response.headers.Location || response.headers.location;
+
+ if (location) {
+ body.location = location;
+ }
+
+ // Add status code
+ body.statusCode = response.statusCode;
+
+ callback(null, body);
+ } else {
+ tryParseJson(body, options.bodyProp, callback);
+ }
+ });
});
};
@@ -363,6 +404,10 @@ exports.init = function (host, defaultOptions, defaultParams) {
*
* Return queue location of newly-created job as per
* https://issues.jenkins-ci.org/browse/JENKINS-12827?focusedCommentId=201381#comment-201381
+ *
+ * @param {string} jobName
+ * @param {object|undefined} customParams is optional
+ * @param {function} callback
*/
build: function build(jobName, customParams, callback) {
var _doArgs3 = doArgs(arguments, ['string', ['object', {}], 'function']);
@@ -394,7 +439,12 @@ exports.init = function (host, defaultOptions, defaultParams) {
});
},
- /** */
+ /**
+ *
+ * @param {string} jobName
+ * @param {object|undefined} customParams is optional
+ * @param {function} callback
+ */
build_with_params: function build_with_params(jobName, customParams, callback) {
var _doArgs5 = doArgs(arguments, ['string', ['object', {}], 'function']);
@@ -425,7 +475,13 @@ exports.init = function (host, defaultOptions, defaultParams) {
});
},
- /** */
+ /**
+ *
+ * @param {string} jobName
+ * @param {string} buildNumber
+ * @param {object|undefined} customParams is optional
+ * @param {function} callback
+ */
stop_build: function stop_build(jobName, buildNumber, customParams, callback) {
var _doArgs7 = doArgs(arguments, ['string', 'string|number', ['object', {}], 'function']);
@@ -453,7 +509,14 @@ exports.init = function (host, defaultOptions, defaultParams) {
});
},
- /** Get the output for a job's build */
+ /**
+ * Get the output for a job's build
+ *
+ * @param {string} jobName
+ * @param {string} buildNumber
+ * @param {object|undefined} customParams is optional
+ * @param {function} callback
+ */
console_output: function console_output(jobName, buildNumber, customParams, callback) {
var _doArgs9 = doArgs(arguments, ['string', 'string|number', ['object', {}], 'function']);
@@ -471,7 +534,13 @@ exports.init = function (host, defaultOptions, defaultParams) {
}, customParams, callback);
},
- /** Get information for the last build of a job */
+ /**
+ * Get information for the last build of a job
+ *
+ * @param {string} jobName
+ * @param {object|undefined} customParams is optional
+ * @param {function} callback
+ */
last_build_info: function last_build_info(jobName, customParams, callback) {
var _doArgs11 = doArgs(arguments, ['string', ['object', {}], 'function']);
@@ -487,7 +556,13 @@ exports.init = function (host, defaultOptions, defaultParams) {
}, customParams, callback);
},
- /** Get information for the last completed build of a job */
+ /**
+ * Get information for the last completed build of a job
+ *
+ * @param {string} jobName
+ * @param {object|undefined} customParams is optional
+ * @param {function} callback
+ */
last_completed_build_info: function last_completed_build_info(jobName, customParams, callback) {
var _doArgs13 = doArgs(arguments, ['string', ['object', {}], 'function']);
@@ -503,7 +578,14 @@ exports.init = function (host, defaultOptions, defaultParams) {
}, customParams, callback);
},
- /** Get information for the build number of a job */
+ /**
+ * Get information for the build number of a job
+ *
+ * @param {string} jobName
+ * @param {string} buildNumber
+ * @param {object|undefined} customParams is optional
+ * @param {function} callback
+ */
build_info: function build_info(jobName, buildNumber, customParams, callback) {
var _doArgs15 = doArgs(arguments, ['string', 'string|number', ['object', {}], 'function']);
@@ -520,7 +602,14 @@ exports.init = function (host, defaultOptions, defaultParams) {
}, customParams, callback);
},
- /** Get information for the all builds */
+ /**
+ * Get information for the all builds
+ *
+ * @param {string} jobName
+ * @param {string} param
+ * @param {object|undefined} customParams is optional
+ * @param {function} callback
+ */
all_builds: function all_builds(jobName, param, customParams, callback) {
// TODO better name and handle the "param" ???
@@ -538,7 +627,14 @@ exports.init = function (host, defaultOptions, defaultParams) {
}, customParams, callback);
},
- /** Get the test results for the build number of a job */
+ /**
+ * Get the test results for the build number of a job
+ *
+ * @param {string} jobName
+ * @param {string} buildNumber
+ * @param {object|undefined} customParams is optional
+ * @param {function} callback
+ */
test_result: function test_result(jobName, buildNumber, customParams, callback) {
var _doArgs19 = doArgs(arguments, ['string', 'string|number', ['object', {}], 'function']);
@@ -555,9 +651,15 @@ exports.init = function (host, defaultOptions, defaultParams) {
}, customParams, callback);
},
- /** Get the last build report for a job.
+ /**
+ * Get the last build report for a job.
* @obsolete Use last_build_info instead.
- * Probly will make this to return the test result. */
+ * Probly will make this to return the test result.
+ *
+ * @param {string} jobName
+ * @param {object|undefined} customParams is optional
+ * @param {function} callback
+ */
last_build_report: function last_build_report(jobName, customParams, callback) {
this.last_build_info(jobName, customParams, callback);
// doRequest({
@@ -565,7 +667,14 @@ exports.init = function (host, defaultOptions, defaultParams) {
// }, customParams, callback);
},
- /** Deletes build data for certain job */
+ /**
+ * Deletes build data for certain job
+ *
+ * @param {string} jobName
+ * @param {string} buildNumber
+ * @param {object|undefined} customParams is optional
+ * @param {function} callback
+ */
delete_build: function delete_build(jobName, buildNumber, customParams, callback) {
var _doArgs21 = doArgs(arguments, ['string', 'string|number', ['object', {}], 'function']);
@@ -595,7 +704,12 @@ exports.init = function (host, defaultOptions, defaultParams) {
|* Jobs *|
\*************************************/
- /** Return a list of object literals containing the name and color of all jobs on the Jenkins server */
+ /**
+ * Return a list of object literals containing the name and color of all jobs on the Jenkins server
+ *
+ * @param {object|undefined} customParams is optional
+ * @param {function} callback
+ */
all_jobs: function all_jobs(customParams, callback) {
var _doArgs23 = doArgs(arguments, [['object', {}], 'function']);
@@ -611,7 +725,13 @@ exports.init = function (host, defaultOptions, defaultParams) {
}, customParams, callback);
},
- /** Get jobs config in xml */
+ /**
+ * Get jobs config in xml
+ *
+ * @param {string} jobName
+ * @param {object|undefined} customParams is optional
+ * @param {function} callback
+ */
get_config_xml: function get_config_xml(jobName, customParams, callback) {
var _doArgs25 = doArgs(arguments, ['string', ['object', {}], 'function']);
@@ -635,7 +755,14 @@ exports.init = function (host, defaultOptions, defaultParams) {
});
},
- /** Update a job config xml by passing it through your modifyFunction. */
+ /**
+ * Update a job config xml by passing it through your modifyFunction.
+ *
+ * @param {string} jobName
+ * @param {function} modifyFunction
+ * @param {object|undefined} customParams is optional
+ * @param {function} callback
+ */
update_config: function update_config(jobName, modifyFunction, customParams, callback) {
var _doArgs27 = doArgs(arguments, ['string', 'function', ['object', {}], 'function']);
@@ -662,7 +789,14 @@ exports.init = function (host, defaultOptions, defaultParams) {
});
},
- /** Update a existing job based on a jobConfig xml string */
+ /**
+ * Update a existing job based on a jobConfig xml string
+ *
+ * @param {string} jobName
+ * @param {string} jobConfig
+ * @param {object|undefined} customParams is optional
+ * @param {function} callback
+ */
update_job: function update_job(jobName, jobConfig, customParams, callback) {
var _doArgs29 = doArgs(arguments, ['string', 'string', ['object', {}], 'function']);
@@ -693,7 +827,13 @@ exports.init = function (host, defaultOptions, defaultParams) {
});
},
- /** Get all information for a job */
+ /**
+ * Get all information for a job
+ *
+ * @param {string} jobName
+ * @param {object|undefined} customParams is optional
+ * @param {function} callback
+ */
job_info: function job_info(jobName, customParams, callback) {
var _doArgs31 = doArgs(arguments, ['string', ['object', {}], 'function']);
@@ -704,12 +844,21 @@ exports.init = function (host, defaultOptions, defaultParams) {
callback = _doArgs32[2];
- doRequest({
+ var params = {
urlPattern: [JOB_INFO, jobName]
- }, customParams, callback);
+ };
+
+ doRequest(params, customParams, callback);
},
- /** Create a new job based on a jobConfig string */
+ /**
+ * Create a new job based on a jobConfig string
+ *
+ * @param {string} jobName
+ * @param {string} jobConfig
+ * @param {object|undefined} customParams is optional
+ * @param {function} callback
+ */
create_job: function create_job(jobName, jobConfig, customParams, callback) {
// Set the created job name!
@@ -742,7 +891,16 @@ exports.init = function (host, defaultOptions, defaultParams) {
});
},
- /** Copies a job and allows you to pass in a function to modify the configuration of the job you would like to copy */
+ /**
+ * Copies a job and allows you to pass in a function to modify the configuration
+ * of the job you would like to copy
+ *
+ * @param {string} jobName
+ * @param {string} newJobName
+ * @param {function} modifyFunction
+ * @param {object|undefined} customParams is optional
+ * @param {function} callback
+ */
copy_job: function copy_job(jobName, newJobName, modifyFunction, customParams, callback) {
var _doArgs35 = doArgs(arguments, ['string', 'string', 'function', ['object', {}], 'function']);
@@ -770,7 +928,13 @@ exports.init = function (host, defaultOptions, defaultParams) {
});
},
- /** Deletes a job */
+ /**
+ * Deletes a job
+ *
+ * @param {string} jobName
+ * @param {object|undefined} customParams is optional
+ * @param {function} callback
+ */
delete_job: function delete_job(jobName, customParams, callback) {
var _doArgs37 = doArgs(arguments, ['string', ['object', {}], 'function']);
@@ -794,7 +958,13 @@ exports.init = function (host, defaultOptions, defaultParams) {
});
},
- /** Disables a job */
+ /**
+ * Disables a job
+ *
+ * @param {string} jobName
+ * @param {object|undefined} customParams is optional
+ * @param {function} callback
+ */
disable_job: function disable_job(jobName, customParams, callback) {
var _doArgs39 = doArgs(arguments, ['string', ['object', {}], 'function']);
@@ -820,7 +990,13 @@ exports.init = function (host, defaultOptions, defaultParams) {
});
},
- /** Enables a job */
+ /**
+ * Enables a job
+ *
+ * @param {string} jobName
+ * @param {object|undefined} customParams is optional
+ * @param {function} callback
+ */
enable_job: function enable_job(jobName, customParams, callback) {
var _doArgs41 = doArgs(arguments, ['string', ['object', {}], 'function']);
@@ -846,7 +1022,13 @@ exports.init = function (host, defaultOptions, defaultParams) {
});
},
- /** Get the last build report for a job */
+ /**
+ * Get the last build report for a job
+ *
+ * @param {string} jobName
+ * @param {object|undefined} customParams is optional
+ * @param {function} callback
+ */
last_success: function last_success(jobName, customParams, callback) {
var _doArgs43 = doArgs(arguments, ['string', ['object', {}], 'function']);
@@ -863,7 +1045,13 @@ exports.init = function (host, defaultOptions, defaultParams) {
}, customParams, callback);
},
- /** Get the last result for a job */
+ /**
+ * Get the last result for a job
+ *
+ * @param {string} jobName
+ * @param {object|undefined} customParams is optional
+ * @param {function} callback
+ */
last_result: function last_result(jobName, customParams, callback) {
var _doArgs45 = doArgs(arguments, ['string', ['object', {}], 'function']);
@@ -892,7 +1080,12 @@ exports.init = function (host, defaultOptions, defaultParams) {
|* Queues *|
\*************************************/
- /** Get all queued items */
+ /**
+ * Get all queued items
+ *
+ * @param {object|undefined} customParams is optional
+ * @param {function} callback
+ */
queue: function queue(customParams, callback) {
var _doArgs47 = doArgs(arguments, [['object', {}], 'function']);
@@ -907,7 +1100,13 @@ exports.init = function (host, defaultOptions, defaultParams) {
}, customParams, callback);
},
- /** Get one queued item */
+ /**
+ * Get one queued item
+ *
+ * @param {string} queueNumber
+ * @param {object|undefined} customParams is optional
+ * @param {function} callback
+ */
queue_item: function queue_item(queueNumber, customParams, callback) {
var _doArgs49 = doArgs(arguments, ['string|number', ['object', {}], 'function']);
@@ -917,13 +1116,18 @@ exports.init = function (host, defaultOptions, defaultParams) {
customParams = _doArgs50[1];
callback = _doArgs50[2];
-
doRequest({
urlPattern: [QUEUE_ITEM, queueNumber]
}, customParams, callback);
},
- /** Cancel a queued item */
+ /**
+ * Cancel a queued item
+ *
+ * @param {string} itemId
+ * @param {object|undefined} customParams is optional
+ * @param {function} callback
+ */
cancel_item: function cancel_item(itemId, customParams, callback) {
var _doArgs51 = doArgs(arguments, ['string|number', ['object', {}], 'function']);
@@ -946,7 +1150,12 @@ exports.init = function (host, defaultOptions, defaultParams) {
|* Computers *|
\*************************************/
- /** Get info about all jenkins workers including currently executing jobs */
+ /**
+ * Get info about all jenkins workers including currently executing jobs
+ *
+ * @param {object|undefined} customParams is optional
+ * @param {function} callback
+ */
computers: function computers(customParams, callback) {
var _doArgs53 = doArgs(arguments, [['object', {}], 'function']);
@@ -965,7 +1174,12 @@ exports.init = function (host, defaultOptions, defaultParams) {
|* Views *|
\*************************************/
- /** Return a list of all the views on the Jenkins server */
+ /**
+ * Return a list of all the views on the Jenkins server
+ *
+ * @param {object|undefined} customParams is optional
+ * @param {function} callback
+ */
all_views: function all_views(customParams, callback) {
var _doArgs55 = doArgs(arguments, [['object', {}], 'function']);
@@ -981,7 +1195,13 @@ exports.init = function (host, defaultOptions, defaultParams) {
}, customParams, callback);
},
- /** */
+ /**
+ *
+ * @param {string} viewName
+ * @param {string|undefined} mode
+ * @param {object|undefined} customParams is optional
+ * @param {function} callback
+ */
create_view: function create_view(viewName, mode, customParams, callback) {
var _doArgs57 = doArgs(arguments, ['string', ['string', 'hudson.model.ListView'], ['object', {}], 'function']);
@@ -1015,23 +1235,34 @@ exports.init = function (host, defaultOptions, defaultParams) {
});
},
- /** */
- view_info: function view_info(viewId, customParams, callback) {
+ /**
+ * @param {string} viewName
+ * @param {object|undefined} customParams is optional
+ * @param {function} callback
+ */
+ view_info: function view_info(viewName, customParams, callback) {
var _doArgs59 = doArgs(arguments, ['string', ['object', {}], 'function']);
var _doArgs60 = _slicedToArray(_doArgs59, 3);
- viewId = _doArgs60[0];
+ viewName = _doArgs60[0];
customParams = _doArgs60[1];
callback = _doArgs60[2];
doRequest({
- urlPattern: [VIEW_INFO, viewId]
+ urlPattern: [VIEW_INFO, viewName]
}, customParams, callback);
},
- /** Update a view based on a viewConfig object */
+ /**
+ * Update a view based on a viewConfig object
+ *
+ * @param {string} viewName
+ * @param {object} viewConfig
+ * @param {object|undefined} customParams is optional
+ * @param {function} callback
+ */
update_view: function update_view(viewName, viewConfig, customParams, callback) {
var _doArgs61 = doArgs(arguments, ['string', 'object', ['object', {}], 'function']);
@@ -1064,7 +1295,11 @@ exports.init = function (host, defaultOptions, defaultParams) {
});
},
- /** */
+ /**
+ * @param {string} viewName
+ * @param {object|undefined} customParams is optional
+ * @param {function} callback
+ */
delete_view: function delete_view(viewName, customParams, callback) {
var _doArgs63 = doArgs(arguments, ['string', ['object', {}], 'function']);
@@ -1088,13 +1323,18 @@ exports.init = function (host, defaultOptions, defaultParams) {
});
},
- /** */
- add_job_to_view: function add_job_to_view(viewId, jobName, customParams, callback) {
+ /**
+ * @param {string} viewName
+ * @param {string} jobName
+ * @param {object|undefined} customParams is optional
+ * @param {function} callback
+ */
+ add_job_to_view: function add_job_to_view(viewName, jobName, customParams, callback) {
var _doArgs65 = doArgs(arguments, ['string', 'string', ['object', {}], 'function']);
var _doArgs66 = _slicedToArray(_doArgs65, 4);
- viewId = _doArgs66[0];
+ viewName = _doArgs66[0];
jobName = _doArgs66[1];
customParams = _doArgs66[2];
callback = _doArgs66[3];
@@ -1104,18 +1344,23 @@ exports.init = function (host, defaultOptions, defaultParams) {
doRequest({
method: 'POST',
- urlPattern: [VIEW_ADD_JOB, viewId],
+ urlPattern: [VIEW_ADD_JOB, viewName],
noparse: true
}, customParams, callback);
},
- /** */
- remove_job_from_view: function remove_job_from_view(viewId, jobName, customParams, callback) {
+ /**
+ * @param {string} viewName
+ * @param {string} jobName
+ * @param {object|undefined} customParams is optional
+ * @param {function} callback
+ */
+ remove_job_from_view: function remove_job_from_view(viewName, jobName, customParams, callback) {
var _doArgs67 = doArgs(arguments, ['string', 'string', ['object', {}], 'function']);
var _doArgs68 = _slicedToArray(_doArgs67, 4);
- viewId = _doArgs68[0];
+ viewName = _doArgs68[0];
jobName = _doArgs68[1];
customParams = _doArgs68[2];
callback = _doArgs68[3];
@@ -1125,24 +1370,30 @@ exports.init = function (host, defaultOptions, defaultParams) {
doRequest({
method: 'POST',
- urlPattern: [VIEW_REMOVE_JOB, viewId],
+ urlPattern: [VIEW_REMOVE_JOB, viewName],
noparse: true
}, customParams, callback);
},
- /* Return a list of objet literals containing the name and color of all the jobs for a view on the Jenkins server */
- all_jobs_in_view: function all_jobs_in_view(viewId, customParams, callback) {
+ /**
+ * Return a list of objet literals containing the name and color of all the jobs for a view on the Jenkins server
+ *
+ * @param {string} viewName
+ * @param {object|undefined} customParams is optional
+ * @param {function} callback
+ */
+ all_jobs_in_view: function all_jobs_in_view(viewName, customParams, callback) {
var _doArgs69 = doArgs(arguments, ['string', ['object', {}], 'function']);
var _doArgs70 = _slicedToArray(_doArgs69, 3);
- viewId = _doArgs70[0];
+ viewName = _doArgs70[0];
customParams = _doArgs70[1];
callback = _doArgs70[2];
doRequest({
- urlPattern: [VIEW_INFO, viewId],
+ urlPattern: [VIEW_INFO, viewName],
bodyProp: 'jobs'
}, customParams, callback);
},
@@ -1151,7 +1402,12 @@ exports.init = function (host, defaultOptions, defaultParams) {
|* Plugins *|
\*************************************/
- /* Get all installed plugins */
+ /**
+ * Get all installed plugins
+ *
+ * @param {object|undefined} customParams is optional
+ * @param {function} callback
+ */
all_installed_plugins: function all_installed_plugins(customParams, callback) {
var _doArgs71 = doArgs(arguments, [['object', {}], 'function']);
@@ -1171,18 +1427,24 @@ exports.init = function (host, defaultOptions, defaultParams) {
}, customParams, callback);
},
- /* Install a plugin */
- install_plugin: function install_plugin(plugin, customParams, callback) {
+ /**
+ * Install a plugin
+ *
+ * @param {string} pluginName
+ * @param {object|undefined} customParams is optional
+ * @param {function} callback
+ */
+ install_plugin: function install_plugin(pluginName, customParams, callback) {
var _doArgs73 = doArgs(arguments, ['string', ['object', {}], 'function']);
var _doArgs74 = _slicedToArray(_doArgs73, 3);
- plugin = _doArgs74[0];
+ pluginName = _doArgs74[0];
customParams = _doArgs74[1];
callback = _doArgs74[2];
- var body = '';
+ var body = '';
doRequest({
method: 'POST',
@@ -1196,10 +1458,16 @@ exports.init = function (host, defaultOptions, defaultParams) {
}, customParams, callback);
},
- /* Create a new folder
- * Needs Folder plugin in Jenkins: https://wiki.jenkins-ci.org/display/JENKINS/CloudBees+Folders+Plugin
- * curl -XPOST 'http://jenkins/createItem?name=FolderName&mode=com.cloudbees.hudson.plugins.folder.Folder&from=&json=%7B%22name%22%3A%22FolderName%22%2C%22mode%22%3A%22com.cloudbees.hudson.plugins.folder.Folder%22%2C%22from%22%3A%22%22%2C%22Submit%22%3A%22OK%22%7D&Submit=OK' --user user.name:YourAPIToken -H "Content-Type:application/x-www-form-urlencoded"
- * https://gist.github.com/stuart-warren/7786892
+ /**
+ * Create a new folder with given name
+ *
+ * Requires Folder plugin in Jenkins:
+ * @see https://wiki.jenkins-ci.org/display/JENKINS/CloudBees+Folders+Plugin
+ * @see https://gist.github.com/stuart-warren/7786892
+ *
+ * @param {string} folderName
+ * @param {object|undefined} customParams is optional
+ * @param {function} callback
*/
create_folder: function create_folder(folderName, customParams, callback) {
var _doArgs75 = doArgs(arguments, ['string', ['object', {}], 'function']);
diff --git a/src/main.js b/src/main.js
index 085dcf6..7f3c658 100644
--- a/src/main.js
+++ b/src/main.js
@@ -228,6 +228,35 @@ exports.init = function (host, defaultOptions, defaultParams) {
return url;
}
+ const getCrumb = function (requestOptions, callback) {
+ const urlPattern = ['/crumbIssuer/api/json'];
+ const url = formatUrl.apply(null, urlPattern);
+
+ if (!requestOptions.method === 'POST' && !requestOptions.method === 'DELETE') {
+ return callback();
+ }
+
+ const getCrumbOptions = Object.assign({
+ method: 'GET',
+ url: url,
+ headers: [],
+ followAllRedirects: true,
+ }, { headers: { 'Content-Type': 'application/json' } });
+
+ request(getCrumbOptions, function(error, response, body) {
+ if (error || response.statusCode !== HTTP_CODE_200
+ || response.should !== HTTP_CODE_201 || response.should !== HTTP_CODE_302 ) {
+ return callback(null);
+ }
+
+ const getCrumbResponse = JSON.parse(body);
+ const crumb = {};
+
+ crumb[getCrumbResponse.crumbRequestField] = getCrumbResponse.crumb;
+ callback(null, crumb);
+ });
+ };
+
/**
* Run the actual HTTP request.
*
@@ -261,40 +290,54 @@ exports.init = function (host, defaultOptions, defaultParams) {
body: null
}, options.request);
- // Do the request
- request(requestOptions, function (error, response, body) {
- if (error) {
- callback(error, response);
+ // Get Crumb if POST ? DELETE?
+ getCrumb(requestOptions, function (err, crumb) {
+
+ if (err) {
+ callback(err);
return;
}
- if ((Array.isArray(options.successStatusCodes) && !options.successStatusCodes.includes(response.statusCode))
- || (Array.isArray(options.failureStatusCodes) && options.failureStatusCodes.includes(response.statusCode))) {
- callback(`Server returned unexpected status code: ${response.statusCode}`, response);
- return;
+ // Do the request
+ if (crumb) {
+ Object.assign(requestOptions.headers, crumb);
}
- if (options.noparse) {
- // Wrap body in the response object
- if (typeof body === 'string') {
- body = { body: body };
+ request(requestOptions, function (error, response, body) {
+ if (error) {
+ callback(error, response);
+ return;
}
- // Add location
- const location = response.headers.Location || response.headers.location;
-
- if (location) {
- body.location = location;
+ if ((Array.isArray(options.successStatusCodes) && !options.successStatusCodes.includes(response.statusCode))
+ || (Array.isArray(options.failureStatusCodes) && options.failureStatusCodes.includes(response.statusCode))) {
+ callback(`Server returned unexpected status code: ${response.statusCode}`, response);
+ return;
}
- // Add status code
- body.statusCode = response.statusCode;
+ if (options.noparse) {
+ // Wrap body in the response object
+ if (typeof body === 'string') {
+ body = { body: body };
+ }
- callback(null, body);
- } else {
- tryParseJson(body, options.bodyProp, callback);
- }
+ // Add location
+ const location = response.headers.Location || response.headers.location;
+
+ if (location) {
+ body.location = location;
+ }
+
+ // Add status code
+ body.statusCode = response.statusCode;
+
+ callback(null, body);
+ } else {
+ tryParseJson(body, options.bodyProp, callback);
+ }
+ });
});
+
};
return {
@@ -636,9 +679,11 @@ exports.init = function (host, defaultOptions, defaultParams) {
job_info: function (jobName, customParams, callback) {
[jobName, customParams, callback] = doArgs(arguments, ['string', ['object', {}], 'function']);
- doRequest({
+ const params = {
urlPattern: [JOB_INFO, jobName]
- }, customParams, callback);
+ };
+
+ doRequest(params, customParams, callback);
},
/**
@@ -842,7 +887,6 @@ exports.init = function (host, defaultOptions, defaultParams) {
*/
queue_item: function (queueNumber, customParams, callback) {
[queueNumber, customParams, callback] = doArgs(arguments, ['string|number', ['object', {}], 'function']);
-
doRequest({
urlPattern: [QUEUE_ITEM, queueNumber]
}, customParams, callback);