From e0766fa5ee7e0d02a4db833108b378a7467f0c94 Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@arkins.net>
Date: Sun, 26 May 2019 11:25:24 +0200
Subject: [PATCH] refactor: bitbucket got (#3813)

---
 lib/platform/bitbucket/bb-got-wrapper.ts      | 42 ++++---------------
 lib/platform/bitbucket/index.ts               |  2 -
 lib/platform/common.ts                        |  1 +
 .../__snapshots__/bb-got-wrapper.spec.ts.snap |  7 ++++
 .../platform/bitbucket/bb-got-wrapper.spec.ts |  8 ++--
 5 files changed, 19 insertions(+), 41 deletions(-)
 create mode 100644 test/platform/bitbucket/__snapshots__/bb-got-wrapper.spec.ts.snap

diff --git a/lib/platform/bitbucket/bb-got-wrapper.ts b/lib/platform/bitbucket/bb-got-wrapper.ts
index b276e25eff..1cc4f44b45 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 67a0374032..3eb0ea2791 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 8e09c24be2..d225ff7ac6 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 0000000000..39af435181
--- /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 5fc6d5ef33..18c6377051 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();
   });
 });
-- 
GitLab