From ea6871d2d907dfa8cb6fca3973d233044449ba58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Adel=C3=B6w?= Date: Tue, 20 Jul 2021 20:12:31 +0200 Subject: [PATCH] simplify isOwnerOf using stringifyEntityRef MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fredrik Adelöw --- plugins/catalog-react/src/utils/isOwnerOf.ts | 25 +++++++++----------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/plugins/catalog-react/src/utils/isOwnerOf.ts b/plugins/catalog-react/src/utils/isOwnerOf.ts index e62a0db1ba..38ad90e4a6 100644 --- a/plugins/catalog-react/src/utils/isOwnerOf.ts +++ b/plugins/catalog-react/src/utils/isOwnerOf.ts @@ -16,10 +16,10 @@ import { Entity, - EntityName, getEntityName, RELATION_MEMBER_OF, RELATION_OWNED_BY, + stringifyEntityRef, } from '@backstage/catalog-model'; import { getEntityRelations } from './getEntityRelations'; @@ -27,22 +27,19 @@ import { getEntityRelations } from './getEntityRelations'; * Get the related entity references. */ export function isOwnerOf(owner: Entity, owned: Entity) { - const possibleOwners: EntityName[] = [ - ...getEntityRelations(owner, RELATION_MEMBER_OF, { kind: 'group' }), - ...(owner ? [getEntityName(owner)] : []), - ]; + const possibleOwners = new Set( + [ + ...getEntityRelations(owner, RELATION_MEMBER_OF, { kind: 'group' }), + ...(owner ? [getEntityName(owner)] : []), + ].map(stringifyEntityRef), + ); - const owners = getEntityRelations(owned, RELATION_OWNED_BY); + const owners = getEntityRelations(owned, RELATION_OWNED_BY).map( + stringifyEntityRef, + ); for (const ownerItem of owners) { - if ( - possibleOwners.find( - o => - ownerItem.kind.toLowerCase() === o.kind.toLowerCase() && - ownerItem.namespace.toLowerCase() === o.namespace.toLowerCase() && - ownerItem.name.toLowerCase() === o.name.toLowerCase(), - ) !== undefined - ) { + if (possibleOwners.has(ownerItem)) { return true; } }