From c5851bee00033c15e684a30d1ad0a2ef4c6549cb Mon Sep 17 00:00:00 2001
From: kroonprins <kroonprins@users.noreply.github.com>
Date: Wed, 25 Mar 2020 07:36:52 +0100
Subject: [PATCH] =?UTF-8?q?feat(azure):=20small=20improvements=20for=20PR?=
 =?UTF-8?q?=20assignees=20and=20reviewers=E2=80=A6=20(#5601)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Co-authored-by: Jamie Magee <JamieMagee@users.noreply.github.com>
---
 lib/platform/azure/index.spec.ts | 19 +++++++--
 lib/platform/azure/index.ts      | 72 +++++++++++++++++++-------------
 2 files changed, 59 insertions(+), 32 deletions(-)

diff --git a/lib/platform/azure/index.spec.ts b/lib/platform/azure/index.spec.ts
index 821e103aef..f92cfe1090 100644
--- a/lib/platform/azure/index.spec.ts
+++ b/lib/platform/azure/index.spec.ts
@@ -820,12 +820,25 @@ describe('platform/azure', () => {
       azureApi.gitApi.mockImplementation(
         () =>
           ({
+            getRepositories: jest.fn(() => [{ id: '1', project: { id: 2 } }]),
             createThread: jest.fn(() => [{ id: 123 }]),
             getThreads: jest.fn(() => []),
           } as any)
       );
-      await azure.addAssignees(123, ['test@bonjour.fr']);
-      expect(azureApi.gitApi).toHaveBeenCalledTimes(3);
+      azureApi.coreApi.mockImplementation(
+        () =>
+          ({
+            getTeams: jest.fn(() => [
+              { id: 3, name: 'abc' },
+              { id: 4, name: 'def' },
+            ]),
+            getTeamMembersWithExtendedProperties: jest.fn(() => [
+              { identity: { displayName: 'jyc', uniqueName: 'jyc', id: 123 } },
+            ]),
+          } as any)
+      );
+      await azure.addAssignees(123, ['test@bonjour.fr', 'jyc', 'def']);
+      expect(azureApi.gitApi).toHaveBeenCalledTimes(4);
     });
   });
 
@@ -852,7 +865,7 @@ describe('platform/azure', () => {
           } as any)
       );
       await azure.addReviewers(123, ['test@bonjour.fr', 'jyc', 'def']);
-      expect(azureApi.gitApi).toHaveBeenCalledTimes(3);
+      expect(azureApi.gitApi).toHaveBeenCalledTimes(4);
     });
   });
 
diff --git a/lib/platform/azure/index.ts b/lib/platform/azure/index.ts
index dff1cea09e..350631f515 100644
--- a/lib/platform/azure/index.ts
+++ b/lib/platform/azure/index.ts
@@ -49,6 +49,11 @@ interface Config {
   repository: string;
 }
 
+interface User {
+  id: string;
+  name: string;
+}
+
 let config: Config = {} as any;
 
 const defaults: any = {
@@ -643,33 +648,7 @@ export /* istanbul ignore next */ function getIssueList(): Promise<Issue[]> {
   return Promise.resolve([]);
 }
 
-/**
- *
- * @param {number} issueNo
- * @param {string[]} assignees
- */
-export async function addAssignees(
-  issueNo: number,
-  assignees: string[]
-): Promise<void> {
-  logger.trace(`addAssignees(${issueNo}, ${assignees})`);
-  await ensureComment({
-    number: issueNo,
-    topic: 'Add Assignees',
-    content: assignees.map(a => `@<${a}>`).join(', '),
-  });
-}
-
-/**
- *
- * @param {number} prNo
- * @param {string[]} reviewers
- */
-export async function addReviewers(
-  prNo: number,
-  reviewers: string[]
-): Promise<void> {
-  logger.trace(`addReviewers(${prNo}, ${reviewers})`);
+async function getUserIds(users: string[]): Promise<User[]> {
   const azureApiGit = await azureApi.gitApi();
   const azureApiCore = await azureApi.coreApi();
   const repos = await azureApiGit.getRepositories();
@@ -689,7 +668,7 @@ export async function addReviewers(
   const ids: { id: string; name: string }[] = [];
   members.forEach(listMembers => {
     listMembers.forEach(m => {
-      reviewers.forEach(r => {
+      users.forEach(r => {
         if (
           r.toLowerCase() === m.identity.displayName.toLowerCase() ||
           r.toLowerCase() === m.identity.uniqueName.toLowerCase()
@@ -703,7 +682,7 @@ export async function addReviewers(
   });
 
   teams.forEach(t => {
-    reviewers.forEach(r => {
+    users.forEach(r => {
       if (r.toLowerCase() === t.name.toLowerCase()) {
         if (ids.filter(c => c.id === t.id).length === 0) {
           ids.push({ id: t.id, name: r });
@@ -712,6 +691,41 @@ export async function addReviewers(
     });
   });
 
+  return ids;
+}
+
+/**
+ *
+ * @param {number} issueNo
+ * @param {string[]} assignees
+ */
+export async function addAssignees(
+  issueNo: number,
+  assignees: string[]
+): Promise<void> {
+  logger.trace(`addAssignees(${issueNo}, ${assignees})`);
+  const ids = await getUserIds(assignees);
+  await ensureComment({
+    number: issueNo,
+    topic: 'Add Assignees',
+    content: ids.map(a => `@<${a.id}>`).join(', '),
+  });
+}
+
+/**
+ *
+ * @param {number} prNo
+ * @param {string[]} reviewers
+ */
+export async function addReviewers(
+  prNo: number,
+  reviewers: string[]
+): Promise<void> {
+  logger.trace(`addReviewers(${prNo}, ${reviewers})`);
+  const azureApiGit = await azureApi.gitApi();
+
+  const ids = await getUserIds(reviewers);
+
   await Promise.all(
     ids.map(async obj => {
       await azureApiGit.createPullRequestReviewer(
-- 
GitLab