diff --git a/lib/platform/github/gh-got-wrapper.js b/lib/platform/github/gh-got-wrapper.js index 24d972574547988c5838d33eb1c513444f8b3967..e7f6ca9ee5c69b315edfff85e3275df2c79c2edf 100644 --- a/lib/platform/github/gh-got-wrapper.js +++ b/lib/platform/github/gh-got-wrapper.js @@ -1,16 +1,13 @@ const ghGot = require('gh-got'); +const delay = require('delay'); const parseLinkHeader = require('parse-link-header'); let cache = {}; -// istanbul ignore next -function sleep(ms) { - // eslint-disable-next-line promise/avoid-new - return new Promise(resolve => setTimeout(resolve, ms)); -} - async function get(path, opts, retries = 5) { - const method = opts && opts.method ? opts.method : 'get'; + /* eslint-disable no-param-reassign */ + opts = Object.assign({}, opts); + const method = opts.method ? opts.method : 'get'; if (method === 'get' && cache[path]) { logger.trace({ path }, 'Returning cached result'); return cache[path]; @@ -18,8 +15,6 @@ async function get(path, opts, retries = 5) { logger.debug(`${method.toUpperCase()} ${path} [retries=${retries}]`); try { if (appMode) { - /* eslint-disable no-param-reassign */ - opts = Object.assign({}, opts); const appAccept = 'application/vnd.github.machine-man-preview+json'; opts.headers = Object.assign( {}, @@ -34,7 +29,7 @@ async function get(path, opts, retries = 5) { } } const res = await ghGot(path, opts); - if (opts && opts.paginate) { + if (opts.paginate) { // Check if result is paginated const linkHeader = parseLinkHeader(res.headers.link); if ( @@ -61,10 +56,9 @@ async function get(path, opts, retries = 5) { { statusCode: err.statusCode, message: err.message }, `Retrying request` ); - // istanbul ignore if - if (process.env.NODE_ENV !== 'test') { - await sleep(5000 / retries); - } + + await delay(5000 / retries); + return get(path, opts, retries - 1); } else if ( retries > 0 && @@ -77,10 +71,9 @@ async function get(path, opts, retries = 5) { { statusCode: err.statusCode, message: err.message }, `Retrying request` ); - // istanbul ignore if - if (process.env.NODE_ENV !== 'test') { - await sleep(180000 / (retries * retries)); - } + + await delay(180000 / (retries * retries)); + return get(path, opts, retries - 1); } else if ( err.statusCode === 403 && @@ -95,10 +88,9 @@ async function get(path, opts, retries = 5) { { statusCode: err.statusCode, message: err.message }, `Retrying request` ); - // istanbul ignore if - if (process.env.NODE_ENV !== 'test') { - await sleep(60000 / (retries * retries)); - } + + await delay(60000 / (retries * retries)); + return get(path, opts, retries - 1); } } else if ( @@ -111,10 +103,9 @@ async function get(path, opts, retries = 5) { { statusCode: err.statusCode, message: err.message }, `Retrying request` ); - // istanbul ignore if - if (process.env.NODE_ENV !== 'test') { - await sleep(5000 / retries); - } + + await delay(5000 / retries); + return get(path, opts, retries - 1); } } diff --git a/test/config/index.spec.js b/test/config/index.spec.js index ffd53cae28554a3f8b1ec11a0883e4dbacaea80e..955bf51d79903a92b62f1622f86d810b0fe10b80 100644 --- a/test/config/index.spec.js +++ b/test/config/index.spec.js @@ -22,6 +22,8 @@ describe('config/index', () => { jest.resetModules(); configParser = require('../../lib/config/index.js'); defaultArgv = argv(); + jest.mock('delay'); + require('delay').mockImplementation(() => Promise.resolve()); jest.mock('gh-got'); ghGot = require('gh-got'); jest.mock('gl-got'); diff --git a/test/platform/github/gh-got-wrapper.spec.js b/test/platform/github/gh-got-wrapper.spec.js index b80d2d08ff4862402f5f3e3cb60bfe52eafb48c9..2d5dba292d8930257191f98289863051c0074212 100644 --- a/test/platform/github/gh-got-wrapper.spec.js +++ b/test/platform/github/gh-got-wrapper.spec.js @@ -1,13 +1,16 @@ const get = require('../../../lib/platform/github/gh-got-wrapper'); const ghGot = require('gh-got'); +const delay = require('delay'); jest.mock('gh-got'); +jest.mock('delay'); describe('platform/gh-got-wrapper', () => { const body = ['a', 'b']; beforeEach(() => { jest.resetAllMocks(); get.setAppMode(false); + delay.mockImplementation(() => Promise.resolve()); }); it('supports app mode', async () => { get.setAppMode(true);