diff --git a/README.md b/README.md index deed56d..baa65e7 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,7 @@ If you need additional request parameters you can add them as explained in 'opti #### build ```javascript -jenkins.build('job-in-jenkins', (optional){token: 'jenkins-token', ...}, function(err, data) { +jenkins.build('job-in-jenkins', (optional){token: 'jenkins-token', crumbFlag: true, ...}, function(err, data) { if (err){ return console.log(err); } console.log(data) }); @@ -59,7 +59,7 @@ jenkins.build('job-in-jenkins', (optional){token: 'jenkins-token', ...}, functio #### build_with_params ```javascript -jenkins.build_with_params('job-in-jenkins', (optional){depth: 1, :, token: 'jenkins-token',...}, function(err, data) { +jenkins.build_with_params('job-in-jenkins', (optional){depth: 1, crumbFlag: true, :, token: 'jenkins-token',...}, function(err, data) { if (err){ return console.log(err); } console.log(data) }); @@ -67,7 +67,7 @@ jenkins.build_with_params('job-in-jenkins', (optional){depth: 1, : #### stop build ```javascript -jenkins.stop_build('job-in-jenkins', 'build-number', (optional){token: 'jenkins-token', ...}, function(err, data) { +jenkins.stop_build('job-in-jenkins', 'build-number', (optional){token: 'jenkins-token', crumbFlag: true, ...}, function(err, data) { if (err){ return console.log(err); } console.log(data) }); @@ -75,7 +75,7 @@ jenkins.stop_build('job-in-jenkins', 'build-number', (optional){token: 'jenkins- #### console output ```javascript -jenkins.console_output('job-in-jenkins', 'buildname', (optional) {depth: 1, :, ...}, function(err, data) { +jenkins.console_output('job-in-jenkins', 'buildname', (optional) {depth: 1, crumbFlag: true, :, ...}, function(err, data) { if (err){ return console.log(err); } console.log(data) }); @@ -83,7 +83,7 @@ jenkins.console_output('job-in-jenkins', 'buildname', (optional) {depth: 1, :, ...}, function(err, data) { +jenkins.build_info('job-in-jenkins', 'build-number', (optional) {depth: 1, crumbFlag: true, :, ...}, function(err, data) { if (err){ return console.log(err); } console.log(data) }); @@ -91,7 +91,7 @@ jenkins.build_info('job-in-jenkins', 'build-number', (optional) {depth: 1, :, ...}, function(err, data) { +jenkins.last_build_info('job-in-jenkins', (optional) {depth: 1, crumbFlag: true, :, ...}, function(err, data) { if (err){ return console.log(err); } console.log(data) }); @@ -99,7 +99,7 @@ jenkins.last_build_info('job-in-jenkins', (optional) {depth: 1, :, #### last completed build info ```javascript -jenkins.last_completed_build_info('job-in-jenkins', (optional) {depth: 1, :, ...}, function(err, data) { +jenkins.last_completed_build_info('job-in-jenkins', (optional) {depth: 1, crumbFlag: true, :, ...}, function(err, data) { if (err){ return console.log(err); } console.log(data) }); @@ -107,7 +107,7 @@ jenkins.last_completed_build_info('job-in-jenkins', (optional) {depth: 1, :, ...}, function(err, data) { +jenkins.all_builds('job-in-jenkins', (optional) {depth: 1, crumbFlag: true, :, ...}, function(err, data) { if (err){ return console.log(err); } console.log(data) }); @@ -115,7 +115,7 @@ jenkins.all_builds('job-in-jenkins', (optional) {depth: 1, :, ...} #### test result/report ```javascript -jenkins.test_result('job-in-jenkins', 'build-number', (optional) {depth: 1, :, ...}, function(err, data) { +jenkins.test_result('job-in-jenkins', 'build-number', (optional) {depth: 1, crumbFlag: true, :, ...}, function(err, data) { if (err){ return console.log(err); } console.log(data) }); @@ -131,7 +131,7 @@ jenkins.test_result('job-in-jenkins', 'build-number', (optional) {depth: 1, :, ...}, function(err, data) { +jenkins.delete_build('job-in-jenkins', 'build-number', (optional) {depth: 1, crumbFlag: true, :, ...}, function(err, data) { if (err){ return console.log(err); } console.log(data) }); @@ -142,7 +142,7 @@ jenkins.delete_build('job-in-jenkins', 'build-number', (optional) {depth: 1, :, ...}, function(err, data) { +jenkins.get_config_xml('job-in-jenkins', (optional) {depth: 1, crumbFlag: true, :, ...}, function(err, data) { if (err){ return console.log(err); } console.log(data) }); @@ -164,7 +164,7 @@ jenkins.update_config('job-to-update' // the new config xml for the new job return config.replace('development','feature-branch'); } - ,(optional){token: 'jenkins-token', ...} + ,(optional){token: 'jenkins-token', crumbFlag: true, ...} ,function(err, data) { // if no error, job was copied if (err){ return console.log(err); } @@ -174,7 +174,7 @@ jenkins.update_config('job-to-update' #### update job ```javascript -jenkins.update_job('job-to-update', xmlConfigString, (optional){token: 'jenkins-token', ...}, function(err, data) { +jenkins.update_job('job-to-update', xmlConfigString, (optional){token: 'jenkins-token', crumbFlag: true, ...}, function(err, data) { // if no error, job was copied if (err){ return console.log(err); } console.log(data) @@ -183,7 +183,7 @@ jenkins.update_job('job-to-update', xmlConfigString, (optional){token: 'jenkins- #### job info ```javascript -jenkins.job_info('job-in-jenkins', (optional) {depth: 1, :, ...}, function(err, data) { +jenkins.job_info('job-in-jenkins', (optional) {depth: 1, crumbFlag: true, :, ...}, function(err, data) { if (err){ return console.log(err); } console.log(data) }); @@ -191,7 +191,7 @@ jenkins.job_info('job-in-jenkins', (optional) {depth: 1, :, ...}, #### create job ```javascript -jenkins.create_job('job-in-jenkins', xmlConfigString, (optional) {depth: 1, :, ...}, function(err, data) { +jenkins.create_job('job-in-jenkins', xmlConfigString, (optional) {depth: 1, crumbFlag: true, :, ...}, function(err, data) { if (err){ return console.log(err); } console.log(data) }); @@ -206,7 +206,7 @@ jenkins.copy_job('job-to-copy' // the new config xml for the new job return config.replace('development','feature-branch'); } - ,(optional){token: 'jenkins-token', ...} + ,(optional){token: 'jenkins-token', crumbFlag: true, ...} ,function(err, data) { // if no error, job was copied if (err){ return console.log(err); } @@ -216,7 +216,7 @@ jenkins.copy_job('job-to-copy' #### delete job ```javascript -jenkins.delete_job('job-in-jenkins', (optional) {depth: 1, :, ...}, function(err, data) { +jenkins.delete_job('job-in-jenkins', (optional) {depth: 1, crumbFlag: true, :, ...}, function(err, data) { if (err){ return console.log(err); } console.log(data) }); @@ -224,7 +224,7 @@ jenkins.delete_job('job-in-jenkins', (optional) {depth: 1, :, ...} #### enable job ```javascript -jenkins.enable_job('job-in-jenkins', (optional) {depth: 1, :, ...}, function(err, data) { +jenkins.enable_job('job-in-jenkins', (optional) {depth: 1, crumbFlag: true, :, ...}, function(err, data) { if (err){ return console.log(err); } console.log(data) }); @@ -232,7 +232,7 @@ jenkins.enable_job('job-in-jenkins', (optional) {depth: 1, :, ...} #### disable job ```javascript -jenkins.disable_job('job-in-jenkins', (optional) {depth: 1, :, ...}, function(err, data) { +jenkins.disable_job('job-in-jenkins', (optional) {depth: 1, crumbFlag: true, :, ...}, function(err, data) { if (err){ return console.log(err); } console.log(data) }); @@ -240,7 +240,7 @@ jenkins.disable_job('job-in-jenkins', (optional) {depth: 1, :, ... #### last success ```javascript -jenkins.last_success('job-in-jenkins', (optional) {depth: 1, :, ...}, function(err, data) { +jenkins.last_success('job-in-jenkins', (optional) {depth: 1, crumbFlag: true, :, ...}, function(err, data) { if (err){ return console.log(err); } console.log(data) }); @@ -248,7 +248,7 @@ jenkins.last_success('job-in-jenkins', (optional) {depth: 1, :, .. #### last result ```javascript -jenkins.last_result('job-in-jenkins', (optional) {depth: 1, :, ...}, function(err, data) { +jenkins.last_result('job-in-jenkins', (optional) {depth: 1, crumbFlag: true, :, ...}, function(err, data) { if (err){ return console.log(err); } console.log(data) }); @@ -259,7 +259,7 @@ jenkins.last_result('job-in-jenkins', (optional) {depth: 1, :, ... #### get all queued items ```javascript -jenkins.queue((optional){token: 'jenkins-token', ...}, function(err, data) { +jenkins.queue((optional){token: 'jenkins-token', crumbFlag: true, ...}, function(err, data) { if (err){ return console.log(err); } console.log(data) }); @@ -267,7 +267,7 @@ jenkins.queue((optional){token: 'jenkins-token', ...}, function(err, data) { #### get one queued item ```javascript -jenkins.queue_item('queue-item-number', (optional) {depth: 1, :, ...}, function(err, data) { +jenkins.queue_item('queue-item-number', (optional) {depth: 1, crumbFlag: true, :, ...}, function(err, data) { if (err){ return console.log(err); } console.log(data) }); @@ -275,7 +275,7 @@ jenkins.queue_item('queue-item-number', (optional) {depth: 1, :, . #### cancel queued item ```javascript -jenkins.cancel_item('queue-item-number', (optional) {depth: 1, :, ...}, function(err, data) { +jenkins.cancel_item('queue-item-number', (optional) {depth: 1, crumbFlag: true, :, ...}, function(err, data) { if (err){ return console.log(err); } console.log(data) }); @@ -283,7 +283,7 @@ jenkins.cancel_item('queue-item-number', (optional) {depth: 1, :, #### get all jenkins computers (aka workers) ```javascript -jenkins.computers((optional){token: 'jenkins-token', ...}, function(err, data) { +jenkins.computers((optional){token: 'jenkins-token', crumbFlag: true, ...}, function(err, data) { if (err){ return console.log(err); } console.log(data) }); @@ -294,7 +294,7 @@ jenkins.computers((optional){token: 'jenkins-token', ...}, function(err, data) { #### get all views ```javascript -jenkins.all_views((optional) {depth: 1, :, ...}, function(err, data) { +jenkins.all_views((optional) {depth: 1, crumbFlag: true, :, ...}, function(err, data) { if (err){ return console.log(err); } console.log(data) }); @@ -302,7 +302,7 @@ jenkins.all_views((optional) {depth: 1, :, ...}, function(err, dat #### create view ```javascript -jenkins.create_view('new-view-name', (optional)viewMode = 'hudson.model.ListView', (optional){token: 'jenkins-token', ...}, function(err, data) { +jenkins.create_view('new-view-name', (optional)viewMode = 'hudson.model.ListView', (optional){token: 'jenkins-token', crumbFlag: true, ...}, function(err, data) { if (err){ return console.log(err); } console.log(data) }); @@ -310,7 +310,7 @@ jenkins.create_view('new-view-name', (optional)viewMode = 'hudson.model.ListView #### view info ```javascript -jenkins.create_view('view-name', (optional){token: 'jenkins-token', ...}, function(err, data) { +jenkins.create_view('view-name', (optional){token: 'jenkins-token', crumbFlag: true, ...}, function(err, data) { if (err){ return console.log(err); } console.log(data) }); @@ -328,7 +328,7 @@ var viewConfig = { "columns": [{"stapler-class": "hudson.views.StatusColumn", "$class": "hudson.views.StatusColumn"}, {"stapler-class": "hudson.views.WeatherColumn", "$class": "hudson.views.WeatherColumn"}, {"stapler-class": "hudson.views.JobColumn", "$class": "hudson.views.JobColumn"}, {"stapler-class": "hudson.views.LastSuccessColumn", "$class": "hudson.views.LastSuccessColumn"}, {"stapler-class": "hudson.views.LastFailureColumn", "$class": "hudson.views.LastFailureColumn"}, {"stapler-class": "hudson.views.LastDurationColumn", "$class": "hudson.views.LastDurationColumn"}, {"stapler-class": "hudson.views.BuildButtonColumn", "$class": "hudson.views.BuildButtonColumn"}] }; -jenkins.update_view('view-in-jenkins', viewConfig, (optional){token: 'jenkins-token', ...}, function(err, data) { +jenkins.update_view('view-in-jenkins', viewConfig, (optional){token: 'jenkins-token', crumbFlag: true, ...}, function(err, data) { if (err){ return console.log(err); } console.log(data) }); @@ -336,7 +336,7 @@ jenkins.update_view('view-in-jenkins', viewConfig, (optional){token: 'jenkins-to #### delete view ```javascript -jenkins.delete_view('view-in-jenkins', (optional){token: 'jenkins-token', ...}, function(err, data) { +jenkins.delete_view('view-in-jenkins', (optional){token: 'jenkins-token', crumbFlag: true, ...}, function(err, data) { if (err){ return console.log(err); } console.log(data) }); @@ -344,7 +344,7 @@ jenkins.delete_view('view-in-jenkins', (optional){token: 'jenkins-token', ...}, #### add job to view ```javascript -jenkins.add_job_to_view('view-in-jenkins', 'job-in-jenkins', (optional) {depth: 1, :, ...}, function(err, data) { +jenkins.add_job_to_view('view-in-jenkins', 'job-in-jenkins', (optional) {depth: 1, crumbFlag: true, :, ...}, function(err, data) { if (err){ return console.log(err); } console.log(data) }); @@ -352,7 +352,7 @@ jenkins.add_job_to_view('view-in-jenkins', 'job-in-jenkins', (optional) {depth: #### remove job from view ```javascript -jenkins.remove_job_from_view('view-in-jenkins', 'job-in-jenkins', (optional) {depth: 1, :, ...}, function(err, data) { +jenkins.remove_job_from_view('view-in-jenkins', 'job-in-jenkins', (optional) {depth: 1, crumbFlag: true, :, ...}, function(err, data) { if (err){ return console.log(err); } console.log(data) }); @@ -360,7 +360,7 @@ jenkins.remove_job_from_view('view-in-jenkins', 'job-in-jenkins', (optional) {de #### get all jobs in view ```javascript -jenkins.all_jobs_in_view('view-in-jenkins', (optional) {depth: 1, :, ...}, function(err, data) { +jenkins.all_jobs_in_view('view-in-jenkins', (optional) {depth: 1, crumbFlag: true, :, ...}, function(err, data) { if (err){ return console.log(err); } console.log(data) }); @@ -371,7 +371,7 @@ jenkins.all_jobs_in_view('view-in-jenkins', (optional) {depth: 1, :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 +739,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']); @@ -577,17 +758,22 @@ exports.init = function (host, defaultOptions, defaultParams) { callback = _doArgs22[3]; - doRequest({ - method: 'POST', - urlPattern: [BUILD_DELETE, jobName, buildNumber], - noparse: true - }, customParams, function (error, data) { - if (error) { - callback(error, data); - } else { - data.body = 'Build ' + buildNumber + ' deleted.'; - callback(null, data); - } + handleCrumbFlagAndRequest(customParams, function (params, headers) { + doRequest({ + method: 'POST', + urlPattern: [BUILD_DELETE, jobName, buildNumber], + noparse: true, + request: { + headers: headers + } + }, params, function (error, data) { + if (error) { + callback(error, data); + } else { + data.body = 'Build ' + buildNumber + ' deleted.'; + callback(null, data); + } + }); }); }, @@ -595,7 +781,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']); @@ -605,13 +796,24 @@ exports.init = function (host, defaultOptions, defaultParams) { callback = _doArgs24[1]; - doRequest({ - urlPattern: [JOB_LIST], - bodyProp: 'jobs' - }, customParams, callback); + handleCrumbFlagAndRequest(customParams, function (params, headers) { + doRequest({ + urlPattern: [JOB_LIST], + bodyProp: 'jobs', + request: { + headers: headers + } + }, params, 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']); @@ -622,20 +824,32 @@ exports.init = function (host, defaultOptions, defaultParams) { callback = _doArgs26[2]; - doRequest({ - urlPattern: [JOB_CONFIG, jobName], - noparse: true - }, customParams, function (error, data) { - // Get only the XML response body - if (error) { - callback(error, data); - } else { - callback(null, data.body); - } + handleCrumbFlagAndRequest(customParams, function (params, headers) { + doRequest({ + urlPattern: [JOB_CONFIG, jobName], + noparse: true, + request: { + headers: headers + } + }, params, function (error, data) { + // Get only the XML response body + if (error) { + callback(error, data); + } else { + callback(null, data.body); + } + }); }); }, - /** 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 +876,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']); @@ -674,26 +895,34 @@ exports.init = function (host, defaultOptions, defaultParams) { callback = _doArgs30[3]; - doRequest({ - method: 'POST', - urlPattern: [JOB_CONFIG, jobName], - request: { - body: jobConfig, - headers: { 'Content-Type': 'application/xml' } - }, - noparse: true - }, customParams, function (error, data) { - if (error) { - callback(error, data); - return; - } - // TODO rather return job_info ??? - // const data = {name: jobName, location: response.headers['Location'] || response.headers['location']}; - callback(null, { name: jobName }); + handleCrumbFlagAndRequest(customParams, function (params, headers) { + doRequest({ + method: 'POST', + urlPattern: [JOB_CONFIG, jobName], + request: { + body: jobConfig, + headers: headers + }, + noparse: true + }, params, function (error, data) { + if (error) { + callback(error, data); + return; + } + // TODO rather return job_info ??? + // const data = {name: jobName, location: response.headers['Location'] || response.headers['location']}; + callback(null, { name: jobName }); + }); }); }, - /** 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 +933,24 @@ exports.init = function (host, defaultOptions, defaultParams) { callback = _doArgs32[2]; - doRequest({ - urlPattern: [JOB_INFO, jobName] - }, customParams, callback); + handleCrumbFlagAndRequest(customParams, function (params, headers) { + doRequest({ + urlPattern: [JOB_INFO, jobName], + request: { + headers: headers + } + }, params, 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! @@ -725,24 +966,35 @@ exports.init = function (host, defaultOptions, defaultParams) { var self = this; - doRequest({ - method: 'POST', - urlPattern: [JOB_CREATE], - request: { - body: jobConfig, - headers: { 'Content-Type': 'application/xml' } - }, - noparse: true - }, customParams, function (error, data) { - if (error) { - callback(error, data); - return; - } - self.job_info(jobName, customParams, callback); + handleCrumbFlagAndRequest(customParams, function (params, headers) { + doRequest({ + method: 'POST', + urlPattern: [JOB_CREATE], + request: { + body: jobConfig, + headers: headers + }, + noparse: true + }, params, function (error, data) { + if (error) { + callback(error, data); + return; + } + self.job_info(jobName, customParams, callback); + }); }); }, - /** 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 +1022,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']); @@ -781,20 +1039,31 @@ exports.init = function (host, defaultOptions, defaultParams) { callback = _doArgs38[2]; - doRequest({ - method: 'POST', - urlPattern: [JOB_DELETE, jobName], - noparse: true - }, customParams, function (error, data) { - if (error) { - callback(error, data); - return; - } - callback(null, { name: jobName }); + handleCrumbFlagAndRequest(customParams, function (params, headers) { + doRequest({ + method: 'POST', + urlPattern: [JOB_DELETE, jobName], + noparse: true, + request: { + headers: headers + } + }, params, function (error, data) { + if (error) { + callback(error, data); + return; + } + callback(null, { name: jobName }); + }); }); }, - /** 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']); @@ -807,20 +1076,31 @@ exports.init = function (host, defaultOptions, defaultParams) { var self = this; - doRequest({ - method: 'POST', - urlPattern: [JOB_DISABLE, jobName], - noparse: true - }, customParams, function (error, data) { - if (error) { - callback(error, data); - return; - } - self.job_info(jobName, customParams, callback); + handleCrumbFlagAndRequest(customParams, function (params, headers) { + doRequest({ + method: 'POST', + urlPattern: [JOB_DISABLE, jobName], + noparse: true, + request: { + headers: headers + } + }, params, function (error, data) { + if (error) { + callback(error, data); + return; + } + self.job_info(jobName, customParams, callback); + }); }); }, - /** 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']); @@ -833,20 +1113,31 @@ exports.init = function (host, defaultOptions, defaultParams) { var self = this; - doRequest({ - method: 'POST', - urlPattern: [JOB_ENABLE, jobName], - noparse: true - }, customParams, function (error, data) { - if (error) { - callback(error, data); - return; - } - self.job_info(jobName, customParams, callback); + handleCrumbFlagAndRequest(customParams, function (params, headers) { + doRequest({ + method: 'POST', + urlPattern: [JOB_ENABLE, jobName], + noparse: true, + request: { + headers: headers + } + }, params, function (error, data) { + if (error) { + callback(error, data); + return; + } + self.job_info(jobName, customParams, callback); + }); }); }, - /** 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']); @@ -857,13 +1148,24 @@ exports.init = function (host, defaultOptions, defaultParams) { callback = _doArgs44[2]; - doRequest({ - method: 'POST', - urlPattern: [LAST_SUCCESS, jobName] - }, customParams, callback); + handleCrumbFlagAndRequest(customParams, function (params, headers) { + doRequest({ + method: 'POST', + urlPattern: [LAST_SUCCESS, jobName], + request: { + headers: headers + } + }, params, 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']); @@ -882,9 +1184,14 @@ exports.init = function (host, defaultOptions, defaultParams) { var lastResultUrl = data.lastBuild.url; - doRequest({ - urlPattern: [lastResultUrl + API, jobName] - }, customParams, callback); + handleCrumbFlagAndRequest(customParams, function (params, headers) { + doRequest({ + urlPattern: [lastResultUrl + API, jobName], + request: { + headers: headers + } + }, params, callback); + }); }); }, @@ -892,7 +1199,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']); @@ -902,12 +1214,23 @@ exports.init = function (host, defaultOptions, defaultParams) { callback = _doArgs48[1]; - doRequest({ - urlPattern: [QUEUE] - }, customParams, callback); + handleCrumbFlagAndRequest(customParams, function (params, headers) { + doRequest({ + urlPattern: [QUEUE], + request: { + headers: headers + } + }, params, 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']); @@ -918,12 +1241,23 @@ exports.init = function (host, defaultOptions, defaultParams) { callback = _doArgs50[2]; - doRequest({ - urlPattern: [QUEUE_ITEM, queueNumber] - }, customParams, callback); + handleCrumbFlagAndRequest(customParams, function (params, headers) { + doRequest({ + urlPattern: [QUEUE_ITEM, queueNumber], + request: { + headers: headers + } + }, params, 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']); @@ -936,17 +1270,27 @@ exports.init = function (host, defaultOptions, defaultParams) { customParams.id = itemId; - doRequest({ - method: 'POST', - urlPattern: [QUEUE_CANCEL_ITEM] - }, customParams, callback); + handleCrumbFlagAndRequest(customParams, function (params, headers) { + doRequest({ + method: 'POST', + urlPattern: [QUEUE_CANCEL_ITEM], + request: { + headers: headers + } + }, params, callback); + }); }, /** ***********************************\ |* 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']); @@ -956,16 +1300,26 @@ exports.init = function (host, defaultOptions, defaultParams) { callback = _doArgs54[1]; - doRequest({ - urlPattern: [COMPUTERS] - }, customParams, callback); + handleCrumbFlagAndRequest(customParams, function (params, headers) { + doRequest({ + urlPattern: [COMPUTERS], + request: { + headers: headers + } + }, params, callback); + }); }, /** ***********************************\ |* 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']); @@ -975,13 +1329,24 @@ exports.init = function (host, defaultOptions, defaultParams) { callback = _doArgs56[1]; - doRequest({ - urlPattern: [VIEW_LIST], - bodyProp: 'views' - }, customParams, callback); + handleCrumbFlagAndRequest(customParams, function (params, headers) { + doRequest({ + urlPattern: [VIEW_LIST], + bodyProp: 'views', + request: { + headers: headers + } + }, params, 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']); @@ -999,39 +1364,58 @@ exports.init = function (host, defaultOptions, defaultParams) { var self = this; - doRequest({ - method: 'POST', - urlPattern: [VIEW_CREATE], - request: { - form: formData - }, - noparse: true - }, customParams, function (error, data) { - if (error) { - callback(error, data); - return; - } - self.view_info(viewName, customParams, callback); + handleCrumbFlagAndRequest(customParams, function (params, headers) { + doRequest({ + method: 'POST', + urlPattern: [VIEW_CREATE], + request: { + form: formData, + headers: headers + }, + noparse: true + }, params, function (error, data) { + if (error) { + callback(error, data); + return; + } + self.view_info(viewName, customParams, callback); + }); }); }, - /** */ - 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] - }, customParams, callback); + handleCrumbFlagAndRequest(customParams, function (params, headers) { + doRequest({ + urlPattern: [VIEW_INFO, viewName], + request: { + headers: headers + } + }, params, 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']); @@ -1047,24 +1431,30 @@ exports.init = function (host, defaultOptions, defaultParams) { var self = this; - doRequest({ - method: 'POST', - urlPattern: [VIEW_CONFIG, viewName], - request: { - form: viewConfig - // headers: {'content-type': 'application/x-www-form-urlencoded'}, - }, - noparse: true - }, customParams, function (error, data) { - if (error) { - callback(error, data); - return; - } - self.view_info(viewName, customParams, callback); + handleCrumbFlagAndRequest(customParams, function (params, headers) { + doRequest({ + method: 'POST', + urlPattern: [VIEW_CONFIG, viewName], + request: { + form: viewConfig, + headers: headers + }, + noparse: true + }, params, function (error, data) { + if (error) { + callback(error, data); + return; + } + self.view_info(viewName, customParams, callback); + }); }); }, - /** */ + /** + * @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']); @@ -1075,26 +1465,36 @@ exports.init = function (host, defaultOptions, defaultParams) { callback = _doArgs64[2]; - doRequest({ - method: 'POST', - urlPattern: [VIEW_DELETE, viewName], - noparse: true - }, customParams, function (error, data) { - if (error) { - callback(error, data); - return; - } - callback(null, { name: viewName }); + handleCrumbFlagAndRequest(customParams, function (params, headers) { + doRequest({ + method: 'POST', + urlPattern: [VIEW_DELETE, viewName], + noparse: true, + request: { + headers: headers + } + }, params, function (error, data) { + if (error) { + callback(error, data); + return; + } + callback(null, { name: viewName }); + }); }); }, - /** */ - 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]; @@ -1102,20 +1502,30 @@ exports.init = function (host, defaultOptions, defaultParams) { customParams.name = jobName; - doRequest({ - method: 'POST', - urlPattern: [VIEW_ADD_JOB, viewId], - noparse: true - }, customParams, callback); + handleCrumbFlagAndRequest(customParams, function (params, headers) { + doRequest({ + method: 'POST', + urlPattern: [VIEW_ADD_JOB, viewName], + noparse: true, + request: { + headers: headers + } + }, params, 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]; @@ -1123,35 +1533,56 @@ exports.init = function (host, defaultOptions, defaultParams) { customParams.name = jobName; - doRequest({ - method: 'POST', - urlPattern: [VIEW_REMOVE_JOB, viewId], - noparse: true - }, customParams, callback); + handleCrumbFlagAndRequest(customParams, function (params, headers) { + doRequest({ + method: 'POST', + urlPattern: [VIEW_REMOVE_JOB, viewName], + noparse: true, + request: { + headers: headers + } + }, params, 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], - bodyProp: 'jobs' - }, customParams, callback); + handleCrumbFlagAndRequest(customParams, function (params, headers) { + doRequest({ + urlPattern: [VIEW_INFO, viewName], + bodyProp: 'jobs', + request: { + headers: headers + } + }, params, callback); + }); }, /** ***********************************\ |* 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']); @@ -1163,43 +1594,63 @@ exports.init = function (host, defaultOptions, defaultParams) { customParams.depth = 1; - doRequest({ - urlPattern: [PLUGINS], - failureStatusCodes: [HTTP_CODE_302], - noparse: true, - bodyProp: 'plugins' - }, customParams, callback); + handleCrumbFlagAndRequest(customParams, function (params, headers) { + doRequest({ + urlPattern: [PLUGINS], + failureStatusCodes: [HTTP_CODE_302], + noparse: true, + bodyProp: 'plugins', + request: { + headers: headers + } + }, params, 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', - urlPattern: [INSTALL_PLUGIN], - request: { - body: body, - headers: { 'Content-Type': 'text/xml' } - }, - noparse: true, - bodyProp: 'plugins' - }, customParams, callback); + handleCrumbFlagAndRequest(customParams, function (params, headers) { + headers['Content-Type'] = 'text/xml'; + doRequest({ + method: 'POST', + urlPattern: [INSTALL_PLUGIN], + request: { + body: body, + headers: headers + }, + noparse: true, + bodyProp: 'plugins' + }, params, 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']); @@ -1217,10 +1668,15 @@ exports.init = function (host, defaultOptions, defaultParams) { customParams.mode = mode; customParams.Submit = 'OK'; - doRequest({ - method: 'POST', - urlPattern: [NEWFOLDER] - }, customParams, callback); + handleCrumbFlagAndRequest(customParams, function (params, headers) { + doRequest({ + method: 'POST', + urlPattern: [NEWFOLDER], + request: { + headers: headers + } + }, params, callback); + }); } }; }; \ No newline at end of file diff --git a/package.json b/package.json index d667406..ada0e46 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "jenkins-api", - "version": "0.3.1", + "version": "0.3.1-bugfix", "description": "Jenkins API written in Node.js", "author": "Shawn Jansepar ", "contributors": [ diff --git a/src/main.js b/src/main.js index 085dcf6..0852327 100644 --- a/src/main.js +++ b/src/main.js @@ -3,6 +3,11 @@ const util = require('util'); const qs = require('querystring'); const request = require('request'); +<<<<<<< HEAD +const url = require('url'); +======= +const url = require('url') +>>>>>>> b20f5de59d576a42b00d6feadcc95a38d6887984 const API = '/api/json'; const LIST = API; @@ -228,6 +233,75 @@ exports.init = function (host, defaultOptions, defaultParams) { return url; } + + /** + * 获取crumb + */ + function getCrumbData() { + var uri = url.parse(host) + var authPattern = uri.auth.split(":"); + var usr = authPattern[0]; + var psd = authPattern[1]; + + var romotePath = "/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,%22:%22,//crumb)"; + var path = uri.protocol + "//" + uri.host + romotePath; + + var requestOptions = Object.assign({ + method: "GET", + url: path, + headers: { + 'Authorization': 'basic ' + new Buffer(usr + ':' + psd).toString('base64'), + 'Accept-Encoding': 'identity', + } + }, {}); + + return new Promise(function (resolve, reject) { + request(requestOptions, function (error, response, body) { + if (response.statusCode != 200) { + reject(response.statusMessage); + return + } + + var res = {} + response.rawHeaders.forEach((value) => { + if (value && value.startsWith("JSESSIONID")) { + res.JSESSIONID = value.split(";")[0]; + } + }) + + if (response.body && response.body.startsWith("Jenkins-Crumb")) { + res.crumb = response.body.split(":")[1]; + } + // console.log(res); + resolve(res); + }) + }) + } + + /** + * jenkins校验,生成header数据,crumbFlag为true时获取crumb数据和session + * @param {*} customParams + * @param {*} func + */ + function handleCrumbFlagAndRequest(customParams, func) { + var headers = { 'Content-Type': 'application/xml;charset=UTF-8' }; + + if (customParams.crumbFlag === true) { + getCrumbData().then(function (crumbData) { + // console.log(crumbData); + headers.Cookie = crumbData.JSESSIONID; + customParams['Jenkins-Crumb'] = crumbData.crumb; + delete customParams.crumbFlag; + func(customParams, headers); + }) + .catch(function () { + console.log("get crumbData failed.") + }) + } else { + func(customParams, headers); + } + } + /** * Run the actual HTTP request. * @@ -315,24 +389,30 @@ exports.init = function (host, defaultOptions, defaultParams) { build: function (jobName, customParams, callback) { [jobName, customParams, callback] = doArgs(arguments, ['string', ['object', {}], 'function']); - doRequest({ - method: 'POST', - urlPattern: [BUILD_START, jobName], - successStatusCodes: [HTTP_CODE_201, HTTP_CODE_302], - noparse: true - }, customParams, function (error, data) { - if (error) { - callback(error, data); - return; - } - - const queueIdRe = /\/queue\/item\/(\d+)/; - const id = +queueIdRe.exec(data.location)[1]; - - data.queueId = id; + handleCrumbFlagAndRequest(customParams, function (params, headers) { + doRequest({ + method: 'POST', + urlPattern: [BUILD_START, jobName], + successStatusCodes: [HTTP_CODE_201, HTTP_CODE_302], + noparse: true, + request: { + headers + } + }, params, function (error, data) { + if (error) { + callback(error, data); + return; + } + + const queueIdRe = /\/queue\/item\/(\d+)/; + const id = +queueIdRe.exec(data.location)[1]; + + data.queueId = id; - callback(null, data); + callback(null, data); + }); }); + }, /** @@ -344,24 +424,30 @@ exports.init = function (host, defaultOptions, defaultParams) { build_with_params: function (jobName, customParams, callback) { [jobName, customParams, callback] = doArgs(arguments, ['string', ['object', {}], 'function']); - doRequest({ - method: 'POST', - urlPattern: [BUILD_START_WITHPARAMS, jobName], - successStatusCodes: [HTTP_CODE_201, HTTP_CODE_302], - noparse: true - }, customParams, function (error, data) { - if (error) { - callback(error, data); - return; - } - - const queueIdRe = /\/queue\/item\/(\d+)/; - const id = +queueIdRe.exec(data.location)[1]; - - data.queueId = id; + handleCrumbFlagAndRequest(customParams, function (params, headers) { + doRequest({ + method: 'POST', + urlPattern: [BUILD_START_WITHPARAMS, jobName], + successStatusCodes: [HTTP_CODE_201, HTTP_CODE_302], + noparse: true, + request: { + headers + } + }, params, function (error, data) { + if (error) { + callback(error, data); + return; + } + + const queueIdRe = /\/queue\/item\/(\d+)/; + const id = +queueIdRe.exec(data.location)[1]; + + data.queueId = id; - callback(null, data); + callback(null, data); + }); }); + }, /** @@ -374,20 +460,26 @@ exports.init = function (host, defaultOptions, defaultParams) { stop_build: function (jobName, buildNumber, customParams, callback) { [jobName, buildNumber, customParams, callback] = doArgs(arguments, ['string', 'string|number', ['object', {}], 'function']); - doRequest({ - method: 'POST', - urlPattern: [BUILD_STOP, jobName, buildNumber], - noparse: true - }, customParams, function (error, data) { - if (error) { - callback(error, data); - return; - } - - data.body = `Build ${buildNumber} stopped.`; + handleCrumbFlagAndRequest(customParams, function (params, headers) { + doRequest({ + method: 'POST', + urlPattern: [BUILD_STOP, jobName, buildNumber], + noparse: true, + request: { + headers + } + }, params, function (error, data) { + if (error) { + callback(error, data); + return; + } + + data.body = `Build ${buildNumber} stopped.`; - callback(null, data); + callback(null, data); + }); }); + }, /** @@ -401,10 +493,16 @@ exports.init = function (host, defaultOptions, defaultParams) { console_output: function (jobName, buildNumber, customParams, callback) { [jobName, buildNumber, customParams, callback] = doArgs(arguments, ['string', 'string|number', ['object', {}], 'function']); - doRequest({ - urlPattern: [JOB_OUTPUT, jobName, buildNumber], - noparse: true - }, customParams, callback); + handleCrumbFlagAndRequest(customParams, function (params, headers) { + doRequest({ + urlPattern: [JOB_OUTPUT, jobName, buildNumber], + noparse: true, + request: { + headers + } + }, params, callback); + }); + }, /** @@ -417,9 +515,15 @@ exports.init = function (host, defaultOptions, defaultParams) { last_build_info: function (jobName, customParams, callback) { [jobName, customParams, callback] = doArgs(arguments, ['string', ['object', {}], 'function']); - doRequest({ - urlPattern: [LAST_BUILD, jobName] - }, customParams, callback); + handleCrumbFlagAndRequest(customParams, function (params, headers) { + doRequest({ + urlPattern: [LAST_BUILD, jobName], + request: { + headers + } + }, params, callback); + }); + }, /** @@ -432,9 +536,15 @@ exports.init = function (host, defaultOptions, defaultParams) { last_completed_build_info: function (jobName, customParams, callback) { [jobName, customParams, callback] = doArgs(arguments, ['string', ['object', {}], 'function']); - doRequest({ - urlPattern: [LAST_COMPLETED_BUILD, jobName] - }, customParams, callback); + handleCrumbFlagAndRequest(customParams, function (params, headers) { + doRequest({ + urlPattern: [LAST_COMPLETED_BUILD, jobName], + request: { + headers + } + }, params, callback); + }); + }, /** @@ -448,9 +558,15 @@ exports.init = function (host, defaultOptions, defaultParams) { build_info: function (jobName, buildNumber, customParams, callback) { [jobName, buildNumber, customParams, callback] = doArgs(arguments, ['string', 'string|number', ['object', {}], 'function']); - doRequest({ - urlPattern: [BUILD_INFO, jobName, buildNumber] - }, customParams, callback); + handleCrumbFlagAndRequest(customParams, function (params, headers) { + doRequest({ + urlPattern: [BUILD_INFO, jobName, buildNumber], + request: { + headers + } + }, params, callback); + }); + }, /** @@ -465,10 +581,16 @@ exports.init = function (host, defaultOptions, defaultParams) { [jobName, param, customParams, callback] = doArgs(arguments, ['string', ['string', 'id,timestamp,result,duration'], ['object', {}], 'function']); // TODO better name and handle the "param" ??? - doRequest({ - urlPattern: [ALL_BUILDS, jobName, param], - bodyProp: 'allBuilds' - }, customParams, callback); + handleCrumbFlagAndRequest(customParams, function (params, headers) { + doRequest({ + urlPattern: [ALL_BUILDS, jobName, param], + bodyProp: 'allBuilds', + request: { + headers + } + }, params, callback); + }); + }, /** @@ -482,9 +604,15 @@ exports.init = function (host, defaultOptions, defaultParams) { test_result: function (jobName, buildNumber, customParams, callback) { [jobName, buildNumber, customParams, callback] = doArgs(arguments, ['string', 'string|number', ['object', {}], 'function']); - doRequest({ - urlPattern: [TEST_REPORT, jobName, buildNumber] - }, customParams, callback); + handleCrumbFlagAndRequest(customParams, function (params, headers) { + doRequest({ + urlPattern: [TEST_REPORT, jobName, buildNumber], + request: { + headers + } + }, params, callback); + }); + }, /** @@ -514,18 +642,24 @@ exports.init = function (host, defaultOptions, defaultParams) { delete_build: function (jobName, buildNumber, customParams, callback) { [jobName, buildNumber, customParams, callback] = doArgs(arguments, ['string', 'string|number', ['object', {}], 'function']); - doRequest({ - method: 'POST', - urlPattern: [BUILD_DELETE, jobName, buildNumber], - noparse: true - }, customParams, function (error, data) { - if (error) { - callback(error, data); - } else { - data.body = `Build ${buildNumber} deleted.`; - callback(null, data); - } + handleCrumbFlagAndRequest(customParams, function (params, headers) { + doRequest({ + method: 'POST', + urlPattern: [BUILD_DELETE, jobName, buildNumber], + noparse: true, + request: { + headers + } + }, params, function (error, data) { + if (error) { + callback(error, data); + } else { + data.body = `Build ${buildNumber} deleted.`; + callback(null, data); + } + }); }); + }, /** ***********************************\ @@ -541,10 +675,16 @@ exports.init = function (host, defaultOptions, defaultParams) { all_jobs: function (customParams, callback) { [customParams, callback] = doArgs(arguments, [['object', {}], 'function']); - doRequest({ - urlPattern: [JOB_LIST], - bodyProp: 'jobs' - }, customParams, callback); + handleCrumbFlagAndRequest(customParams, function (params, headers) { + doRequest({ + urlPattern: [JOB_LIST], + bodyProp: 'jobs', + request: { + headers + } + }, params, callback); + }); + }, /** @@ -557,17 +697,23 @@ exports.init = function (host, defaultOptions, defaultParams) { get_config_xml: function (jobName, customParams, callback) { [jobName, customParams, callback] = doArgs(arguments, ['string', ['object', {}], 'function']); - doRequest({ - urlPattern: [JOB_CONFIG, jobName], - noparse: true - }, customParams, function (error, data) { - // Get only the XML response body - if (error) { - callback(error, data); - } else { - callback(null, data.body); - } + handleCrumbFlagAndRequest(customParams, function (params, headers) { + doRequest({ + urlPattern: [JOB_CONFIG, jobName], + noparse: true, + request: { + headers + } + }, params, function (error, data) { + // Get only the XML response body + if (error) { + callback(error, data); + } else { + callback(null, data.body); + } + }); }); + }, /** @@ -607,23 +753,26 @@ exports.init = function (host, defaultOptions, defaultParams) { update_job: function (jobName, jobConfig, customParams, callback) { [jobName, jobConfig, customParams, callback] = doArgs(arguments, ['string', 'string', ['object', {}], 'function']); - doRequest({ - method: 'POST', - urlPattern: [JOB_CONFIG, jobName], - request: { - body: jobConfig, - headers: { 'Content-Type': 'application/xml' } - }, - noparse: true - }, customParams, function (error, data) { - if (error) { - callback(error, data); - return; - } - // TODO rather return job_info ??? - // const data = {name: jobName, location: response.headers['Location'] || response.headers['location']}; - callback(null, { name: jobName }); + handleCrumbFlagAndRequest(customParams, function (params, headers) { + doRequest({ + method: 'POST', + urlPattern: [JOB_CONFIG, jobName], + request: { + body: jobConfig, + headers + }, + noparse: true + }, params, function (error, data) { + if (error) { + callback(error, data); + return; + } + // TODO rather return job_info ??? + // const data = {name: jobName, location: response.headers['Location'] || response.headers['location']}; + callback(null, { name: jobName }); + }); }); + }, /** @@ -636,9 +785,15 @@ exports.init = function (host, defaultOptions, defaultParams) { job_info: function (jobName, customParams, callback) { [jobName, customParams, callback] = doArgs(arguments, ['string', ['object', {}], 'function']); - doRequest({ - urlPattern: [JOB_INFO, jobName] - }, customParams, callback); + handleCrumbFlagAndRequest(customParams, function (params, headers) { + doRequest({ + urlPattern: [JOB_INFO, jobName], + request: { + headers + } + }, params, callback); + }); + }, /** @@ -657,21 +812,24 @@ exports.init = function (host, defaultOptions, defaultParams) { const self = this; - doRequest({ - method: 'POST', - urlPattern: [JOB_CREATE], - request: { - body: jobConfig, - headers: { 'Content-Type': 'application/xml' } - }, - noparse: true - }, customParams, function (error, data) { - if (error) { - callback(error, data); - return; - } - self.job_info(jobName, customParams, callback); + handleCrumbFlagAndRequest(customParams, function (params, headers) { + doRequest({ + method: 'POST', + urlPattern: [JOB_CREATE], + request: { + body: jobConfig, + headers + }, + noparse: true + }, params, function (error, data) { + if (error) { + callback(error, data); + return; + } + self.job_info(jobName, customParams, callback); + }); }); + }, /** @@ -712,17 +870,23 @@ exports.init = function (host, defaultOptions, defaultParams) { delete_job: function (jobName, customParams, callback) { [jobName, customParams, callback] = doArgs(arguments, ['string', ['object', {}], 'function']); - doRequest({ - method: 'POST', - urlPattern: [JOB_DELETE, jobName], - noparse: true - }, customParams, function (error, data) { - if (error) { - callback(error, data); - return; - } - callback(null, { name: jobName }); + handleCrumbFlagAndRequest(customParams, function (params, headers) { + doRequest({ + method: 'POST', + urlPattern: [JOB_DELETE, jobName], + noparse: true, + request: { + headers + } + }, params, function (error, data) { + if (error) { + callback(error, data); + return; + } + callback(null, { name: jobName }); + }); }); + }, /** @@ -737,17 +901,23 @@ exports.init = function (host, defaultOptions, defaultParams) { const self = this; - doRequest({ - method: 'POST', - urlPattern: [JOB_DISABLE, jobName], - noparse: true - }, customParams, function (error, data) { - if (error) { - callback(error, data); - return; - } - self.job_info(jobName, customParams, callback); + handleCrumbFlagAndRequest(customParams, function (params, headers) { + doRequest({ + method: 'POST', + urlPattern: [JOB_DISABLE, jobName], + noparse: true, + request: { + headers + } + }, params, function (error, data) { + if (error) { + callback(error, data); + return; + } + self.job_info(jobName, customParams, callback); + }); }); + }, /** @@ -762,17 +932,23 @@ exports.init = function (host, defaultOptions, defaultParams) { const self = this; - doRequest({ - method: 'POST', - urlPattern: [JOB_ENABLE, jobName], - noparse: true - }, customParams, function (error, data) { - if (error) { - callback(error, data); - return; - } - self.job_info(jobName, customParams, callback); + handleCrumbFlagAndRequest(customParams, function (params, headers) { + doRequest({ + method: 'POST', + urlPattern: [JOB_ENABLE, jobName], + noparse: true, + request: { + headers + } + }, params, function (error, data) { + if (error) { + callback(error, data); + return; + } + self.job_info(jobName, customParams, callback); + }); }); + }, /** @@ -785,10 +961,16 @@ exports.init = function (host, defaultOptions, defaultParams) { last_success: function (jobName, customParams, callback) { [jobName, customParams, callback] = doArgs(arguments, ['string', ['object', {}], 'function']); - doRequest({ - method: 'POST', - urlPattern: [LAST_SUCCESS, jobName] - }, customParams, callback); + handleCrumbFlagAndRequest(customParams, function (params, headers) { + doRequest({ + method: 'POST', + urlPattern: [LAST_SUCCESS, jobName], + request: { + headers + } + }, params, callback); + }); + }, /** @@ -809,9 +991,15 @@ exports.init = function (host, defaultOptions, defaultParams) { const lastResultUrl = data.lastBuild.url; - doRequest({ - urlPattern: [lastResultUrl + API, jobName] - }, customParams, callback); + handleCrumbFlagAndRequest(customParams, function (params, headers) { + doRequest({ + urlPattern: [lastResultUrl + API, jobName], + request: { + headers + } + }, params, callback); + }); + }); }, @@ -828,9 +1016,15 @@ exports.init = function (host, defaultOptions, defaultParams) { queue: function (customParams, callback) { [customParams, callback] = doArgs(arguments, [['object', {}], 'function']); - doRequest({ - urlPattern: [QUEUE] - }, customParams, callback); + handleCrumbFlagAndRequest(customParams, function (params, headers) { + doRequest({ + urlPattern: [QUEUE], + request: { + headers + } + }, params, callback); + }); + }, /** @@ -843,9 +1037,15 @@ 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); + handleCrumbFlagAndRequest(customParams, function (params, headers) { + doRequest({ + urlPattern: [QUEUE_ITEM, queueNumber], + request: { + headers + } + }, params, callback); + }); + }, /** @@ -860,10 +1060,16 @@ exports.init = function (host, defaultOptions, defaultParams) { customParams.id = itemId; - doRequest({ - method: 'POST', - urlPattern: [QUEUE_CANCEL_ITEM] - }, customParams, callback); + handleCrumbFlagAndRequest(customParams, function (params, headers) { + doRequest({ + method: 'POST', + urlPattern: [QUEUE_CANCEL_ITEM], + request: { + headers + } + }, params, callback); + }); + }, /** ***********************************\ @@ -879,9 +1085,15 @@ exports.init = function (host, defaultOptions, defaultParams) { computers: function (customParams, callback) { [customParams, callback] = doArgs(arguments, [['object', {}], 'function']); - doRequest({ - urlPattern: [COMPUTERS] - }, customParams, callback); + handleCrumbFlagAndRequest(customParams, function (params, headers) { + doRequest({ + urlPattern: [COMPUTERS], + request: { + headers + } + }, params, callback); + }); + }, /** ***********************************\ @@ -897,10 +1109,16 @@ exports.init = function (host, defaultOptions, defaultParams) { all_views: function (customParams, callback) { [customParams, callback] = doArgs(arguments, [['object', {}], 'function']); - doRequest({ - urlPattern: [VIEW_LIST], - bodyProp: 'views' - }, customParams, callback); + handleCrumbFlagAndRequest(customParams, function (params, headers) { + doRequest({ + urlPattern: [VIEW_LIST], + bodyProp: 'views', + request: { + headers + } + }, params, callback); + }); + }, /** @@ -919,20 +1137,24 @@ exports.init = function (host, defaultOptions, defaultParams) { const self = this; - doRequest({ - method: 'POST', - urlPattern: [VIEW_CREATE], - request: { - form: formData - }, - noparse: true - }, customParams, function (error, data) { - if (error) { - callback(error, data); - return; - } - self.view_info(viewName, customParams, callback); + handleCrumbFlagAndRequest(customParams, function (params, headers) { + doRequest({ + method: 'POST', + urlPattern: [VIEW_CREATE], + request: { + form: formData, + headers + }, + noparse: true + }, params, function (error, data) { + if (error) { + callback(error, data); + return; + } + self.view_info(viewName, customParams, callback); + }); }); + }, /** @@ -943,9 +1165,15 @@ exports.init = function (host, defaultOptions, defaultParams) { view_info: function (viewName, customParams, callback) { [viewName, customParams, callback] = doArgs(arguments, ['string', ['object', {}], 'function']); - doRequest({ - urlPattern: [VIEW_INFO, viewName] - }, customParams, callback); + handleCrumbFlagAndRequest(customParams, function (params, headers) { + doRequest({ + urlPattern: [VIEW_INFO, viewName], + request: { + headers + } + }, params, callback); + }); + }, /** @@ -963,21 +1191,24 @@ exports.init = function (host, defaultOptions, defaultParams) { const self = this; - doRequest({ - method: 'POST', - urlPattern: [VIEW_CONFIG, viewName], - request: { - form: viewConfig - // headers: {'content-type': 'application/x-www-form-urlencoded'}, - }, - noparse: true - }, customParams, function (error, data) { - if (error) { - callback(error, data); - return; - } - self.view_info(viewName, customParams, callback); + handleCrumbFlagAndRequest(customParams, function (params, headers) { + doRequest({ + method: 'POST', + urlPattern: [VIEW_CONFIG, viewName], + request: { + form: viewConfig, + headers + }, + noparse: true + }, params, function (error, data) { + if (error) { + callback(error, data); + return; + } + self.view_info(viewName, customParams, callback); + }); }); + }, /** @@ -988,17 +1219,23 @@ exports.init = function (host, defaultOptions, defaultParams) { delete_view: function (viewName, customParams, callback) { [viewName, customParams, callback] = doArgs(arguments, ['string', ['object', {}], 'function']); - doRequest({ - method: 'POST', - urlPattern: [VIEW_DELETE, viewName], - noparse: true - }, customParams, function (error, data) { - if (error) { - callback(error, data); - return; - } - callback(null, { name: viewName }); + handleCrumbFlagAndRequest(customParams, function (params, headers) { + doRequest({ + method: 'POST', + urlPattern: [VIEW_DELETE, viewName], + noparse: true, + request: { + headers + } + }, params, function (error, data) { + if (error) { + callback(error, data); + return; + } + callback(null, { name: viewName }); + }); }); + }, /** @@ -1012,11 +1249,17 @@ exports.init = function (host, defaultOptions, defaultParams) { customParams.name = jobName; - doRequest({ - method: 'POST', - urlPattern: [VIEW_ADD_JOB, viewName], - noparse: true - }, customParams, callback); + handleCrumbFlagAndRequest(customParams, function (params, headers) { + doRequest({ + method: 'POST', + urlPattern: [VIEW_ADD_JOB, viewName], + noparse: true, + request: { + headers + } + }, params, callback); + }); + }, /** @@ -1030,11 +1273,17 @@ exports.init = function (host, defaultOptions, defaultParams) { customParams.name = jobName; - doRequest({ - method: 'POST', - urlPattern: [VIEW_REMOVE_JOB, viewName], - noparse: true - }, customParams, callback); + handleCrumbFlagAndRequest(customParams, function (params, headers) { + doRequest({ + method: 'POST', + urlPattern: [VIEW_REMOVE_JOB, viewName], + noparse: true, + request: { + headers + } + }, params, callback); + }); + }, /** @@ -1047,10 +1296,16 @@ exports.init = function (host, defaultOptions, defaultParams) { all_jobs_in_view: function (viewName, customParams, callback) { [viewName, customParams, callback] = doArgs(arguments, ['string', ['object', {}], 'function']); - doRequest({ - urlPattern: [VIEW_INFO, viewName], - bodyProp: 'jobs' - }, customParams, callback); + handleCrumbFlagAndRequest(customParams, function (params, headers) { + doRequest({ + urlPattern: [VIEW_INFO, viewName], + bodyProp: 'jobs', + request: { + headers + } + }, params, callback); + }); + }, /** ***********************************\ @@ -1068,12 +1323,18 @@ exports.init = function (host, defaultOptions, defaultParams) { customParams.depth = 1; - doRequest({ - urlPattern: [PLUGINS], - failureStatusCodes: [HTTP_CODE_302], - noparse: true, - bodyProp: 'plugins' - }, customParams, callback); + handleCrumbFlagAndRequest(customParams, function (params, headers) { + doRequest({ + urlPattern: [PLUGINS], + failureStatusCodes: [HTTP_CODE_302], + noparse: true, + bodyProp: 'plugins', + request: { + headers + } + }, params, callback); + }); + }, /** @@ -1088,16 +1349,20 @@ exports.init = function (host, defaultOptions, defaultParams) { const body = ``; - doRequest({ - method: 'POST', - urlPattern: [INSTALL_PLUGIN], - request: { - body: body, - headers: { 'Content-Type': 'text/xml' } - }, - noparse: true, - bodyProp: 'plugins' - }, customParams, callback); + handleCrumbFlagAndRequest(customParams, function (params, headers) { + headers['Content-Type'] = 'text/xml'; + doRequest({ + method: 'POST', + urlPattern: [INSTALL_PLUGIN], + request: { + body: body, + headers + }, + noparse: true, + bodyProp: 'plugins' + }, params, callback); + }); + }, /** @@ -1120,10 +1385,16 @@ exports.init = function (host, defaultOptions, defaultParams) { customParams.mode = mode; customParams.Submit = 'OK'; - doRequest({ - method: 'POST', - urlPattern: [NEWFOLDER] - }, customParams, callback); + handleCrumbFlagAndRequest(customParams, function (params, headers) { + doRequest({ + method: 'POST', + urlPattern: [NEWFOLDER], + request: { + headers + } + }, params, callback); + }); + } }; };