/
flatten.ts
160 lines (157 loc) · 5.9 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
import {
filterConfig,
getManagerConfig,
mergeChildConfig,
} from '../../../config';
import type { RenovateConfig } from '../../../config/types';
import { getDefaultConfig } from '../../../datasource';
import { get } from '../../../manager';
import { applyPackageRules } from '../../../util/package-rules';
import type { BranchUpgradeConfig } from '../../types';
import { generateBranchName } from './branch-name';
const upper = (str: string): string =>
str.charAt(0).toUpperCase() + str.substr(1);
export function applyUpdateConfig(input: BranchUpgradeConfig): any {
const updateConfig = { ...input };
delete updateConfig.packageRules;
// TODO: Remove next line once #8075 is complete
updateConfig.depNameSanitized = updateConfig.depName
? updateConfig.depName
.replace('@types/', '')
.replace('@', '')
.replace(/\//g, '-')
.replace(/\s+/g, '-')
.replace(/-+/, '-')
.toLowerCase()
: undefined;
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',
];
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 = '';
}
for (const dep of packageFile.deps) {
if (dep.updates.length) {
const depConfig = mergeChildConfig(packageFileConfig, dep);
delete depConfig.deps;
for (const update of dep.updates) {
let updateConfig = mergeChildConfig(depConfig, update);
delete updateConfig.updates;
updateConfig.newVersion =
updateConfig.newVersion || updateConfig.newValue;
if (updateConfig.updateType) {
updateConfig[`is${upper(updateConfig.updateType)}`] = true;
}
if (updateConfig.updateTypes) {
updateConfig.updateTypes.forEach((updateType) => {
updateConfig[`is${upper(updateType)}`] = true;
});
}
// apply config from datasource
const datasourceConfig = await getDefaultConfig(
depConfig.datasource
);
updateConfig = mergeChildConfig(updateConfig, datasourceConfig);
updateConfig = applyPackageRules(updateConfig);
// 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 = applyPackageRules(updateConfig);
updateConfig = applyUpdateConfig(updateConfig);
updateConfig.baseDeps = packageFile.deps;
update.branchName = updateConfig.branchName;
updates.push(updateConfig);
}
}
}
if (
get(manager, 'supportsLockFileMaintenance') &&
packageFileConfig.lockFileMaintenance.enabled
) {
// Apply lockFileMaintenance config before packageRules
let lockFileConfig = mergeChildConfig(
packageFileConfig,
packageFileConfig.lockFileMaintenance
);
lockFileConfig.updateType = 'lockFileMaintenance';
lockFileConfig = applyPackageRules(lockFileConfig);
// Apply lockFileMaintenance and packageRules again
lockFileConfig = mergeChildConfig(
lockFileConfig,
lockFileConfig.lockFileMaintenance
);
lockFileConfig = applyPackageRules(lockFileConfig);
// 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 remediations = config.remediations?.[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);
}
}
}
}
}
}
return updates
.filter((update) => update.enabled)
.map(({ vulnerabilityAlerts, ...update }) => update)
.map((update) => filterConfig(update, 'branch'));
}