Skip to content
Snippets Groups Projects
Unverified Commit 972fa944 authored by Rhys Arkins's avatar Rhys Arkins Committed by GitHub
Browse files

fix(postUpgradeCommands): support undeletion (#34766)

parent 725b00df
No related branches found
No related tags found
No related merge requests found
......@@ -167,5 +167,53 @@ describe('workers/repository/update/branch/execute-post-upgrade-commands', () =>
'Post-upgrade file did not match any file filters',
);
});
it('handles previously-deleted files which are re-added', async () => {
const commands = partial<BranchUpgradeConfig>([
{
manager: 'some-manager',
branchName: 'main',
postUpgradeTasks: {
executionMode: 'branch',
commands: ['command'],
fileFilters: ['*.txt'],
},
},
]);
const config: BranchConfig = {
manager: 'some-manager',
updatedPackageFiles: [
{ type: 'addition', path: 'unchanged.txt', contents: 'changed' },
{ type: 'deletion', path: 'was-deleted.txt' },
],
upgrades: [],
branchName: 'main',
baseBranch: 'base',
};
git.getRepoStatus.mockResolvedValueOnce(
partial<StatusResult>({
modified: [],
not_added: [],
deleted: [],
}),
);
GlobalConfig.set({
localDir: __dirname,
allowedCommands: ['some-command'],
});
fs.localPathIsFile
.mockResolvedValueOnce(true)
.mockResolvedValueOnce(false);
fs.localPathExists
.mockResolvedValueOnce(true)
.mockResolvedValueOnce(true);
const res = await postUpgradeCommands.postUpgradeCommandsExecutor(
commands,
config,
);
expect(res.updatedArtifacts).toHaveLength(0);
});
});
});
......@@ -46,7 +46,9 @@ export async function postUpgradeCommandsExecutor(
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 previouslyModifiedFiles =
config.updatedPackageFiles!.concat(updatedArtifacts);
for (const file of previouslyModifiedFiles) {
const canWriteFile = await localPathIsFile(file.path);
if (file.type === 'addition' && !file.isSymlink && canWriteFile) {
let contents: Buffer | null;
......@@ -122,6 +124,25 @@ export async function postUpgradeCommandsExecutor(
`Checking ${addedOrModifiedFiles.length} added or modified files for post-upgrade changes`,
);
// Check for files which were previously deleted but have been re-added without modification
const previouslyDeletedFiles = updatedArtifacts.filter(
(ua) => ua.type === 'deletion',
);
for (const previouslyDeletedFile of previouslyDeletedFiles) {
if (!addedOrModifiedFiles.includes(previouslyDeletedFile.path)) {
logger.debug(
{ file: previouslyDeletedFile.path },
'Previously deleted file has been restored without modification',
);
updatedArtifacts = updatedArtifacts.filter(
(ua) =>
!(
ua.type === 'deletion' && ua.path === previouslyDeletedFile.path
),
);
}
}
for (const relativePath of addedOrModifiedFiles) {
let fileMatched = false;
for (const pattern of fileFilters) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment