From c3e5e07344abc122be3427cc8ef11e3c6c7c6b81 Mon Sep 17 00:00:00 2001
From: Michael Kriese <michael.kriese@visualon.de>
Date: Wed, 10 Aug 2022 11:54:31 +0200
Subject: [PATCH] fix: wrong commit message title for groups (#17100)

---
 .../repository/updates/branch-name.spec.ts    | 18 ++++++++++
 lib/workers/repository/updates/branch-name.ts |  4 +--
 .../repository/updates/generate.spec.ts       | 28 ++++++++++-----
 lib/workers/repository/updates/generate.ts    | 35 +++++++++++--------
 4 files changed, 59 insertions(+), 26 deletions(-)

diff --git a/lib/workers/repository/updates/branch-name.spec.ts b/lib/workers/repository/updates/branch-name.spec.ts
index 88d91f8651..93ff3a94ee 100644
--- a/lib/workers/repository/updates/branch-name.spec.ts
+++ b/lib/workers/repository/updates/branch-name.spec.ts
@@ -198,6 +198,24 @@ describe('workers/repository/updates/branch-name', () => {
       expect(upgrade.branchName).toBe('dep-df9ca0');
     });
 
+    it('hashedBranchLength no topic', () => {
+      const upgrade: RenovateConfig = {
+        hashedBranchLength: 3,
+        branchPrefix: 'dep-',
+        depNameSanitized: 'jest',
+        newMajor: '42',
+        groupName: 'some group name',
+        group: {
+          branchName:
+            '{{{branchPrefix}}}{{{additionalBranchPrefix}}}{{{branchTopic}}}',
+          additionalBranchPrefix:
+            '{{{depNameSanitized}}}-{{{newMajor}}}{{#if isPatch}}.{{{newMinor}}}{{/if}}.x{{#if isLockfileUpdate}}-lockfile{{/if}}',
+        },
+      };
+      generateBranchName(upgrade);
+      expect(upgrade.branchName).toBe('dep-cf83e1');
+    });
+
     it('enforces valid git branch name', () => {
       const fixtures = [
         {
diff --git a/lib/workers/repository/updates/branch-name.ts b/lib/workers/repository/updates/branch-name.ts
index a44ceaf220..da71eae75a 100644
--- a/lib/workers/repository/updates/branch-name.ts
+++ b/lib/workers/repository/updates/branch-name.ts
@@ -67,12 +67,12 @@ export function generateBranchName(update: RenovateConfig): void {
     }
 
     const additionalBranchPrefix = template.compile(
-      String(update.additionalBranchPrefix || ''),
+      String(update.additionalBranchPrefix ?? ''),
       update
     );
 
     const branchTopic = template.compile(
-      String(update.branchTopic || ''),
+      String(update.branchTopic ?? ''),
       update
     );
 
diff --git a/lib/workers/repository/updates/generate.spec.ts b/lib/workers/repository/updates/generate.spec.ts
index de9aae117a..2b98079568 100644
--- a/lib/workers/repository/updates/generate.spec.ts
+++ b/lib/workers/repository/updates/generate.spec.ts
@@ -242,9 +242,10 @@ describe('workers/repository/updates/generate', () => {
           depName: 'depB',
           groupName: 'some-group',
           branchName: 'some-branch',
-          prTitle: 'some-title',
+          commitMessage:
+            '{{{groupName}}} {{{commitMessageExtra}}} {{{commitMessageSuffix}}}',
           commitMessageExtra:
-            'to {{#if isMajor}}{{prettyNewMajor}}{{else}}{{#unless isRange}}v{{/unless}}{{newValue}}{{/if}}',
+            'to {{#if isMajor}}{{prettyNewMajor}}{{else}}{{prettyNewVersion}}{{/if}}',
           foo: 1,
           newValue: '5.1.2',
           newVersion: '5.1.2',
@@ -252,15 +253,18 @@ describe('workers/repository/updates/generate', () => {
             foo: 2,
           },
           releaseTimestamp: '2017-02-07T20:01:41+00:00',
+          updateType: 'minor',
+          separateMinorPatch: true,
         },
         {
           manager: 'some-manager',
           depName: 'depA',
           groupName: 'some-group',
           branchName: 'some-branch',
-          prTitle: 'some-title',
+          commitMessage:
+            '{{{groupName}}} {{{commitMessageExtra}}} {{{commitMessageSuffix}}}',
           commitMessageExtra:
-            'to {{#if isMajor}}{{prettyNewMajor}}{{else}}{{#unless isRange}}v{{/unless}}{{newValue}}{{/if}}',
+            'to {{#if isMajor}}{{prettyNewMajor}}{{else}}{{prettyNewVersion}}{{/if}}',
           foo: 1,
           newValue: '1.1.0',
           newVersion: '1.1.0',
@@ -268,14 +272,20 @@ describe('workers/repository/updates/generate', () => {
             foo: 2,
           },
           releaseTimestamp: '2017-02-08T20:01:41+00:00',
+          updateType: 'minor',
+          separateMinorPatch: true,
         },
       ];
       const res = generateBranchConfig(branch);
-      expect(res.foo).toBe(2);
-      expect(res.singleVersion).toBeUndefined();
-      expect(res.recreateClosed).toBeTrue();
-      expect(res.groupName).toBeDefined();
-      expect(res.releaseTimestamp).toBe('2017-02-08T20:01:41+00:00');
+      expect(res).toMatchObject({
+        foo: 2,
+        isGroup: true,
+        recreateClosed: true,
+        prTitle: 'some-group (minor)',
+        commitMessage: 'some-group',
+        groupName: 'some-group',
+        releaseTimestamp: '2017-02-08T20:01:41+00:00',
+      });
     });
 
     it('groups multiple upgrades different version but same value', () => {
diff --git a/lib/workers/repository/updates/generate.ts b/lib/workers/repository/updates/generate.ts
index 9ffec7ff5f..080914bc5d 100644
--- a/lib/workers/repository/updates/generate.ts
+++ b/lib/workers/repository/updates/generate.ts
@@ -74,7 +74,7 @@ export function generateBranchConfig(
   const newValue: string[] = [];
   const toVersions: string[] = [];
   const toValues = new Set<string>();
-  branchUpgrades.forEach((upg) => {
+  for (const upg of branchUpgrades) {
     if (!depNames.includes(upg.depName!)) {
       depNames.push(upg.depName!);
     }
@@ -82,20 +82,27 @@ export function generateBranchConfig(
       toVersions.push(upg.newVersion!);
     }
     toValues.add(upg.newValue!);
+    // prettify newVersion and newMajor for printing
+    if (upg.newVersion) {
+      upg.prettyNewVersion = upg.newVersion.startsWith('v')
+        ? upg.newVersion
+        : `v${upg.newVersion}`;
+    }
+    if (upg.newMajor) {
+      upg.prettyNewMajor = `v${upg.newMajor}`;
+    }
     if (upg.commitMessageExtra) {
       const extra = template.compile(upg.commitMessageExtra, upg);
       if (!newValue.includes(extra)) {
         newValue.push(extra);
       }
     }
-  });
+  }
   const groupEligible =
     depNames.length > 1 ||
     toVersions.length > 1 ||
     (!toVersions[0] && newValue.length > 1);
-  if (newValue.length > 1 && !groupEligible) {
-    branchUpgrades[0].commitMessageExtra = `to v${toVersions[0]}`;
-  }
+
   const typesGroup =
     depNames.length > 1 && !hasGroupName && isTypesGroup(branchUpgrades);
   logger.trace(`groupEligible: ${groupEligible}`);
@@ -104,6 +111,12 @@ export function generateBranchConfig(
   let releaseTimestamp: string;
   for (const branchUpgrade of branchUpgrades) {
     let upgrade: BranchUpgradeConfig = { ...branchUpgrade };
+
+    // needs to be done for each upgrade, as we reorder them below
+    if (newValue.length > 1 && !groupEligible) {
+      upgrade.commitMessageExtra = `to v${toVersions[0]}`;
+    }
+
     if (upgrade.currentDigest) {
       upgrade.currentDigestShort =
         upgrade.currentDigestShort ??
@@ -182,15 +195,7 @@ export function generateBranchConfig(
         regEx(/[A-Z]/).exec(upgrade.semanticCommitType!) === null &&
         !upgrade.semanticCommitType!.startsWith(':');
     }
-    // prettify newVersion and newMajor for printing
-    if (upgrade.newVersion) {
-      upgrade.prettyNewVersion = upgrade.newVersion.startsWith('v')
-        ? upgrade.newVersion
-        : `v${upgrade.newVersion}`;
-    }
-    if (upgrade.newMajor) {
-      upgrade.prettyNewMajor = `v${upgrade.newMajor}`;
-    }
+
     // Compile a few times in case there are nested templates
     upgrade.commitMessage = template.compile(
       upgrade.commitMessage ?? '',
@@ -372,7 +377,7 @@ export function generateBranchConfig(
     config.updateType = 'major';
   }
   config.constraints = {};
-  for (const upgrade of config.upgrades || []) {
+  for (const upgrade of config.upgrades) {
     if (upgrade.constraints) {
       config.constraints = { ...config.constraints, ...upgrade.constraints };
     }
-- 
GitLab