-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
host-rules-migration.ts
112 lines (99 loc) · 3.01 KB
/
host-rules-migration.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import is from '@sindresorhus/is';
import { CONFIG_VALIDATION } from '../../../constants/error-messages';
import { logger } from '../../../logger';
import type { HostRule } from '../../../types';
import type { LegacyHostRule } from '../../../util/host-rules';
import { AbstractMigration } from '../base/abstract-migration';
import { migrateDatasource } from './datasource-migration';
export class HostRulesMigration extends AbstractMigration {
override readonly propertyName = 'hostRules';
override run(value: (LegacyHostRule & HostRule)[]): void {
const newHostRules: HostRule[] = [];
for (const hostRule of value) {
validateHostRule(hostRule);
const newRule: any = {};
for (const [key, value] of Object.entries(hostRule)) {
if (key === 'platform') {
if (is.string(value)) {
newRule.hostType ??= value;
}
continue;
}
if (key === 'matchHost') {
if (is.string(value)) {
newRule.matchHost ??= massageUrl(value);
}
continue;
}
if (key === 'hostType') {
if (is.string(value)) {
newRule.hostType ??= migrateDatasource(value);
}
continue;
}
if (
key === 'endpoint' ||
key === 'host' ||
key === 'baseUrl' ||
key === 'hostName' ||
key === 'domainName'
) {
if (is.string(value)) {
newRule.matchHost ??= massageUrl(value);
}
continue;
}
newRule[key] = value;
}
newHostRules.push(newRule);
}
this.rewrite(newHostRules);
}
}
function validateHostRule(rule: LegacyHostRule & HostRule): void {
const { matchHost, hostName, domainName, baseUrl, endpoint, host } = rule;
const hosts: Record<string, string> = removeUndefinedFields({
matchHost,
hostName,
domainName,
baseUrl,
endpoint,
host,
});
if (Object.keys(hosts).length > 1) {
const distinctHostValues = new Set(Object.values(hosts));
// check if the host values are duplicated
if (distinctHostValues.size > 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;
} else {
logger.warn(
{ hosts },
'Duplicate host values found, please only use `matchHost` to specify the host'
);
}
}
}
function massageUrl(url: string): string {
if (!url.includes('://') && url.includes('/')) {
return 'https://' + url;
} else {
return url;
}
}
function removeUndefinedFields(
obj: Record<string, any>
): Record<string, string> {
const result: Record<string, string> = {};
for (const key of Object.keys(obj)) {
if (is.string(obj[key])) {
result[key] = obj[key];
}
}
return result;
}