diff --git a/lib/api/gh-got-retry.js b/lib/api/gh-got-retry.js new file mode 100644 index 0000000000000000000000000000000000000000..6269246cabaf593798d1b30156dc9363ada38ad7 --- /dev/null +++ b/lib/api/gh-got-retry.js @@ -0,0 +1,56 @@ +const logger = require('../logger'); +const ghGot = require('gh-got'); + +// istanbul ignore next +function sleep(ms) { + // eslint-disable-next-line promise/avoid-new + return new Promise(resolve => setTimeout(resolve, ms)); +} + +async function ghGotRetry(path, opts, retries = 5) { + try { + const res = await ghGot(path, opts); + return res; + } catch (err) { + if (err.statusCode >= 500 && err.statusCode < 600 && retries > 0) { + logger.debug(`Retrying statusCode ${err.statusCode}`); + // istanbul ignore if + if (process.env.NODE_ENV !== 'test') { + await sleep(5000 / retries); + } + return ghGotRetry(path, opts, retries - 1); + } + if ( + err.statusCode === 403 && + err.message && + err.message.indexOf('You have triggered an abuse detection mechanism') === + 0 + ) { + logger.debug(`Retrying abuse detection trigger`); + // istanbul ignore if + if (process.env.NODE_ENV !== 'test') { + await sleep(180000 / (retries * retries)); + } + return ghGotRetry(path, opts, retries - 1); + } + throw err; + } +} + +const helpers = ['get', 'post', 'put', 'patch', 'head', 'delete']; + +for (const x of helpers) { + ghGotRetry[x] = async (path, opts, retries = 3) => { + try { + const res = await ghGot[x](path, opts); + return res; + } catch (err) { + if (err.statusCode === 502 && retries > 0) { + return ghGotRetry[x](path, opts, retries - 1); + } + throw err; + } + }; +} + +module.exports = ghGotRetry; diff --git a/lib/api/github.js b/lib/api/github.js index 65152aef0fceec54778b058703af08323cd96af1..da7bbff387dac541fc1ef5793987be96b7fcc164 100644 --- a/lib/api/github.js +++ b/lib/api/github.js @@ -1,57 +1,5 @@ let logger = require('../logger'); -const ghGot = require('gh-got'); - -// istanbul ignore next -function sleep(ms) { - // eslint-disable-next-line promise/avoid-new - return new Promise(resolve => setTimeout(resolve, ms)); -} - -async function ghGotRetry(path, opts, retries = 5) { - try { - const res = await ghGot(path, opts); - return res; - } catch (err) { - if (err.statusCode >= 500 && err.statusCode < 600 && retries > 0) { - logger.debug(`Retrying statusCode ${err.statusCode}`); - // istanbul ignore if - if (process.env.NODE_ENV !== 'test') { - await sleep(5000 / retries); - } - return ghGotRetry(path, opts, retries - 1); - } - if ( - err.statusCode === 403 && - err.message && - err.message.indexOf('You have triggered an abuse detection mechanism') === - 0 - ) { - logger.debug(`Retrying abuse detection trigger`); - // istanbul ignore if - if (process.env.NODE_ENV !== 'test') { - await sleep(180000 / (retries * retries)); - } - return ghGotRetry(path, opts, retries - 1); - } - throw err; - } -} - -const helpers = ['get', 'post', 'put', 'patch', 'head', 'delete']; - -for (const x of helpers) { - ghGotRetry[x] = async (path, opts, retries = 3) => { - try { - const res = await ghGot[x](path, opts); - return res; - } catch (err) { - if (err.statusCode === 502 && retries > 0) { - return ghGotRetry[x](path, opts, retries - 1); - } - throw err; - } - }; -} +const ghGotRetry = require('./gh-got-retry'); const config = {};