-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
get-updated.ts
246 lines (244 loc) · 8.41 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
import is from '@sindresorhus/is';
import { WORKER_FILE_UPDATE_FAILED } from '../../constants/error-messages';
import * as datasourceGitSubmodules from '../../datasource/git-submodules';
import { logger } from '../../logger';
import { get } from '../../manager';
import { ArtifactError } from '../../manager/common';
import { File, getFile } from '../../util/git';
import { BranchConfig } from '../common';
import { doAutoReplace } from './auto-replace';
export interface PackageFilesResult {
artifactErrors: ArtifactError[];
reuseExistingBranch?: boolean;
updatedPackageFiles: File[];
updatedArtifacts: File[];
}
export async function getUpdatedPackageFiles(
config: BranchConfig
): Promise<PackageFilesResult> {
logger.trace({ config });
const { reuseExistingBranch } = config;
logger.debug(
`manager.getUpdatedPackageFiles() reuseExistinbranch=${reuseExistingBranch}`
);
const updatedFileContents: Record<string, string> = {};
const nonUpdatedFileContents: Record<string, string> = {};
const packageFileManagers: Record<string, string> = {};
const packageFileUpdatedDeps: Record<string, string[]> = {};
const lockFileMaintenanceFiles = [];
for (const upgrade of config.upgrades) {
const { manager, packageFile, depName } = upgrade;
packageFileManagers[packageFile] = manager;
packageFileUpdatedDeps[packageFile] =
packageFileUpdatedDeps[packageFile] || [];
packageFileUpdatedDeps[packageFile].push(depName);
let packageFileContent = updatedFileContents[packageFile];
if (!packageFileContent) {
packageFileContent = await getFile(
packageFile,
reuseExistingBranch ? config.branchName : config.baseBranch
);
}
// istanbul ignore if
if (config.reuseExistingBranch && !packageFileContent) {
logger.debug(
{ packageFile, depName },
'Rebasing branch after file not found'
);
return getUpdatedPackageFiles({
...config,
reuseExistingBranch: false,
});
}
if (upgrade.updateType === 'lockFileMaintenance') {
lockFileMaintenanceFiles.push(packageFile);
} 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');
if (upgrade.rangeStrategy === 'update-lockfile') {
logger.debug({ packageFile, depName }, 'update-lockfile add');
nonUpdatedFileContents[packageFile] = res;
}
} else {
logger.debug({ packageFile, depName }, 'Contents updated');
updatedFileContents[packageFile] = res;
}
continue; // eslint-disable-line no-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 (config.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 (config.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}`);
updatedFileContents[packageFile] = newContent;
}
if (newContent === packageFileContent) {
// istanbul ignore else
if (upgrade.datasource === datasourceGitSubmodules.id) {
updatedFileContents[packageFile] = newContent;
} else if (upgrade.rangeStrategy === 'update-lockfile') {
nonUpdatedFileContents[packageFile] = newContent;
}
}
}
}
const updatedPackageFiles = Object.keys(updatedFileContents).map((name) => ({
name,
contents: updatedFileContents[name],
}));
const updatedArtifacts: File[] = [];
const artifactErrors: ArtifactError[] = [];
for (const packageFile of updatedPackageFiles) {
const manager = packageFileManagers[packageFile.name];
const updatedDeps = packageFileUpdatedDeps[packageFile.name];
const updateArtifacts = get(manager, 'updateArtifacts');
if (updateArtifacts) {
const results = await updateArtifacts({
packageFileName: packageFile.name,
updatedDeps,
newPackageFileContent: packageFile.contents,
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 = Object.keys(nonUpdatedFileContents).map(
(name) => ({
name,
contents: nonUpdatedFileContents[name],
})
);
for (const packageFile of nonUpdatedPackageFiles) {
const manager = packageFileManagers[packageFile.name];
const updatedDeps = packageFileUpdatedDeps[packageFile.name];
const updateArtifacts = get(manager, 'updateArtifacts');
if (updateArtifacts) {
const results = await updateArtifacts({
packageFileName: packageFile.name,
updatedDeps,
newPackageFileContent: packageFile.contents,
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 (!config.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,
config.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: config.reuseExistingBranch, // Need to overwrite original config
updatedPackageFiles,
updatedArtifacts,
artifactErrors,
};
}