Add optional topTeams parameter for getAllTeams method, defaults to 100 which is default in azure-devops-node-api

Signed-off-by: Quadman <QuadmanSWE@gmail.com>
This commit is contained in:
Quadman
2024-03-16 19:13:21 +01:00
parent 66670589d4
commit 95b057332a
6 changed files with 36 additions and 10 deletions
+6
View File
@@ -0,0 +1,6 @@
---
'@backstage/plugin-azure-devops-backend': patch
'@backstage/plugin-azure-devops': patch
---
getAllTeams accepts optional topTeams parameter which can be used to return more than the default top 100 teams from Azure Devops API
@@ -397,12 +397,18 @@ export class AzureDevOpsApi {
.filter((policy): policy is Policy => Boolean(policy));
}
public async getAllTeams(): Promise<Team[]> {
public async getAllTeams(topTeams?: number): Promise<Team[]> {
this.logger?.debug('Getting all teams.');
const webApi = await this.getWebApi();
const client = await webApi.getCoreApi();
const webApiTeams: WebApiTeam[] = await client.getAllTeams();
const webApiTeams: WebApiTeam[] = await client.getAllTeams(
undefined,
topTeams,
undefined,
undefined,
);
const teams: Team[] = webApiTeams.map(team => ({
id: team.id,
@@ -25,6 +25,8 @@ import { AzureDevOpsApi } from './AzureDevOpsApi';
import { Logger } from 'winston';
import limiterFactory from 'p-limit';
const DEFAULT_TOP_TEAMS = 100;
export class PullRequestsDashboardProvider {
private teams = new Map<string, Team>();
@@ -43,10 +45,10 @@ export class PullRequestsDashboardProvider {
return provider;
}
public async readTeams(): Promise<void> {
public async readTeams(topTeams?: number): Promise<void> {
this.logger.info('Reading teams.');
let teams = await this.azureDevOpsApi.getAllTeams();
let teams = await this.azureDevOpsApi.getAllTeams(topTeams);
// This is used to filter out the default Azure Devops project teams.
teams = teams.filter(team =>
@@ -134,9 +136,10 @@ export class PullRequestsDashboardProvider {
);
}
public async getAllTeams(): Promise<Team[]> {
public async getAllTeams(topTeams?: number): Promise<Team[]> {
if (!this.teams.size) {
await this.readTeams();
const maxTeams = topTeams ?? DEFAULT_TOP_TEAMS;
await this.readTeams(maxTeams);
}
return Array.from(this.teams.values());
@@ -267,7 +267,10 @@ export async function createRouter(
});
router.get('/all-teams', async (_req, res) => {
const allTeams = await pullRequestsDashboardProvider.getAllTeams();
const topTeams = _req.query.topTeams
? Number(_req.query.topTeams)
: undefined;
const allTeams = await pullRequestsDashboardProvider.getAllTeams(topTeams);
res.status(200).json(allTeams);
});
@@ -66,7 +66,7 @@ export interface AzureDevOpsApi {
projectName: string,
): Promise<DashboardPullRequest[]>;
getAllTeams(): Promise<Team[]>;
getAllTeams(topTeams?: number): Promise<Team[]>;
getUserTeamIds(userId: string): Promise<string[]>;
@@ -130,8 +130,16 @@ export class AzureDevOpsClient implements AzureDevOpsApi {
);
}
public getAllTeams(): Promise<Team[]> {
return this.get<Team[]>('all-teams');
public getAllTeams(topTeams?: number): Promise<Team[]> {
const queryString = new URLSearchParams();
if (topTeams) {
queryString.append('topTeams', topTeams.toString());
}
let urlSegment = 'all-teams';
if (queryString.toString()) {
urlSegment += `?${queryString}`;
}
return this.get<Team[]>(urlSegment);
}
public getUserTeamIds(userId: string): Promise<string[]> {