forked from renovatebot/renovate
/
execute-post-upgrade-commands.ts
201 lines (186 loc) · 6.89 KB
/
execute-post-upgrade-commands.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
// TODO #7154
import is from '@sindresorhus/is';
import { minimatch } from 'minimatch';
import { mergeChildConfig } from '../../../../config';
import { GlobalConfig } from '../../../../config/global';
import { addMeta, logger } from '../../../../logger';
import type { ArtifactError } from '../../../../modules/manager/types';
import { exec } from '../../../../util/exec';
import {
localPathIsFile,
readLocalFile,
writeLocalFile,
} from '../../../../util/fs';
import { getRepoStatus } from '../../../../util/git';
import type { FileChange } from '../../../../util/git/types';
import { regEx } from '../../../../util/regex';
import { sanitize } from '../../../../util/sanitize';
import { compile } from '../../../../util/template';
import type { BranchConfig, BranchUpgradeConfig } from '../../../types';
export interface PostUpgradeCommandsExecutionResult {
updatedArtifacts: FileChange[];
artifactErrors: ArtifactError[];
}
export async function postUpgradeCommandsExecutor(
filteredUpgradeCommands: BranchUpgradeConfig[],
config: BranchConfig
): Promise<PostUpgradeCommandsExecutionResult> {
let updatedArtifacts = [...(config.updatedArtifacts ?? [])];
const artifactErrors = [...(config.artifactErrors ?? [])];
const { allowedPostUpgradeCommands, allowPostUpgradeCommandTemplating } =
GlobalConfig.get();
for (const upgrade of filteredUpgradeCommands) {
addMeta({ dep: upgrade.depName });
logger.trace(
{
tasks: upgrade.postUpgradeTasks,
allowedCommands: allowedPostUpgradeCommands,
},
`Checking for post-upgrade tasks`
);
const commands = upgrade.postUpgradeTasks?.commands ?? [];
const fileFilters = upgrade.postUpgradeTasks?.fileFilters ?? [];
if (is.nonEmptyArray(commands)) {
// Persist updated files in file system so any executed commands can see them
for (const file of config.updatedPackageFiles!.concat(updatedArtifacts)) {
const canWriteFile = await localPathIsFile(file.path);
if (file.type === 'addition' && canWriteFile) {
let contents: Buffer | null;
if (typeof file.contents === 'string') {
contents = Buffer.from(file.contents);
} else {
contents = file.contents;
}
// TODO #7154
await writeLocalFile(file.path, contents!);
}
}
for (const cmd of commands) {
if (
allowedPostUpgradeCommands!.some((pattern) =>
regEx(pattern).test(cmd)
)
) {
try {
const compiledCmd = allowPostUpgradeCommandTemplating
? compile(cmd, mergeChildConfig(config, upgrade))
: cmd;
logger.trace({ cmd: compiledCmd }, 'Executing post-upgrade task');
const execResult = await exec(compiledCmd, {
cwd: GlobalConfig.get('localDir'),
});
logger.debug(
{ cmd: compiledCmd, ...execResult },
'Executed post-upgrade task'
);
} catch (error) {
artifactErrors.push({
lockFile: upgrade.packageFile,
stderr: sanitize(error.message),
});
}
} else {
logger.warn(
{
cmd,
allowedPostUpgradeCommands,
},
'Post-upgrade task did not match any on allowedPostUpgradeCommands list'
);
artifactErrors.push({
lockFile: upgrade.packageFile,
stderr: sanitize(
`Post-upgrade command '${cmd}' has not been added to the allowed list in allowedPostUpgradeCommands`
),
});
}
}
const status = await getRepoStatus();
for (const relativePath of status.modified.concat(status.not_added)) {
for (const pattern of fileFilters) {
if (minimatch(relativePath, pattern)) {
logger.debug(
{ file: relativePath, pattern },
'Post-upgrade file saved'
);
const existingContent = await readLocalFile(relativePath);
const existingUpdatedArtifacts = updatedArtifacts.find(
(ua) => ua.path === relativePath
);
if (existingUpdatedArtifacts?.type === 'addition') {
existingUpdatedArtifacts.contents = existingContent;
} else {
updatedArtifacts.push({
type: 'addition',
path: relativePath,
contents: existingContent,
});
}
// If the file is deleted by a previous post-update command, remove the deletion from updatedArtifacts
updatedArtifacts = updatedArtifacts.filter(
(ua) => !(ua.type === 'deletion' && ua.path === relativePath)
);
}
}
}
for (const relativePath of status.deleted || []) {
for (const pattern of fileFilters) {
if (minimatch(relativePath, pattern)) {
logger.debug(
{ file: relativePath, pattern },
'Post-upgrade file removed'
);
updatedArtifacts.push({
type: 'deletion',
path: relativePath,
});
// If the file is created or modified by a previous post-update command, remove the modification from updatedArtifacts
updatedArtifacts = updatedArtifacts.filter(
(ua) => !(ua.type === 'addition' && ua.path === relativePath)
);
}
}
}
}
}
return { updatedArtifacts, artifactErrors };
}
export default async function executePostUpgradeCommands(
config: BranchConfig
): Promise<PostUpgradeCommandsExecutionResult | null> {
const { allowedPostUpgradeCommands } = GlobalConfig.get();
const hasChangedFiles =
(config.updatedPackageFiles && config.updatedPackageFiles.length > 0) ||
(config.updatedArtifacts && config.updatedArtifacts.length > 0);
if (
/* Only run post-upgrade tasks if there are changes to package files... */
!hasChangedFiles ||
is.emptyArray(allowedPostUpgradeCommands)
) {
return null;
}
const branchUpgradeCommands: BranchUpgradeConfig[] = [
{
manager: config.manager,
depName: config.upgrades.map(({ depName }) => depName).join(' '),
branchName: config.branchName,
postUpgradeTasks:
config.postUpgradeTasks!.executionMode === 'branch'
? config.postUpgradeTasks
: undefined,
fileFilters: config.fileFilters,
},
];
const updateUpgradeCommands: BranchUpgradeConfig[] = config.upgrades.filter(
({ postUpgradeTasks }) =>
!postUpgradeTasks?.executionMode ||
postUpgradeTasks.executionMode === 'update'
);
const { updatedArtifacts, artifactErrors } =
await postUpgradeCommandsExecutor(updateUpgradeCommands, config);
return postUpgradeCommandsExecutor(branchUpgradeCommands, {
...config,
updatedArtifacts,
artifactErrors,
});
}