Skip to content

Commit

Permalink
fix(azure): paginate getTeams() (#29060)
Browse files Browse the repository at this point in the history
Co-authored-by: Dovile Dikoviciute <Dovile.Dikoviciute@bentley.com>
  • Loading branch information
ddovile and doviledikoviciute committed May 14, 2024
1 parent 0f838d2 commit 8d78ca2
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 13 deletions.
23 changes: 23 additions & 0 deletions lib/modules/platform/azure/azure-helper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -434,4 +434,27 @@ describe('modules/platform/azure/azure-helper', () => {
).toEqual(GitPullRequestMergeStrategy.Rebase);
});
});

describe('getAllProjectTeams', () => {
it('should get all teams ', async () => {
const team1 = Array.from({ length: 100 }, (_, index) => ({
description: `team1 ${index + 1}`,
}));
const team2 = Array.from({ length: 3 }, (_, index) => ({
description: `team2 ${index + 1}`,
}));
const allTeams = team1.concat(team2);
azureApi.coreApi.mockImplementationOnce(
() =>
({
getTeams: jest
.fn()
.mockResolvedValueOnce(team1)
.mockResolvedValueOnce(team2),
}) as any,
);
const res = await azureHelper.getAllProjectTeams('projectId');
expect(res).toEqual(allTeams);
});
});
});
20 changes: 20 additions & 0 deletions lib/modules/platform/azure/azure-helper.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { WebApiTeam } from 'azure-devops-node-api/interfaces/CoreInterfaces.js';
import {
GitCommit,
GitPullRequestMergeStrategy,
Expand Down Expand Up @@ -178,3 +179,22 @@ export async function getMergeMethod(
return GitPullRequestMergeStrategy.NoFastForward;
}
}

export async function getAllProjectTeams(
projectId: string,
): Promise<WebApiTeam[]> {
const allTeams: WebApiTeam[] = [];
const azureApiCore = await azureApi.coreApi();
const top = 100;
let skip = 0;
let length = 0;

do {
const teams = await azureApiCore.getTeams(projectId, undefined, top, skip);
length = teams.length;
allTeams.push(...teams);
skip += top;
} while (top <= length);

return allTeams;
}
24 changes: 12 additions & 12 deletions lib/modules/platform/azure/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1486,15 +1486,15 @@ describe('modules/platform/azure/index', () => {
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,
);
azureHelper.getAllProjectTeams = jest.fn().mockReturnValue([
{ id: 3, name: 'abc' },
{ id: 4, name: 'def' },
]);
await azure.addAssignees(123, ['test@bonjour.fr', 'jyc', 'def']);
expect(azureApi.gitApi).toHaveBeenCalledTimes(3);
});
Expand All @@ -1513,15 +1513,15 @@ describe('modules/platform/azure/index', () => {
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,
);
azureHelper.getAllProjectTeams = jest.fn().mockReturnValue([
{ id: 3, name: 'abc' },
{ id: 4, name: 'def' },
]);
await azure.addReviewers(123, ['test@bonjour.fr', 'jyc', 'required:def']);
expect(azureApi.gitApi).toHaveBeenCalledTimes(3);
expect(logger.once.info).toHaveBeenCalledTimes(1);
Expand All @@ -1539,15 +1539,15 @@ describe('modules/platform/azure/index', () => {
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,
);
azureHelper.getAllProjectTeams = jest.fn().mockReturnValue([
{ id: 3, name: 'abc' },
{ id: 4, name: 'def' },
]);
await azure.addReviewers(123, ['required:jyc']);
expect(azureApi.gitApi).toHaveBeenCalledTimes(3);
expect(logger.once.info).toHaveBeenCalledTimes(0);
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/platform/azure/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,7 @@ async function getUserIds(users: string[]): Promise<User[]> {
const validReviewers = new Set<string>();

// TODO #22198
const teams = await azureApiCore.getTeams(repo.project!.id!);
const teams = await azureHelper.getAllProjectTeams(repo.project!.id!);
const members = await Promise.all(
teams.map(
async (t) =>
Expand Down

0 comments on commit 8d78ca2

Please sign in to comment.