Browse files

Initial commit of Jenkins Api! 0.1 release =)

  • Loading branch information...
0 parents commit 331fdfefb6a3f053fa17588ecdebb2cce3af49e8 @jansepar committed Dec 23, 2011
Showing with 288 additions and 0 deletions.
  1. +288 −0 jenkinsapi.js
288 jenkinsapi.js
@@ -0,0 +1,288 @@
+var util = require('util')
+ ,qs = require('querystring')
+ ,request = require('request');
+
+API = '/api/json';
+NEWJOB = '%s/createItem/?name=%s';
+DELETE = '%s/job/%s/doDelete';
+BUILD = '%s/job/%s/build';
+BUILDWITHPARAMS = '%s/job/%s/buildWithParameters';
+CONFIG = '%s/job/%s/config.xml';
+JOBINFO = '%s/job/%s' + API;
+LIST = '%s' + API;
+LAST_SUCCESS = '%s/job/%s/lastSuccessfulBuild' + API;
+TEST_REPORT = '%s/job/%s/lastSuccessfulBuild/testReport' + API;
+LAST_BUILD = '%s/job/%s/lastBuild' + API;
+LAST_REPORT = '%s/job/%s/lastBuild/testReport' + API;
+
+
+
+var init = exports.init = function(host) {
+
+ //Jenkin variables
+ var host = host;
+
+ //Helper Functions
+ var build_url = function(command) {
+ /*
+ Builds REST url to Jenkins
+ */
+
+ // Create the url using the format function
+ var url = util.format.call(this, command, host);
+
+ // if command is the only arg, we are done
+ if (arguments.length == 1) return url;
+
+ // Grab the arguments except for the first (command)
+ var args = Array.prototype.slice.call(arguments).slice(1);
+
+ // Append url to front of args array
+ args.unshift(url);
+
+ // Create full url with all the arguments
+ url = util.format.apply(this, args);
+
+ return url;
+
+
+ }
+ return {
+ build: function(jobname, callback, params) {
+ /*
+ Trigger Jenkins to build.
+ */
+ if (!params) {
+ request({method: 'POST', url: build_url(BUILD, jobname)}, function(error, response, body) {
+ });
+ }else {
+ request({method: 'POST', body: qs.stringify(params),url: build_url(BUILDWITHPARAMS, jobname)}, function(error, response, body) {
+ });
+ }
+
+ },
+ all_jobs: function(callback) {
+ /*
+ Return a list of object literals containing the name and color of all jobs on the Jenkins server
+ */
+ request({method: 'GET', url: build_url(LIST)}, function(error, response, body) {
+ if (response.statusCode != 200 || error){
+ callback(error || true, response);
+ return;
+ }
+ data = JSON.parse(body.toString()).jobs;
+ callback(null, data);
+ });
+ },
+ job_info: function(jobname, callback) {
+ /*
+ Get all information for a job
+ */
+ request({method: 'GET', url: build_url(JOBINFO, jobname)}, function(error, response, body) {
+ if (response.statusCode != 200 || error){
+ callback(error || true, response);
+ return;
+ }
+ data = JSON.parse(body.toString());
+ callback(null, data);
+ });
+ },
+ last_build_info: function(jobname, callback) {
+ /*
+ Get information for the last build of a job
+ */
+ request({method: 'GET', url: build_url(LAST_BUILD, jobname)}, function(error, response, body) {
+ if (response.statusCode != 200 || error){
+ callback(error || true, response);
+ return;
+ }
+ data = JSON.parse(body.toString());
+ callback(null, data);
+ });
+ },
+ last_build_report: function(jobname, callback) {
+ /*
+ Get the last build report for a job
+ */
+ request({method: 'GET', url: build_url(LAST_REPORT, jobname)}, function(error, response, body) {
+ if (response.statusCode != 200 || error){
+ callback(error || true, response);
+ return;
+ }
+ data = JSON.parse(body.toString());
+ callback(null, data);
+ });
+ },
+ get_config_xml: function(jobname, callback) {
+ /*
+ Get the config xml for a job
+ */
+ request({method: 'GET', url: build_url(CONFIG, jobname)}, function(error, response, body) {
+ if (response.statusCode != 200 || error){
+ callback(error || true, response);
+ return;
+ }
+ data = body;
+ callback(null, data);
+ });
+ },
+ create_job: function(jobname, job_config, callback) {
+ /*
+ Create a new job based on a job_config string
+ */
+
+ console.log(build_url(NEWJOB, jobname));
+ request(
+ {method: 'POST'
+ ,url: build_url(NEWJOB, jobname)
+ ,body: job_config
+ ,headers: { "content-type": "application/xml"}
+ },
+
+ function(error, response, body) {
+ if (response.statusCode != 200 || error){
+ callback(error || true, response);
+ return;
+ }
+ data = body;
+ callback(null, data);
+ }
+ );
+ },
+ copy_job: function(jobname, new_job, modifyfunction, callback) {
+ /*
+ Copies a job and allows you to pass in a function to modify the configuration
+ of the job you would like to copy
+ */
+
+ var self = this;
+ self.get_config_xml(jobname, function(error, data) {
+ if (error) {
+ callback(error, data);
+ return;
+ }
+ self.create_job(new_job, modifyfunction(data), function(error, data) {
+ if (error) {
+ callback(error, data);
+ return;
+ }
+ callback(null, data);
+ });
+ });
+
+ },
+ delete_job: function(jobname, callback) {
+ /*
+ Deletes a job
+ */
+ request({method: 'POST', url: build_url(DELETE, jobname)}, function(error, response, body) {
+ if (response.statusCode == 404 || error){
+ callback(error || true, response);
+ return;
+ }
+ callback(null, body);
+ });
+
+ },
+ last_success: function(jobname, callback) {
+ /*
+ Get the last build report for a job
+ */
+ request({method: 'POST', url: build_url(LAST_SUCCESS, jobname)}, function(error, response, body) {
+ if (response.statusCode != 200 || error){
+ callback(error || true, response);
+ return;
+ }
+ data = JSON.parse(body);
+ callback(null, data);
+ });
+
+ },
+ last_result: function(jobname, callback) {
+ /*
+ Get the last result for a job
+ */
+ self = this;
+ self.job_info(jobname, function(error, data) {
+ last_result_url = data['lastBuild']['url'];
+
+ request({method: 'GET', url: build_url(last_result_url + API, jobname)}, function(error, response, body) {
+ if (response.statusCode != 200 || error){
+ callback(error || true, response);
+ return;
+ }
+ data = JSON.parse(body);
+ callback(null, data);
+ });
+ });
+
+ },
+
+
+
+
+ }
+
+}
+
+if (!module.parent) {
+ var jenkins = init("http://github:thisismadness@jenkins.mobify.com");
+ /*
+ jenkins.build('crush-development', function(error, data) {
+ if (error) {
+ console.log(error);
+ }
+ console.log(data);
+ });
+ */
+ //jenkins.all_jobs(function(error, data) { console.log(data)});
+ /*
+ jenkins.job_info('crush-madness', function(error, data) {
+ if(!error) {
+ console.log(data);
+ }
+
+ });
+ */
+ //jenkins.last_build_info('crush-development', function(error, data) { console.log(data); });
+ //jenkins.last_build_report('crush-development', function(error, data) { console.log(data); });
+
+ /*
+ jenkins.get_config_xml('crush-development', function(error, data) {
+ //console.log(data);
+ jenkins.create_job('crush-copy', data, function(error, data) {
+ //console.log(data);
+ });
+ });
+ */
+ /*
+ jenkins.copy_job('crush-development'
+ ,'crush-new'
+ ,function(data) {
+ return data.replace('development','feature-branch');
+ }
+ ,function(error, data) {
+ //console.log(data);
+ jenkins.delete_job('crush-new', function(error, data) {
+ if(error) {
+ console.log("error!");
+ }
+ console.log(data);
+ });
+ });
+ */
+ /*
+ jenkins.delete_job('crush-new', function(error, data) {
+ if(error) {
+ console.log("error!");
+ console.log(data.body, data.statusCode);
+ }
+ });
+ */
+ jenkins.last_result('crush-development', function(error, data) {
+ console.log(data);
+ });
+ jenkins.last_success('crush-development', function(error, data) {
+ console.log(data);
+ });
+}

0 comments on commit 331fdfe

Please sign in to comment.