From e5a0ca905bbe60b2d35eb527e9740e1ffaed8617 Mon Sep 17 00:00:00 2001
From: jatladams <98293819+jatladams@users.noreply.github.com>
Date: Sat, 2 Apr 2022 16:41:53 +1100
Subject: [PATCH] refactor(platform/bitbucket-server): split initRepo (#14606)

* Move getting repo git url logic into a function similar to gitlab implementation as per request in another PR:
https://github.com/renovatebot/renovate/pull/13875#issuecomment-1023980178

* Bitbucket server - Move getRepoGitUrl into utils

Co-authored-by: Rhys Arkins <rhys@arkins.net>
Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
---
 .../platform/bitbucket-server/index.ts        | 35 +++-------------
 .../platform/bitbucket-server/utils.ts        | 42 ++++++++++++++++++-
 tsconfig.strict.json                          |  1 +
 3 files changed, 47 insertions(+), 31 deletions(-)

diff --git a/lib/modules/platform/bitbucket-server/index.ts b/lib/modules/platform/bitbucket-server/index.ts
index 6d544f09c4..9440b56e89 100644
--- a/lib/modules/platform/bitbucket-server/index.ts
+++ b/lib/modules/platform/bitbucket-server/index.ts
@@ -1,4 +1,3 @@
-import url from 'url';
 import is from '@sindresorhus/is';
 import delay from 'delay';
 import JSON5 from 'json5';
@@ -11,7 +10,6 @@ import {
 } from '../../../constants/error-messages';
 import { logger } from '../../../logger';
 import { BranchStatus, PrState, VulnerabilityAlert } from '../../../types';
-import type { GitProtocol } from '../../../types/git';
 import type { FileData } from '../../../types/platform/bitbucket-server';
 import * as git from '../../../util/git';
 import { deleteBranch } from '../../../util/git';
@@ -193,33 +191,12 @@ export async function initRepo({
       throw new Error(REPOSITORY_EMPTY);
     }
 
-    let cloneUrl = info.links.clone?.find(({ name }) => name === 'http');
-    if (!cloneUrl) {
-      // Http access might be disabled, try to find ssh url in this case
-      cloneUrl = info.links.clone?.find(({ name }) => name === 'ssh');
-    }
-
-    let gitUrl: string;
-    if (!cloneUrl) {
-      // Fallback to generating the url if the API didn't give us an URL
-      const { host, pathname } = url.parse(defaults.endpoint);
-      gitUrl = git.getUrl({
-        protocol: defaults.endpoint.split(':')[0] as GitProtocol,
-        auth: `${opts.username}:${opts.password}`,
-        host: `${host}${pathname}${
-          pathname.endsWith('/') ? '' : /* istanbul ignore next */ '/'
-        }scm`,
-        repository,
-      });
-    } else if (cloneUrl.name === 'http') {
-      // Inject auth into the API provided URL
-      const repoUrl = url.parse(cloneUrl.href);
-      repoUrl.auth = `${opts.username}:${opts.password}`;
-      gitUrl = url.format(repoUrl);
-    } else {
-      // SSH urls can be used directly
-      gitUrl = cloneUrl.href;
-    }
+    const gitUrl = utils.getRepoGitUrl(
+      config.repositorySlug,
+      defaults.endpoint,
+      info,
+      opts
+    );
 
     await git.initRepo({
       ...config,
diff --git a/lib/modules/platform/bitbucket-server/utils.ts b/lib/modules/platform/bitbucket-server/utils.ts
index 1652762388..1a6b47e660 100644
--- a/lib/modules/platform/bitbucket-server/utils.ts
+++ b/lib/modules/platform/bitbucket-server/utils.ts
@@ -1,14 +1,16 @@
 // SEE for the reference https://github.com/renovatebot/renovate/blob/c3e9e572b225085448d94aa121c7ec81c14d3955/lib/platform/bitbucket/utils.js
 import url from 'url';
 import is from '@sindresorhus/is';
-import { PrState } from '../../../types';
+import { HostRule, PrState } from '../../../types';
+import type { GitProtocol } from '../../../types/git';
+import * as git from '../../../util/git';
 import { BitbucketServerHttp } from '../../../util/http/bitbucket-server';
 import type {
   HttpOptions,
   HttpPostOptions,
   HttpResponse,
 } from '../../../util/http/types';
-import type { BbsPr, BbsRestPr, BitbucketError } from './types';
+import type { BbsPr, BbsRestPr, BbsRestRepo, BitbucketError } from './types';
 
 export const BITBUCKET_INVALID_REVIEWERS_EXCEPTION =
   'com.atlassian.bitbucket.pull.InvalidPullRequestReviewersException';
@@ -152,3 +154,39 @@ export function getInvalidReviewers(err: BitbucketError): string[] {
 
   return invalidReviewers;
 }
+
+export function getRepoGitUrl(
+  repository: string,
+  defaultEndpoint: string,
+  info: BbsRestRepo,
+  opts: HostRule
+): string {
+  let cloneUrl = info.links.clone?.find(({ name }) => name === 'http');
+  if (!cloneUrl) {
+    // Http access might be disabled, try to find ssh url in this case
+    cloneUrl = info.links.clone?.find(({ name }) => name === 'ssh');
+  }
+
+  let gitUrl: string;
+  if (!cloneUrl) {
+    // Fallback to generating the url if the API didn't give us an URL
+    const { host, pathname } = url.parse(defaultEndpoint);
+    gitUrl = git.getUrl({
+      protocol: defaultEndpoint.split(':')[0] as GitProtocol,
+      auth: `${opts.username}:${opts.password}`,
+      host: `${host}${pathname}${
+        pathname.endsWith('/') ? '' : /* istanbul ignore next */ '/'
+      }scm`,
+      repository,
+    });
+  } else if (cloneUrl.name === 'http') {
+    // Inject auth into the API provided URL
+    const repoUrl = url.parse(cloneUrl.href);
+    repoUrl.auth = `${opts.username}:${opts.password}`;
+    gitUrl = url.format(repoUrl);
+  } else {
+    // SSH urls can be used directly
+    gitUrl = cloneUrl.href;
+  }
+  return gitUrl;
+}
diff --git a/tsconfig.strict.json b/tsconfig.strict.json
index 6206aa695a..44a1dcb20a 100644
--- a/tsconfig.strict.json
+++ b/tsconfig.strict.json
@@ -306,6 +306,7 @@
     "lib/modules/platform/azure/util.ts",
     "lib/modules/platform/bitbucket/index.ts",
     "lib/modules/platform/bitbucket-server/index.ts",
+    "lib/modules/platform/bitbucket-server/utils.ts",
     "lib/modules/platform/comment.ts",
     "lib/modules/platform/commit.ts",
     "lib/modules/platform/gitea/index.ts",
-- 
GitLab