From a49c994ca60b7a2f8c22563053bc7a4113de488f Mon Sep 17 00:00:00 2001
From: Kyle Welch <1295580+kwelch@users.noreply.github.com>
Date: Wed, 15 Feb 2023 10:25:11 -0600
Subject: [PATCH] feat(platform/bitbucket): Add ability to use UUID for
 reviewers (#20382)

Co-authored-by: Rhys Arkins <rhys@arkins.net>
Co-authored-by: Sebastian Poxhofer <secustor@users.noreply.github.com>
Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
---
 lib/modules/platform/bitbucket/index.spec.ts | 21 ++++++++++++++++++++
 lib/modules/platform/bitbucket/index.ts      |  9 +++++++--
 lib/util/regex.spec.ts                       |  9 ++++++++-
 lib/util/regex.ts                            |  9 +++++++++
 4 files changed, 45 insertions(+), 3 deletions(-)

diff --git a/lib/modules/platform/bitbucket/index.spec.ts b/lib/modules/platform/bitbucket/index.spec.ts
index 9f4cb4c475..fdce165e05 100644
--- a/lib/modules/platform/bitbucket/index.spec.ts
+++ b/lib/modules/platform/bitbucket/index.spec.ts
@@ -603,6 +603,27 @@ describe('modules/platform/bitbucket/index', () => {
         bitbucket.addReviewers(5, ['someuser', 'someotheruser'])
       ).toResolve();
     });
+
+    it('should handle reviewers as username or UUID', async () => {
+      const scope = await initRepoMock();
+      scope
+        .get('/2.0/repositories/some/repo/pullrequests/5')
+        .reply(200, pr)
+        .put('/2.0/repositories/some/repo/pullrequests/5', {
+          title: pr.title,
+          reviewers: [
+            { username: 'someuser' },
+            { uuid: '{90b6646d-1724-4a64-9fd9-539515fe94e9}' },
+          ],
+        })
+        .reply(200);
+      await expect(
+        bitbucket.addReviewers(5, [
+          'someuser',
+          '{90b6646d-1724-4a64-9fd9-539515fe94e9}',
+        ])
+      ).toResolve();
+    });
   });
 
   describe('ensureComment()', () => {
diff --git a/lib/modules/platform/bitbucket/index.ts b/lib/modules/platform/bitbucket/index.ts
index 7b34f72650..e8b79da387 100644
--- a/lib/modules/platform/bitbucket/index.ts
+++ b/lib/modules/platform/bitbucket/index.ts
@@ -8,7 +8,7 @@ import * as git from '../../../util/git';
 import * as hostRules from '../../../util/host-rules';
 import { BitbucketHttp, setBaseUrl } from '../../../util/http/bitbucket';
 import type { HttpOptions } from '../../../util/http/types';
-import { regEx } from '../../../util/regex';
+import { isUUID, regEx } from '../../../util/regex';
 import { sanitize } from '../../../util/sanitize';
 import type {
   BranchStatusConfig,
@@ -622,7 +622,12 @@ export async function addReviewers(
 
   const body = {
     title,
-    reviewers: reviewers.map((username: string) => ({ username })),
+    reviewers: reviewers.map((username: string) => {
+      const key = isUUID(username) ? 'uuid' : 'username';
+      return {
+        [key]: username,
+      };
+    }),
   };
 
   await bitbucketHttp.putJson(
diff --git a/lib/util/regex.spec.ts b/lib/util/regex.spec.ts
index f8d24b1f05..3283c40ec1 100644
--- a/lib/util/regex.spec.ts
+++ b/lib/util/regex.spec.ts
@@ -1,6 +1,6 @@
 import RE2 from 're2';
 import { CONFIG_VALIDATION } from '../constants/error-messages';
-import { regEx } from './regex';
+import { isUUID, regEx } from './regex';
 
 describe('util/regex', () => {
   beforeEach(() => {
@@ -37,4 +37,11 @@ describe('util/regex', () => {
     const regex = require('./regex');
     expect(regex.regEx('foo')).toBeInstanceOf(RegExp);
   });
+
+  describe('isUUID', () => {
+    it('proper checks valid and invalid UUID strings', () => {
+      expect(isUUID('{90b6646d-1724-4a64-9fd9-539515fe94e9}')).toBe(true);
+      expect(isUUID('not-a-uuid')).toBe(false);
+    });
+  });
 });
diff --git a/lib/util/regex.ts b/lib/util/regex.ts
index 6e6bdf2952..fd227ec2da 100644
--- a/lib/util/regex.ts
+++ b/lib/util/regex.ts
@@ -98,3 +98,12 @@ export function configRegexPredicate(
   }
   return null;
 }
+
+const UUIDRegex = regEx(
+  /^\{[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}\}$/,
+  'i'
+);
+
+export function isUUID(input: string): boolean {
+  return UUIDRegex.test(input);
+}
-- 
GitLab