From fc200d4e0a4cafde230e9348370423d78c7d63bf Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@arkins.net>
Date: Sat, 21 Sep 2024 09:04:56 +0200
Subject: [PATCH] feat(postUpgradeTasks): improve debug logging (#31514)

---
 .../execute-post-upgrade-commands.spec.ts     | 55 ++++++++++++++++++-
 .../branch/execute-post-upgrade-commands.ts   | 31 ++++++++++-
 2 files changed, 84 insertions(+), 2 deletions(-)

diff --git a/lib/workers/repository/update/branch/execute-post-upgrade-commands.spec.ts b/lib/workers/repository/update/branch/execute-post-upgrade-commands.spec.ts
index da750d0b0a..dd823de3ab 100644
--- a/lib/workers/repository/update/branch/execute-post-upgrade-commands.spec.ts
+++ b/lib/workers/repository/update/branch/execute-post-upgrade-commands.spec.ts
@@ -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';
@@ -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',
+      );
+    });
   });
 });
diff --git a/lib/workers/repository/update/branch/execute-post-upgrade-commands.ts b/lib/workers/repository/update/branch/execute-post-upgrade-commands.ts
index fd4aa9d14b..b295198c37 100644
--- a/lib/workers/repository/update/branch/execute-post-upgrade-commands.ts
+++ b/lib/workers/repository/update/branch/execute-post-upgrade-commands.ts
@@ -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',
@@ -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)) {
-- 
GitLab