From fc8cdf7f73bd5df04c04c6b31b647769d30050d3 Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@arkins.net>
Date: Wed, 24 Mar 2021 10:33:45 +0100
Subject: [PATCH] fix(poetry): better getNewValue replace (#9267)

---
 lib/versioning/poetry/index.spec.ts | 10 ++++++++++
 lib/versioning/poetry/index.ts      | 15 +++++++++++++++
 2 files changed, 25 insertions(+)

diff --git a/lib/versioning/poetry/index.spec.ts b/lib/versioning/poetry/index.spec.ts
index 2b2ee1aa4a..9df065233a 100644
--- a/lib/versioning/poetry/index.spec.ts
+++ b/lib/versioning/poetry/index.spec.ts
@@ -300,6 +300,16 @@ describe(getName(__filename), () => {
         })
       ).toEqual('^2.0.0');
     });
+    it('handles precision change caret', () => {
+      expect(
+        versionig.getNewValue({
+          currentValue: '^5.0.3',
+          rangeStrategy: 'replace',
+          currentVersion: '5.3.1',
+          newVersion: '5.5',
+        })
+      ).toEqual('^5.0.3');
+    });
     it('replaces naked version', () => {
       expect(
         versionig.getNewValue({
diff --git a/lib/versioning/poetry/index.ts b/lib/versioning/poetry/index.ts
index 219f3658e2..15daa8434f 100644
--- a/lib/versioning/poetry/index.ts
+++ b/lib/versioning/poetry/index.ts
@@ -1,4 +1,5 @@
 import { parseRange } from 'semver-utils';
+import { logger } from '../../logger';
 import { api as npm } from '../npm';
 import type { NewValueConfig, VersioningApi } from '../types';
 
@@ -130,6 +131,20 @@ function getNewValue({
 }: NewValueConfig): string {
   if (rangeStrategy === 'replace') {
     const npmCurrentValue = poetry2npm(currentValue);
+    try {
+      const massagedNewVersion = padZeroes(newVersion);
+      if (
+        npm.isVersion(massagedNewVersion) &&
+        npm.matches(massagedNewVersion, npmCurrentValue)
+      ) {
+        return currentValue;
+      }
+    } catch (err) /* istanbul ignore next */ {
+      logger.info(
+        { err },
+        'Poetry versioning: Error caught checking if newVersion satisfies currentValue'
+      );
+    }
     const parsedRange = parseRange(npmCurrentValue);
     const element = parsedRange[parsedRange.length - 1];
     if (parsedRange.length === 1 && element.operator) {
-- 
GitLab