Skip to content
Snippets Groups Projects
Unverified Commit e0766fa5 authored by Rhys Arkins's avatar Rhys Arkins Committed by GitHub
Browse files

refactor: bitbucket got (#3813)

parent aa38e6af
No related merge requests found
import got from 'got'; import { GotJSONOptions } from 'got';
import URL from 'url'; import got from '../../util/got';
import * as hostRules from '../../util/host-rules';
import { IGotApi, IGotApiOptions } from '../common'; import { IGotApi, IGotApiOptions } from '../common';
let cache: Renovate.IDict<got.Response<any>> = {}; async function get(path: string, options: IGotApiOptions & GotJSONOptions) {
const opts: IGotApiOptions & GotJSONOptions = {
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,
...options, ...options,
json: true,
hostType: 'bitbucket',
baseUrl: 'https://api.bitbucket.org/',
}; };
const method = ( const res = await got(path, opts);
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;
}
return res; return res;
} }
...@@ -44,8 +22,4 @@ for (const x of helpers) { ...@@ -44,8 +22,4 @@ for (const x of helpers) {
get(url, Object.assign({}, opts, { method: x.toUpperCase() })); get(url, Object.assign({}, opts, { method: x.toUpperCase() }));
} }
api.reset = function reset() {
cache = {};
};
export default api; export default api;
...@@ -74,7 +74,6 @@ export async function initRepo({ ...@@ -74,7 +74,6 @@ export async function initRepo({
hostType: 'bitbucket', hostType: 'bitbucket',
url: 'https://api.bitbucket.org/', url: 'https://api.bitbucket.org/',
}); });
api.reset();
config = {} as any; config = {} as any;
// TODO: get in touch with @rarkins about lifting up the caching into the app layer // TODO: get in touch with @rarkins about lifting up the caching into the app layer
config.repository = repository; config.repository = repository;
...@@ -636,7 +635,6 @@ export function cleanRepo() { ...@@ -636,7 +635,6 @@ export function cleanRepo() {
if (config.storage && config.storage.cleanRepo) { if (config.storage && config.storage.cleanRepo) {
config.storage.cleanRepo(); config.storage.cleanRepo();
} }
api.reset();
config = {} as any; config = {} as any;
} }
......
...@@ -2,6 +2,7 @@ import got from 'got'; ...@@ -2,6 +2,7 @@ import got from 'got';
export interface IGotApiOptions { export interface IGotApiOptions {
useCache?: boolean; useCache?: boolean;
hostType?: string;
body?: any; body?: any;
} }
......
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`platform/gl-got-wrapper returns cached 1`] = `
Object {
"body": Object {},
}
`;
...@@ -7,8 +7,8 @@ describe('platform/gl-got-wrapper', () => { ...@@ -7,8 +7,8 @@ describe('platform/gl-got-wrapper', () => {
beforeEach(() => { beforeEach(() => {
// reset module // reset module
jest.resetAllMocks(); jest.resetAllMocks();
jest.mock('got'); jest.mock('../../../lib/util/got');
got = require('got'); got = require('../../../lib/util/got');
hostRules = require('../../../lib/util/host-rules'); hostRules = require('../../../lib/util/host-rules');
api = require('../../../lib/platform/bitbucket/bb-got-wrapper').api; api = require('../../../lib/platform/bitbucket/bb-got-wrapper').api;
...@@ -32,12 +32,10 @@ describe('platform/gl-got-wrapper', () => { ...@@ -32,12 +32,10 @@ describe('platform/gl-got-wrapper', () => {
expect(res.body).toEqual(body); expect(res.body).toEqual(body);
}); });
it('returns cached', async () => { it('returns cached', async () => {
api.reset();
got.mockReturnValueOnce({ got.mockReturnValueOnce({
body: {}, body: {},
} as any); } as any);
const res1 = await api.get('projects/foo'); const res1 = await api.get('projects/foo');
const res2 = await api.get('projects/foo'); expect(res1).toMatchSnapshot();
expect(res1).toEqual(res2);
}); });
}); });
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment