Skip to content

Commit

Permalink
- revert changes to add() and migrateRule()
Browse files Browse the repository at this point in the history
- refactor custom migration code
- update type of LegacyHostRule
  • Loading branch information
RahulGautamSingh committed Apr 26, 2023
1 parent 34b86ee commit 588fda1
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 34 deletions.
33 changes: 6 additions & 27 deletions lib/config/migrations/custom/host-rules-migration.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
import is from '@sindresorhus/is';
import { CONFIG_VALIDATION } from '../../../constants/error-messages';
import type { HostRule } from '../../../types';
import { clone } from '../../../util/clone';
import type { LegacyHostRule } from '../../../util/host-rules';
import { AbstractMigration } from '../base/abstract-migration';
import { migrateDatasource } from './datasource-migration';

interface LegacyHostRule {
hostName?: string;
domainName?: string;
baseUrl?: string;
host?: string;
endpoint?: string;
}

export class HostRulesMigration extends AbstractMigration {
override readonly propertyName = 'hostRules';

override run(value: (LegacyHostRule & HostRule)[]): void {
const newHostRules: HostRule[] = [];
for (let hostRule of value) {
hostRule = migrateRule(hostRule);
for (const hostRule of value) {
validateHostRule(hostRule);

const newRule: any = {};

Expand Down Expand Up @@ -68,16 +60,8 @@ export class HostRulesMigration extends AbstractMigration {
}
}

function migrateRule(rule: LegacyHostRule & HostRule): HostRule {
const cloned: LegacyHostRule & HostRule = clone(rule);
delete cloned.hostName;
delete cloned.domainName;
delete cloned.baseUrl;
delete cloned.endpoint;
delete cloned.host;
const result: HostRule = cloned;

const { matchHost } = result;
function validateHostRule(rule: LegacyHostRule & HostRule): void {
const { matchHost } = rule;
const { hostName, domainName, baseUrl, endpoint, host } = rule;
const hostValues = [
matchHost,
Expand All @@ -87,19 +71,14 @@ function migrateRule(rule: LegacyHostRule & HostRule): HostRule {
endpoint,
host,
].filter(is.string);
if (hostValues.length === 1) {
const [matchHost] = hostValues;
result.matchHost = massageUrl(matchHost);
} else if (hostValues.length > 1) {
if (hostValues.length > 1) {
const error = new Error(CONFIG_VALIDATION);
error.validationSource = 'config';
error.validationMessage = `hostRules cannot contain more than one host-matching field - use "matchHost" only.`;
error.validationError =
'The renovate configuration file contains some invalid settings';
throw error;
}

return result;
}

function massageUrl(url: string): string {
Expand Down
56 changes: 50 additions & 6 deletions lib/util/host-rules.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,50 @@ describe('util/host-rules', () => {
clear();
});

describe('add()', () => {
it('throws if both domainName and hostName', () => {
expect(() =>
add({
hostType: 'azure',
domainName: 'github.com',
hostName: 'api.github.com',
} as HostRule)
).toThrow();
});

it('throws if both domainName and baseUrl', () => {
expect(() =>
add({
hostType: 'azure',
domainName: 'github.com',
matchHost: 'https://api.github.com',
} as HostRule)
).toThrow();
});

it('throws if both hostName and baseUrl', () => {
expect(() =>
add({
hostType: 'azure',
hostName: 'api.github.com',
matchHost: 'https://api.github.com',
} as HostRule)
).toThrow();
});

it('supports baseUrl-only', () => {
add({
matchHost: 'https://some.endpoint',
username: 'user1',
password: 'pass1',
});
expect(find({ url: 'https://some.endpoint/v3/' })).toEqual({
password: 'pass1',
username: 'user1',
});
});
});

describe('find()', () => {
beforeEach(() => {
clear();
Expand All @@ -27,11 +71,11 @@ describe('util/host-rules', () => {
it('needs exact host matches', () => {
add({
hostType: NugetDatasource.id,
matchHost: 'nuget.org',
hostName: 'nuget.org',
username: 'root',
password: 'p4$$w0rd',
token: undefined,
});
} as HostRule);
expect(find({ hostType: NugetDatasource.id })).toEqual({});
expect(
find({ hostType: NugetDatasource.id, url: 'https://nuget.org' })
Expand Down Expand Up @@ -65,7 +109,7 @@ describe('util/host-rules', () => {

it('matches on domainName', () => {
add({
matchHost: 'github.com',
domainName: 'github.com',
token: 'def',
} as HostRule);
expect(
Expand Down Expand Up @@ -148,7 +192,7 @@ describe('util/host-rules', () => {

it('matches on hostName', () => {
add({
matchHost: 'nuget.local',
hostName: 'nuget.local',
token: 'abc',
} as HostRule);
expect(
Expand Down Expand Up @@ -247,7 +291,7 @@ describe('util/host-rules', () => {
});
add({
hostType: NugetDatasource.id,
matchHost: 'my.local.registry',
hostName: 'my.local.registry',
token: 'def',
} as HostRule);
add({
Expand Down Expand Up @@ -280,7 +324,7 @@ describe('util/host-rules', () => {
it('needs exact host matches', () => {
const hostRule = {
hostType: 'nuget',
matchHost: 'nuget.org',
hostName: 'nuget.org',
username: 'root',
password: 'p4$$w0rd',
};
Expand Down
33 changes: 32 additions & 1 deletion lib/util/host-rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,39 @@ import { parseUrl, validateUrl } from './url';

let hostRules: HostRule[] = [];

export interface LegacyHostRule {
hostName?: string;
domainName?: string;
baseUrl?: string;
host?: string;
endpoint?: string;
}

export function migrateRule(rule: LegacyHostRule & HostRule): HostRule {
const cloned: LegacyHostRule & HostRule = clone(rule);
delete cloned.hostName;
delete cloned.domainName;
delete cloned.baseUrl;
const result: HostRule = cloned;

const { matchHost } = result;
const { hostName, domainName, baseUrl } = rule;
const hostValues = [matchHost, hostName, domainName, baseUrl].filter(Boolean);
if (hostValues.length === 1) {
const [matchHost] = hostValues;
result.matchHost = matchHost;
} else if (hostValues.length > 1) {
throw new Error(
`hostRules cannot contain more than one host-matching field - use "matchHost" only.`
);
}

return result;
}

export function add(params: HostRule): void {
const rule = clone(params);
const rule = migrateRule(params);

const confidentialFields: (keyof HostRule)[] = ['password', 'token'];
if (rule.matchHost) {
const parsedUrl = parseUrl(rule.matchHost);
Expand Down

0 comments on commit 588fda1

Please sign in to comment.