diff --git a/index.js b/index.js index b44dd63..53b299f 100644 --- a/index.js +++ b/index.js @@ -70,17 +70,24 @@ class JenkinsExecutor extends Executor { params: [{ name: jobName }] }); - if (!(job && job.lastBuild && job.lastBuild.number)) { - throw new Error('No build has been started yet, try later'); + if (!job) { + throw new Error('No jobs in process yet, try later'); } - await this.breaker.runCommand({ - module: 'build', - action: 'stop', - params: [{ name: jobName, number: job.lastBuild.number }] - }); - - return this._jenkinsJobWaitStop(jobName, 0); + if (job.inQueue && job.queueItem && job.queueItem.id) { + await this.breaker.runCommand({ + module: 'queue', + action: 'cancel', + params: [{ number: job.queueItem.id }] + }); + } else if (job.lastBuild && job.lastBuild.number) { + await this.breaker.runCommand({ + module: 'build', + action: 'stop', + params: [{ name: jobName, number: job.lastBuild.number }] + }); + await this._jenkinsJobWaitStop(jobName, 0); + } } /** diff --git a/test/index.test.js b/test/index.test.js index 37e0d56..25b437f 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -428,18 +428,14 @@ describe('index', () => { }); }); - it('return error when there is no build to be stopped yet', done => { - const noBuildJobInfo = { - lastBuild: null - }; - - breakerMock.runCommand.withArgs(getOpts).resolves(noBuildJobInfo); + it('return error when there are no jobs in process yet', done => { + breakerMock.runCommand.withArgs(getOpts).resolves(null); breakerMock.runCommand.withArgs(stopOpts).resolves(null); executor.stop({ buildId: config.buildId }).catch(err => { assert.deepEqual( err.message, - 'No build has been started yet, try later' + 'No jobs in process yet, try later' ); done(); });