diff --git a/.changeset/kind-experts-lay.md b/.changeset/kind-experts-lay.md new file mode 100644 index 0000000000..00524bfe3a --- /dev/null +++ b/.changeset/kind-experts-lay.md @@ -0,0 +1,7 @@ +--- +'@backstage/plugin-catalog-react': minor +--- + +**BREAKING**: Removed the deprecated `loadCatalogOwnerRefs` function. Usages of this function can be directly replaced with `ownershipEntityRefs` from `identityApi.getBackstageIdentity()`. + +This also affects the `useEntityOwnership` hook in that it no longer uses `loadCatalogOwnerRefs`, meaning it will no longer load in additional relations and instead only rely on the `ownershipEntityRefs` from the `IdentityApi`. diff --git a/plugins/catalog-react/api-report.md b/plugins/catalog-react/api-report.md index e85f8d3c63..4912c857cf 100644 --- a/plugins/catalog-react/api-report.md +++ b/plugins/catalog-react/api-report.md @@ -443,12 +443,6 @@ export function InspectEntityDialog(props: { // @alpha export function isOwnerOf(owner: Entity, entity: Entity): boolean; -// @public @deprecated -export function loadCatalogOwnerRefs( - catalogApi: CatalogApi, - identityOwnerRefs: string[], -): Promise; - // @public (undocumented) export const MockEntityListContextProvider: ({ children, diff --git a/plugins/catalog-react/src/hooks/index.ts b/plugins/catalog-react/src/hooks/index.ts index 40183c1035..3570ed897c 100644 --- a/plugins/catalog-react/src/hooks/index.ts +++ b/plugins/catalog-react/src/hooks/index.ts @@ -37,5 +37,5 @@ export { useEntityTypeFilter } from './useEntityTypeFilter'; export { useRelatedEntities } from './useRelatedEntities'; export { useStarredEntities } from './useStarredEntities'; export { useStarredEntity } from './useStarredEntity'; -export { loadCatalogOwnerRefs, useEntityOwnership } from './useEntityOwnership'; +export { useEntityOwnership } from './useEntityOwnership'; export { useEntityPermission } from './useEntityPermission'; diff --git a/plugins/catalog-react/src/hooks/useEntityOwnership.test.tsx b/plugins/catalog-react/src/hooks/useEntityOwnership.test.tsx index 4730f71a7d..d4d80203d3 100644 --- a/plugins/catalog-react/src/hooks/useEntityOwnership.test.tsx +++ b/plugins/catalog-react/src/hooks/useEntityOwnership.test.tsx @@ -15,18 +15,13 @@ */ import { CatalogApi } from '@backstage/catalog-client'; -import { - ComponentEntity, - RELATION_MEMBER_OF, - RELATION_OWNED_BY, - UserEntity, -} from '@backstage/catalog-model'; +import { ComponentEntity, RELATION_OWNED_BY } from '@backstage/catalog-model'; import { IdentityApi, identityApiRef } from '@backstage/core-plugin-api'; import { TestApiProvider } from '@backstage/test-utils'; import { renderHook } from '@testing-library/react-hooks'; import React from 'react'; import { catalogApiRef } from '../api'; -import { loadCatalogOwnerRefs, useEntityOwnership } from './useEntityOwnership'; +import { useEntityOwnership } from './useEntityOwnership'; describe('useEntityOwnership', () => { type MockIdentityApi = jest.Mocked>; @@ -77,55 +72,10 @@ describe('useEntityOwnership', () => { ], }; - const user2Entity: UserEntity = { - apiVersion: 'backstage.io/v1beta1', - kind: 'User', - metadata: { - name: 'user2', - namespace: 'default', - }, - spec: { - /* should not be accessed */ - } as any, - relations: [ - { - type: RELATION_MEMBER_OF, - targetRef: 'group:default/group1', - target: { kind: 'Group', namespace: 'default', name: 'group1' }, - }, - ], - }; - afterEach(() => { jest.resetAllMocks(); }); - describe('loadCatalogOwnerRefs', () => { - it('loads the first user from the catalog', async () => { - mockCatalogApi.getEntityByRef.mockResolvedValueOnce(user2Entity); - await expect( - loadCatalogOwnerRefs(catalogApi, ['user:default/user2']), - ).resolves.toEqual(['group:default/group1']); - expect(mockCatalogApi.getEntityByRef).toBeCalledWith({ - kind: 'user', - namespace: 'default', - name: 'user2', - }); - }); - - it('gracefully handles missing user', async () => { - mockCatalogApi.getEntityByRef.mockResolvedValueOnce(undefined); - await expect( - loadCatalogOwnerRefs(catalogApi, ['user:default/user2']), - ).resolves.toEqual([]); - expect(mockCatalogApi.getEntityByRef).toBeCalledWith({ - kind: 'user', - namespace: 'default', - name: 'user2', - }); - }); - }); - describe('useEntityOwnership', () => { it('matches ownership via ownership entity refs', async () => { mockIdentityApi.getBackstageIdentity.mockResolvedValue({ diff --git a/plugins/catalog-react/src/hooks/useEntityOwnership.ts b/plugins/catalog-react/src/hooks/useEntityOwnership.ts index 55b4e75ace..f8a37a2c83 100644 --- a/plugins/catalog-react/src/hooks/useEntityOwnership.ts +++ b/plugins/catalog-react/src/hooks/useEntityOwnership.ts @@ -14,56 +14,16 @@ * limitations under the License. */ -import { CatalogApi } from '@backstage/catalog-client'; import { Entity, - parseEntityRef, - RELATION_MEMBER_OF, RELATION_OWNED_BY, stringifyEntityRef, } from '@backstage/catalog-model'; import { identityApiRef, useApi } from '@backstage/core-plugin-api'; import { useMemo } from 'react'; import useAsync from 'react-use/lib/useAsync'; -import { catalogApiRef } from '../api'; import { getEntityRelations } from '../utils/getEntityRelations'; -/** - * Takes the relevant parts of the User entity corresponding to the Backstage - * identity, and translates them into a list of entity refs on string form that - * represent the user's ownership connections. - * - * @public - * - * @param catalogApi - The Catalog API implementation - * @param identityOwnerRefs - List of identity owner refs as strings - * @returns OwnerRefs as a string array - * @deprecated Use `ownershipEntityRefs` from `identityApi.getBackstageIdentity()` instead. - */ -export async function loadCatalogOwnerRefs( - catalogApi: CatalogApi, - identityOwnerRefs: string[], -): Promise { - const result = new Array(); - - const primaryUserRef = identityOwnerRefs.find(ref => ref.startsWith('user:')); - if (primaryUserRef) { - const entity = await catalogApi.getEntityByRef( - parseEntityRef(primaryUserRef), - ); - if (entity) { - const memberOf = getEntityRelations(entity, RELATION_MEMBER_OF, { - kind: 'Group', - }); - for (const group of memberOf) { - result.push(stringifyEntityRef(group)); - } - } - } - - return result; -} - /** * Returns a function that checks whether the currently signed-in user is an * owner of a given entity. When the hook is initially mounted, the loading @@ -79,16 +39,11 @@ export function useEntityOwnership(): { isOwnedEntity: (entity: Entity) => boolean; } { const identityApi = useApi(identityApiRef); - const catalogApi = useApi(catalogApiRef); // Trigger load only on mount const { loading, value: refs } = useAsync(async () => { const { ownershipEntityRefs } = await identityApi.getBackstageIdentity(); - const catalogRefs = await loadCatalogOwnerRefs( - catalogApi, - ownershipEntityRefs, - ); - return new Set([...ownershipEntityRefs, ...catalogRefs]); + return ownershipEntityRefs; }, []); const isOwnedEntity = useMemo(() => {