From a025178e71bc63930e4afbe3f9d36081a2def862 Mon Sep 17 00:00:00 2001
From: Maksym Romanowski <max.romanovsky@gmail.com>
Date: Fri, 2 Feb 2024 11:15:28 +0100
Subject: [PATCH] fix(pypi): trim simple repo markup with extra whitespaces and
 new lines (#27013)

Co-authored-by: Rhys Arkins <rhys@arkins.net>
---
 .../versions-html-with-whitespaces.html       | 14 +++++++++++
 lib/modules/datasource/pypi/index.spec.ts     | 23 +++++++++++++++++++
 lib/modules/datasource/pypi/index.ts          |  2 +-
 3 files changed, 38 insertions(+), 1 deletion(-)
 create mode 100644 lib/modules/datasource/pypi/__fixtures__/versions-html-with-whitespaces.html

diff --git a/lib/modules/datasource/pypi/__fixtures__/versions-html-with-whitespaces.html b/lib/modules/datasource/pypi/__fixtures__/versions-html-with-whitespaces.html
new file mode 100644
index 0000000000..8d0f828c6d
--- /dev/null
+++ b/lib/modules/datasource/pypi/__fixtures__/versions-html-with-whitespaces.html
@@ -0,0 +1,14 @@
+<!DOCTYPE html>
+<html><head>
+<meta http-equiv="content-type" content="text/html; charset=UTF-8">
+    <title>   Links for package with extra whitespaces   </title>
+  </head>
+  <body>
+    <h1>   Links for package with extra whitespaces   </h1>
+    <a href="">
+      package_with_whitespaces-2.0.0.tar.gz
+      </a><br>
+    <a href="">      package_with_whitespaces-2.0.1-py3-none-any.whl </a><br>
+    <a href="">   package_with_whitespaces-2.0.2-py3-none-any.whl</a><br>
+    <a href="">package_with_whitespaces-2.0.2.tar.gz   </a><br>
+</body></html>
diff --git a/lib/modules/datasource/pypi/index.spec.ts b/lib/modules/datasource/pypi/index.spec.ts
index 4172900b26..c4cb541f25 100644
--- a/lib/modules/datasource/pypi/index.spec.ts
+++ b/lib/modules/datasource/pypi/index.spec.ts
@@ -14,6 +14,9 @@ const dataRequiresPythonResponse = Fixtures.get(
 const mixedHyphensResponse = Fixtures.get('versions-html-mixed-hyphens.html');
 const mixedCaseResponse = Fixtures.get('versions-html-mixed-case.html');
 const withPeriodsResponse = Fixtures.get('versions-html-with-periods.html');
+const withWhitespacesResponse = Fixtures.get(
+  'versions-html-with-whitespaces.html',
+);
 const hyphensResponse = Fixtures.get('versions-html-hyphens.html');
 
 const baseUrl = 'https://pypi.org/pypi';
@@ -430,6 +433,26 @@ describe('modules/datasource/pypi/index', () => {
       ]);
     });
 
+    it('process data from simple endpoint with extra whitespaces in html', async () => {
+      httpMock
+        .scope('https://some.registry.org/simple/')
+        .get('/package-with-whitespaces/')
+        .reply(200, withWhitespacesResponse);
+      const config = {
+        registryUrls: ['https://some.registry.org/simple/'],
+      };
+      const res = await getPkgReleases({
+        datasource,
+        ...config,
+        packageName: 'package-with-whitespaces',
+      });
+      expect(res?.releases).toMatchObject([
+        { version: '2.0.0' },
+        { version: '2.0.1' },
+        { version: '2.0.2' },
+      ]);
+    });
+
     it('returns null for empty response', async () => {
       httpMock
         .scope('https://some.registry.org/simple/')
diff --git a/lib/modules/datasource/pypi/index.ts b/lib/modules/datasource/pypi/index.ts
index 11e4c2dd5f..4aab61635c 100644
--- a/lib/modules/datasource/pypi/index.ts
+++ b/lib/modules/datasource/pypi/index.ts
@@ -246,7 +246,7 @@ export class PypiDatasource extends Datasource {
     const releases: Releases = {};
     for (const link of Array.from(links)) {
       const version = PypiDatasource.extractVersionFromLinkText(
-        link.text,
+        link.text?.trim(),
         packageName,
       );
       if (version) {
-- 
GitLab