diff --git a/lib/platform/gitlab/gl-got-wrapper.js b/lib/platform/gitlab/gl-got-wrapper.js index 1f6fcbcc6b942b652b961173035895895e58578a..7f303a5aeec17470f990b97feccfd58d8428c935 100644 --- a/lib/platform/gitlab/gl-got-wrapper.js +++ b/lib/platform/gitlab/gl-got-wrapper.js @@ -1,59 +1,39 @@ const URL = require('url'); -const glGot = require('gl-got'); -const delay = require('delay'); - const parseLinkHeader = require('parse-link-header'); const hostRules = require('../../util/host-rules'); -let cache = {}; -let endpoint = 'https://gitlab.com/api/v4/'; +const got = require('../../util/got'); + +let baseUrl = 'https://gitlab.com/api/v4/'; -async function get(path, options, retries = 5) { - const url = URL.resolve(endpoint, path); +async function get(path, options) { + const url = URL.resolve(baseUrl, path); const opts = { - // TODO: Move to configurable host rules, or use utils/got - timeout: 60 * 1000, - ...hostRules.find({ hostType: 'gitlab', url }), ...options, + ...hostRules.find({ hostType: 'gitlab', url }), + json: true, }; - delete opts.endpoint; - const method = opts.method || 'get'; - const useCache = opts.useCache || true; - if (method === 'get' && useCache && cache[path]) { - logger.trace({ path }, 'Returning cached result'); - return cache[path]; + if (opts.token) { + opts.headers = { + ...opts.headers, + 'PRIVATE-TOKEN': opts.token, + }; + delete opts.token; } - logger.debug({ path }, method.toUpperCase()); - try { - const res = await glGot(url, 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) /* istanbul ignore next */ { - logger.warn({ err }, 'Pagination error'); + delete opts.endpoint; + const res = await got(url, 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)).body); } + } catch (err) /* istanbul ignore next */ { + 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; } + return res; } const helpers = ['get', 'post', 'put', 'patch', 'head', 'delete']; @@ -63,13 +43,8 @@ for (const x of helpers) { get(url, Object.assign({}, opts, { method: x.toUpperCase() })); } -get.reset = function reset() { - cache = null; - cache = {}; -}; - -get.setEndpoint = e => { - endpoint = e; +get.setBaseUrl = e => { + baseUrl = e; }; module.exports = get; diff --git a/lib/platform/gitlab/index.js b/lib/platform/gitlab/index.js index 3161bbeacc354d0531e993e34809595db824e1e1..66bc77ef8a7aabd9801e964071021355de7b6881 100644 --- a/lib/platform/gitlab/index.js +++ b/lib/platform/gitlab/index.js @@ -69,7 +69,7 @@ function initPlatform({ endpoint, token }) { const res = {}; if (endpoint) { res.endpoint = endpoint.replace(/\/?$/, '/'); // always add a trailing slash - get.setEndpoint(res.endpoint); + get.setBaseUrl(res.endpoint); defaults.endpoint = res.endpoint; } else { res.endpoint = defaults.endpoint; @@ -103,14 +103,12 @@ function cleanRepo() { config.storage.cleanRepo(); } // In theory most of this isn't necessary. In practice.. - get.reset(); config = {}; } // Initialize GitLab by getting base branch async function initRepo({ repository, localDir }) { config = {}; - get.reset(); config.repository = urlEscape(repository); config.localDir = localDir; let res; diff --git a/package.json b/package.json index bcd9b2357c1a1735e650eac21339a7311dfff395..2d22289699b51a868eaca906b3d7f326ee6d43bc 100644 --- a/package.json +++ b/package.json @@ -113,7 +113,6 @@ "get-installed-path": "4.0.8", "gh-got": "8.1.0", "github-url-from-git": "1.5.0", - "gl-got": "9.0.3", "got": "9.6.0", "handlebars": "4.1.2", "ini": "1.3.5", diff --git a/test/globals.js b/test/globals.js index c86469b8b22ce301d6793f8c553075f3e0ae22fb..68b973942f12d75c95d35d98efa89342f61d5f29 100644 --- a/test/globals.js +++ b/test/globals.js @@ -2,7 +2,6 @@ const upath = require('upath'); const os = require('os'); jest.mock('gh-got'); -jest.mock('gl-got'); const cache = require('../lib/workers/global/cache'); diff --git a/test/platform/gitlab/gl-got-wrapper.spec.js b/test/platform/gitlab/gl-got-wrapper.spec.js index 0941135bc5becdd5152e30e2630670b73e26ad9c..b28c9a36b520b9951531e185e2aa0ae1e0d2711c 100644 --- a/test/platform/gitlab/gl-got-wrapper.spec.js +++ b/test/platform/gitlab/gl-got-wrapper.spec.js @@ -1,67 +1,66 @@ -const glGot = require('gl-got'); +const got = require('../../../lib/util/got'); const get = require('../../../lib/platform/gitlab/gl-got-wrapper'); +const hostRules = require('../../../lib/util/host-rules'); -describe('platform/gl-got-wrapper', () => { +jest.mock('../../../lib/util/got'); + +hostRules.add({ + hostType: 'gitlab', + token: 'abc123', +}); + +describe('platform/gitlab/gl-got-wrapper', () => { const body = ['a', 'b']; afterEach(() => { jest.resetAllMocks(); }); it('paginates', async () => { - glGot.mockReturnValueOnce({ + got.mockReturnValueOnce({ headers: { link: '<https://api.gitlab.com/search/code?q=addClass+user%3Amozilla&page=2>; rel="next", <https://api.gitlab.com/search/code?q=addClass+user%3Amozilla&page=34>; rel="last"', }, body: ['a'], }); - glGot.mockReturnValueOnce({ + got.mockReturnValueOnce({ headers: { link: '<https://api.gitlab.com/search/code?q=addClass+user%3Amozilla&page=3>; rel="next", <https://api.gitlab.com/search/code?q=addClass+user%3Amozilla&page=34>; rel="last"', }, body: ['b', 'c'], }); - glGot.mockReturnValueOnce({ + got.mockReturnValueOnce({ headers: {}, body: ['d'], }); const res = await get('some-url', { paginate: true }); expect(res.body).toHaveLength(4); - expect(glGot).toHaveBeenCalledTimes(3); + expect(got).toHaveBeenCalledTimes(3); }); it('attempts to paginate', async () => { - glGot.mockReturnValueOnce({ + got.mockReturnValueOnce({ headers: { link: '<https://api.gitlab.com/search/code?q=addClass+user%3Amozilla&page=34>; rel="last"', }, body: ['a'], }); - glGot.mockReturnValueOnce({ + got.mockReturnValueOnce({ headers: {}, body: ['b'], }); const res = await get('some-url', { paginate: true }); expect(res.body).toHaveLength(1); - expect(glGot).toHaveBeenCalledTimes(1); + expect(got).toHaveBeenCalledTimes(1); }); it('posts', async () => { - glGot.mockImplementationOnce(() => ({ + got.mockImplementationOnce(() => ({ body, })); const res = await get.post('some-url'); expect(res.body).toEqual(body); }); - it('returns cached', async () => { - get.reset(); - glGot.mockReturnValueOnce({ - body: {}, - }); - const res1 = await get('projects/foo'); - const res2 = await get('projects/foo'); - expect(res1).toEqual(res2); - }); - it('sets endpoint', () => { - get.setEndpoint('https://gitlab.renovatebot.com/api/v4/'); + it('sets baseUrl', () => { + get.setBaseUrl('https://gitlab.renovatebot.com/api/v4/'); }); }); diff --git a/yarn.lock b/yarn.lock index 40ee58292893f2cfda0e88896236824cd1bbe3ad..a6bb6d9213c56c1146289e0ab020d84f81a8deb4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3040,13 +3040,6 @@ github-url-from-git@1.5.0: resolved "https://registry.yarnpkg.com/github-url-from-git/-/github-url-from-git-1.5.0.tgz#f985fedcc0a9aa579dc88d7aff068d55cc6251a0" integrity sha1-+YX+3MCpqledyI16/waNVcxiUaA= -gl-got@9.0.3: - version "9.0.3" - resolved "https://registry.yarnpkg.com/gl-got/-/gl-got-9.0.3.tgz#ceddbbf71f09d9369d9179a35ca919ffb289259b" - integrity sha512-qT1Jx80DvD99G/gjG5ZHOEv9RS55mwyRakMhe4U4+BSvoTn0Mjjs01t7mRUHoB6TxaoublB2Bv0IYcXzOgKGrQ== - dependencies: - got "^9.2.0" - glob-parent@^3.1.0: version "3.1.0" resolved "https://registry.yarnpkg.com/glob-parent/-/glob-parent-3.1.0.tgz#9e6af6299d8d3bd2bd40430832bd113df906c5ae" @@ -3129,7 +3122,7 @@ globby@^9.0.0: pify "^4.0.1" slash "^2.0.0" -got@9.6.0, got@^9.2.0, got@^9.5.0: +got@9.6.0, got@^9.5.0: version "9.6.0" resolved "https://registry.yarnpkg.com/got/-/got-9.6.0.tgz#edf45e7d67f99545705de1f7bbeeeb121765ed85" integrity sha512-R7eWptXuGYxwijs0eV+v3o6+XH1IqVK8dJOEecQfTmkncw9AV4dcw/Dhxi8MdlqPthxxpZyizMzyg8RTmEsG+Q==