From 5cc4260ba7afecda435a45333e017f174a964f2b Mon Sep 17 00:00:00 2001
From: Adam Setch <adam.setch@outlook.com>
Date: Wed, 22 Feb 2023 12:01:03 -0500
Subject: [PATCH] feat(platform/bitbucket): consolidate types (#20570)

---
 .../datasource/bitbucket-tags/index.ts        |  5 +-
 lib/modules/platform/bitbucket/comments.ts    |  3 +-
 lib/modules/platform/bitbucket/index.ts       | 33 ++++---
 lib/modules/platform/bitbucket/types.ts       | 88 +++++++++++++++++++
 lib/modules/platform/bitbucket/utils.ts       | 88 ++-----------------
 5 files changed, 117 insertions(+), 100 deletions(-)

diff --git a/lib/modules/datasource/bitbucket-tags/index.ts b/lib/modules/datasource/bitbucket-tags/index.ts
index 5e4441e702..ff5c436bf9 100644
--- a/lib/modules/datasource/bitbucket-tags/index.ts
+++ b/lib/modules/datasource/bitbucket-tags/index.ts
@@ -1,6 +1,7 @@
 import { cache } from '../../../util/cache/package/decorator';
 import { BitbucketHttp } from '../../../util/http/bitbucket';
 import { ensureTrailingSlash } from '../../../util/url';
+import type { PagedResult, RepoInfoBody } from '../../platform/bitbucket/types';
 import * as utils from '../../platform/bitbucket/utils';
 import { Datasource } from '../datasource';
 import type { DigestConfig, GetReleasesConfig, ReleaseResult } from '../types';
@@ -97,7 +98,7 @@ export class BitBucketTagsDatasource extends Datasource {
   })
   async getMainBranch(repo: string): Promise<string> {
     return (
-      await this.bitbucketHttp.getJson<utils.RepoInfoBody>(
+      await this.bitbucketHttp.getJson<RepoInfoBody>(
         `/2.0/repositories/${repo}`
       )
     ).body.mainbranch.name;
@@ -122,7 +123,7 @@ export class BitBucketTagsDatasource extends Datasource {
 
     const url = `/2.0/repositories/${repo}/commits/${mainBranch}`;
     const bitbucketCommits = (
-      await this.bitbucketHttp.getJson<utils.PagedResult<BitbucketCommit>>(url)
+      await this.bitbucketHttp.getJson<PagedResult<BitbucketCommit>>(url)
     ).body;
 
     if (bitbucketCommits.values.length === 0) {
diff --git a/lib/modules/platform/bitbucket/comments.ts b/lib/modules/platform/bitbucket/comments.ts
index ddb4916cf5..d077fb583f 100644
--- a/lib/modules/platform/bitbucket/comments.ts
+++ b/lib/modules/platform/bitbucket/comments.ts
@@ -1,7 +1,8 @@
 import { logger } from '../../../logger';
 import { BitbucketHttp } from '../../../util/http/bitbucket';
 import type { EnsureCommentConfig, EnsureCommentRemovalConfig } from '../types';
-import { Config, accumulateValues } from './utils';
+import type { Config } from './types';
+import { accumulateValues } from './utils';
 
 const bitbucketHttp = new BitbucketHttp();
 
diff --git a/lib/modules/platform/bitbucket/index.ts b/lib/modules/platform/bitbucket/index.ts
index 2b547488a6..818351c0d8 100644
--- a/lib/modules/platform/bitbucket/index.ts
+++ b/lib/modules/platform/bitbucket/index.ts
@@ -31,20 +31,25 @@ import { repoFingerprint } from '../util';
 import { smartTruncate } from '../utils/pr-body';
 import { readOnlyIssueBody } from '../utils/read-only-issue-body';
 import * as comments from './comments';
-import * as utils from './utils';
-import {
+import type {
   Account,
+  BitbucketStatus,
+  BranchResponse,
+  Config,
   EffectiveReviewer,
+  PagedResult,
   PrResponse,
+  RepoInfo,
   RepoInfoBody,
-  mergeBodyTransformer,
-} from './utils';
+} from './types';
+import * as utils from './utils';
+import { mergeBodyTransformer } from './utils';
 
 const bitbucketHttp = new BitbucketHttp();
 
 const BITBUCKET_PROD_ENDPOINT = 'https://api.bitbucket.org/';
 
-let config: utils.Config = {} as any;
+let config: Config = {} as any;
 
 const defaults = { endpoint: BITBUCKET_PROD_ENDPOINT };
 
@@ -127,7 +132,7 @@ export async function getRawFile(
 
   let finalBranchOrTag = branchOrTag;
   if (branchOrTag?.includes(pathSeparator)) {
-    // Branch name contans slash, so we have to replace branch name with SHA1 of the head commit; otherwise the API will not work.
+    // Branch name contains slash, so we have to replace branch name with SHA1 of the head commit; otherwise the API will not work.
     finalBranchOrTag = await getBranchCommit(branchOrTag);
   }
 
@@ -164,8 +169,8 @@ export async function initRepo({
     repository,
     username: opts.username,
     ignorePrAuthor,
-  } as utils.Config;
-  let info: utils.RepoInfo;
+  } as Config;
+  let info: RepoInfo;
   try {
     info = utils.repoInfoTransformer(
       (
@@ -306,12 +311,6 @@ export async function getPr(prNo: number): Promise<Pr | null> {
 const escapeHash = (input: string): string =>
   input ? input.replace(regEx(/#/g), '%23') : input;
 
-interface BranchResponse {
-  target: {
-    hash: string;
-  };
-}
-
 // Return the commit SHA for a branch
 async function getBranchCommit(
   branchName: string
@@ -344,9 +343,9 @@ export async function getBranchPr(branchName: string): Promise<Pr | null> {
 async function getStatus(
   branchName: string,
   useCache = true
-): Promise<utils.BitbucketStatus[]> {
+): Promise<BitbucketStatus[]> {
   const sha = await getBranchCommit(branchName);
-  return utils.accumulateValues<utils.BitbucketStatus>(
+  return utils.accumulateValues<BitbucketStatus>(
     // TODO: types (#7154)
     // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
     `/2.0/repositories/${config.repository}/commit/${sha}/statuses`,
@@ -764,7 +763,7 @@ export async function createPr({
 
   if (platformOptions?.bbUseDefaultReviewers) {
     const reviewersResponse = (
-      await bitbucketHttp.getJson<utils.PagedResult<EffectiveReviewer>>(
+      await bitbucketHttp.getJson<PagedResult<EffectiveReviewer>>(
         `/2.0/repositories/${config.repository}/effective-default-reviewers`
       )
     ).body;
diff --git a/lib/modules/platform/bitbucket/types.ts b/lib/modules/platform/bitbucket/types.ts
index 907cc4071c..94e58f6167 100644
--- a/lib/modules/platform/bitbucket/types.ts
+++ b/lib/modules/platform/bitbucket/types.ts
@@ -1,3 +1,5 @@
+import type { Pr } from '../types';
+
 export type BitbucketMergeStrategy = 'fast_forward' | 'merge_commit' | 'squash';
 
 export interface MergeRequestBody {
@@ -5,3 +7,89 @@ export interface MergeRequestBody {
   message?: string;
   merge_strategy?: BitbucketMergeStrategy;
 }
+
+export interface Config {
+  defaultBranch: string;
+  has_issues: boolean;
+  mergeMethod: string;
+  owner: string;
+  prList: Pr[];
+  repository: string;
+  username: string;
+  userUuid: string;
+  ignorePrAuthor: boolean;
+}
+
+export interface PagedResult<T = any> {
+  pagelen: number;
+  size?: number;
+  next?: string;
+  values: T[];
+}
+
+export interface RepoInfo {
+  isFork: boolean;
+  owner: string;
+  mainbranch: string;
+  mergeMethod: string;
+  has_issues: boolean;
+  uuid: string;
+}
+
+export interface BranchResponse {
+  target: {
+    hash: string;
+  };
+}
+
+export type BitbucketBranchState = 'SUCCESSFUL' | 'FAILED' | 'INPROGRESS';
+
+export interface BitbucketStatus {
+  key: string;
+  state: BitbucketBranchState;
+}
+
+export interface RepoInfoBody {
+  parent?: any;
+  owner: { username: string };
+  mainbranch: { name: string };
+  has_issues: boolean;
+  uuid: string;
+}
+
+export interface PrResponse {
+  id: number;
+  title: string;
+  state: string;
+  links: {
+    commits: {
+      href: string;
+    };
+  };
+  summary?: { raw: string };
+  source: {
+    branch: {
+      name: string;
+    };
+  };
+  destination: {
+    branch: {
+      name: string;
+    };
+  };
+  reviewers: Array<Account>;
+  created_on: string;
+}
+
+export interface Account {
+  display_name?: string;
+  uuid: string;
+  nickname?: string;
+  account_status?: string;
+}
+
+export interface EffectiveReviewer {
+  type: string;
+  reviewer_type: string;
+  user: Account;
+}
diff --git a/lib/modules/platform/bitbucket/utils.ts b/lib/modules/platform/bitbucket/utils.ts
index 83b6b4f51b..09e4b9e0ff 100644
--- a/lib/modules/platform/bitbucket/utils.ts
+++ b/lib/modules/platform/bitbucket/utils.ts
@@ -5,52 +5,17 @@ import { BitbucketHttp } from '../../../util/http/bitbucket';
 import type { HttpOptions, HttpResponse } from '../../../util/http/types';
 import { getPrBodyStruct } from '../pr-body';
 import type { Pr } from '../types';
-import type { BitbucketMergeStrategy, MergeRequestBody } from './types';
+import type {
+  BitbucketBranchState,
+  BitbucketMergeStrategy,
+  MergeRequestBody,
+  PrResponse,
+  RepoInfo,
+  RepoInfoBody,
+} from './types';
 
 const bitbucketHttp = new BitbucketHttp();
 
-export interface Config {
-  defaultBranch: string;
-  has_issues: boolean;
-  mergeMethod: string;
-  owner: string;
-  prList: Pr[];
-  repository: string;
-  username: string;
-  userUuid: string;
-  ignorePrAuthor: boolean;
-}
-
-export interface PagedResult<T = any> {
-  pagelen: number;
-  size?: number;
-  next?: string;
-  values: T[];
-}
-
-export interface RepoInfo {
-  isFork: boolean;
-  owner: string;
-  mainbranch: string;
-  mergeMethod: string;
-  has_issues: boolean;
-  uuid: string;
-}
-
-export type BitbucketBranchState = 'SUCCESSFUL' | 'FAILED' | 'INPROGRESS';
-export interface BitbucketStatus {
-  key: string;
-  state: BitbucketBranchState;
-}
-
-export interface RepoInfoBody {
-  parent?: any;
-  owner: { username: string };
-  mainbranch: { name: string };
-  has_issues: boolean;
-  uuid: string;
-}
-
 export function repoInfoTransformer(repoInfoBody: RepoInfoBody): RepoInfo {
   return {
     isFork: !!repoInfoBody.parent,
@@ -152,30 +117,6 @@ export async function accumulateValues<T = any>(
   return accumulator;
 }
 
-export interface PrResponse {
-  id: number;
-  title: string;
-  state: string;
-  links: {
-    commits: {
-      href: string;
-    };
-  };
-  summary?: { raw: string };
-  source: {
-    branch: {
-      name: string;
-    };
-  };
-  destination: {
-    branch: {
-      name: string;
-    };
-  };
-  reviewers: Array<Account>;
-  created_on: string;
-}
-
 export function prInfo(pr: PrResponse): Pr {
   return {
     number: pr.id,
@@ -189,16 +130,3 @@ export function prInfo(pr: PrResponse): Pr {
     createdAt: pr.created_on,
   };
 }
-
-export interface Account {
-  display_name?: string;
-  uuid: string;
-  nickname?: string;
-  account_status?: string;
-}
-
-export interface EffectiveReviewer {
-  type: string;
-  reviewer_type: string;
-  user: Account;
-}
-- 
GitLab