From fe039fea6324eabfc943e5597cd62e4cc814e781 Mon Sep 17 00:00:00 2001
From: Philip <42116482+PhilipAbed@users.noreply.github.com>
Date: Mon, 14 Aug 2023 10:19:04 +0300
Subject: [PATCH] feat: Enhance lookup logs to show branches extended info
 (#23696)

Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
---
 .../finalize/__fixtures__/branches.json       | 68 +++++++++++++++++++
 lib/workers/repository/finalize/index.ts      |  6 +-
 .../finalize/repository-statistics.spec.ts    | 26 +++++--
 .../finalize/repository-statistics.ts         | 66 +++++++++++++++++-
 lib/workers/repository/index.ts               |  2 +
 5 files changed, 154 insertions(+), 14 deletions(-)
 create mode 100644 lib/workers/repository/finalize/__fixtures__/branches.json

diff --git a/lib/workers/repository/finalize/__fixtures__/branches.json b/lib/workers/repository/finalize/__fixtures__/branches.json
new file mode 100644
index 0000000000..847cc57f96
--- /dev/null
+++ b/lib/workers/repository/finalize/__fixtures__/branches.json
@@ -0,0 +1,68 @@
+[
+  {
+    "branchName": "renovate/lodash-monorepo",
+    "prTitle": "Update dependency lodash to v4.17.21",
+    "upgrades": [
+      {
+        "datasource": "npm",
+        "depName": "lodash",
+        "fixedVersion": "4.17.20",
+        "currentVersion": "4.17.20",
+        "currentValue": "4.17.20",
+        "newValue": "4.17.21",
+        "newVersion": "4.17.21",
+        "packageFile": "package.json",
+        "updateType": "patch",
+        "packageName": "lodash"
+      }
+    ]
+  },
+  {
+    "branchName": "renovate/date-io-monorepo",
+    "prTitle": "Update date-io monorepo to v2.17.0",
+    "upgrades": [
+      {
+        "datasource": "npm",
+        "depName": "@date-io/date-fns",
+        "fixedVersion": "2.10.0",
+        "currentVersion": "2.10.0",
+        "currentValue": "2.10.0",
+        "newValue": "2.17.0",
+        "newVersion": "2.17.0",
+        "packageFile": "package.json",
+        "updateType": "minor",
+        "packageName": "@date-io/date-fns"
+      },
+      {
+        "datasource": "npm",
+        "depName": "@date-io/moment",
+        "fixedVersion": "2.10.0",
+        "currentVersion": "2.10.0",
+        "currentValue": "2.10.0",
+        "newValue": "2.17.0",
+        "newVersion": "2.17.0",
+        "packageFile": "package.json",
+        "updateType": "minor",
+        "packageName": "@date-io/moment"
+      }
+    ]
+  },
+  {
+    "branchName": "renovate/commander-11.x",
+    "prTitle": "Update dependency commander to v11",
+    "upgrades": [
+      {
+        "datasource": "npm",
+        "depName": "commander",
+        "fixedVersion": "2.20.3",
+        "currentVersion": "2.20.3",
+        "currentValue": "2.20.3",
+        "newValue": "11.0.0",
+        "newVersion": "11.0.0",
+        "packageFile": "package.json",
+        "updateType": "major",
+        "packageName": "commander"
+      }
+    ]
+  }
+]
diff --git a/lib/workers/repository/finalize/index.ts b/lib/workers/repository/finalize/index.ts
index e0afb3893f..4c7b39364d 100644
--- a/lib/workers/repository/finalize/index.ts
+++ b/lib/workers/repository/finalize/index.ts
@@ -6,10 +6,7 @@ import { clearRenovateRefs } from '../../../util/git';
 import { configMigration } from '../config-migration';
 import { PackageFiles } from '../package-files';
 import { pruneStaleBranches } from './prune';
-import {
-  runBranchSummary,
-  runRenovateRepoStats,
-} from './repository-statistics';
+import { runRenovateRepoStats } from './repository-statistics';
 
 // istanbul ignore next
 export async function finalizeRepo(
@@ -34,7 +31,6 @@ export async function finalizeRepo(
     logger.debug('Repo is activated');
     config.repoIsActivated = true;
   }
-  runBranchSummary(config);
   runRenovateRepoStats(config, prList);
 }
 
diff --git a/lib/workers/repository/finalize/repository-statistics.spec.ts b/lib/workers/repository/finalize/repository-statistics.spec.ts
index 6d65e88af8..6ed74cbae3 100644
--- a/lib/workers/repository/finalize/repository-statistics.spec.ts
+++ b/lib/workers/repository/finalize/repository-statistics.spec.ts
@@ -18,6 +18,7 @@ jest.mock('../../../modules/platform/github/pr');
 jest.mock('../../../util/http/github');
 
 const prJson = Fixtures.getJson('./pr-list.json');
+const branchesJson = Fixtures.getJson('./branches.json');
 const result = Object.keys(prJson).map((key) => {
   return prJson[key];
 });
@@ -66,7 +67,7 @@ describe('workers/repository/finalize/repository-statistics', () => {
       getCacheSpy.mockReturnValueOnce(cache);
       isCacheModifiedSpy.mockReturnValueOnce(true);
 
-      runBranchSummary(config);
+      runBranchSummary(config, []);
 
       expect(logger.debug).toHaveBeenCalledWith(
         {
@@ -127,7 +128,7 @@ describe('workers/repository/finalize/repository-statistics', () => {
       getCacheSpy.mockReturnValueOnce(cache);
       isCacheModifiedSpy.mockReturnValueOnce(false);
 
-      runBranchSummary(config);
+      runBranchSummary(config, []);
 
       expect(logger.debug).toHaveBeenCalledWith(
         {
@@ -153,11 +154,10 @@ describe('workers/repository/finalize/repository-statistics', () => {
       );
     });
 
-    it('logs extended branch info if branchSummaryExtended', () => {
+    it('logs extended branch info', () => {
       const defaultBranch = 'main';
       const config: RenovateConfig = {
         defaultBranch,
-        branchSummaryExtended: true,
       };
       const branchCache = partial<BranchCache>({
         result: 'done',
@@ -171,7 +171,6 @@ describe('workers/repository/finalize/repository-statistics', () => {
           },
         ]),
       });
-
       const branches: BranchCache[] = [{ ...branchCache, branchName: 'b1' }];
       const cache = partial<RepoCacheData>({
         scan: {},
@@ -180,7 +179,22 @@ describe('workers/repository/finalize/repository-statistics', () => {
       getCacheSpy.mockReturnValueOnce(cache);
       isCacheModifiedSpy.mockReturnValueOnce(false);
 
-      runBranchSummary(config);
+      runBranchSummary(config, []);
+
+      expect(logger.debug).toHaveBeenCalledTimes(2);
+    });
+
+    it('logs extended branch info on lookup only', () => {
+      const config: RenovateConfig = {
+        defaultBranch: 'main',
+      };
+      const cache = partial<RepoCacheData>({
+        scan: {},
+      });
+      getCacheSpy.mockReturnValueOnce(cache);
+      isCacheModifiedSpy.mockReturnValueOnce(false);
+
+      runBranchSummary(config, branchesJson);
 
       expect(logger.debug).toHaveBeenCalledTimes(2);
     });
diff --git a/lib/workers/repository/finalize/repository-statistics.ts b/lib/workers/repository/finalize/repository-statistics.ts
index e2b5224a5b..3eba820733 100644
--- a/lib/workers/repository/finalize/repository-statistics.ts
+++ b/lib/workers/repository/finalize/repository-statistics.ts
@@ -1,6 +1,7 @@
 import type { RenovateConfig } from '../../../config/types';
 import { logger } from '../../../logger';
 import type { Pr } from '../../../modules/platform';
+import { coerceArray } from '../../../util/array';
 import { getCache, isCacheModified } from '../../../util/cache/repository';
 import type {
   BranchCache,
@@ -8,8 +9,10 @@ import type {
 } from '../../../util/cache/repository/types';
 import type {
   BaseBranchMetadata,
+  BranchConfig,
   BranchMetadata,
   BranchSummary,
+  BranchUpgradeConfig,
 } from '../../types';
 
 export function runRenovateRepoStats(
@@ -63,6 +66,55 @@ function branchCacheToMetadata({
   };
 }
 
+function filterDependencyLookupData(
+  branches: BranchConfig[]
+): Partial<BranchConfig>[] {
+  const branchesFiltered: Partial<BranchConfig>[] = [];
+  for (const branch of branches) {
+    const upgradesFiltered: Partial<BranchUpgradeConfig>[] = [];
+    const { branchName, prTitle, upgrades } = branch;
+
+    for (const upgrade of coerceArray(upgrades)) {
+      const {
+        datasource,
+        depName,
+        fixedVersion,
+        currentVersion,
+        currentValue,
+        newValue,
+        newVersion,
+        packageFile,
+        updateType,
+        packageName,
+      } = upgrade;
+
+      const filteredUpgrade: Partial<BranchUpgradeConfig> = {
+        datasource,
+        depName,
+        fixedVersion,
+        currentVersion,
+        currentValue,
+        newValue,
+        newVersion,
+        packageFile,
+        updateType,
+        packageName,
+      };
+      upgradesFiltered.push(filteredUpgrade);
+    }
+
+    const filteredBranch: Partial<BranchConfig> = {
+      branchName,
+      prTitle,
+      result: 'no-work',
+      upgrades: upgradesFiltered as BranchUpgradeConfig[],
+    };
+    branchesFiltered.push(filteredBranch);
+  }
+
+  return branchesFiltered;
+}
+
 function filterDependencyDashboardData(
   branches: BranchCache[]
 ): Partial<BranchCache>[] {
@@ -71,7 +123,7 @@ function filterDependencyDashboardData(
     const upgradesFiltered: Partial<BranchUpgradeCache>[] = [];
     const { branchName, prNo, prTitle, result, upgrades, prBlockedBy } = branch;
 
-    for (const upgrade of upgrades ?? []) {
+    for (const upgrade of coerceArray(upgrades)) {
       const {
         datasource,
         depName,
@@ -116,7 +168,10 @@ function filterDependencyDashboardData(
   return branchesFiltered;
 }
 
-export function runBranchSummary(config: RenovateConfig): void {
+export function runBranchSummary(
+  config: RenovateConfig,
+  lookupBranchConfig: BranchConfig[]
+): void {
   const defaultBranch = config.defaultBranch;
   const { scan, branches } = getCache();
 
@@ -146,8 +201,13 @@ export function runBranchSummary(config: RenovateConfig): void {
 
   logger.debug(res, 'Branch summary');
 
+  let branchesInformation;
   if (branches?.length) {
-    const branchesInformation = filterDependencyDashboardData(branches);
+    branchesInformation = filterDependencyDashboardData(branches);
+  } else if (lookupBranchConfig?.length) {
+    branchesInformation = filterDependencyLookupData(lookupBranchConfig);
+  }
+  if (branchesInformation) {
     logger.debug({ branchesInformation }, 'branches info extended');
   }
 }
diff --git a/lib/workers/repository/index.ts b/lib/workers/repository/index.ts
index e19cad910c..3fb1ccbf51 100644
--- a/lib/workers/repository/index.ts
+++ b/lib/workers/repository/index.ts
@@ -23,6 +23,7 @@ import { ensureDependencyDashboard } from './dependency-dashboard';
 import handleError from './error';
 import { finalizeRepo } from './finalize';
 import { pruneStaleBranches } from './finalize/prune';
+import { runBranchSummary } from './finalize/repository-statistics';
 import { initRepo } from './init';
 import { OnboardingState } from './onboarding/common';
 import { ensureOnboardingPr } from './onboarding/pr';
@@ -94,6 +95,7 @@ export async function renovateRepository(
       // TODO #7154
       repoResult = processResult(config, res!);
     }
+    runBranchSummary(config, branches);
   } catch (err) /* istanbul ignore next */ {
     setMeta({ repository: config.repository });
     const errorRes = await handleError(config, err);
-- 
GitLab