-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
flatten.ts
216 lines (212 loc) · 7.77 KB
/
flatten.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import {
filterConfig,
getManagerConfig,
mergeChildConfig,
} from '../../../config';
import type { RenovateConfig } from '../../../config/types';
import { getDefaultConfig } from '../../../modules/datasource';
import { get } from '../../../modules/manager';
import { detectSemanticCommits } from '../../../util/git/semantic';
import { applyPackageRules } from '../../../util/package-rules';
import { regEx } from '../../../util/regex';
import { parseUrl } from '../../../util/url';
import type { BranchUpgradeConfig } from '../../types';
import { generateBranchName } from './branch-name';
const upper = (str: string): string =>
str.charAt(0).toUpperCase() + str.substring(1);
function sanitizeDepName(depName: string): string {
return depName
.replace('@types/', '')
.replace('@', '')
.replace(regEx(/\//g), '-')
.replace(regEx(/\s+/g), '-')
.replace(regEx(/-+/), '-')
.toLowerCase();
}
export function applyUpdateConfig(input: BranchUpgradeConfig): any {
const updateConfig = { ...input };
delete updateConfig.packageRules;
// TODO: Remove next line once #8075 is complete
updateConfig.depNameSanitized = updateConfig.depName
? sanitizeDepName(updateConfig.depName)
: undefined;
updateConfig.newNameSanitized = updateConfig.newName
? sanitizeDepName(updateConfig.newName)
: undefined;
if (updateConfig.sourceUrl) {
const parsedSourceUrl = parseUrl(updateConfig.sourceUrl);
if (parsedSourceUrl?.pathname) {
updateConfig.sourceRepoSlug = parsedSourceUrl.pathname
.replace(regEx(/^\//), '') // remove leading slash
.replace(regEx(/\//g), '-') // change slashes to hyphens
.replace(regEx(/-+/g), '-'); // remove multiple hyphens
updateConfig.sourceRepo = parsedSourceUrl.pathname.replace(
regEx(/^\//),
'',
); // remove leading slash
updateConfig.sourceRepoOrg = updateConfig.sourceRepo.replace(
regEx(/\/.*/g),
'',
); // remove everything after first slash
updateConfig.sourceRepoName = updateConfig.sourceRepo.replace(
regEx(/.*\//g),
'',
); // remove everything up to the last slash
}
}
generateBranchName(updateConfig);
return updateConfig;
}
export async function flattenUpdates(
config: RenovateConfig,
packageFiles: Record<string, any[]>,
): Promise<RenovateConfig[]> {
const updates = [];
const updateTypes = [
'major',
'minor',
'patch',
'pin',
'digest',
'lockFileMaintenance',
'replacement',
];
for (const [manager, files] of Object.entries(packageFiles)) {
const managerConfig = getManagerConfig(config, manager);
for (const packageFile of files) {
const packageFileConfig = mergeChildConfig(managerConfig, packageFile);
const packagePath = packageFile.packageFile?.split('/');
// istanbul ignore else: can never happen and would throw
if (packagePath.length > 0) {
packagePath.splice(-1, 1);
}
if (packagePath.length > 0) {
packageFileConfig.parentDir = packagePath[packagePath.length - 1];
packageFileConfig.packageFileDir = packagePath.join('/');
} else {
packageFileConfig.parentDir = '';
packageFileConfig.packageFileDir = '';
}
let depIndex = 0;
for (const dep of packageFile.deps) {
if (dep.updates.length) {
const depConfig = mergeChildConfig(packageFileConfig, dep);
delete depConfig.deps;
depConfig.depIndex = depIndex; // used for autoreplace
for (const update of dep.updates) {
let updateConfig = mergeChildConfig(depConfig, update);
delete updateConfig.updates;
if (updateConfig.updateType) {
updateConfig[`is${upper(updateConfig.updateType)}`] = true;
}
if (updateConfig.updateTypes) {
updateConfig.updateTypes.forEach((updateType: string) => {
updateConfig[`is${upper(updateType)}`] = true;
});
}
// apply config from datasource
const datasourceConfig = await getDefaultConfig(
depConfig.datasource,
);
updateConfig = mergeChildConfig(updateConfig, datasourceConfig);
updateConfig = await applyPackageRules(
updateConfig,
'datasource-merge',
);
// apply major/minor/patch/pin/digest
updateConfig = mergeChildConfig(
updateConfig,
updateConfig[updateConfig.updateType],
);
for (const updateType of updateTypes) {
delete updateConfig[updateType];
}
// Apply again in case any were added by the updateType config
updateConfig = await applyPackageRules(
updateConfig,
'update-type-merge',
);
updateConfig = applyUpdateConfig(updateConfig);
updateConfig.baseDeps = packageFile.deps;
update.branchName = updateConfig.branchName;
updates.push(updateConfig);
}
}
depIndex += 1;
}
if (
get(manager, 'supportsLockFileMaintenance') &&
packageFileConfig.lockFileMaintenance.enabled
) {
// Apply lockFileMaintenance config before packageRules
let lockFileConfig = mergeChildConfig(
packageFileConfig,
packageFileConfig.lockFileMaintenance,
);
lockFileConfig.updateType = 'lockFileMaintenance';
lockFileConfig.isLockFileMaintenance = true;
lockFileConfig = await applyPackageRules(
lockFileConfig,
'lock-file-maintenance-merge',
);
// Apply lockFileMaintenance and packageRules again
lockFileConfig = mergeChildConfig(
lockFileConfig,
lockFileConfig.lockFileMaintenance,
);
lockFileConfig = await applyPackageRules(
lockFileConfig,
'lock-file-maintenance-merge-2',
);
// Remove unnecessary objects
for (const updateType of updateTypes) {
delete lockFileConfig[updateType];
}
delete lockFileConfig.packageRules;
delete lockFileConfig.deps;
generateBranchName(lockFileConfig);
updates.push(lockFileConfig);
}
if (get(manager, 'updateLockedDependency')) {
for (const lockFile of packageFileConfig.lockFiles || []) {
const lockfileRemediations = config.remediations as Record<
string,
Record<string, any>[]
>;
const remediations = lockfileRemediations?.[lockFile];
if (remediations) {
for (const remediation of remediations) {
let updateConfig = mergeChildConfig(
packageFileConfig,
remediation,
);
updateConfig = mergeChildConfig(
updateConfig,
config.vulnerabilityAlerts,
);
delete updateConfig.vulnerabilityAlerts;
updateConfig.isVulnerabilityAlert = true;
updateConfig.isRemediation = true;
updateConfig.lockFile = lockFile;
updateConfig.currentValue = updateConfig.currentVersion;
updateConfig.newValue = updateConfig.newVersion;
updateConfig = applyUpdateConfig(updateConfig);
updateConfig.enabled = true;
updates.push(updateConfig);
}
}
}
}
}
}
if (config.semanticCommits === 'auto') {
const semanticCommits = await detectSemanticCommits();
for (const update of updates) {
update.semanticCommits = semanticCommits;
}
}
return updates
.filter((update) => update.enabled)
.map(({ vulnerabilityAlerts, ...update }) => update)
.map((update) => filterConfig(update, 'branch'));
}