Skip to content

Commit

Permalink
fix: Include apiUri in config passed to startPeriodic (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pranav Ravichandran authored May 7, 2018
1 parent caae9e4 commit 585a3d6
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
7 changes: 5 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,17 @@ class ExecutorQueue extends Executor {
* @param {Object} config Configuration
* @param {Object} config.pipeline Pipeline of the job
* @param {Object} config.job Job object to create periodic builds for
* @param {String} config.apiUri Base URL of the Screwdriver API
* @return {Promise}
*/
async postBuildEvent({ pipeline, job }) {
async postBuildEvent({ pipeline, job, apiUri }) {
const jwt = this.tokenGen(Object.keys(pipeline.admins)[0], {}, pipeline.scmContext);

console.log('QUEUE postBuildEvent: jwt: ', jwt);
console.log('QUEUE postBuildEvent: apiUri: ', apiUri);

const options = {
url: '/events',
url: `${apiUri}/v4/events`,
method: 'POST',
headers: {
Authorization: `Bearer ${jwt}`,
Expand Down Expand Up @@ -197,6 +199,7 @@ class ExecutorQueue extends Executor {
* @param {Object} config Configuration
* @param {Object} config.pipeline Pipeline of the job
* @param {Object} config.job Job object to create periodic builds for
* @param {String} config.apiUri Base URL of the Screwdriver API
* @param {Function} config.tokenGen Function to generate JWT from username, scope and scmContext
* @param {Boolean} config.isUpdate Boolean to determine if updating existing periodic build
* @param {Boolean} config.triggerBuild Flag to post new build event
Expand Down
40 changes: 39 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ const testJob = require('./data/testJob.json');
const partialTestConfig = {
buildId: testConfig.buildId
};
const tokenGen = sinon.stub.returns('123456abc');
const tokenGen = sinon.stub().returns('123456abc');
const testDelayedConfig = {
pipeline: testPipeline,
job: testJob,
apiUri: 'http://localhost',
tokenGen
};
const EventEmitter = require('events').EventEmitter;
Expand All @@ -37,6 +38,7 @@ describe('index test', () => {
let redisConstructorMock;
let cronMock;
let winstonMock;
let reqMock;

before(() => {
mockery.enable({
Expand Down Expand Up @@ -87,11 +89,13 @@ describe('index test', () => {
transform: sinon.stub().returns('H H H H H'),
next: sinon.stub().returns(1500000)
};
reqMock = sinon.stub().resolves();

mockery.registerMock('node-resque', resqueMock);
mockery.registerMock('ioredis', redisConstructorMock);
mockery.registerMock('./lib/cron', cronMock);
mockery.registerMock('winston', winstonMock);
mockery.registerMock('request', reqMock);

/* eslint-disable global-require */
Executor = require('../index');
Expand Down Expand Up @@ -245,6 +249,40 @@ describe('index test', () => {
assert.calledWith(redisMock.hdel, 'periodicBuildConfigs', testJob.id);
});
});

it('sends an API event request if triggerBuild is true', () => {
testDelayedConfig.isUpdate = true;
testDelayedConfig.job.state = 'ENABLED';
testDelayedConfig.job.archived = true;
testDelayedConfig.triggerBuild = true;

const options = {
url: 'http://localhost/v4/events',
method: 'POST',
headers: {
Authorization: 'Bearer 123456abc',
'Content-Type': 'application/json'
},
json: true,
body: {
pipelineId: testDelayedConfig.pipeline.id,
startFrom: testDelayedConfig.job.name
}
};

executor.tokenGen = tokenGen;

executor.startPeriodic(testDelayedConfig).then(() => {
assert.calledOnce(queueMock.connect);
assert.notCalled(redisMock.hset);
assert.notCalled(queueMock.enqueueAt);
assert.calledWith(queueMock.delDelayed, 'periodicBuilds', 'startDelayed', [{
jobId: testJob.id
}]);
assert.calledWith(redisMock.hdel, 'periodicBuildConfigs', testJob.id);
assert.calledWith(reqMock, options);
});
});
});

describe('_start', () => {
Expand Down

0 comments on commit 585a3d6

Please sign in to comment.