diff --git a/lib/config/presets/__snapshots__/github.spec.ts.snap b/lib/config/presets/__snapshots__/github.spec.ts.snap deleted file mode 100644 index e1ce92515027c8..00000000000000 --- a/lib/config/presets/__snapshots__/github.spec.ts.snap +++ /dev/null @@ -1,70 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`config/presets/github getPreset() uses default endpoint 1`] = ` -Array [ - Array [ - "https://api.github.com/repos/some/repo/contents/default.json", - Object { - "headers": Object { - "accept": "application/vnd.github.v3+json", - }, - "hooks": Object { - "beforeRedirect": Array [ - [Function], - ], - }, - "hostType": "github", - "json": true, - "method": "get", - }, - ], - Array [ - "https://api.github.com/repos/some/repo/contents/renovate.json", - Object { - "headers": Object { - "accept": "application/vnd.github.v3+json", - }, - "hooks": Object { - "beforeRedirect": Array [ - [Function], - ], - }, - "hostType": "github", - "json": true, - "method": "get", - }, - ], - Array [ - "https://api.github.com/repos/some/repo/contents/default.json", - Object { - "headers": Object { - "accept": "application/vnd.github.v3+json", - }, - "hooks": Object { - "beforeRedirect": Array [ - [Function], - ], - }, - "hostType": "github", - "json": true, - "method": "get", - }, - ], - Array [ - "https://api.github.com/repos/some/repo/contents/renovate.json", - Object { - "headers": Object { - "accept": "application/vnd.github.v3+json", - }, - "hooks": Object { - "beforeRedirect": Array [ - [Function], - ], - }, - "hostType": "github", - "json": true, - "method": "get", - }, - ], -] -`; diff --git a/lib/config/presets/__snapshots__/gitlab.spec.ts.snap b/lib/config/presets/__snapshots__/gitlab.spec.ts.snap deleted file mode 100644 index 34e8c20b4f2375..00000000000000 --- a/lib/config/presets/__snapshots__/gitlab.spec.ts.snap +++ /dev/null @@ -1,12 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`config/presets/gitlab getPreset() uses default endpoint 1`] = ` -Array [ - Array [ - "https://gitlab.com/api/v4/projects/some%2Frepo/repository/branches", - ], - Array [ - "https://gitlab.com/api/v4/projects/some%2Frepo/repository/branches", - ], -] -`; diff --git a/lib/config/presets/__snapshots__/local.spec.ts.snap b/lib/config/presets/__snapshots__/local.spec.ts.snap index f2713740ee5bcb..130b863db6d22b 100644 --- a/lib/config/presets/__snapshots__/local.spec.ts.snap +++ b/lib/config/presets/__snapshots__/local.spec.ts.snap @@ -5,10 +5,7 @@ Array [ Array [ "some/repo", "", - Object { - "endpoint": "https://api.github.example.com", - "platform": "GitHub", - }, + "https://api.github.example.com", ], ] `; @@ -24,10 +21,7 @@ Array [ Array [ "some/repo", "", - Object { - "endpoint": "https://gitlab.example.com/api/v4", - "platform": "gitlab", - }, + "https://gitlab.example.com/api/v4", ], ] `; @@ -43,9 +37,7 @@ Array [ Array [ "some/repo", "default", - Object { - "platform": "github", - }, + undefined, ], ] `; @@ -61,9 +53,7 @@ Array [ Array [ "some/repo", "", - Object { - "platform": "GitLab", - }, + undefined, ], ] `; diff --git a/lib/config/presets/github.spec.ts b/lib/config/presets/github.spec.ts index 16cc36ddf62d82..c899f2550bed5a 100644 --- a/lib/config/presets/github.spec.ts +++ b/lib/config/presets/github.spec.ts @@ -5,7 +5,6 @@ import * as _hostRules from '../../util/host-rules'; import { PLATFORM_FAILURE } from '../../constants/error-messages'; import { mocked } from '../../../test/util'; import { GotResponse } from '../../platform'; -import { PLATFORM_TYPE_GITHUB } from '../../constants/platforms'; jest.mock('../../platform/github/gh-got-wrapper'); jest.mock('../../util/got'); @@ -73,25 +72,15 @@ describe('config/presets/github', () => { delete global.appMode; } }); - - it('uses default endpoint', async () => { - await github.getPreset('some/repo', 'default').catch((_) => {}); - await github - .getPreset('some/repo', 'default', { - endpoint: 'https://api.github.example.org', - }) - .catch((_) => {}); - expect(got.mock.calls[0][0]).toEqual( - 'https://api.github.com/repos/some/repo/contents/default.json' - ); - expect(got.mock.calls).toMatchSnapshot(); - }); + }); + describe('getPresetFromEndpoint()', () => { it('uses custom endpoint', async () => { await github - .getPreset('some/repo', 'default', { - platform: PLATFORM_TYPE_GITHUB, - endpoint: 'https://api.github.example.org', - }) + .getPresetFromEndpoint( + 'some/repo', + 'default', + 'https://api.github.example.org' + ) .catch((_) => {}); expect(got.mock.calls[0][0]).toEqual( 'https://api.github.example.org/repos/some/repo/contents/default.json' diff --git a/lib/config/presets/github.ts b/lib/config/presets/github.ts index d92a358b862fd1..9d73a5293da3e8 100644 --- a/lib/config/presets/github.ts +++ b/lib/config/presets/github.ts @@ -3,7 +3,6 @@ import { Preset } from './common'; import { Http, HttpOptions } from '../../util/http'; import { PLATFORM_FAILURE } from '../../constants/error-messages'; import { ensureTrailingSlash } from '../../util/url'; -import { RenovateConfig } from '../common'; import { PLATFORM_TYPE_GITHUB } from '../../constants/platforms'; const http = new Http(PLATFORM_TYPE_GITHUB); @@ -43,16 +42,13 @@ async function fetchJSONFile( } } -export async function getPreset( +export async function getPresetFromEndpoint( pkgName: string, - presetName = 'default', - baseConfig?: RenovateConfig + presetName: string, + endpoint = 'https://api.github.com/' ): Promise { - const endpoint = ensureTrailingSlash( - (baseConfig?.platform === PLATFORM_TYPE_GITHUB - ? baseConfig?.endpoint - : null) ?? 'https://api.github.com/' - ); + // eslint-disable-next-line no-param-reassign + endpoint = ensureTrailingSlash(endpoint); if (presetName === 'default') { try { const defaultJson = await fetchJSONFile( @@ -74,3 +70,10 @@ export async function getPreset( } return fetchJSONFile(pkgName, `${presetName}.json`, endpoint); } + +export async function getPreset( + pkgName: string, + presetName = 'default' +): Promise { + return getPresetFromEndpoint(pkgName, presetName); +} diff --git a/lib/config/presets/gitlab.spec.ts b/lib/config/presets/gitlab.spec.ts index 88a3a009428387..ff3a357901b5bc 100644 --- a/lib/config/presets/gitlab.spec.ts +++ b/lib/config/presets/gitlab.spec.ts @@ -2,7 +2,6 @@ import { PartialDeep } from 'type-fest'; import * as gitlab from './gitlab'; import { api } from '../../platform/gitlab/gl-got-wrapper'; import { GotResponse } from '../../platform'; -import { PLATFORM_TYPE_GITLAB } from '../../constants/platforms'; jest.mock('../../platform/gitlab/gl-got-wrapper'); jest.mock('../../util/got'); @@ -55,24 +54,15 @@ describe('config/presets/gitlab', () => { const content = await gitlab.getPreset('some/repo'); expect(content).toEqual({ foo: 'bar' }); }); - it('uses default endpoint', async () => { - await gitlab.getPreset('some/repo', 'default').catch((_) => {}); - await gitlab - .getPreset('some/repo', 'default', { - endpoint: 'https://gitlab.example.org/api/v4', - }) - .catch((_) => {}); - expect(glGot.mock.calls[0][0]).toEqual( - 'https://gitlab.com/api/v4/projects/some%2Frepo/repository/branches' - ); - expect(glGot.mock.calls).toMatchSnapshot(); - }); + }); + describe('getPresetFromEndpoint()', () => { it('uses custom endpoint', async () => { await gitlab - .getPreset('some/repo', 'default', { - platform: PLATFORM_TYPE_GITLAB, - endpoint: 'https://gitlab.example.org/api/v4', - }) + .getPresetFromEndpoint( + 'some/repo', + 'default', + 'https://gitlab.example.org/api/v4' + ) .catch((_) => {}); expect(glGot.mock.calls[0][0]).toEqual( 'https://gitlab.example.org/api/v4/projects/some%2Frepo/repository/branches' diff --git a/lib/config/presets/gitlab.ts b/lib/config/presets/gitlab.ts index d2d7d9e78b9979..6c213f0f2af6b8 100644 --- a/lib/config/presets/gitlab.ts +++ b/lib/config/presets/gitlab.ts @@ -2,8 +2,6 @@ import { api } from '../../platform/gitlab/gl-got-wrapper'; import { logger } from '../../logger'; import { Preset } from './common'; import { ensureTrailingSlash } from '../../util/url'; -import { RenovateConfig } from '../common'; -import { PLATFORM_TYPE_GITLAB } from '../../constants/platforms'; const { get: glGot } = api; @@ -30,16 +28,13 @@ async function getDefaultBranchName( return defautlBranchName; } -export async function getPreset( +export async function getPresetFromEndpoint( pkgName: string, - presetName = 'default', - baseConfig?: RenovateConfig + presetName: string, + endpoint = 'https://gitlab.com/api/v4/' ): Promise { - const endpoint = ensureTrailingSlash( - (baseConfig?.platform === PLATFORM_TYPE_GITLAB - ? baseConfig?.endpoint - : null) ?? 'https://gitlab.com/api/v4/' - ); + // eslint-disable-next-line no-param-reassign + endpoint = ensureTrailingSlash(endpoint); if (presetName !== 'default') { // TODO: proper error contructor throw new Error( @@ -71,3 +66,10 @@ export async function getPreset( throw new Error('invalid preset JSON'); } } + +export async function getPreset( + pkgName: string, + presetName = 'default' +): Promise { + return getPresetFromEndpoint(pkgName, presetName); +} diff --git a/lib/config/presets/local.spec.ts b/lib/config/presets/local.spec.ts index affa203bf2d0ba..5780818d572896 100644 --- a/lib/config/presets/local.spec.ts +++ b/lib/config/presets/local.spec.ts @@ -5,8 +5,12 @@ import * as local from './local'; jest.mock('./gitlab'); jest.mock('./github'); -const gitlabGetPreset: jest.Mock> = gitlab.getPreset as never; -const githubGetPreset: jest.Mock> = github.getPreset as never; +const gitlabGetPreset: jest.Mock> = gitlab.getPresetFromEndpoint as never; +const githubGetPreset: jest.Mock> = github.getPresetFromEndpoint as never; describe('config/presets/local', () => { beforeEach(() => { @@ -25,6 +29,13 @@ describe('config/presets/local', () => { }) ).rejects.toThrow(); }); + it('throws for missing platform', async () => { + await expect( + local.getPreset('some/repo', 'default', { + platform: undefined, + }) + ).rejects.toThrow(); + }); it('forwards to gitlab', async () => { const content = await local.getPreset('some/repo', '', { platform: 'GitLab', diff --git a/lib/config/presets/local.ts b/lib/config/presets/local.ts index 5b466f52f238b8..801db16dd77bfe 100644 --- a/lib/config/presets/local.ts +++ b/lib/config/presets/local.ts @@ -2,19 +2,28 @@ import { Preset } from './common'; import * as gitlab from './gitlab'; import * as github from './github'; import { RenovateConfig } from '../common'; +import { + PLATFORM_TYPE_GITHUB, + PLATFORM_TYPE_GITLAB, +} from '../../constants/platforms'; export async function getPreset( pkgName: string, presetName = 'default', baseConfig: RenovateConfig ): Promise { - if (baseConfig.platform?.toLowerCase() === 'gitlab') { - return gitlab.getPreset(pkgName, presetName, baseConfig); + const { platform, endpoint } = baseConfig; + if (!platform) { + throw new Error(`Missing platform config for local preset.`); } - if (baseConfig.platform?.toLowerCase() === 'github') { - return github.getPreset(pkgName, presetName, baseConfig); + switch (platform.toLowerCase()) { + case PLATFORM_TYPE_GITLAB: + return gitlab.getPresetFromEndpoint(pkgName, presetName, endpoint); + case PLATFORM_TYPE_GITHUB: + return github.getPresetFromEndpoint(pkgName, presetName, endpoint); + default: + throw new Error( + `Unsupported platform '${baseConfig.platform}' for local preset.` + ); } - throw new Error( - `Unsupported platform '${baseConfig.platform}' for local preset.` - ); }