From 73268a67fffb712ab5316e56034bc03ae6f58319 Mon Sep 17 00:00:00 2001 From: Niall McCullagh Date: Fri, 22 Jul 2022 23:06:04 +0100 Subject: [PATCH] fix(github-pull-requests-board): filter out github accounts that have been deleted Pull request that contain interactions with user accounts that have been deleted are null in the response from the graphql API. This change prevents deleted accounts from breaking the rendering of the PR. Signed-off-by: Niall McCullagh --- .changeset/silver-poets-push.md | 5 ++ .../src/utils/functions.test.ts | 54 +++++++++++++++++++ .../src/utils/functions.ts | 6 ++- 3 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 .changeset/silver-poets-push.md create mode 100644 plugins/github-pull-requests-board/src/utils/functions.test.ts diff --git a/.changeset/silver-poets-push.md b/.changeset/silver-poets-push.md new file mode 100644 index 0000000000..2e64915a80 --- /dev/null +++ b/.changeset/silver-poets-push.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-github-pull-requests-board': patch +--- + +Fixed rendering when PR contains references to deleted Github accounts diff --git a/plugins/github-pull-requests-board/src/utils/functions.test.ts b/plugins/github-pull-requests-board/src/utils/functions.test.ts new file mode 100644 index 0000000000..b968d68af9 --- /dev/null +++ b/plugins/github-pull-requests-board/src/utils/functions.test.ts @@ -0,0 +1,54 @@ +/* + * Copyright 2022 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 { filterSameUser } from './functions'; + +import { Author } from './types'; + +const users = [ + null, + { + login: 'myuserlogin', + avatarUrl: '', + id: '', + email: '', + name: '', + }, + { + login: 'anotheruserlogin', + avatarUrl: '', + id: '', + email: '', + name: '', + }, + { + login: 'myuserlogin', + avatarUrl: '', + id: '', + email: '', + name: '', + }, +] as Author[]; + +describe('filterSameUser', () => { + it('should return distinct users', () => { + expect(filterSameUser(users).length).toEqual(2); + }); + + // null users a.k.a. Ghost users are accounts that have been deleted + it('should contain null user', () => { + expect(filterSameUser(users)).not.toContain(null); + }); +}); diff --git a/plugins/github-pull-requests-board/src/utils/functions.ts b/plugins/github-pull-requests-board/src/utils/functions.ts index bfb0e0edc5..69b3ecf794 100644 --- a/plugins/github-pull-requests-board/src/utils/functions.ts +++ b/plugins/github-pull-requests-board/src/utils/functions.ts @@ -42,7 +42,11 @@ export const getChangeRequests = (reviews: Reviews = []): Reviews => { }; export const filterSameUser = (users: Author[]): Author[] => { - return users.reduce((acc, curr) => { + const filterGhostUsers = (usersToFilter: Author[]): Author[] => { + return usersToFilter.filter(user => user !== null); + }; + + return filterGhostUsers(users).reduce((acc, curr) => { const containsUser = acc.find(({ login }) => login === curr.login); if (!containsUser) {