diff --git a/lib/platform/github/gh-got-wrapper.js b/lib/platform/github/gh-got-wrapper.js index 9ebb1cb134665c9d30f826ed4b8fd4bee56b63ec..ec00486f0e40be76b04c5a7c06dae755760b5613 100644 --- a/lib/platform/github/gh-got-wrapper.js +++ b/lib/platform/github/gh-got-wrapper.js @@ -1,6 +1,8 @@ const ghGot = require('gh-got'); const parseLinkHeader = require('parse-link-header'); +let cache = {}; + // istanbul ignore next function sleep(ms) { // eslint-disable-next-line promise/avoid-new @@ -8,9 +10,13 @@ function sleep(ms) { } async function get(path, opts, retries = 5) { + const method = opts && opts.method ? opts.method : 'get'; + if (method === 'get' && cache[path]) { + logger.debug({ path }, 'Returning cached result'); + return cache[path]; + } if (retries === 5) { - const method = opts && opts.method ? opts.method.toUpperCase() : 'GET'; - logger.debug(`${method} ${path}`); + logger.debug(`${method.toUpperCase()} ${path}`); } try { if (appMode) { @@ -39,6 +45,9 @@ async function get(path, opts, retries = 5) { ); } } + if (method === 'get' && path.startsWith('repos/')) { + cache[path] = res; + } return res; } catch (err) { if (err.statusCode >= 500 && err.statusCode < 600 && retries > 0) { @@ -116,4 +125,8 @@ get.setAppMode = function setAppMode(val) { appMode = val; }; +get.reset = function reset() { + cache = {}; +}; + module.exports = get; diff --git a/lib/platform/github/index.js b/lib/platform/github/index.js index ab8bfe9f24c5d87c20e76cecd2c0cc43a9b6cbce..3ef5226937dce83fe96dee4aab11e15da93d1e8b 100644 --- a/lib/platform/github/index.js +++ b/lib/platform/github/index.js @@ -73,8 +73,8 @@ async function initRepo(repoName, token, endpoint) { process.env.GITHUB_ENDPOINT = endpoint; } config = {}; + get.reset(); config.repoName = repoName; - config.prs = {}; const platformConfig = {}; let res; try { @@ -536,9 +536,6 @@ async function getPr(prNo) { if (!prNo) { return null; } - if (config.prs[prNo]) { - return config.prs[prNo]; - } const pr = (await get(`repos/${config.repoName}/pulls/${prNo}`)).body; if (!pr) { return null; @@ -599,7 +596,6 @@ async function getPr(prNo) { pr.isStale = true; } } - config.prs[prNo] = pr; return pr; } diff --git a/test/platform/github/gh-got-wrapper.spec.js b/test/platform/github/gh-got-wrapper.spec.js index 9f88a1f6e1e9968307e069e9175624fe2de3e6a0..6f148dd343d450b131375992fa9d103289266710 100644 --- a/test/platform/github/gh-got-wrapper.spec.js +++ b/test/platform/github/gh-got-wrapper.spec.js @@ -175,4 +175,13 @@ describe('platform/gh-got-wrapper', () => { const res = await get.post('some-url'); expect(res.body).toEqual(body); }); + it('returns cached', async () => { + get.reset(); + ghGot.mockReturnValueOnce({ + body: {}, + }); + const res1 = await get('repos/foo'); + const res2 = await get('repos/foo'); + expect(res1).toEqual(res2); + }); }); diff --git a/test/platform/github/index.spec.js b/test/platform/github/index.spec.js index 08bcb1bc30e8f00820f649a42fe4b126abcaae33..f5b9315545aecffc42f7dfda5752cd93ccf0ba5f 100644 --- a/test/platform/github/index.spec.js +++ b/test/platform/github/index.spec.js @@ -1039,8 +1039,6 @@ describe('platform/github', () => { const pr = await github.getPr(1234); expect(pr.canRebase).toBe(true); expect(pr).toMatchSnapshot(); - const cachedPr = await github.getPr(1234); - expect(cachedPr).toEqual(pr); }); }); describe('getPrFiles()', () => {