-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathget-updated.ts
192 lines (190 loc) · 6.43 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
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 { branchName, reuseExistingBranch } = config;
logger.debug(
{ reuseExistingBranch, branchName },
'manager.getUpdatedPackageFiles()'
);
const updatedFileContents: 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);
if (upgrade.updateType === 'lockFileMaintenance') {
lockFileMaintenanceFiles.push(packageFile);
} else {
let existingContent = updatedFileContents[packageFile];
if (!existingContent) {
existingContent = await getFile(
packageFile,
reuseExistingBranch ? config.branchName : config.baseBranch
);
}
// istanbul ignore if
if (config.reuseExistingBranch && !existingContent) {
logger.debug(
{ packageFile, depName },
'Rebasing branch after file not found'
);
return getUpdatedPackageFiles({
...config,
reuseExistingBranch: false,
});
}
const updateDependency = get(manager, 'updateDependency');
if (!updateDependency) {
const res = await doAutoReplace(
upgrade,
existingContent,
reuseExistingBranch
);
if (res) {
if (res === existingContent) {
logger.debug({ packageFile, depName }, 'No content changed');
} 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);
}
const newContent = await updateDependency({
fileContent: existingContent,
upgrade,
});
if (!newContent) {
if (config.reuseExistingBranch) {
logger.debug(
{ packageFile, depName },
'Rebasing branch after error updating content'
);
return getUpdatedPackageFiles({
...config,
reuseExistingBranch: false,
});
}
logger.debug(
{ existingContent, config: upgrade },
'Error updating file'
);
throw new Error(WORKER_FILE_UPDATE_FAILED);
}
if (newContent !== existingContent) {
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({ packageFile, depName }, 'Updating packageFile content');
updatedFileContents[packageFile] = newContent;
}
if (
newContent === existingContent &&
upgrade.datasource === datasourceGitSubmodules.id
) {
updatedFileContents[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);
}
}
}
}
}
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,
};
}