simplify isOwnerOf using stringifyEntityRef

Signed-off-by: Fredrik Adelöw <freben@gmail.com>
This commit is contained in:
Fredrik Adelöw
2021-07-20 20:12:31 +02:00
parent 6b690fb633
commit ea6871d2d9
+11 -14
View File
@@ -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;
}
}