From 797fd09e7166b15f5bd9b6e5003c32c378717b69 Mon Sep 17 00:00:00 2001 From: Michael Kriese <michael.kriese@visualon.de> Date: Wed, 29 Apr 2020 11:37:52 +0200 Subject: [PATCH] fix: missing repoCache (#6089) --- lib/config/presets/github.spec.ts | 3 ++- lib/config/presets/github.ts | 10 ++++++---- lib/util/cache.ts | 8 ++++++++ 3 files changed, 16 insertions(+), 5 deletions(-) create mode 100644 lib/util/cache.ts diff --git a/lib/config/presets/github.spec.ts b/lib/config/presets/github.spec.ts index 3fd6c664ec..3a3637ef0d 100644 --- a/lib/config/presets/github.spec.ts +++ b/lib/config/presets/github.spec.ts @@ -5,6 +5,7 @@ import * as _hostRules from '../../util/host-rules'; import { PLATFORM_FAILURE } from '../../constants/error-messages'; import { mocked } from '../../../test/util'; import { GotResponse } from '../../platform'; +import { clearRepoCache } from '../../util/cache'; jest.mock('../../platform/github/gh-got-wrapper'); jest.mock('../../util/got'); @@ -25,7 +26,7 @@ describe('config/presets/github', () => { }); describe('fetchJSONFile()', () => { beforeEach(() => { - delete global.repoCache.internalPresets; + clearRepoCache(); }); it('returns JSON', async () => { hostRules.find.mockReturnValueOnce({ token: 'abc' }); diff --git a/lib/config/presets/github.ts b/lib/config/presets/github.ts index 8bf562be16..432a5caa62 100644 --- a/lib/config/presets/github.ts +++ b/lib/config/presets/github.ts @@ -4,11 +4,13 @@ import { Http, HttpOptions } from '../../util/http'; import { PLATFORM_FAILURE } from '../../constants/error-messages'; import { ensureTrailingSlash } from '../../util/url'; import { PLATFORM_TYPE_GITHUB } from '../../constants/platforms'; +import { getRepoCache } from '../../util/cache'; const http = new Http(PLATFORM_TYPE_GITHUB); export function setInternalPreset(content: { body: Preset }): void { - global.repoCache.internalPresets = Promise.resolve(content); + const cache = getRepoCache(); + cache.internalPresets = Promise.resolve(content); } async function fetchInternalPreset(): Promise<Preset> { @@ -19,9 +21,9 @@ async function fetchInternalPreset(): Promise<Preset> { } function getInternalPreset(): Promise<Preset> { - global.repoCache.internalPresets = - global.repoCache.internalPresets || fetchInternalPreset(); - return global.repoCache.internalPresets; + const cache = getRepoCache(); + cache.internalPresets = cache.internalPresets || fetchInternalPreset(); + return cache.internalPresets; } export async function fetchJSONFile( diff --git a/lib/util/cache.ts b/lib/util/cache.ts new file mode 100644 index 0000000000..d4282d9ded --- /dev/null +++ b/lib/util/cache.ts @@ -0,0 +1,8 @@ +export function getRepoCache(): Record<string, any> { + // eslint-disable-next-line no-return-assign + return global.repoCache ?? (global.repoCache = {}); +} + +export function clearRepoCache(): void { + global.repoCache = {}; +} -- GitLab