From a9af34cf8c39127df3c7ef2f44d5ad2b10a306d0 Mon Sep 17 00:00:00 2001
From: bjuraga <b.juraga@itandcare.nl>
Date: Thu, 27 Jul 2023 16:56:30 +0200
Subject: [PATCH] fix(manager/nuget): don't bump version if already done 
 (#23546)

---
 lib/modules/manager/nuget/update.spec.ts | 19 +++++++++++++++++++
 lib/modules/manager/nuget/update.ts      | 17 ++++++++++-------
 2 files changed, 29 insertions(+), 7 deletions(-)

diff --git a/lib/modules/manager/nuget/update.spec.ts b/lib/modules/manager/nuget/update.spec.ts
index ecf80a4957..1701d0625c 100644
--- a/lib/modules/manager/nuget/update.spec.ts
+++ b/lib/modules/manager/nuget/update.spec.ts
@@ -7,6 +7,10 @@ const minimumContent =
   '<Project Sdk="Microsoft.NET.Sdk"><PropertyGroup><Version>1</Version></PropertyGroup></Project>';
 const prereleaseContent =
   '<Project Sdk="Microsoft.NET.Sdk"><PropertyGroup><Version>1.0.0-1</Version></PropertyGroup></Project>';
+const issue23526InitialContent =
+  '<Project Sdk="Microsoft.NET.Sdk"><PropertyGroup><Version>4.9.0</Version></PropertyGroup></Project>';
+const issue23526ExpectedContent =
+  '<Project Sdk="Microsoft.NET.Sdk"><PropertyGroup><Version>4.10.0</Version></PropertyGroup></Project>';
 
 describe('modules/manager/nuget/update', () => {
   describe('bumpPackageVersion', () => {
@@ -36,6 +40,21 @@ describe('modules/manager/nuget/update', () => {
       expect(bumpedContent).toEqual(bumpedContent2);
     });
 
+    it('issue 23526 does not bump version incorrectly', () => {
+      const { bumpedContent } = bumpPackageVersion(
+        issue23526InitialContent,
+        '4.9.0',
+        'minor'
+      );
+      const { bumpedContent: bumpedContent2 } = bumpPackageVersion(
+        bumpedContent!,
+        '4.9.0',
+        'minor'
+      );
+
+      expect(bumpedContent2).toEqual(issue23526ExpectedContent);
+    });
+
     it('does not bump version if version is not a semantic version', () => {
       const { bumpedContent } = bumpPackageVersion(
         minimumContent,
diff --git a/lib/modules/manager/nuget/update.ts b/lib/modules/manager/nuget/update.ts
index 4921abdd1e..ad258f2ff3 100644
--- a/lib/modules/manager/nuget/update.ts
+++ b/lib/modules/manager/nuget/update.ts
@@ -31,14 +31,23 @@ export function bumpPackageVersion(
   try {
     const project = new XmlDocument(content);
     const versionNode = project.descendantWithPath('PropertyGroup.Version')!;
+    const currentProjVersion = versionNode.val;
     const startTagPosition = versionNode.startTagPosition;
-    const versionPosition = content.indexOf(versionNode.val, startTagPosition);
+    const versionPosition = content.indexOf(
+      currentProjVersion,
+      startTagPosition
+    );
 
     const newProjVersion = semver.inc(currentValue, bumpVersion as ReleaseType);
     if (!newProjVersion) {
       throw new Error('semver inc failed');
     }
 
+    if (currentProjVersion === newProjVersion) {
+      logger.debug('Version was already bumped');
+      return { bumpedContent };
+    }
+
     logger.debug(`newProjVersion: ${newProjVersion}`);
     bumpedContent = replaceAt(
       content,
@@ -46,12 +55,6 @@ export function bumpPackageVersion(
       currentValue,
       newProjVersion
     );
-
-    if (bumpedContent === content) {
-      logger.debug('Version was already bumped');
-    } else {
-      logger.debug('project version bumped');
-    }
   } catch (err) {
     logger.warn(
       {
-- 
GitLab