Skip to content

Commit

Permalink
feat(1961): Replace deprecated request package (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
tkyi authored Aug 30, 2021
1 parent d9b9ea9 commit 5f47568
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 34 deletions.
34 changes: 17 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@

const Executor = require('screwdriver-executor-base');
const logger = require('screwdriver-logger');
const requestretry = require('requestretry');
const requestretry = require('screwdriver-request');
const RETRY_LIMIT = 3;
const RETRY_DELAY = 5;

class ExecutorQueue extends Executor {
constructor(config = {}) {
super();
this.requestRetryStrategy = (err, response) =>
!!err || (response.statusCode !== 201 && response.statusCode !== 200);
this.requestRetryStrategy = (response, retryWithMergedOptions) => {
if (response.statusCode !== 201 && response.statusCode !== 200) {
retryWithMergedOptions({});
}

return response;
};
this.queueUri = config.ecosystem.queue;
}

Expand Down Expand Up @@ -215,23 +220,18 @@ class ExecutorQueue extends Executor {
'Content-Type': 'application/json'
},
url: `${this.queueUri}${args.path}`,
json: true,
maxAttempts: RETRY_LIMIT,
retryDelay: RETRY_DELAY * 1000, // in ms
retryStrategy: this.requestRetryStrategy,
...args,
retryOptions: {
limit: RETRY_LIMIT,
calculateDelay: ({ computedValue }) => (computedValue ? RETRY_DELAY * 1000 : 0) // in ms
},
hooks: {
afterResponse: [this.requestRetryStrategy]
},
method: args.method,
body
};

return new Promise((resolve, reject) => {
requestretry(options, (err, response) => {
if (!err) {
return resolve(response.body);
}

return reject(err);
});
});
return requestretry(options).then(response => response.body);
}
}

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"Tiffany Kyi <[email protected]>"
],
"devDependencies": {
"chai": "^4.2.0",
"chai": "^4.3.4",
"eslint": "^7.7.0",
"eslint-config-screwdriver": "^5.0.4",
"mocha": "^8.2.1",
Expand All @@ -44,10 +44,10 @@
"sinon": "^4.5.0"
},
"dependencies": {
"requestretry": "^3.1.0",
"screwdriver-data-schema": "^21.0.0",
"screwdriver-executor-base": "^8.0.0",
"screwdriver-logger": "^1.0.0",
"screwdriver-request": "^1.0.3",
"string-hash": "^1.1.3"
},
"release": {
Expand Down
33 changes: 18 additions & 15 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('index test', () => {
token: 'admintoken'
});
mockRequest = sinon.stub();
mockery.registerMock('requestretry', mockRequest);
mockery.registerMock('screwdriver-request', mockRequest);

/* eslint-disable global-require */
Executor = require('../index');
Expand All @@ -48,11 +48,14 @@ describe('index test', () => {
Authorization: 'Bearer admintoken',
'Content-Type': 'application/json'
},
json: true,
body: testConfig,
maxAttempts: 3,
retryDelay: 5000,
retryStrategy: executor.requestRetryStrategy
retryOptions: {
limit: 3,
calculateDelay: ({ computedValue }) => (computedValue ? 5000 : 0)
},
hooks: {
afterResponse: [executor.requestRetryStrategy]
}
};
});

Expand All @@ -78,7 +81,7 @@ describe('index test', () => {

describe('_startPeriodic', done => {
it('Calls api to start periodic build', () => {
mockRequest.yieldsAsync(null, { statusCode: 200 });
mockRequest.resolves({ statusCode: 200 });
const periodicConfig = { ...testConfig, username: 'admin', pipeline: { id: 123 }, job: { id: 777 } };

const options = {
Expand All @@ -98,7 +101,7 @@ describe('index test', () => {

describe('_stopPeriodic', done => {
it('Calls api to stop periodic build', () => {
mockRequest.yieldsAsync(null, { statusCode: 200 });
mockRequest.resolves({ statusCode: 200 });

const options = {
...requestOptions,
Expand All @@ -117,7 +120,7 @@ describe('index test', () => {

describe('_start', done => {
it('Calls api to start build', () => {
mockRequest.yieldsAsync(null, { statusCode: 200 });
mockRequest.resolves({ statusCode: 200 });
const startConfig = { ...testConfig, pipeline: testPipeline };

Object.assign(requestOptions, {
Expand All @@ -136,7 +139,7 @@ describe('index test', () => {

describe('_startFrozen', done => {
it('Calls api to start frozen build', () => {
mockRequest.yieldsAsync(null, { statusCode: 200 });
mockRequest.resolves({ statusCode: 200 });

Object.assign(requestOptions, {
url: 'http://localhost/v1/queue/message?type=frozen',
Expand All @@ -154,7 +157,7 @@ describe('index test', () => {

describe('_stopFrozen', done => {
it('Calls api to stop frozen builds', () => {
mockRequest.yieldsAsync(null, { statusCode: 200 });
mockRequest.resolves({ statusCode: 200 });

Object.assign(requestOptions, {
url: 'http://localhost/v1/queue/message?type=frozen',
Expand All @@ -172,7 +175,7 @@ describe('index test', () => {

describe('_stop', done => {
it('Calls api to stop a build', () => {
mockRequest.yieldsAsync(null, { statusCode: 200 });
mockRequest.resolves({ statusCode: 200 });
const stopConfig = {
annotations: testConfig.annotations,
blockedBy: testConfig.blockedBy,
Expand Down Expand Up @@ -200,7 +203,7 @@ describe('index test', () => {

describe('stats', done => {
it('Calls api to get stats', () => {
mockRequest.yieldsAsync(null, { statusCode: 200 });
mockRequest.resolves({ statusCode: 200 });
const statsConfig = {
buildId: testConfig.buildId,
jobId: testConfig.jobId,
Expand All @@ -213,7 +216,7 @@ describe('index test', () => {
method: 'GET'
});

mockRequest.yieldsAsync(null, { body: 'Hello', statusCode: 200 });
mockRequest.resolves({ body: 'Hello', statusCode: 200 });

return executor.stats(statsConfig, (err, res) => {
assert.calledWithArgs(mockRequest, {}, requestOptions);
Expand All @@ -226,7 +229,7 @@ describe('index test', () => {

describe('_stopTimer', done => {
it('Calls api to stop timer', () => {
mockRequest.yieldsAsync(null, { statusCode: 200 });
mockRequest.resolves({ statusCode: 200 });
const dateNow = Date.now();
const isoTime = new Date(dateNow).toISOString();
const sandbox = sinon.sandbox.create({
Expand Down Expand Up @@ -261,7 +264,7 @@ describe('index test', () => {

describe('_startTimer', done => {
it('Calls api to start timer', () => {
mockRequest.yieldsAsync(null, { statusCode: 200 });
mockRequest.resolves({ statusCode: 200 });
const dateNow = Date.now();
const isoTime = new Date(dateNow).toISOString();
const sandbox = sinon.sandbox.create({
Expand Down

0 comments on commit 5f47568

Please sign in to comment.