Skip to content

Commit

Permalink
fix: missing repoCache (#6089)
Browse files Browse the repository at this point in the history
  • Loading branch information
viceice committed Apr 29, 2020
1 parent 07b4046 commit 797fd09
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
3 changes: 2 additions & 1 deletion lib/config/presets/github.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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' });
Expand Down
10 changes: 6 additions & 4 deletions lib/config/presets/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand All @@ -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(
Expand Down
8 changes: 8 additions & 0 deletions lib/util/cache.ts
Original file line number Diff line number Diff line change
@@ -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 = {};
}

0 comments on commit 797fd09

Please sign in to comment.