From f19eb5b841c0643705b6316a615b05ffa08c77fc Mon Sep 17 00:00:00 2001
From: Sergei Zharinov <zharinov@users.noreply.github.com>
Date: Thu, 16 Jun 2022 09:42:23 +0300
Subject: [PATCH] fix(github): Don't append `/api/v3/` to the endpoint twice
 (#16097)

* fix(github): Don't attach `/api/v3/` to the endpoint twice

* Add test

* More correct replacing
---
 .../github-releases/cache/cache-base.ts          |  2 +-
 .../datasource/github-releases/common.spec.ts    |  8 ++++++--
 lib/modules/datasource/github-releases/common.ts | 16 +++++++++++++---
 3 files changed, 20 insertions(+), 6 deletions(-)

diff --git a/lib/modules/datasource/github-releases/cache/cache-base.ts b/lib/modules/datasource/github-releases/cache/cache-base.ts
index 11b2797a8d..90c80a18ea 100644
--- a/lib/modules/datasource/github-releases/cache/cache-base.ts
+++ b/lib/modules/datasource/github-releases/cache/cache-base.ts
@@ -181,7 +181,7 @@ export abstract class AbstractGithubDatasourceCache<
     // so that soft update mechanics is immediately starting.
     let cacheUpdatedAt = now.minus(this.updateDuration).toISO();
 
-    const baseUrl = getApiBaseUrl(registryUrl).replace('/v3/', '/'); // Replace for GHE
+    const baseUrl = getApiBaseUrl(registryUrl).replace(/\/v3\/$/, '/'); // Replace for GHE
 
     const [owner, name] = packageName.split('/');
     if (owner && name) {
diff --git a/lib/modules/datasource/github-releases/common.spec.ts b/lib/modules/datasource/github-releases/common.spec.ts
index e386f33750..ebcaffcbbf 100644
--- a/lib/modules/datasource/github-releases/common.spec.ts
+++ b/lib/modules/datasource/github-releases/common.spec.ts
@@ -20,8 +20,12 @@ describe('modules/datasource/github-releases/common', () => {
     });
 
     it('supports local github installations', () => {
-      const apiUrl = getApiBaseUrl('https://gh.my-company.com/');
-      expect(apiUrl).toBe('https://gh.my-company.com/api/v3/');
+      expect(getApiBaseUrl('https://gh.my-company.com/')).toBe(
+        'https://gh.my-company.com/api/v3/'
+      );
+      expect(getApiBaseUrl('https://gh.my-company.com/api/v3/')).toBe(
+        'https://gh.my-company.com/api/v3/'
+      );
     });
   });
 });
diff --git a/lib/modules/datasource/github-releases/common.ts b/lib/modules/datasource/github-releases/common.ts
index 638ebdeff5..58969644bd 100644
--- a/lib/modules/datasource/github-releases/common.ts
+++ b/lib/modules/datasource/github-releases/common.ts
@@ -10,9 +10,19 @@ export function getSourceUrlBase(registryUrl: string | undefined): string {
 
 export function getApiBaseUrl(registryUrl: string | undefined): string {
   const sourceUrlBase = getSourceUrlBase(registryUrl);
-  return [defaultSourceUrlBase, defaultApiBaseUrl].includes(sourceUrlBase)
-    ? defaultApiBaseUrl
-    : `${sourceUrlBase}api/v3/`;
+
+  if (
+    sourceUrlBase === defaultSourceUrlBase ||
+    sourceUrlBase === defaultApiBaseUrl
+  ) {
+    return defaultApiBaseUrl;
+  }
+
+  if (sourceUrlBase.endsWith('/api/v3/')) {
+    return sourceUrlBase;
+  }
+
+  return `${sourceUrlBase}api/v3/`;
 }
 
 export function getSourceUrl(
-- 
GitLab