diff --git a/lib/platform/azure/index.spec.ts b/lib/platform/azure/index.spec.ts index 821e103aef7684895bcbff3968e0b508300080b8..f92cfe1090cf897a04cfb4339173ac11d8af8d5a 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 dff1cea09edaf1b2d159c2aa386d215483d1a87d..350631f51557b2512b37337406ffb67e44ba8235 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(