Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Move console command chaining from jenkins to cli.

  • Loading branch information...
commit be77381fcbec7110db398ccf75bf1d859ff82bb8 1 parent 421f237
@cliffano authored
Showing with 76 additions and 9 deletions.
  1. +4 −0 README.md
  2. +31 −5 lib/cli.js
  3. +41 −4 test/cli.js
View
4 README.md
@@ -21,6 +21,10 @@ Trigger a parameterised build:
nestor build <jobname> ["param1=value1&param2=value2"]
+Trigger a build followed by console output:
+
+ nestor build --console <jobname>
+
Display latest build console output:
nestor console <jobname>
View
36 lib/cli.js
@@ -12,19 +12,42 @@ function exec() {
proxy = process.env.http_proxy;
function _build() {
- return function (jobName, params) {
+ return function (jobName, params, args) {
+ if (!args) {
+ args = params || {};
+ }
+
+ var message = 'Job %s was started successfully',
+ cb;
+
+ if (args.console) {
+ cb = function (err, result) {
+ if (err) {
+ bag.cli.exit(err, result);
+ } else {
+ console.log(message, jobName);
+ // wait for pending period
+ setTimeout(function () {
+ _console()(jobName);
+ }, 5000);
+ }
+ };
+ } else {
+ cb = bag.cli.exitCb(null, function (result) {
+ console.log(message, jobName);
+ });
+ }
+
new Jenkins(url, proxy).build(
jobName,
(_.isString(params)) ? params : undefined,
- bag.cli.exitCb(null, function (result) {
- console.log('Job %s was started successfully', jobName);
- })
+ cb
);
};
}
function _console() {
- return function (jobName, params) {
+ return function (jobName) {
new Jenkins(url, proxy).console(
jobName,
bag.cli.exit
@@ -124,6 +147,9 @@ function exec() {
var commands = {
build: {
desc: 'Trigger a build with optional parameters\n\tnestor build <jobname> ["param1=value1&param2=value2"]',
+ options: [
+ { arg: '-c, --console', desc: 'Display console ouptput of the triggered build progress' }
+ ],
action: _build()
},
console: {
View
45 test/cli.js
@@ -52,11 +52,11 @@ describe('cli', function () {
_cb(cb);
},
console: function (jobName, cb) {
- checks.build_jobName = jobName;
+ checks.console_jobName = jobName;
cb(mocks.jenkins_action_err, mocks.jenkins_action_result);
},
stop: function (jobName, cb) {
- checks.build_jobName = jobName;
+ checks.stop_jobName = jobName;
_cb(cb);
},
dashboard: _cb,
@@ -76,7 +76,11 @@ describe('cli', function () {
},
globals: {
console: bag.mock.console(checks),
- process: bag.mock.process(checks, mocks)
+ process: bag.mock.process(checks, mocks),
+ setTimeout: function (cb, timeout) {
+ should.exist(timeout);
+ cb();
+ }
},
locals: {
__dirname: '/somedir/nestor/lib'
@@ -109,7 +113,11 @@ describe('cli', function () {
cli = create(checks, mocks);
cli.exec();
checks.bag_parse_commands.build.desc.should.equal('Trigger a build with optional parameters\n\tnestor build <jobname> ["param1=value1&param2=value2"]');
+ checks.bag_parse_commands.build.options[0].arg.should.equal('-c, --console');
+ checks.bag_parse_commands.build.options[0].desc.should.equal('Display console ouptput of the triggered build progress');
checks.bag_parse_commands.build.action('job1', 'foo=bar&abc=xyz');
+ checks.build_jobName.should.equal('job1');
+ checks.build_params.should.equal('foo=bar&abc=xyz');
checks.console_log_messages.length.should.equal(1);
checks.console_log_messages[0].should.equal('Job job1 was started successfully');
});
@@ -119,18 +127,47 @@ describe('cli', function () {
cli = create(checks, mocks);
cli.exec();
checks.bag_parse_commands.build.desc.should.equal('Trigger a build with optional parameters\n\tnestor build <jobname> ["param1=value1&param2=value2"]');
+ checks.bag_parse_commands.build.options[0].arg.should.equal('-c, --console');
+ checks.bag_parse_commands.build.options[0].desc.should.equal('Display console ouptput of the triggered build progress');
checks.bag_parse_commands.build.action('job1');
checks.console_error_messages.length.should.equal(1);
checks.console_error_messages[0].should.equal('Job not found');
});
+ it('should follow build with console command when build is called with console flag', function () {
+ mocks.jenkins_action_err = null;
+ cli = create(checks, mocks);
+ cli.exec();
+ checks.bag_parse_commands.build.desc.should.equal('Trigger a build with optional parameters\n\tnestor build <jobname> ["param1=value1&param2=value2"]');
+ checks.bag_parse_commands.build.options[0].arg.should.equal('-c, --console');
+ checks.bag_parse_commands.build.options[0].desc.should.equal('Display console ouptput of the triggered build progress');
+ checks.bag_parse_commands.build.action('job1', { console: true });
+ should.not.exist(checks.build_params);
+ checks.console_log_messages.length.should.equal(1);
+ checks.console_log_messages[0].should.equal('Job job1 was started successfully');
+ checks.console_jobName.should.equal('job1');
+ });
+
+ it('should pass error when error occurs after build is called with console flag', function () {
+ mocks.jenkins_action_err = new Error('Job not found');
+ cli = create(checks, mocks);
+ cli.exec();
+ checks.bag_parse_commands.build.desc.should.equal('Trigger a build with optional parameters\n\tnestor build <jobname> ["param1=value1&param2=value2"]');
+ checks.bag_parse_commands.build.options[0].arg.should.equal('-c, --console');
+ checks.bag_parse_commands.build.options[0].desc.should.equal('Display console ouptput of the triggered build progress');
+ checks.bag_parse_commands.build.action('job1', { console: true });
+ should.not.exist(checks.build_params);
+ checks.console_log_messages.length.should.equal(0);
+ should.not.exist(checks.console_jobName);
+ });
+
it('should pass job name when exec console is called', function () {
mocks.jenkins_action_err = null;
cli = create(checks, mocks);
cli.exec();
checks.bag_parse_commands.console.desc.should.equal('Display latest build console output\n\tnestor console <jobname>');
checks.bag_parse_commands.console.action('job1');
- checks.build_jobName.should.equal('job1');
+ checks.console_jobName.should.equal('job1');
});
it('should log job not found error when exec console is called and job does not exist', function () {
Please sign in to comment.
Something went wrong with that request. Please try again.