From d76ee63f8533ab0eecc129070b1175f4cb914d51 Mon Sep 17 00:00:00 2001 From: Marley Powell Date: Thu, 2 Dec 2021 11:37:43 +0000 Subject: [PATCH 01/16] feat: Created types for filters. Signed-off-by: Marley Powell --- .../PullRequestsPage/lib/filters/types.ts | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 plugins/azure-devops/src/components/PullRequestsPage/lib/filters/types.ts diff --git a/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/types.ts b/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/types.ts new file mode 100644 index 0000000000..1e6d9fe30e --- /dev/null +++ b/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/types.ts @@ -0,0 +1,46 @@ +/* + * Copyright 2021 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export enum FilterType { + All = 'All', + AssignedToUser = 'AssignedToUser', + CreatedByUser = 'CreatedByUser', + AssignedToCurrentUser = 'AssignedToCurrentUser', + CreatedByCurrentUser = 'CreatedByCurrentUser', + AssignedToTeam = 'AssignedToTeam', + CreatedByTeam = 'CreatedByTeam', + AssignedToTeams = 'AssignedToTeams', + CreatedByTeams = 'CreatedByTeams', + AssignedToCurrentUsersTeams = 'AssignedToCurrentUsersTeams', + CreatedByCurrentUsersTeams = 'CreatedByCurrentUsersTeams', +} + +export const FilterTypes = [ + FilterType.All, + FilterType.AssignedToUser, + FilterType.CreatedByUser, + FilterType.AssignedToCurrentUser, + FilterType.CreatedByCurrentUser, + FilterType.AssignedToTeam, + FilterType.CreatedByTeam, + FilterType.AssignedToTeams, + FilterType.CreatedByTeams, + FilterType.CreatedByCurrentUsersTeams, +] as const; + +export type BaseFilter = { + type: FilterType; +}; From 256d4983013ac3ada51ab03c6b9312e01ec20a2a Mon Sep 17 00:00:00 2001 From: Marley Powell Date: Thu, 2 Dec 2021 11:38:14 +0000 Subject: [PATCH 02/16] feat: Created `allFilter`. Signed-off-by: Marley Powell --- .../PullRequestsPage/lib/filters/allFilter.ts | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 plugins/azure-devops/src/components/PullRequestsPage/lib/filters/allFilter.ts diff --git a/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/allFilter.ts b/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/allFilter.ts new file mode 100644 index 0000000000..0ec7fc36af --- /dev/null +++ b/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/allFilter.ts @@ -0,0 +1,28 @@ +/* + * Copyright 2021 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { BaseFilter, FilterType } from './types'; + +import { DashboardPullRequest } from '@backstage/plugin-azure-devops-common'; +import { PullRequestFilter } from '../types'; + +export type AllFilter = BaseFilter & { + type: FilterType.All; +}; + +export function createAllFilter(): PullRequestFilter { + return (_pullRequest: DashboardPullRequest): boolean => true; +} From 8e2d3c9d548e1aada4decdb03fce784f7147fff5 Mon Sep 17 00:00:00 2001 From: Marley Powell Date: Thu, 2 Dec 2021 11:55:43 +0000 Subject: [PATCH 03/16] feat: Created `assignedToTeamFilter`, `assignedToTeamsFilter` and `assignedToUserFilter`. Signed-off-by: Marley Powell --- .../lib/filters/assignedToTeamFilter.ts | 40 ++++++++++++++ .../lib/filters/assignedToTeamsFilter.ts | 52 +++++++++++++++++++ .../lib/filters/assignedToUserFilter.ts | 51 ++++++++++++++++++ .../PullRequestsPage/lib/filters/types.ts | 21 +++++--- 4 files changed, 157 insertions(+), 7 deletions(-) create mode 100644 plugins/azure-devops/src/components/PullRequestsPage/lib/filters/assignedToTeamFilter.ts create mode 100644 plugins/azure-devops/src/components/PullRequestsPage/lib/filters/assignedToTeamsFilter.ts create mode 100644 plugins/azure-devops/src/components/PullRequestsPage/lib/filters/assignedToUserFilter.ts diff --git a/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/assignedToTeamFilter.ts b/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/assignedToTeamFilter.ts new file mode 100644 index 0000000000..a15895e08f --- /dev/null +++ b/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/assignedToTeamFilter.ts @@ -0,0 +1,40 @@ +/* + * Copyright 2021 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { BaseFilter, FilterType } from './types'; + +import { DashboardPullRequest } from '@backstage/plugin-azure-devops-common'; +import { PullRequestFilter } from '../types'; +import { stringArrayHas } from '../utils'; + +export type AssignedToTeamFilter = BaseFilter & { + type: FilterType.AssignedToTeam; + teamId: string; +}; + +export function createAssignedToTeamFilter( + filter: AssignedToTeamFilter, +): PullRequestFilter { + return (pullRequest: DashboardPullRequest): boolean => { + const reviewerIds = pullRequest.reviewers?.map(reviewer => reviewer.id); + + if (!reviewerIds) { + return false; + } + + return stringArrayHas(reviewerIds, filter.teamId, true); + }; +} diff --git a/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/assignedToTeamsFilter.ts b/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/assignedToTeamsFilter.ts new file mode 100644 index 0000000000..0cc347e0ea --- /dev/null +++ b/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/assignedToTeamsFilter.ts @@ -0,0 +1,52 @@ +/* + * Copyright 2021 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { BaseFilter, FilterType } from './types'; + +import { DashboardPullRequest } from '@backstage/plugin-azure-devops-common'; +import { PullRequestFilter } from '../types'; +import { createAssignedToTeamFilter } from './assignedToTeamFilter'; + +export type AssignedToTeamsFilter = BaseFilter & + ( + | { + type: FilterType.AssignedToTeams; + teamIds: string[]; + } + | { + type: FilterType.AssignedToCurrentUsersTeams; + teamIds?: string[]; + } + ); + +export function createAssignedToTeamsFilter( + filter: AssignedToTeamsFilter, +): PullRequestFilter { + const teamIds = filter.teamIds; + + return (pullRequest: DashboardPullRequest): boolean => { + if (!teamIds) { + return false; + } + + return teamIds.some(teamId => { + return createAssignedToTeamFilter({ + type: FilterType.AssignedToTeam, + teamId, + })(pullRequest); + }); + }; +} diff --git a/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/assignedToUserFilter.ts b/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/assignedToUserFilter.ts new file mode 100644 index 0000000000..9e6cbe4c38 --- /dev/null +++ b/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/assignedToUserFilter.ts @@ -0,0 +1,51 @@ +/* + * Copyright 2021 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { BaseFilter, FilterType } from './types'; + +import { DashboardPullRequest } from '@backstage/plugin-azure-devops-common'; +import { PullRequestFilter } from '../types'; +import { stringArrayHas } from '../utils'; + +export type AssignedToUserFilter = BaseFilter & + ( + | { + type: FilterType.AssignedToUser; + email: string; + } + | { + type: FilterType.AssignedToCurrentUser; + email?: string; + } + ); + +export function createAssignedToUserFilter( + filter: AssignedToUserFilter, +): PullRequestFilter { + const email = filter.email; + + return (pullRequest: DashboardPullRequest): boolean => { + const uniqueNames = pullRequest.reviewers?.map( + reviewer => reviewer.uniqueName, + ); + + if (!email || !uniqueNames) { + return false; + } + + return stringArrayHas(uniqueNames, email, true); + }; +} diff --git a/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/types.ts b/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/types.ts index 1e6d9fe30e..37870703af 100644 --- a/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/types.ts +++ b/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/types.ts @@ -16,27 +16,34 @@ export enum FilterType { All = 'All', + + // Assigned To AssignedToUser = 'AssignedToUser', - CreatedByUser = 'CreatedByUser', AssignedToCurrentUser = 'AssignedToCurrentUser', - CreatedByCurrentUser = 'CreatedByCurrentUser', AssignedToTeam = 'AssignedToTeam', - CreatedByTeam = 'CreatedByTeam', AssignedToTeams = 'AssignedToTeams', - CreatedByTeams = 'CreatedByTeams', AssignedToCurrentUsersTeams = 'AssignedToCurrentUsersTeams', + + // Created By + CreatedByUser = 'CreatedByUser', + CreatedByCurrentUser = 'CreatedByCurrentUser', + CreatedByTeam = 'CreatedByTeam', + CreatedByTeams = 'CreatedByTeams', CreatedByCurrentUsersTeams = 'CreatedByCurrentUsersTeams', } export const FilterTypes = [ FilterType.All, + FilterType.AssignedToUser, - FilterType.CreatedByUser, FilterType.AssignedToCurrentUser, - FilterType.CreatedByCurrentUser, FilterType.AssignedToTeam, - FilterType.CreatedByTeam, FilterType.AssignedToTeams, + FilterType.AssignedToCurrentUsersTeams, + + FilterType.CreatedByUser, + FilterType.CreatedByCurrentUser, + FilterType.CreatedByTeam, FilterType.CreatedByTeams, FilterType.CreatedByCurrentUsersTeams, ] as const; From 50b3d1737dab6b17a0890d2144c991dc97758f39 Mon Sep 17 00:00:00 2001 From: Marley Powell Date: Thu, 2 Dec 2021 12:14:42 +0000 Subject: [PATCH 04/16] feat: Created `createdByTeamFilter`, `createdByTeamsFilter` and `createdByUserFilter`. Signed-off-by: Marley Powell --- .../lib/filters/createdByTeamFilter.ts | 43 ++++++++++++ .../lib/filters/createdByTeamsFilter.ts | 66 +++++++++++++++++++ .../lib/filters/createdByUserFilter.ts | 49 ++++++++++++++ 3 files changed, 158 insertions(+) create mode 100644 plugins/azure-devops/src/components/PullRequestsPage/lib/filters/createdByTeamFilter.ts create mode 100644 plugins/azure-devops/src/components/PullRequestsPage/lib/filters/createdByTeamsFilter.ts create mode 100644 plugins/azure-devops/src/components/PullRequestsPage/lib/filters/createdByUserFilter.ts diff --git a/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/createdByTeamFilter.ts b/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/createdByTeamFilter.ts new file mode 100644 index 0000000000..e0fca3a75f --- /dev/null +++ b/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/createdByTeamFilter.ts @@ -0,0 +1,43 @@ +/* + * Copyright 2021 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { BaseFilter, FilterType } from './types'; + +import { DashboardPullRequest } from '@backstage/plugin-azure-devops-common'; +import { PullRequestFilter } from '../types'; +import { stringArrayHas } from '../utils'; + +export type CreatedByTeamFilter = BaseFilter & + ({ + type: FilterType.CreatedByTeam; + } & ({ teamId: string } | { teamName: string })); + +export function createCreatedByTeamFilter( + filter: CreatedByTeamFilter, +): PullRequestFilter { + return (pullRequest: DashboardPullRequest): boolean => { + const [createdByTeams, team] = + 'teamId' in filter + ? [pullRequest.createdBy?.teamIds, filter.teamId] + : [pullRequest.createdBy?.teamNames, filter.teamName]; + + if (!createdByTeams) { + return false; + } + + return stringArrayHas(createdByTeams, team, true); + }; +} diff --git a/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/createdByTeamsFilter.ts b/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/createdByTeamsFilter.ts new file mode 100644 index 0000000000..ff10deaa14 --- /dev/null +++ b/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/createdByTeamsFilter.ts @@ -0,0 +1,66 @@ +/* + * Copyright 2021 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { BaseFilter, FilterType } from './types'; + +import { DashboardPullRequest } from '@backstage/plugin-azure-devops-common'; +import { PullRequestFilter } from '../types'; +import { createCreatedByTeamFilter } from './createdByTeamFilter'; + +export type CreatedByTeamsFilter = BaseFilter & + ( + | ({ + type: FilterType.CreatedByTeams; + } & ({ teamIds: string[] } | { teamNames: string[] })) + | { + type: FilterType.CreatedByCurrentUsersTeams; + teamIds?: string[]; + } + ); + +export function createCreatedByTeamsFilter( + filter: CreatedByTeamsFilter, +): PullRequestFilter { + return (pullRequest: DashboardPullRequest): boolean => { + if ('teamNames' in filter) { + const teamNames = filter.teamNames; + + if (!teamNames) { + return false; + } + + return teamNames.some(teamName => { + return createCreatedByTeamFilter({ + type: FilterType.CreatedByTeam, + teamName, + })(pullRequest); + }); + } + + const teamIds = filter.teamIds; + + if (!teamIds) { + return false; + } + + return teamIds.some(teamId => { + return createCreatedByTeamFilter({ + type: FilterType.CreatedByTeam, + teamId, + })(pullRequest); + }); + }; +} diff --git a/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/createdByUserFilter.ts b/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/createdByUserFilter.ts new file mode 100644 index 0000000000..19c5cbc1b1 --- /dev/null +++ b/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/createdByUserFilter.ts @@ -0,0 +1,49 @@ +/* + * Copyright 2021 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { BaseFilter, FilterType } from './types'; + +import { DashboardPullRequest } from '@backstage/plugin-azure-devops-common'; +import { PullRequestFilter } from '../types'; +import { equalsIgnoreCase } from '../utils'; + +export type CreatedByUserFilter = BaseFilter & + ( + | { + type: FilterType.CreatedByUser; + email: string; + } + | { + type: FilterType.CreatedByCurrentUser; + email?: string; + } + ); + +export function createCreatedByUserFilter( + filter: CreatedByUserFilter, +): PullRequestFilter { + const email = filter.email; + + return (pullRequest: DashboardPullRequest): boolean => { + const uniqueName = pullRequest.createdBy?.uniqueName; + + if (!email || !uniqueName) { + return false; + } + + return equalsIgnoreCase(email, uniqueName); + }; +} From 2a02690b1154a6484d275147b2f229a9ee2ac4b4 Mon Sep 17 00:00:00 2001 From: Marley Powell Date: Thu, 2 Dec 2021 13:34:14 +0000 Subject: [PATCH 05/16] refactor: Moved around filter types. Signed-off-by: Marley Powell --- .../lib/filters/assignedToTeamFilter.ts | 3 +- .../lib/filters/assignedToTeamsFilter.ts | 3 +- .../lib/filters/assignedToUserFilter.ts | 3 +- .../lib/filters/createFilter.ts | 71 +++++++++++++++++++ .../lib/filters/createdByTeamFilter.ts | 3 +- .../lib/filters/createdByTeamsFilter.ts | 3 +- .../lib/filters/createdByUserFilter.ts | 3 +- .../PullRequestsPage/lib/filters/index.ts | 19 +++++ .../PullRequestsPage/lib/filters/types.ts | 20 ++++++ .../components/PullRequestsPage/lib/types.ts | 21 +++--- 10 files changed, 127 insertions(+), 22 deletions(-) create mode 100644 plugins/azure-devops/src/components/PullRequestsPage/lib/filters/createFilter.ts create mode 100644 plugins/azure-devops/src/components/PullRequestsPage/lib/filters/index.ts diff --git a/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/assignedToTeamFilter.ts b/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/assignedToTeamFilter.ts index a15895e08f..3c48f04541 100644 --- a/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/assignedToTeamFilter.ts +++ b/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/assignedToTeamFilter.ts @@ -14,10 +14,9 @@ * limitations under the License. */ -import { BaseFilter, FilterType } from './types'; +import { BaseFilter, FilterType, PullRequestFilter } from './types'; import { DashboardPullRequest } from '@backstage/plugin-azure-devops-common'; -import { PullRequestFilter } from '../types'; import { stringArrayHas } from '../utils'; export type AssignedToTeamFilter = BaseFilter & { diff --git a/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/assignedToTeamsFilter.ts b/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/assignedToTeamsFilter.ts index 0cc347e0ea..ce5e5bcc11 100644 --- a/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/assignedToTeamsFilter.ts +++ b/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/assignedToTeamsFilter.ts @@ -14,10 +14,9 @@ * limitations under the License. */ -import { BaseFilter, FilterType } from './types'; +import { BaseFilter, FilterType, PullRequestFilter } from './types'; import { DashboardPullRequest } from '@backstage/plugin-azure-devops-common'; -import { PullRequestFilter } from '../types'; import { createAssignedToTeamFilter } from './assignedToTeamFilter'; export type AssignedToTeamsFilter = BaseFilter & diff --git a/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/assignedToUserFilter.ts b/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/assignedToUserFilter.ts index 9e6cbe4c38..128c9103ab 100644 --- a/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/assignedToUserFilter.ts +++ b/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/assignedToUserFilter.ts @@ -14,10 +14,9 @@ * limitations under the License. */ -import { BaseFilter, FilterType } from './types'; +import { BaseFilter, FilterType, PullRequestFilter } from './types'; import { DashboardPullRequest } from '@backstage/plugin-azure-devops-common'; -import { PullRequestFilter } from '../types'; import { stringArrayHas } from '../utils'; export type AssignedToUserFilter = BaseFilter & diff --git a/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/createFilter.ts b/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/createFilter.ts new file mode 100644 index 0000000000..492d0539c5 --- /dev/null +++ b/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/createFilter.ts @@ -0,0 +1,71 @@ +/* + * Copyright 2021 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { Filter, FilterType, PullRequestFilter } from './types'; + +import { DashboardPullRequest } from '@backstage/plugin-azure-devops-common'; +import { createAllFilter } from './allFilter'; +import { createAssignedToTeamFilter } from './assignedToTeamFilter'; +import { createAssignedToTeamsFilter } from './assignedToTeamsFilter'; +import { createAssignedToUserFilter } from './assignedToUserFilter'; +import { createCreatedByTeamFilter } from './createdByTeamFilter'; +import { createCreatedByTeamsFilter } from './createdByTeamsFilter'; +import { createCreatedByUserFilter } from './createdByUserFilter'; + +export function createFilter(filters: Filter | Filter[]): PullRequestFilter { + const mapFilter = (filter: Filter): PullRequestFilter => { + switch (filter.type) { + case FilterType.AssignedToUser: + case FilterType.AssignedToCurrentUser: + return createAssignedToUserFilter(filter); + + case FilterType.CreatedByUser: + case FilterType.CreatedByCurrentUser: + return createCreatedByUserFilter(filter); + + case FilterType.AssignedToTeam: + return createAssignedToTeamFilter(filter); + + case FilterType.CreatedByTeam: + return createCreatedByTeamFilter(filter); + + case FilterType.AssignedToTeams: + case FilterType.AssignedToCurrentUsersTeams: + return createAssignedToTeamsFilter(filter); + + case FilterType.CreatedByTeams: + case FilterType.CreatedByCurrentUsersTeams: + return createCreatedByTeamsFilter(filter); + + case FilterType.All: + return createAllFilter(); + + default: + return _ => false; + } + }; + + if (Array.isArray(filters)) { + if (filters.length === 1) { + return mapFilter(filters[0]); + } + + return (pullRequest: DashboardPullRequest): boolean => + filters.every(filter => mapFilter(filter)(pullRequest)); + } + + return mapFilter(filters); +} diff --git a/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/createdByTeamFilter.ts b/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/createdByTeamFilter.ts index e0fca3a75f..0d476af9b9 100644 --- a/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/createdByTeamFilter.ts +++ b/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/createdByTeamFilter.ts @@ -14,10 +14,9 @@ * limitations under the License. */ -import { BaseFilter, FilterType } from './types'; +import { BaseFilter, FilterType, PullRequestFilter } from './types'; import { DashboardPullRequest } from '@backstage/plugin-azure-devops-common'; -import { PullRequestFilter } from '../types'; import { stringArrayHas } from '../utils'; export type CreatedByTeamFilter = BaseFilter & diff --git a/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/createdByTeamsFilter.ts b/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/createdByTeamsFilter.ts index ff10deaa14..34ed81db5d 100644 --- a/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/createdByTeamsFilter.ts +++ b/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/createdByTeamsFilter.ts @@ -14,10 +14,9 @@ * limitations under the License. */ -import { BaseFilter, FilterType } from './types'; +import { BaseFilter, FilterType, PullRequestFilter } from './types'; import { DashboardPullRequest } from '@backstage/plugin-azure-devops-common'; -import { PullRequestFilter } from '../types'; import { createCreatedByTeamFilter } from './createdByTeamFilter'; export type CreatedByTeamsFilter = BaseFilter & diff --git a/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/createdByUserFilter.ts b/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/createdByUserFilter.ts index 19c5cbc1b1..e0a1a2b535 100644 --- a/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/createdByUserFilter.ts +++ b/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/createdByUserFilter.ts @@ -14,10 +14,9 @@ * limitations under the License. */ -import { BaseFilter, FilterType } from './types'; +import { BaseFilter, FilterType, PullRequestFilter } from './types'; import { DashboardPullRequest } from '@backstage/plugin-azure-devops-common'; -import { PullRequestFilter } from '../types'; import { equalsIgnoreCase } from '../utils'; export type CreatedByUserFilter = BaseFilter & diff --git a/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/index.ts b/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/index.ts new file mode 100644 index 0000000000..e20dfdbd18 --- /dev/null +++ b/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/index.ts @@ -0,0 +1,19 @@ +/* + * Copyright 2021 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export { createFilter } from './createFilter'; +export { FilterTypes } from './types'; +export type { Filter, PullRequestFilter, FilterType } from './types'; diff --git a/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/types.ts b/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/types.ts index 37870703af..085f38cb30 100644 --- a/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/types.ts +++ b/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/types.ts @@ -14,6 +14,15 @@ * limitations under the License. */ +import { AllFilter } from './allFilter'; +import { AssignedToTeamFilter } from './assignedToTeamFilter'; +import { AssignedToTeamsFilter } from './assignedToTeamsFilter'; +import { AssignedToUserFilter } from './assignedToUserFilter'; +import { CreatedByTeamFilter } from './createdByTeamFilter'; +import { CreatedByTeamsFilter } from './createdByTeamsFilter'; +import { CreatedByUserFilter } from './createdByUserFilter'; +import { DashboardPullRequest } from '@backstage/plugin-azure-devops-common'; + export enum FilterType { All = 'All', @@ -51,3 +60,14 @@ export const FilterTypes = [ export type BaseFilter = { type: FilterType; }; + +export type Filter = + | AssignedToUserFilter + | CreatedByUserFilter + | AssignedToTeamFilter + | CreatedByTeamFilter + | AssignedToTeamsFilter + | CreatedByTeamsFilter + | AllFilter; + +export type PullRequestFilter = (pullRequest: DashboardPullRequest) => boolean; diff --git a/plugins/azure-devops/src/components/PullRequestsPage/lib/types.ts b/plugins/azure-devops/src/components/PullRequestsPage/lib/types.ts index 2d936f7b64..74c8ebe605 100644 --- a/plugins/azure-devops/src/components/PullRequestsPage/lib/types.ts +++ b/plugins/azure-devops/src/components/PullRequestsPage/lib/types.ts @@ -14,23 +14,24 @@ * limitations under the License. */ -import { - DashboardPullRequest, - Team, -} from '@backstage/plugin-azure-devops-common'; +import { Filter, PullRequestFilter } from './filters'; -export interface PullRequestGroup { +import { DashboardPullRequest } from '@backstage/plugin-azure-devops-common'; + +export interface PullRequestColumnConfig { title: string; - pullRequests: DashboardPullRequest[]; + filters: Filter[]; simplified?: boolean; } -export type PullRequestFilter = (pullRequest: DashboardPullRequest) => boolean; - -export type TeamFilter = (team: Team) => boolean; - export interface PullRequestGroupConfig { title: string; filter: PullRequestFilter; simplified?: boolean; } + +export interface PullRequestGroup { + title: string; + pullRequests: DashboardPullRequest[]; + simplified?: boolean; +} From 4e50a33f8edd713ba78f93b0678d16f8e3ffe78b Mon Sep 17 00:00:00 2001 From: Marley Powell Date: Mon, 6 Dec 2021 14:21:45 +0000 Subject: [PATCH 06/16] refactor: Update import references. Signed-off-by: Marley Powell --- .../src/components/PullRequestsPage/lib/filters/allFilter.ts | 3 +-- .../PullRequestsPage/lib/filters/assignedToTeamFilter.ts | 2 +- .../PullRequestsPage/lib/filters/assignedToUserFilter.ts | 2 +- .../PullRequestsPage/lib/filters/createdByTeamFilter.ts | 2 +- .../PullRequestsPage/lib/filters/createdByUserFilter.ts | 2 +- .../src/components/PullRequestsPage/lib/filters/index.ts | 4 ++-- 6 files changed, 7 insertions(+), 8 deletions(-) diff --git a/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/allFilter.ts b/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/allFilter.ts index 0ec7fc36af..90ec822ef6 100644 --- a/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/allFilter.ts +++ b/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/allFilter.ts @@ -14,10 +14,9 @@ * limitations under the License. */ -import { BaseFilter, FilterType } from './types'; +import { BaseFilter, FilterType, PullRequestFilter } from './types'; import { DashboardPullRequest } from '@backstage/plugin-azure-devops-common'; -import { PullRequestFilter } from '../types'; export type AllFilter = BaseFilter & { type: FilterType.All; diff --git a/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/assignedToTeamFilter.ts b/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/assignedToTeamFilter.ts index 3c48f04541..8e816db2c3 100644 --- a/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/assignedToTeamFilter.ts +++ b/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/assignedToTeamFilter.ts @@ -17,7 +17,7 @@ import { BaseFilter, FilterType, PullRequestFilter } from './types'; import { DashboardPullRequest } from '@backstage/plugin-azure-devops-common'; -import { stringArrayHas } from '../utils'; +import { stringArrayHas } from '../../../../utils'; export type AssignedToTeamFilter = BaseFilter & { type: FilterType.AssignedToTeam; diff --git a/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/assignedToUserFilter.ts b/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/assignedToUserFilter.ts index 128c9103ab..95ee3d5a59 100644 --- a/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/assignedToUserFilter.ts +++ b/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/assignedToUserFilter.ts @@ -17,7 +17,7 @@ import { BaseFilter, FilterType, PullRequestFilter } from './types'; import { DashboardPullRequest } from '@backstage/plugin-azure-devops-common'; -import { stringArrayHas } from '../utils'; +import { stringArrayHas } from '../../../../utils'; export type AssignedToUserFilter = BaseFilter & ( diff --git a/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/createdByTeamFilter.ts b/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/createdByTeamFilter.ts index 0d476af9b9..70a388b540 100644 --- a/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/createdByTeamFilter.ts +++ b/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/createdByTeamFilter.ts @@ -17,7 +17,7 @@ import { BaseFilter, FilterType, PullRequestFilter } from './types'; import { DashboardPullRequest } from '@backstage/plugin-azure-devops-common'; -import { stringArrayHas } from '../utils'; +import { stringArrayHas } from '../../../../utils'; export type CreatedByTeamFilter = BaseFilter & ({ diff --git a/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/createdByUserFilter.ts b/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/createdByUserFilter.ts index e0a1a2b535..69d4f23db5 100644 --- a/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/createdByUserFilter.ts +++ b/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/createdByUserFilter.ts @@ -17,7 +17,7 @@ import { BaseFilter, FilterType, PullRequestFilter } from './types'; import { DashboardPullRequest } from '@backstage/plugin-azure-devops-common'; -import { equalsIgnoreCase } from '../utils'; +import { equalsIgnoreCase } from '../../../../utils'; export type CreatedByUserFilter = BaseFilter & ( diff --git a/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/index.ts b/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/index.ts index e20dfdbd18..5470b349cf 100644 --- a/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/index.ts +++ b/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/index.ts @@ -15,5 +15,5 @@ */ export { createFilter } from './createFilter'; -export { FilterTypes } from './types'; -export type { Filter, PullRequestFilter, FilterType } from './types'; +export { FilterTypes, FilterType } from './types'; +export type { Filter, PullRequestFilter } from './types'; From 72bd5eb0753105e51459a71784891bbd435e69a2 Mon Sep 17 00:00:00 2001 From: Marley Powell Date: Mon, 6 Dec 2021 14:43:23 +0000 Subject: [PATCH 07/16] feat: Created `arrayHas` and `stringArrayHas` util functions. Signed-off-by: Marley Powell --- plugins/azure-devops/src/utils/arrayHas.ts | 34 ++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 plugins/azure-devops/src/utils/arrayHas.ts diff --git a/plugins/azure-devops/src/utils/arrayHas.ts b/plugins/azure-devops/src/utils/arrayHas.ts new file mode 100644 index 0000000000..8dac8b8ec9 --- /dev/null +++ b/plugins/azure-devops/src/utils/arrayHas.ts @@ -0,0 +1,34 @@ +/* + * Copyright 2021 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export function arrayHas(arr: T[], value: T): boolean { + return new Set(arr).has(value); +} + +export function stringArrayHas( + arr: Array, + value: string | undefined, + ignoreCase: boolean = false, +): boolean { + if (ignoreCase) { + return arrayHas( + arr.map(a => a?.toLocaleLowerCase('en-US')), + value?.toLocaleLowerCase('en-US'), + ); + } + + return arrayHas(arr, value); +} From 362998f62eefadbe3744f39501f3d09cbefe6a88 Mon Sep 17 00:00:00 2001 From: Marley Powell Date: Mon, 6 Dec 2021 14:44:03 +0000 Subject: [PATCH 08/16] feat: Created `equalsIgnoreCase` util function. Signed-off-by: Marley Powell --- .../src/utils/equalsIgnoreCase.ts | 19 +++++++++++++++++++ plugins/azure-devops/src/utils/index.ts | 19 +++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 plugins/azure-devops/src/utils/equalsIgnoreCase.ts create mode 100644 plugins/azure-devops/src/utils/index.ts diff --git a/plugins/azure-devops/src/utils/equalsIgnoreCase.ts b/plugins/azure-devops/src/utils/equalsIgnoreCase.ts new file mode 100644 index 0000000000..12b058e6ae --- /dev/null +++ b/plugins/azure-devops/src/utils/equalsIgnoreCase.ts @@ -0,0 +1,19 @@ +/* + * Copyright 2021 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export function equalsIgnoreCase(str1: string, str2: string): boolean { + return str1.toLocaleLowerCase('en-US') === str2.toLocaleLowerCase('en-US'); +} diff --git a/plugins/azure-devops/src/utils/index.ts b/plugins/azure-devops/src/utils/index.ts new file mode 100644 index 0000000000..f2214e8106 --- /dev/null +++ b/plugins/azure-devops/src/utils/index.ts @@ -0,0 +1,19 @@ +/* + * Copyright 2021 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export * from './arrayHas'; +export * from './equalsIgnoreCase'; +export * from './getDurationFromDates'; From ed133e68cbf14bff9a17410ab84000e9cf75e817 Mon Sep 17 00:00:00 2001 From: Marley Powell Date: Mon, 6 Dec 2021 14:55:23 +0000 Subject: [PATCH 09/16] feat: Updated `PullRequestsPage` to use new column config approach. Signed-off-by: Marley Powell --- .../PullRequestsPage/PullRequestsPage.tsx | 88 ++++++++----------- 1 file changed, 37 insertions(+), 51 deletions(-) diff --git a/plugins/azure-devops/src/components/PullRequestsPage/PullRequestsPage.tsx b/plugins/azure-devops/src/components/PullRequestsPage/PullRequestsPage.tsx index 9f21c4b81b..158c4bca5b 100644 --- a/plugins/azure-devops/src/components/PullRequestsPage/PullRequestsPage.tsx +++ b/plugins/azure-devops/src/components/PullRequestsPage/PullRequestsPage.tsx @@ -21,56 +21,17 @@ import { Progress, ResponseErrorPanel, } from '@backstage/core-components'; -import { PullRequestGroup, PullRequestGroupConfig } from './lib/types'; -import React, { useEffect, useState } from 'react'; -import { getCreatedByUserFilter, getPullRequestGroups } from './lib/utils'; -import { useDashboardPullRequests, useUserEmail } from '../../hooks'; +import { PullRequestColumnConfig, PullRequestGroup } from './lib/types'; +import React, { useState } from 'react'; +import { getPullRequestGroupConfigs, getPullRequestGroups } from './lib/utils'; -import { DashboardPullRequest } from '@backstage/plugin-azure-devops-common'; +import { FilterType } from './lib/filters'; import { PullRequestGrid } from './lib/PullRequestGrid'; - -function usePullRequestGroupConfigs( - userEmail: string | undefined, -): PullRequestGroupConfig[] { - const [pullRequestGroupConfigs, setPullRequestGroupConfigs] = useState< - PullRequestGroupConfig[] - >([]); - - useEffect(() => { - const prGroupConfigs: PullRequestGroupConfig[] = [ - { title: 'Created by me', filter: getCreatedByUserFilter(userEmail) }, - { title: 'Other PRs', filter: _ => true, simplified: false }, - ]; - - setPullRequestGroupConfigs(prGroupConfigs); - }, [userEmail]); - - return pullRequestGroupConfigs; -} - -function usePullRequestGroups( - pullRequests: DashboardPullRequest[] | undefined, - pullRequestGroupConfigs: PullRequestGroupConfig[], -): PullRequestGroup[] { - const [pullRequestGroups, setPullRequestGroups] = useState< - PullRequestGroup[] - >([]); - - useEffect(() => { - if (pullRequests) { - const groups = getPullRequestGroups( - pullRequests, - pullRequestGroupConfigs, - ); - setPullRequestGroups(groups); - } - }, [pullRequests, pullRequestGroupConfigs]); - - return pullRequestGroups; -} +import { useDashboardPullRequests } from '../../hooks'; +import { useFilterProcessor } from './lib/hooks'; type PullRequestsPageContentProps = { - pullRequestGroups: PullRequestGroup[]; + pullRequestGroups: PullRequestGroup[] | undefined; loading: boolean; error?: Error; }; @@ -80,7 +41,7 @@ const PullRequestsPageContent = ({ loading, error, }: PullRequestsPageContentProps) => { - if (loading && pullRequestGroups.length <= 0) { + if (loading && (!pullRequestGroups || pullRequestGroups.length <= 0)) { return ; } @@ -88,25 +49,50 @@ const PullRequestsPageContent = ({ return ; } - return ; + return ; }; +const DEFAULT_COLUMN_CONFIGS: PullRequestColumnConfig[] = [ + { + title: 'Created by me', + filters: [{ type: FilterType.CreatedByCurrentUser }], + simplified: false, + }, + { + title: 'Other PRs', + filters: [{ type: FilterType.All }], + simplified: true, + }, +]; + type PullRequestsPageProps = { projectName?: string; pollingInterval?: number; + defaultColumnConfigs?: PullRequestColumnConfig[]; }; export const PullRequestsPage = ({ projectName, pollingInterval, + defaultColumnConfigs, }: PullRequestsPageProps) => { const { pullRequests, loading, error } = useDashboardPullRequests( projectName, pollingInterval, ); - const userEmail = useUserEmail(); - const pullRequestGroupConfigs = usePullRequestGroupConfigs(userEmail); - const pullRequestGroups = usePullRequestGroups( + + const [columnConfigs] = useState( + defaultColumnConfigs ?? DEFAULT_COLUMN_CONFIGS, + ); + + const filterProcessor = useFilterProcessor(); + + const pullRequestGroupConfigs = getPullRequestGroupConfigs( + columnConfigs, + filterProcessor, + ); + + const pullRequestGroups = getPullRequestGroups( pullRequests, pullRequestGroupConfigs, ); From 949e73e125651ff629b8bf5c65a4940b158114b9 Mon Sep 17 00:00:00 2001 From: Marley Powell Date: Mon, 6 Dec 2021 14:56:37 +0000 Subject: [PATCH 10/16] feat: Created new `getPullRequestGroupConfigs` function. Signed-off-by: Marley Powell --- .../PullRequestsPage/lib/utils.test.ts | 2 +- .../components/PullRequestsPage/lib/utils.ts | 38 +++++++++++-------- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/plugins/azure-devops/src/components/PullRequestsPage/lib/utils.test.ts b/plugins/azure-devops/src/components/PullRequestsPage/lib/utils.test.ts index da27b2e06e..ffe58e2918 100644 --- a/plugins/azure-devops/src/components/PullRequestsPage/lib/utils.test.ts +++ b/plugins/azure-devops/src/components/PullRequestsPage/lib/utils.test.ts @@ -132,7 +132,7 @@ describe('getPullRequestGroups', () => { { title: 'Other PRs', filter: (_: unknown) => true, simplified: true }, ]; - const result = getPullRequestGroups(pullRequests, configs); + const result = getPullRequestGroups(pullRequests, configs) ?? []; expect(result.length).toBe(2); diff --git a/plugins/azure-devops/src/components/PullRequestsPage/lib/utils.ts b/plugins/azure-devops/src/components/PullRequestsPage/lib/utils.ts index c9cf866010..c4fcdc9f7e 100644 --- a/plugins/azure-devops/src/components/PullRequestsPage/lib/utils.ts +++ b/plugins/azure-devops/src/components/PullRequestsPage/lib/utils.ts @@ -19,25 +19,13 @@ import { PullRequestVoteStatus, Reviewer, } from '@backstage/plugin-azure-devops-common'; +import { Filter, createFilter } from './filters'; import { - PullRequestFilter, + PullRequestColumnConfig, PullRequestGroup, PullRequestGroupConfig, } from './types'; -/** - * Creates a filter that matches pull requests created by `userEmail`. - * @param userEmail an email to filter by. - * @returns a filter for pull requests created by `userEmail`. - */ -export function getCreatedByUserFilter( - userEmail: string | undefined, -): PullRequestFilter { - return (pullRequest: DashboardPullRequest): boolean => - pullRequest.createdBy?.uniqueName?.toLocaleLowerCase() === - userEmail?.toLocaleLowerCase(); -} - /** * Filters a reviewer based on vote status and if the reviewer is required. * @param reviewer a reviewer to filter. @@ -97,9 +85,13 @@ export function arrayExtract(arr: T[], filter: (value: T) => unknown): T[] { * @returns a list of pull request groups. */ export function getPullRequestGroups( - pullRequests: DashboardPullRequest[], + pullRequests: DashboardPullRequest[] | undefined, configs: PullRequestGroupConfig[], -): PullRequestGroup[] { +): PullRequestGroup[] | undefined { + if (!pullRequests) { + return undefined; + } + const remainingPullRequests: DashboardPullRequest[] = [...pullRequests]; const pullRequestGroups: PullRequestGroup[] = []; @@ -115,3 +107,17 @@ export function getPullRequestGroups( return pullRequestGroups; } + +export function getPullRequestGroupConfigs( + columnConfigs: PullRequestColumnConfig[], + filterProcessor: (filters: Filter[]) => Filter[], +): PullRequestGroupConfig[] { + return columnConfigs.map(columnConfig => { + const filters = filterProcessor(columnConfig.filters); + return { + title: columnConfig.title, + filter: createFilter(filters), + simplified: columnConfig.simplified, + }; + }); +} From 6c77a8ccf510807ae13814da06838621d1eec142 Mon Sep 17 00:00:00 2001 From: Marley Powell Date: Mon, 6 Dec 2021 14:58:09 +0000 Subject: [PATCH 11/16] feat: Created `useFilterProcessor` hook for populating filter values. Signed-off-by: Marley Powell --- .../PullRequestsPage/lib/hooks/index.ts | 17 ++++++++ .../lib/hooks/useFilterProcessor.ts | 39 +++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 plugins/azure-devops/src/components/PullRequestsPage/lib/hooks/index.ts create mode 100644 plugins/azure-devops/src/components/PullRequestsPage/lib/hooks/useFilterProcessor.ts diff --git a/plugins/azure-devops/src/components/PullRequestsPage/lib/hooks/index.ts b/plugins/azure-devops/src/components/PullRequestsPage/lib/hooks/index.ts new file mode 100644 index 0000000000..2e87dfea0c --- /dev/null +++ b/plugins/azure-devops/src/components/PullRequestsPage/lib/hooks/index.ts @@ -0,0 +1,17 @@ +/* + * Copyright 2021 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export * from './useFilterProcessor'; diff --git a/plugins/azure-devops/src/components/PullRequestsPage/lib/hooks/useFilterProcessor.ts b/plugins/azure-devops/src/components/PullRequestsPage/lib/hooks/useFilterProcessor.ts new file mode 100644 index 0000000000..546b23430d --- /dev/null +++ b/plugins/azure-devops/src/components/PullRequestsPage/lib/hooks/useFilterProcessor.ts @@ -0,0 +1,39 @@ +/* + * Copyright 2021 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { Filter, FilterType } from '../filters'; + +import { useUserEmail } from '../../../../hooks'; + +export function useFilterProcessor(): (filters: Filter[]) => Filter[] { + const userEmail = useUserEmail(); + + return (filters: Filter[]): Filter[] => { + for (const filter of filters) { + switch (filter.type) { + case FilterType.AssignedToCurrentUser: + case FilterType.CreatedByCurrentUser: + filter.email = userEmail; + break; + + default: + break; + } + } + + return filters; + }; +} From c0ac89d531a877da2f2654899dcfb0bcdda9c810 Mon Sep 17 00:00:00 2001 From: Marley Powell Date: Mon, 6 Dec 2021 15:12:15 +0000 Subject: [PATCH 12/16] feat: Added additional types. Signed-off-by: Marley Powell --- .../src/utils/azure-devops-utils.ts | 1 + plugins/azure-devops-common/src/types.ts | 3 ++ .../PullRequestsPage/lib/utils.test.ts | 34 ++++--------------- 3 files changed, 10 insertions(+), 28 deletions(-) diff --git a/plugins/azure-devops-backend/src/utils/azure-devops-utils.ts b/plugins/azure-devops-backend/src/utils/azure-devops-utils.ts index aa843e9c17..7a0d0a279e 100644 --- a/plugins/azure-devops-backend/src/utils/azure-devops-utils.ts +++ b/plugins/azure-devops-backend/src/utils/azure-devops-utils.ts @@ -215,6 +215,7 @@ function convertReviewer( return { id: identityRef.id, displayName: identityRef.displayName, + uniqueName: identityRef.uniqueName, imageUrl: getAvatarUrl(identityRef), isRequired: identityRef.isRequired, isContainer: identityRef.isContainer, diff --git a/plugins/azure-devops-common/src/types.ts b/plugins/azure-devops-common/src/types.ts index da41e08d1f..c87d8641ad 100644 --- a/plugins/azure-devops-common/src/types.ts +++ b/plugins/azure-devops-common/src/types.ts @@ -145,6 +145,7 @@ export interface DashboardPullRequest { export interface Reviewer { id?: string; displayName?: string; + uniqueName?: string; imageUrl?: string; isRequired?: boolean; isContainer?: boolean; @@ -164,6 +165,8 @@ export interface CreatedBy { displayName?: string; uniqueName?: string; imageUrl?: string; + teamIds?: string[]; + teamNames?: string[]; } export interface Repository { diff --git a/plugins/azure-devops/src/components/PullRequestsPage/lib/utils.test.ts b/plugins/azure-devops/src/components/PullRequestsPage/lib/utils.test.ts index ffe58e2918..d128cbd1db 100644 --- a/plugins/azure-devops/src/components/PullRequestsPage/lib/utils.test.ts +++ b/plugins/azure-devops/src/components/PullRequestsPage/lib/utils.test.ts @@ -19,33 +19,7 @@ import { PullRequestVoteStatus, Reviewer, } from '@backstage/plugin-azure-devops-common'; -import { - arrayExtract, - getCreatedByUserFilter, - getPullRequestGroups, - reviewerFilter, -} from './utils'; - -describe('getCreatedByUserFilter', () => { - it('should filter if pull request is created by user', () => { - const userEmail = 'user1@backstage.com'; - const pr = { - createdBy: { uniqueName: userEmail }, - } as DashboardPullRequest; - const result = getCreatedByUserFilter(userEmail)(pr); - expect(result).toBe(true); - }); - - it('should not filter if pull request is not created by user', () => { - const userEmail1 = 'user1@backstage.com'; - const userEmail2 = 'user2@backstage.com'; - const pr = { - createdBy: { uniqueName: userEmail1 }, - } as DashboardPullRequest; - const result = getCreatedByUserFilter(userEmail2)(pr); - expect(result).toBe(false); - }); -}); +import { arrayExtract, getPullRequestGroups, reviewerFilter } from './utils'; describe('reviewerFilter', () => { it('should return false if reviewer has no vote and is not required', () => { @@ -128,7 +102,11 @@ describe('getPullRequestGroups', () => { ]; const configs = [ - { title: 'Created by me', filter: getCreatedByUserFilter(userEmail) }, + { + title: 'Created by me', + filter: (pullRequest: DashboardPullRequest): boolean => + pullRequest.createdBy?.uniqueName === userEmail, + }, { title: 'Other PRs', filter: (_: unknown) => true, simplified: true }, ]; From daf32e2c9bd184db43fd370918672ff66a1ad870 Mon Sep 17 00:00:00 2001 From: Marley Powell Date: Tue, 14 Dec 2021 15:04:09 +0000 Subject: [PATCH 13/16] chore: Generated changeset. Signed-off-by: Marley Powell --- .changeset/silly-dryers-smile.md | 58 ++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 .changeset/silly-dryers-smile.md diff --git a/.changeset/silly-dryers-smile.md b/.changeset/silly-dryers-smile.md new file mode 100644 index 0000000000..7337651532 --- /dev/null +++ b/.changeset/silly-dryers-smile.md @@ -0,0 +1,58 @@ +--- +'@backstage/plugin-azure-devops': patch +'@backstage/plugin-azure-devops-backend': patch +'@backstage/plugin-azure-devops-common': patch +--- + +Created some initial filters that can be used to create pull request columns: + +- All +- AssignedToUser +- AssignedToCurrentUser +- AssignedToTeam +- AssignedToTeams +- AssignedToCurrentUsersTeams +- CreatedByUser +- CreatedByCurrentUser +- CreatedByTeam +- CreatedByTeams +- CreatedByCurrentUsersTeams + +Example custom column creation: + +```tsx +const COLUMN_CONFIGS: PullRequestColumnConfig[] = [ + { + title: 'Created by me', + filters: [{ type: FilterType.CreatedByCurrentUser }], + }, + { + title: 'Created by Backstage Core', + filters: [ + { + type: FilterType.CreatedByTeam, + teamName: 'Backstage Core', + }, + ], + }, + { + title: 'Assigned to my teams', + filters: [{ type: FilterType.AssignedToCurrentUsersTeams }], + }, + { + title: 'Other PRs', + filters: [{ type: FilterType.All }], + simplified: true, + }, +]; + + + } +/>; +``` From 98e01d0f2b0677a56af39b0864d9c1545b1cad99 Mon Sep 17 00:00:00 2001 From: Marley Powell Date: Tue, 14 Dec 2021 15:04:52 +0000 Subject: [PATCH 14/16] feat: Updated type exports. Signed-off-by: Marley Powell --- .../src/components/PullRequestsPage/index.ts | 14 ++++++++++++++ .../PullRequestsPage/lib/filters/index.ts | 13 ++++++++++++- .../PullRequestsPage/lib/filters/types.ts | 10 ++++++++++ plugins/azure-devops/src/index.ts | 15 +++++++++++++++ 4 files changed, 51 insertions(+), 1 deletion(-) diff --git a/plugins/azure-devops/src/components/PullRequestsPage/index.ts b/plugins/azure-devops/src/components/PullRequestsPage/index.ts index e8b6cfa6bb..4061c72a8e 100644 --- a/plugins/azure-devops/src/components/PullRequestsPage/index.ts +++ b/plugins/azure-devops/src/components/PullRequestsPage/index.ts @@ -15,3 +15,17 @@ */ export { PullRequestsPage } from './PullRequestsPage'; +export type { PullRequestColumnConfig } from './lib/types'; +export { FilterType } from './lib/filters'; +export type { + BaseFilter, + Filter, + PullRequestFilter, + AssignedToUserFilter, + CreatedByUserFilter, + AssignedToTeamFilter, + CreatedByTeamFilter, + AssignedToTeamsFilter, + CreatedByTeamsFilter, + AllFilter, +} from './lib/filters'; diff --git a/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/index.ts b/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/index.ts index 5470b349cf..df165dc9ca 100644 --- a/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/index.ts +++ b/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/index.ts @@ -16,4 +16,15 @@ export { createFilter } from './createFilter'; export { FilterTypes, FilterType } from './types'; -export type { Filter, PullRequestFilter } from './types'; +export type { + BaseFilter, + Filter, + PullRequestFilter, + AssignedToUserFilter, + CreatedByUserFilter, + AssignedToTeamFilter, + CreatedByTeamFilter, + AssignedToTeamsFilter, + CreatedByTeamsFilter, + AllFilter, +} from './types'; diff --git a/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/types.ts b/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/types.ts index 085f38cb30..bc4643d51b 100644 --- a/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/types.ts +++ b/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/types.ts @@ -70,4 +70,14 @@ export type Filter = | CreatedByTeamsFilter | AllFilter; +export type { + AssignedToUserFilter, + CreatedByUserFilter, + AssignedToTeamFilter, + CreatedByTeamFilter, + AssignedToTeamsFilter, + CreatedByTeamsFilter, + AllFilter, +}; + export type PullRequestFilter = (pullRequest: DashboardPullRequest) => boolean; diff --git a/plugins/azure-devops/src/index.ts b/plugins/azure-devops/src/index.ts index c9b804bc8d..8289a93a5c 100644 --- a/plugins/azure-devops/src/index.ts +++ b/plugins/azure-devops/src/index.ts @@ -23,3 +23,18 @@ export { } from './plugin'; export { AzurePullRequestsIcon } from './components/AzurePullRequestsIcon'; + +export { FilterType } from './components/PullRequestsPage'; +export type { + PullRequestColumnConfig, + BaseFilter, + Filter, + PullRequestFilter, + AssignedToUserFilter, + CreatedByUserFilter, + AssignedToTeamFilter, + CreatedByTeamFilter, + AssignedToTeamsFilter, + CreatedByTeamsFilter, + AllFilter, +} from './components/PullRequestsPage'; From cc519974963a521ea82bc89bc7b9cdf873cd596a Mon Sep 17 00:00:00 2001 From: Marley Powell Date: Tue, 14 Dec 2021 15:09:14 +0000 Subject: [PATCH 15/16] chore: Updated API reports. Signed-off-by: Marley Powell --- plugins/azure-devops-common/api-report.md | 6 + plugins/azure-devops/api-report.md | 163 ++++++++++++++++++++++ 2 files changed, 169 insertions(+) diff --git a/plugins/azure-devops-common/api-report.md b/plugins/azure-devops-common/api-report.md index bb458f169a..85a9e54847 100644 --- a/plugins/azure-devops-common/api-report.md +++ b/plugins/azure-devops-common/api-report.md @@ -61,6 +61,10 @@ export interface CreatedBy { // (undocumented) imageUrl?: string; // (undocumented) + teamIds?: string[]; + // (undocumented) + teamNames?: string[]; + // (undocumented) uniqueName?: string; } @@ -254,6 +258,8 @@ export interface Reviewer { // (undocumented) isRequired?: boolean; // (undocumented) + uniqueName?: string; + // (undocumented) voteStatus: PullRequestVoteStatus; } diff --git a/plugins/azure-devops/api-report.md b/plugins/azure-devops/api-report.md index 0f3e3310ef..11e238e6f5 100644 --- a/plugins/azure-devops/api-report.md +++ b/plugins/azure-devops/api-report.md @@ -6,9 +6,55 @@ /// import { BackstagePlugin } from '@backstage/core-plugin-api'; +import { DashboardPullRequest } from '@backstage/plugin-azure-devops-common'; import { Entity } from '@backstage/catalog-model'; import { SvgIconProps } from '@material-ui/core'; +// Warning: (ae-missing-release-tag) "AllFilter" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) +// +// @public (undocumented) +export type AllFilter = BaseFilter & { + type: FilterType.All; +}; + +// Warning: (ae-missing-release-tag) "AssignedToTeamFilter" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) +// +// @public (undocumented) +export type AssignedToTeamFilter = BaseFilter & { + type: FilterType.AssignedToTeam; + teamId: string; +}; + +// Warning: (ae-missing-release-tag) "AssignedToTeamsFilter" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) +// +// @public (undocumented) +export type AssignedToTeamsFilter = BaseFilter & + ( + | { + type: FilterType.AssignedToTeams; + teamIds: string[]; + } + | { + type: FilterType.AssignedToCurrentUsersTeams; + teamIds?: string[]; + } + ); + +// Warning: (ae-missing-release-tag) "AssignedToUserFilter" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) +// +// @public (undocumented) +export type AssignedToUserFilter = BaseFilter & + ( + | { + type: FilterType.AssignedToUser; + email: string; + } + | { + type: FilterType.AssignedToCurrentUser; + email?: string; + } + ); + // Warning: (ae-missing-release-tag) "azureDevOpsPlugin" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) // // @public (undocumented) @@ -25,11 +71,71 @@ export const AzurePullRequestsIcon: (props: SvgIconProps) => JSX.Element; export const AzurePullRequestsPage: ({ projectName, pollingInterval, + defaultColumnConfigs, }: { projectName?: string | undefined; pollingInterval?: number | undefined; + defaultColumnConfigs?: PullRequestColumnConfig[] | undefined; }) => JSX.Element; +// Warning: (ae-missing-release-tag) "BaseFilter" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) +// +// @public (undocumented) +export type BaseFilter = { + type: FilterType; +}; + +// Warning: (ae-missing-release-tag) "CreatedByTeamFilter" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) +// +// @public (undocumented) +export type CreatedByTeamFilter = BaseFilter & + ({ + type: FilterType.CreatedByTeam; + } & ( + | { + teamId: string; + } + | { + teamName: string; + } + )); + +// Warning: (ae-missing-release-tag) "CreatedByTeamsFilter" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) +// +// @public (undocumented) +export type CreatedByTeamsFilter = BaseFilter & + ( + | ({ + type: FilterType.CreatedByTeams; + } & ( + | { + teamIds: string[]; + } + | { + teamNames: string[]; + } + )) + | { + type: FilterType.CreatedByCurrentUsersTeams; + teamIds?: string[]; + } + ); + +// Warning: (ae-missing-release-tag) "CreatedByUserFilter" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) +// +// @public (undocumented) +export type CreatedByUserFilter = BaseFilter & + ( + | { + type: FilterType.CreatedByUser; + email: string; + } + | { + type: FilterType.CreatedByCurrentUser; + email?: string; + } + ); + // Warning: (ae-missing-release-tag) "EntityAzurePipelinesContent" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) // // @public (undocumented) @@ -48,10 +154,67 @@ export const EntityAzurePullRequestsContent: ({ defaultLimit?: number | undefined; }) => JSX.Element; +// Warning: (ae-missing-release-tag) "Filter" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) +// +// @public (undocumented) +export type Filter = + | AssignedToUserFilter + | CreatedByUserFilter + | AssignedToTeamFilter + | CreatedByTeamFilter + | AssignedToTeamsFilter + | CreatedByTeamsFilter + | AllFilter; + +// Warning: (ae-missing-release-tag) "FilterType" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) +// +// @public (undocumented) +export enum FilterType { + // (undocumented) + All = 'All', + // (undocumented) + AssignedToCurrentUser = 'AssignedToCurrentUser', + // (undocumented) + AssignedToCurrentUsersTeams = 'AssignedToCurrentUsersTeams', + // (undocumented) + AssignedToTeam = 'AssignedToTeam', + // (undocumented) + AssignedToTeams = 'AssignedToTeams', + // (undocumented) + AssignedToUser = 'AssignedToUser', + // (undocumented) + CreatedByCurrentUser = 'CreatedByCurrentUser', + // (undocumented) + CreatedByCurrentUsersTeams = 'CreatedByCurrentUsersTeams', + // (undocumented) + CreatedByTeam = 'CreatedByTeam', + // (undocumented) + CreatedByTeams = 'CreatedByTeams', + // (undocumented) + CreatedByUser = 'CreatedByUser', +} + // Warning: (ae-missing-release-tag) "isAzureDevOpsAvailable" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) // // @public (undocumented) export const isAzureDevOpsAvailable: (entity: Entity) => boolean; +// Warning: (ae-missing-release-tag) "PullRequestColumnConfig" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) +// +// @public (undocumented) +export interface PullRequestColumnConfig { + // (undocumented) + filters: Filter[]; + // (undocumented) + simplified?: boolean; + // (undocumented) + title: string; +} + +// Warning: (ae-missing-release-tag) "PullRequestFilter" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) +// +// @public (undocumented) +export type PullRequestFilter = (pullRequest: DashboardPullRequest) => boolean; + // (No @packageDocumentation comment for this package) ``` From bd658a58b7647f986a666ff85e9a193847d4de0d Mon Sep 17 00:00:00 2001 From: Marley Powell Date: Tue, 14 Dec 2021 15:40:51 +0000 Subject: [PATCH 16/16] test: Added some unit test for pull request filters. Signed-off-by: Marley Powell --- .../lib/filters/createFilter.test.ts | 150 ++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100644 plugins/azure-devops/src/components/PullRequestsPage/lib/filters/createFilter.test.ts diff --git a/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/createFilter.test.ts b/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/createFilter.test.ts new file mode 100644 index 0000000000..b0ac4d402b --- /dev/null +++ b/plugins/azure-devops/src/components/PullRequestsPage/lib/filters/createFilter.test.ts @@ -0,0 +1,150 @@ +/* + * Copyright 2021 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { Filter, FilterType } from './types'; + +import { DashboardPullRequest } from '@backstage/plugin-azure-devops-common'; +import { createFilter } from './createFilter'; + +describe('createFilter', () => { + const pullRequest = { + createdBy: { + uniqueName: 'user1@backstage.com', + teamIds: ['team1Id', 'team2Id'], + }, + reviewers: [ + { uniqueName: 'user2@backstage.com' }, + { id: 'team2Id' }, + { id: 'team3Id' }, + ], + } as DashboardPullRequest; + + const testCases: Array<{ filter: Filter; result: boolean }> = [ + { + filter: { + type: FilterType.AssignedToUser, + email: 'user2@backstage.com', + }, + result: true, + }, + { + filter: { + type: FilterType.AssignedToUser, + email: 'random-user@backstage.com', + }, + result: false, + }, + { + filter: { + type: FilterType.CreatedByUser, + email: 'user1@backstage.com', + }, + result: true, + }, + { + filter: { + type: FilterType.CreatedByUser, + email: 'random-user@backstage.com', + }, + result: false, + }, + { + filter: { + type: FilterType.AssignedToTeam, + teamId: 'team2Id', + }, + result: true, + }, + { + filter: { + type: FilterType.AssignedToTeam, + teamId: 'randomTeamId', + }, + result: false, + }, + { + filter: { + type: FilterType.CreatedByTeam, + teamId: 'team1Id', + }, + result: true, + }, + { + filter: { + type: FilterType.CreatedByTeam, + teamId: 'randomTeamId', + }, + result: false, + }, + { + filter: { + type: FilterType.AssignedToTeams, + teamIds: ['team2Id', 'randomTeamId'], + }, + result: true, + }, + { + filter: { + type: FilterType.AssignedToTeams, + teamIds: ['team2Id', 'team3Id'], + }, + result: true, + }, + { + filter: { + type: FilterType.AssignedToTeams, + teamIds: ['randomTeam1Id', 'randomTeam2Id'], + }, + result: false, + }, + { + filter: { + type: FilterType.CreatedByTeams, + teamIds: ['team1Id', 'randomTeamId'], + }, + result: true, + }, + { + filter: { + type: FilterType.CreatedByTeams, + teamIds: ['team1Id', 'team2Id'], + }, + result: true, + }, + { + filter: { + type: FilterType.CreatedByTeams, + teamIds: ['randomTeam1Id', 'randomTeam2Id'], + }, + result: false, + }, + { + filter: { + type: FilterType.All, + }, + result: true, + }, + ]; + + testCases.forEach(({ filter, result }) => { + it(`should return ${String(result)} when pull request ${ + result ? 'is' : 'is not' + } ${filter.type}`, () => { + const pullRequestFilter = createFilter(filter); + expect(pullRequestFilter(pullRequest)).toBe(result); + }); + }); +});