diff --git a/lib/platform/gitlab/gl-got-wrapper.js b/lib/platform/gitlab/gl-got-wrapper.js index daae2542051986126fd52ecc19cb392891ec3da5..b5d8e04b89ffed2534cfebb1853d67c0e5c7fa30 100644 --- a/lib/platform/gitlab/gl-got-wrapper.js +++ b/lib/platform/gitlab/gl-got-wrapper.js @@ -1,5 +1,7 @@ const URL = require('url'); const glGot = require('gl-got'); +const delay = require('delay'); + const parseLinkHeader = require('parse-link-header'); const endpoints = require('../../util/endpoints'); @@ -18,24 +20,36 @@ async function get(path, options, retries = 5) { return cache[path]; } logger.debug({ path }, method.toUpperCase()); - const res = await glGot(path, opts); - if (opts.paginate) { - // Check if result is paginated - try { - const linkHeader = parseLinkHeader(res.headers.link); - if (linkHeader && linkHeader.next) { - res.body = res.body.concat( - (await get(linkHeader.next.url, opts, retries)).body - ); + try { + const res = await glGot(path, opts); + if (opts.paginate) { + // Check if result is paginated + try { + const linkHeader = parseLinkHeader(res.headers.link); + if (linkHeader && linkHeader.next) { + res.body = res.body.concat( + (await get(linkHeader.next.url, opts, retries)).body + ); + } + } catch (err) { + logger.warn({ err }, 'Pagination error'); } - } catch (err) { - logger.warn({ err }, 'Pagination error'); } + if (method === 'get' && path.startsWith('projects/')) { + cache[path] = res; + } + return res; + } catch (err) /* istanbul ignore next */ { + if (retries < 1) { + throw err; + } + if (err.statusCode === 429) { + logger.info(`Sleeping 1 minute before retrying 429`); + await delay(60000); + return get(path, opts, retries - 1); + } + throw err; } - if (method === 'get' && path.startsWith('projects/')) { - cache[path] = res; - } - return res; } const helpers = ['get', 'post', 'put', 'patch', 'head', 'delete'];