Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add create, copy, and delete commands #25 .

  • Loading branch information...
commit 12265b17c4f5f3e5ff08f3f264ea6dd3b599854c 1 parent 8303a09
Cliffano Subagio authored
1  CHANGELOG.md
View
@@ -2,6 +2,7 @@
* Change test lib to buster-node + referee
* Set min node engine to >= v0.8.0
* Add blink on failure flag to buildlight command #23
+* Add create, copy, and delete commands #25
### 0.2.1
* BuildLight notifier blinks red on build failure
12 README.md
View
@@ -63,6 +63,18 @@ View job status reports:
nestor job <job>
+Create a new job with a specified config.xml:
+
+ nestor create path/to/config.xml
+
+Copy an existing job1 to a new job2:
+
+ nestor copy job1 job2
+
+Delete an existing job:
+
+ nestor delete job
+
View queued jobs:
nestor queue
29 conf/commands.json
View
@@ -56,6 +56,35 @@
{ "name": "job", "rules": [ "notEmpty" ] }
]
},
+ "create": {
+ "desc": "Create a new job with a specified config.xml",
+ "args": [
+ { "name": "job", "rules": [ "notEmpty" ] },
+ { "name": "config", "rules": [ "notEmpty" ] }
+ ],
+ "examples": [
+ "nestor create job path/to/config.xml"
+ ]
+ },
+ "copy": {
+ "desc": "Copy an existing job",
+ "args": [
+ { "name": "job1", "rules": [ "notEmpty" ] },
+ { "name": "job2", "rules": [ "notEmpty" ] }
+ ],
+ "examples": [
+ "nestor copy job1 job2"
+ ]
+ },
+ "delete": {
+ "desc": "Delete an existing job",
+ "args": [
+ { "name": "job", "rules": [ "notEmpty" ] }
+ ],
+ "examples": [
+ "nestor delete job"
+ ]
+ },
"queue": {
"desc": "View queued jobs"
},
30 lib/cli.js
View
@@ -170,6 +170,33 @@ function _job(name, args) {
__exec(args, execCb);
}
+function _create(job, configFile, args) {
+ function execCb(jenkins) {
+ jenkins.create(job, configFile, cli.exitCb(null, function (result) {
+ console.log(text.__('Job %s was created successfully'), job);
+ }));
+ }
+ __exec(args, execCb);
+}
+
+function _copy(sourceJob, newJob, args) {
+ function execCb(jenkins) {
+ jenkins.copy(sourceJob, newJob, cli.exitCb(null, function (result) {
+ console.log(text.__('Job %s was copied to job %s'), sourceJob, newJob);
+ }));
+ }
+ __exec(args, execCb);
+}
+
+function _delete(job, args) {
+ function execCb(jenkins) {
+ jenkins.delete(job, cli.exitCb(null, function (result) {
+ console.log(text.__('Job %s was deleted successfully'), job);
+ }));
+ }
+ __exec(args, execCb);
+}
+
function _queue(args) {
function execCb(jenkins) {
jenkins.queue(cli.exitCb(null, function (result) {
@@ -264,6 +291,9 @@ function exec() {
discover: { action: _discover },
executor: { action: _executor },
job: { action: _job },
+ create: { action: _create },
+ copy: { action: _copy },
+ 'delete': { action: _delete },
queue: { action: _queue },
ver: { action: _version },
irc: { action: _irc },
66 lib/jenkins.js
View
@@ -5,6 +5,7 @@ var _ = require('lodash'),
cron = require('cron'),
dgram = require('dgram'),
feedparser = require('feedparser'),
+ fs = require('fs'),
p = require('path'),
req = require('bagofrequest'),
request = require('request'),
@@ -376,6 +377,71 @@ Jenkins.prototype.job = function (name, cb) {
req.request('get', this.url + '/job/' + name + '/api/json', this.opts, cb);
};
+Jenkins.prototype.create = function (jobName, configFile, cb) {
+
+ function _success(result, cb) {
+ cb();
+ }
+
+ // TODO: investigate how to get JSON response instead
+ function _error(result, cb) {
+ var message = result.body
+ .match(/<h1>Error<\/h1>.+<\/p>/).toString()
+ .replace(/<h1>Error<\/h1>/, '')
+ .replace(/<\/?p>/g, '');
+ cb(new Error(message));
+ }
+
+ this.opts.queryStrings = { name: jobName };
+ this.opts.headers = { 'content-type': 'application/xml' };
+ this.opts.body = fs.readFileSync(configFile).toString();
+
+ this.opts.handlers[200] = _success;
+ this.opts.handlers[400] = _error;
+
+ req.request('post', this.url + '/createItem/api/json', this.opts, cb);
+};
+
+Jenkins.prototype.copy = function (sourceJobName, newJobName, cb) {
+
+ function _success(result, cb) {
+ cb();
+ }
+
+ // TODO: investigate how to get JSON response instead
+ function _error(result, cb) {
+ var message = result.body
+ .match(/<h1>Error<\/h1>.+<\/p>/).toString()
+ .replace(/<h1>Error<\/h1>/, '')
+ .replace(/<\/?p>/g, '');
+ cb(new Error(message));
+ }
+
+ this.opts.queryStrings = { name: newJobName, mode: 'copy', from: sourceJobName };
+ this.opts.headers = { 'content-type': 'text/plain' };
+
+ this.opts.handlers[200] = _success;
+ this.opts.handlers[400] = _error;
+
+ req.request('post', this.url + '/createItem', this.opts, cb);
+};
+
+Jenkins.prototype.delete = function (jobName, cb) {
+
+ function _success(result, cb) {
+ cb();
+ }
+
+ function _notFound(result, cb) {
+ cb(new Error(text.__('Job %s does not exist', jobName)));
+ }
+
+ this.opts.handlers[200] = _success;
+ this.opts.handlers[404] = _notFound;
+
+ req.request('post', this.url + '/job/' + jobName + '/doDelete', this.opts, cb);
+};
+
/**
* Retrieve jobs in the queue waiting for available executor or
* for a previously running build of the same job to finish.
Please sign in to comment.
Something went wrong with that request. Please try again.