Skip to content

Commit

Permalink
fix(config-presets): do not use custom endpoints when using git… (#6022)
Browse files Browse the repository at this point in the history
Fixes #6014
Supersedes #5973
  • Loading branch information
fgreinacher committed Apr 21, 2020
1 parent 4fa3948 commit 9647ea7
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 159 deletions.
70 changes: 0 additions & 70 deletions lib/config/presets/__snapshots__/github.spec.ts.snap

This file was deleted.

12 changes: 0 additions & 12 deletions lib/config/presets/__snapshots__/gitlab.spec.ts.snap

This file was deleted.

18 changes: 4 additions & 14 deletions lib/config/presets/__snapshots__/local.spec.ts.snap
Expand Up @@ -5,10 +5,7 @@ Array [
Array [
"some/repo",
"",
Object {
"endpoint": "https://api.github.example.com",
"platform": "GitHub",
},
"https://api.github.example.com",
],
]
`;
Expand All @@ -24,10 +21,7 @@ Array [
Array [
"some/repo",
"",
Object {
"endpoint": "https://gitlab.example.com/api/v4",
"platform": "gitlab",
},
"https://gitlab.example.com/api/v4",
],
]
`;
Expand All @@ -43,9 +37,7 @@ Array [
Array [
"some/repo",
"default",
Object {
"platform": "github",
},
undefined,
],
]
`;
Expand All @@ -61,9 +53,7 @@ Array [
Array [
"some/repo",
"",
Object {
"platform": "GitLab",
},
undefined,
],
]
`;
Expand Down
25 changes: 7 additions & 18 deletions lib/config/presets/github.spec.ts
Expand Up @@ -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');
Expand Down Expand Up @@ -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'
Expand Down
21 changes: 12 additions & 9 deletions lib/config/presets/github.ts
Expand Up @@ -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);
Expand Down Expand Up @@ -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<Preset> {
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(
Expand All @@ -74,3 +70,10 @@ export async function getPreset(
}
return fetchJSONFile(pkgName, `${presetName}.json`, endpoint);
}

export async function getPreset(
pkgName: string,
presetName = 'default'
): Promise<Preset> {
return getPresetFromEndpoint(pkgName, presetName);
}
24 changes: 7 additions & 17 deletions lib/config/presets/gitlab.spec.ts
Expand Up @@ -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');
Expand Down Expand Up @@ -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'
Expand Down
22 changes: 12 additions & 10 deletions lib/config/presets/gitlab.ts
Expand Up @@ -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;

Expand All @@ -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<Preset> {
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(
Expand Down Expand Up @@ -71,3 +66,10 @@ export async function getPreset(
throw new Error('invalid preset JSON');
}
}

export async function getPreset(
pkgName: string,
presetName = 'default'
): Promise<Preset> {
return getPresetFromEndpoint(pkgName, presetName);
}
15 changes: 13 additions & 2 deletions lib/config/presets/local.spec.ts
Expand Up @@ -5,8 +5,12 @@ import * as local from './local';
jest.mock('./gitlab');
jest.mock('./github');

const gitlabGetPreset: jest.Mock<Promise<any>> = gitlab.getPreset as never;
const githubGetPreset: jest.Mock<Promise<any>> = github.getPreset as never;
const gitlabGetPreset: jest.Mock<Promise<
any
>> = gitlab.getPresetFromEndpoint as never;
const githubGetPreset: jest.Mock<Promise<
any
>> = github.getPresetFromEndpoint as never;

describe('config/presets/local', () => {
beforeEach(() => {
Expand All @@ -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',
Expand Down
23 changes: 16 additions & 7 deletions lib/config/presets/local.ts
Expand Up @@ -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<Preset> {
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.`
);
}

0 comments on commit 9647ea7

Please sign in to comment.