Skip to content

Commit

Permalink
fix: deep merge config and platformInfo to avoid losing hostRules (#2…
Browse files Browse the repository at this point in the history
…5116)

Co-authored-by: Rhys Arkins <rhys@arkins.net>
  • Loading branch information
ReinAchten-TomTom and rarkins committed Oct 10, 2023
1 parent 20d6c00 commit 95076df
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 29 deletions.
101 changes: 74 additions & 27 deletions lib/modules/platform/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,34 +119,81 @@ describe('modules/platform/index', () => {
});
});

it('merges platform hostRules with additionalHostRules', async () => {
const config = {
platform: 'github' as PlatformId,
endpoint: 'https://api.github.com',
gitAuthor: 'user@domain.com',
username: 'abc',
token: '123',
};
describe('when platform endpoint is https://api.github.com/', () => {
it('merges config hostRules with platform hostRules', async () => {
const config = {
platform: 'github' as PlatformId,
endpoint: 'https://api.github.com',
gitAuthor: 'user@domain.com',
username: 'abc',
token: '123',
hostRules: [
{
hostType: 'github',
matchHost: 'github.com',
token: '456',
username: 'def',
},
],
};

expect(await platform.initPlatform(config)).toEqual({
endpoint: 'https://api.github.com/',
gitAuthor: 'user@domain.com',
hostRules: [
{
hostType: 'docker',
matchHost: 'ghcr.io',
password: '123',
username: 'USERNAME',
},
{
hostType: 'github',
matchHost: 'api.github.com',
token: '123',
username: 'abc',
},
],
platform: 'github',
renovateUsername: 'abc',
expect(await platform.initPlatform(config)).toEqual({
endpoint: 'https://api.github.com/',
gitAuthor: 'user@domain.com',
hostRules: [
{
hostType: 'github',
matchHost: 'github.com',
token: '456',
username: 'def',
},
{
hostType: 'docker',
matchHost: 'ghcr.io',
password: '123',
username: 'USERNAME',
},
{
hostType: 'github',
matchHost: 'api.github.com',
token: '123',
username: 'abc',
},
],
platform: 'github',
renovateUsername: 'abc',
});
});

it('merges platform hostRules with additionalHostRules', async () => {
const config = {
platform: 'github' as PlatformId,
endpoint: 'https://api.github.com',
gitAuthor: 'user@domain.com',
username: 'abc',
token: '123',
};

expect(await platform.initPlatform(config)).toEqual({
endpoint: 'https://api.github.com/',
gitAuthor: 'user@domain.com',
hostRules: [
{
hostType: 'docker',
matchHost: 'ghcr.io',
password: '123',
username: 'USERNAME',
},
{
hostType: 'github',
matchHost: 'api.github.com',
token: '123',
username: 'abc',
},
],
platform: 'github',
renovateUsername: 'abc',
});
});
});
});
10 changes: 8 additions & 2 deletions lib/modules/platform/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,14 @@ export async function initPlatform(config: AllConfig): Promise<AllConfig> {
setPlatformApi(config.platform!);
// TODO: types
const platformInfo = await platform.initPlatform(config);
const returnConfig: any = { ...config, ...platformInfo };
const returnConfig: any = {
...config,
...platformInfo,
hostRules: [
...(config.hostRules ?? []),
...(platformInfo?.hostRules ?? []),
],
};
// istanbul ignore else
if (config?.gitAuthor) {
logger.debug(`Using configured gitAuthor (${config.gitAuthor})`);
Expand Down Expand Up @@ -75,7 +82,6 @@ export async function initPlatform(config: AllConfig): Promise<AllConfig> {
delete returnConfig[field];
}
});
returnConfig.hostRules = returnConfig.hostRules || [];
const typedPlatformRule = {
...platformRule,
hostType: returnConfig.platform,
Expand Down

0 comments on commit 95076df

Please sign in to comment.