/
get-updated.ts
337 lines (335 loc) · 11.2 KB
/
get-updated.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/* eslint-disable @typescript-eslint/no-unnecessary-type-assertion */
import is from '@sindresorhus/is';
import { WORKER_FILE_UPDATE_FAILED } from '../../../../constants/error-messages';
import { logger } from '../../../../logger';
import { get } from '../../../../modules/manager';
import type {
ArtifactError,
PackageDependency,
} from '../../../../modules/manager/types';
import { getFile } from '../../../../util/git';
import type { FileAddition, FileChange } from '../../../../util/git/types';
import type { BranchConfig } from '../../../types';
import { doAutoReplace } from './auto-replace';
export interface PackageFilesResult {
artifactErrors: ArtifactError[];
reuseExistingBranch?: boolean;
updatedPackageFiles: FileChange[];
updatedArtifacts: FileChange[];
}
export async function getUpdatedPackageFiles(
config: BranchConfig
): Promise<PackageFilesResult> {
logger.trace({ config });
const reuseExistingBranch = config.reuseExistingBranch!;
logger.debug(
`manager.getUpdatedPackageFiles() reuseExistingBranch=${reuseExistingBranch}`
);
let updatedFileContents: Record<string, string> = {};
const nonUpdatedFileContents: Record<string, string> = {};
const packageFileManagers: Record<string, string> = {};
const packageFileUpdatedDeps: Record<string, PackageDependency[]> = {};
const lockFileMaintenanceFiles = [];
for (const upgrade of config.upgrades) {
const manager = upgrade.manager!;
const packageFile = upgrade.packageFile!;
const depName = upgrade.depName!;
const newVersion = upgrade.newVersion!;
const updateLockedDependency = get(manager, 'updateLockedDependency')!;
packageFileManagers[packageFile] = manager;
packageFileUpdatedDeps[packageFile] =
packageFileUpdatedDeps[packageFile] || [];
packageFileUpdatedDeps[packageFile].push({ ...upgrade });
let packageFileContent: string | null = updatedFileContents[packageFile];
if (!packageFileContent) {
packageFileContent = await getFile(
packageFile,
reuseExistingBranch ? config.branchName : config.baseBranch
);
}
let lockFileContent: string | null = null;
const lockFile = upgrade.lockFile ?? upgrade.lockFiles?.[0] ?? '';
if (lockFile) {
lockFileContent = updatedFileContents[lockFile];
if (!lockFileContent) {
lockFileContent = await getFile(
lockFile,
reuseExistingBranch ? config.branchName : config.baseBranch
);
}
}
// istanbul ignore if
if (
reuseExistingBranch &&
(!packageFileContent || (lockFile && !lockFileContent))
) {
logger.debug(
{ packageFile, depName },
'Rebasing branch after file not found'
);
return getUpdatedPackageFiles({
...config,
reuseExistingBranch: false,
});
}
if (upgrade.updateType === 'lockFileMaintenance') {
lockFileMaintenanceFiles.push(packageFile);
} else if (upgrade.isRemediation) {
const { status, files } = await updateLockedDependency({
...upgrade,
depName,
newVersion,
packageFile,
packageFileContent: packageFileContent!,
lockFile,
lockFileContent: lockFileContent!,
allowParentUpdates: true,
allowHigherOrRemoved: true,
});
if (reuseExistingBranch && status !== 'already-updated') {
logger.debug(
{ lockFile, depName, status },
'Need to retry branch as it is not already up-to-date'
);
return getUpdatedPackageFiles({
...config,
reuseExistingBranch: false,
});
}
if (files) {
updatedFileContents = { ...updatedFileContents, ...files };
}
if (status === 'update-failed' || status === 'unsupported') {
upgrade.remediationNotPossible = true;
}
} else if (upgrade.isLockfileUpdate) {
if (updateLockedDependency) {
const { status, files } = await updateLockedDependency({
...upgrade,
depName,
newVersion,
packageFile,
packageFileContent: packageFileContent!,
lockFile,
lockFileContent: lockFileContent!,
allowParentUpdates: false,
});
if (status === 'unsupported') {
// incompatible lock file
nonUpdatedFileContents[packageFile] = packageFileContent!;
} else if (status === 'already-updated') {
logger.debug(
`Upgrade of ${depName} to ${newVersion} is already done in existing branch`
);
} else {
// something changed
if (reuseExistingBranch) {
logger.debug(
{ lockFile, depName, status },
'Need to retry branch as upgrade requirements are not mets'
);
return getUpdatedPackageFiles({
...config,
reuseExistingBranch: false,
});
}
if (files) {
updatedFileContents = { ...updatedFileContents, ...files };
}
}
} else {
logger.debug(
{ manager },
'isLockFileUpdate without updateLockedDependency'
);
nonUpdatedFileContents[packageFile] = packageFileContent!;
}
} else {
const bumpPackageVersion = get(manager, 'bumpPackageVersion');
const updateDependency = get(manager, 'updateDependency');
if (!updateDependency) {
let res = await doAutoReplace(
upgrade,
packageFileContent!,
reuseExistingBranch
);
if (res) {
if (bumpPackageVersion && upgrade.bumpVersion) {
const { bumpedContent } = await bumpPackageVersion(
res,
upgrade.packageFileVersion!,
upgrade.bumpVersion
);
res = bumpedContent;
}
if (res === packageFileContent) {
logger.debug({ packageFile, depName }, 'No content changed');
} else {
logger.debug({ packageFile, depName }, 'Contents updated');
updatedFileContents[packageFile] = res!;
}
continue;
} else if (reuseExistingBranch) {
return getUpdatedPackageFiles({
...config,
reuseExistingBranch: false,
});
}
logger.error({ packageFile, depName }, 'Could not autoReplace');
throw new Error(WORKER_FILE_UPDATE_FAILED);
}
let newContent = await updateDependency({
fileContent: packageFileContent!,
upgrade,
});
if (bumpPackageVersion && upgrade.bumpVersion) {
const { bumpedContent } = await bumpPackageVersion(
newContent!,
upgrade.packageFileVersion!,
upgrade.bumpVersion
);
newContent = bumpedContent;
}
if (!newContent) {
if (reuseExistingBranch) {
logger.debug(
{ packageFile, depName },
'Rebasing branch after error updating content'
);
return getUpdatedPackageFiles({
...config,
reuseExistingBranch: false,
});
}
logger.debug(
{ existingContent: packageFileContent, config: upgrade },
'Error updating file'
);
throw new Error(WORKER_FILE_UPDATE_FAILED);
}
if (newContent !== packageFileContent) {
if (reuseExistingBranch) {
// This ensure it's always 1 commit from the bot
logger.debug(
{ packageFile, depName },
'Need to update package file so will rebase first'
);
return getUpdatedPackageFiles({
...config,
reuseExistingBranch: false,
});
}
logger.debug(`Updating ${depName} in ${packageFile || lockFile}`);
updatedFileContents[packageFile] = newContent;
}
if (newContent === packageFileContent) {
if (upgrade.manager === 'git-submodules') {
updatedFileContents[packageFile] = newContent;
}
}
}
}
const updatedPackageFiles: FileAddition[] = Object.keys(
updatedFileContents
).map((name) => ({
type: 'addition',
path: name,
contents: updatedFileContents[name],
}));
const updatedArtifacts: FileChange[] = [];
const artifactErrors: ArtifactError[] = [];
for (const packageFile of updatedPackageFiles) {
const manager = packageFileManagers[packageFile.path];
const updatedDeps = packageFileUpdatedDeps[packageFile.path];
const updateArtifacts = get(manager, 'updateArtifacts');
if (updateArtifacts) {
const results = await updateArtifacts({
packageFileName: packageFile.path,
updatedDeps,
// TODO #7154
newPackageFileContent: packageFile.contents!.toString(),
config,
});
if (is.nonEmptyArray(results)) {
for (const res of results) {
const { file, artifactError } = res;
if (file) {
updatedArtifacts.push(file);
} else if (artifactError) {
artifactErrors.push(artifactError);
}
}
}
}
}
const nonUpdatedPackageFiles: FileAddition[] = Object.keys(
nonUpdatedFileContents
).map((name) => ({
type: 'addition',
path: name,
contents: nonUpdatedFileContents[name],
}));
for (const packageFile of nonUpdatedPackageFiles) {
const manager = packageFileManagers[packageFile.path];
const updatedDeps = packageFileUpdatedDeps[packageFile.path];
const updateArtifacts = get(manager, 'updateArtifacts');
if (updateArtifacts) {
const results = await updateArtifacts({
packageFileName: packageFile.path,
updatedDeps,
// TODO #7154
newPackageFileContent: packageFile.contents!.toString(),
config,
});
if (is.nonEmptyArray(results)) {
updatedPackageFiles.push(packageFile);
for (const res of results) {
const { file, artifactError } = res;
// istanbul ignore else
if (file) {
updatedArtifacts.push(file);
} else if (artifactError) {
artifactErrors.push(artifactError);
}
}
}
}
}
if (!reuseExistingBranch) {
// Only perform lock file maintenance if it's a fresh commit
for (const packageFile of lockFileMaintenanceFiles) {
const manager = packageFileManagers[packageFile];
const updateArtifacts = get(manager, 'updateArtifacts');
if (updateArtifacts) {
const packageFileContents =
updatedFileContents[packageFile] ||
(await getFile(
packageFile,
reuseExistingBranch ? config.branchName : config.baseBranch
));
const results = await updateArtifacts({
packageFileName: packageFile,
updatedDeps: [],
newPackageFileContent: packageFileContents!,
config,
});
if (is.nonEmptyArray(results)) {
for (const res of results) {
const { file, artifactError } = res;
if (file) {
updatedArtifacts.push(file);
} else if (artifactError) {
artifactErrors.push(artifactError);
}
}
}
}
}
}
return {
reuseExistingBranch, // Need to overwrite original config
updatedPackageFiles,
updatedArtifacts,
artifactErrors,
};
}