This repository has been archived by the owner on May 21, 2024. It is now read-only.
forked from renovatebot/renovate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
migration.ts
197 lines (193 loc) · 6.53 KB
/
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import is from '@sindresorhus/is';
import { dequal } from 'dequal';
import { logger } from '../logger';
import { clone } from '../util/clone';
import { regEx } from '../util/regex';
import { MigrationsService } from './migrations';
import { getOptions } from './options';
import type {
MigratedConfig,
MigratedRenovateConfig,
PackageRule,
RenovateConfig,
RenovateOptions,
} from './types';
import { mergeChildConfig } from './utils';
const options = getOptions();
export function fixShortHours(input: string): string {
return input.replace(regEx(/( \d?\d)((a|p)m)/g), '$1:00$2');
}
let optionTypes: Record<string, RenovateOptions['type']>;
// Returns a migrated config
export function migrateConfig(config: RenovateConfig): MigratedConfig {
try {
if (!optionTypes) {
optionTypes = {};
options.forEach((option) => {
optionTypes[option.name] = option.type;
});
}
const newConfig = MigrationsService.run(config);
const migratedConfig = clone(newConfig) as MigratedRenovateConfig;
for (const [key, val] of Object.entries(newConfig)) {
if (is.string(val) && val.includes('{{baseDir}}')) {
migratedConfig[key] = val.replace(
regEx(/{{baseDir}}/g),
'{{packageFileDir}}',
);
} else if (is.string(val) && val.includes('{{lookupName}}')) {
migratedConfig[key] = val.replace(
regEx(/{{lookupName}}/g),
'{{packageName}}',
);
} else if (is.string(val) && val.includes('{{depNameShort}}')) {
migratedConfig[key] = val.replace(
regEx(/{{depNameShort}}/g),
'{{depName}}',
);
} else if (is.string(val) && val.startsWith('{{semanticPrefix}}')) {
migratedConfig[key] = val.replace(
'{{semanticPrefix}}',
'{{#if semanticCommitType}}{{semanticCommitType}}{{#if semanticCommitScope}}({{semanticCommitScope}}){{/if}}: {{/if}}',
);
} else if (optionTypes[key] === 'object' && is.boolean(val)) {
migratedConfig[key] = { enabled: val };
} else if (optionTypes[key] === 'boolean') {
if (val === 'true') {
migratedConfig[key] = true;
} else if (val === 'false') {
migratedConfig[key] = false;
}
} else if (
optionTypes[key] === 'string' &&
is.array(val) &&
val.length === 1
) {
migratedConfig[key] = String(val[0]);
} else if (is.array(val)) {
if (is.array(migratedConfig?.[key])) {
const newArray = [];
for (const item of migratedConfig[key] as unknown[]) {
if (is.object(item) && !is.array(item)) {
const arrMigrate = migrateConfig(item as RenovateConfig);
newArray.push(arrMigrate.migratedConfig);
} else {
newArray.push(item);
}
}
migratedConfig[key] = newArray;
}
} else if (is.object(val)) {
const subMigrate = migrateConfig(migratedConfig[key] as RenovateConfig);
if (subMigrate.isMigrated) {
migratedConfig[key] = subMigrate.migratedConfig;
}
}
const migratedTemplates = {
fromVersion: 'currentVersion',
newValueMajor: 'newMajor',
newValueMinor: 'newMinor',
newVersionMajor: 'newMajor',
newVersionMinor: 'newMinor',
toVersion: 'newVersion',
};
if (is.string(migratedConfig[key])) {
for (const [from, to] of Object.entries(migratedTemplates)) {
migratedConfig[key] = (migratedConfig[key] as string).replace(
regEx(from, 'g'),
to,
);
}
}
}
const languages = [
'docker',
'dotnet',
'golang',
'java',
'js',
'node',
'php',
'python',
'ruby',
'rust',
];
for (const language of languages) {
if (is.nonEmptyObject(migratedConfig[language])) {
migratedConfig.packageRules ??= [];
const currentContent = migratedConfig[language] as any;
const packageRule = {
matchCategories: [language],
...currentContent,
};
migratedConfig.packageRules.unshift(packageRule);
delete migratedConfig[language];
}
}
// Migrate nested packageRules
if (is.nonEmptyArray(migratedConfig.packageRules)) {
const existingRules = migratedConfig.packageRules;
migratedConfig.packageRules = [];
for (const packageRule of existingRules) {
if (is.array(packageRule.packageRules)) {
logger.debug('Flattening nested packageRules');
// merge each subrule and add to the parent list
for (const subrule of packageRule.packageRules) {
// TODO: fix types #22198
const combinedRule = mergeChildConfig(
packageRule,
subrule as PackageRule,
);
delete combinedRule.packageRules;
migratedConfig.packageRules.push(combinedRule);
}
} else {
migratedConfig.packageRules.push(packageRule);
}
}
}
if (
is.nonEmptyObject(migratedConfig['pip-compile']) &&
is.nonEmptyArray(migratedConfig['pip-compile'].fileMatch)
) {
migratedConfig['pip-compile'].fileMatch = migratedConfig[
'pip-compile'
].fileMatch.map((fileMatch) => {
const match = fileMatch as string;
if (match.endsWith('.in')) {
return match.replace(/\.in$/, '.txt');
}
return match.replace(/\.in\$$/, '.txt$');
});
}
if (is.nonEmptyArray(migratedConfig.matchManagers)) {
if (migratedConfig.matchManagers.includes('gradle-lite')) {
if (!migratedConfig.matchManagers.includes('gradle')) {
migratedConfig.matchManagers.push('gradle');
}
migratedConfig.matchManagers = migratedConfig.matchManagers.filter(
(manager) => manager !== 'gradle-lite',
);
}
}
if (is.nonEmptyObject(migratedConfig['gradle-lite'])) {
migratedConfig.gradle = mergeChildConfig(
migratedConfig.gradle ?? {},
migratedConfig['gradle-lite'],
);
}
delete migratedConfig['gradle-lite'];
const isMigrated = !dequal(config, migratedConfig);
if (isMigrated) {
// recursive call in case any migrated configs need further migrating
return {
isMigrated,
migratedConfig: migrateConfig(migratedConfig).migratedConfig,
};
}
return { isMigrated, migratedConfig };
} catch (err) /* istanbul ignore next */ {
logger.debug({ config, err }, 'migrateConfig() error');
throw err;
}
}