diff --git a/lib/platform/bitbucket/bb-got-wrapper.ts b/lib/platform/bitbucket/bb-got-wrapper.ts index b276e25effc49af3286d6749e0b4c50364280775..1cc4f44b45291c13b5bbcd1f85c140303ffbd5d7 100644 --- a/lib/platform/bitbucket/bb-got-wrapper.ts +++ b/lib/platform/bitbucket/bb-got-wrapper.ts @@ -1,37 +1,15 @@ -import got from 'got'; -import URL from 'url'; -import * as hostRules from '../../util/host-rules'; +import { GotJSONOptions } from 'got'; +import got from '../../util/got'; import { IGotApi, IGotApiOptions } from '../common'; -let cache: Renovate.IDict<got.Response<any>> = {}; - -const endpoint = 'https://api.bitbucket.org/'; - -async function get(path: string, options: IGotApiOptions & got.GotJSONOptions) { - const url = URL.resolve(endpoint, path); - const opts: IGotApiOptions & hostRules.HostRule & got.GotJSONOptions = { - // TODO: Move to configurable host rules, or use utils/got - timeout: 60 * 1000, - json: true, +async function get(path: string, options: IGotApiOptions & GotJSONOptions) { + const opts: IGotApiOptions & GotJSONOptions = { ...options, + json: true, + hostType: 'bitbucket', + baseUrl: 'https://api.bitbucket.org/', }; - const method = ( - opts.method || /* istanbul ignore next */ 'get' - ).toLowerCase(); - if (method === 'get' && cache[path]) { - logger.trace({ path }, 'Returning cached result'); - return cache[path]; - } - opts.headers = { - 'user-agent': 'https://github.com/renovatebot/renovate', - ...opts.headers, - }; - const { username, password } = hostRules.find({ hostType: 'bitbucket', url }); - opts.auth = `${username}:${password}`; - const res = await got(url, opts); - if (method.toLowerCase() === 'get') { - cache[path] = res; - } + const res = await got(path, opts); return res; } @@ -44,8 +22,4 @@ for (const x of helpers) { get(url, Object.assign({}, opts, { method: x.toUpperCase() })); } -api.reset = function reset() { - cache = {}; -}; - export default api; diff --git a/lib/platform/bitbucket/index.ts b/lib/platform/bitbucket/index.ts index 67a0374032027ffd831b8664bbee5cc027010e4d..3eb0ea2791ad42dae43e70a3db981fccd904b0bd 100644 --- a/lib/platform/bitbucket/index.ts +++ b/lib/platform/bitbucket/index.ts @@ -74,7 +74,6 @@ export async function initRepo({ hostType: 'bitbucket', url: 'https://api.bitbucket.org/', }); - api.reset(); config = {} as any; // TODO: get in touch with @rarkins about lifting up the caching into the app layer config.repository = repository; @@ -636,7 +635,6 @@ export function cleanRepo() { if (config.storage && config.storage.cleanRepo) { config.storage.cleanRepo(); } - api.reset(); config = {} as any; } diff --git a/lib/platform/common.ts b/lib/platform/common.ts index 8e09c24be2b487ec20c3c82a2cfb6b21e5107bd9..d225ff7ac6d27fc3cf6afbd12a8f4768897c78b8 100644 --- a/lib/platform/common.ts +++ b/lib/platform/common.ts @@ -2,6 +2,7 @@ import got from 'got'; export interface IGotApiOptions { useCache?: boolean; + hostType?: string; body?: any; } diff --git a/test/platform/bitbucket/__snapshots__/bb-got-wrapper.spec.ts.snap b/test/platform/bitbucket/__snapshots__/bb-got-wrapper.spec.ts.snap new file mode 100644 index 0000000000000000000000000000000000000000..39af435181794fe50274fa0c5c1d74d28ab18c7d --- /dev/null +++ b/test/platform/bitbucket/__snapshots__/bb-got-wrapper.spec.ts.snap @@ -0,0 +1,7 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`platform/gl-got-wrapper returns cached 1`] = ` +Object { + "body": Object {}, +} +`; diff --git a/test/platform/bitbucket/bb-got-wrapper.spec.ts b/test/platform/bitbucket/bb-got-wrapper.spec.ts index 5fc6d5ef33c132b2dd9c7de7f064eefe073054f4..18c63770515d4c1b10732226470e2614c731b469 100644 --- a/test/platform/bitbucket/bb-got-wrapper.spec.ts +++ b/test/platform/bitbucket/bb-got-wrapper.spec.ts @@ -7,8 +7,8 @@ describe('platform/gl-got-wrapper', () => { beforeEach(() => { // reset module jest.resetAllMocks(); - jest.mock('got'); - got = require('got'); + jest.mock('../../../lib/util/got'); + got = require('../../../lib/util/got'); hostRules = require('../../../lib/util/host-rules'); api = require('../../../lib/platform/bitbucket/bb-got-wrapper').api; @@ -32,12 +32,10 @@ describe('platform/gl-got-wrapper', () => { expect(res.body).toEqual(body); }); it('returns cached', async () => { - api.reset(); got.mockReturnValueOnce({ body: {}, } as any); const res1 = await api.get('projects/foo'); - const res2 = await api.get('projects/foo'); - expect(res1).toEqual(res2); + expect(res1).toMatchSnapshot(); }); });