Skip to content

Commit

Permalink
feat(postUpgradeTasks): improve debug logging (#31514)
Browse files Browse the repository at this point in the history
  • Loading branch information
rarkins authored Sep 21, 2024
1 parent 954b5b3 commit fc200d4
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fs, git, partial } from '../../../../../test/util';
import { fs, git, logger, partial } from '../../../../../test/util';
import { GlobalConfig } from '../../../../config/global';
import type { StatusResult } from '../../../../util/git/types';
import type { BranchConfig, BranchUpgradeConfig } from '../../../types';
Expand Down Expand Up @@ -114,5 +114,58 @@ describe('workers/repository/update/branch/execute-post-upgrade-commands', () =>
expect(res.updatedArtifacts).toHaveLength(0);
expect(fs.writeLocalFile).toHaveBeenCalledTimes(1);
});

it('logs files which do not match fileFilters', 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: 'some-existing-dir', contents: '' },
{ type: 'addition', path: 'artifact', contents: '' },
],
upgrades: [],
branchName: 'main',
baseBranch: 'base',
};
git.getRepoStatus.mockResolvedValueOnce(
partial<StatusResult>({
modified: ['not-a-txt-file'],
not_added: [],
deleted: [],
}),
);
GlobalConfig.set({
localDir: __dirname,
allowedPostUpgradeCommands: ['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);
expect(fs.writeLocalFile).toHaveBeenCalledTimes(1);
expect(logger.logger.debug).toHaveBeenCalledWith(
{ file: 'not-a-txt-file' },
'Post-upgrade file did not match any file filters',
);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,32 @@ export async function postUpgradeCommandsExecutor(

const status = await getRepoStatus();

for (const relativePath of status.modified.concat(status.not_added)) {
logger.trace({ status }, 'git status after post-upgrade tasks');

logger.debug(
{
addedCount: status.not_added?.length,
modifiedCount: status.modified?.length,
deletedCount: status.deleted?.length,
},
'git status counts after post-upgrade tasks',
);

const addedOrModifiedFiles = [
...coerceArray(status.not_added),
...coerceArray(status.modified),
];

logger.trace({ addedOrModifiedFiles }, 'Added or modified files');
logger.debug(
`Checking ${addedOrModifiedFiles.length} added or modified files for post-upgrade changes`,
);

for (const relativePath of addedOrModifiedFiles) {
let fileMatched = false;
for (const pattern of fileFilters) {
if (minimatch(pattern, { dot: true }).match(relativePath)) {
fileMatched = true;
logger.debug(
{ file: relativePath, pattern },
'Post-upgrade file saved',
Expand All @@ -135,6 +158,12 @@ export async function postUpgradeCommandsExecutor(
);
}
}
if (!fileMatched) {
logger.debug(
{ file: relativePath },
'Post-upgrade file did not match any file filters',
);
}
}

for (const relativePath of coerceArray(status.deleted)) {
Expand Down

0 comments on commit fc200d4

Please sign in to comment.