diff --git a/.changeset/spicy-forks-return.md b/.changeset/spicy-forks-return.md new file mode 100644 index 0000000000..d23ce6995b --- /dev/null +++ b/.changeset/spicy-forks-return.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-org': patch +--- + +Added the ability to use an additional `filter` when fetching groups in `MyGroupsSidebarItem` component diff --git a/plugins/org/src/components/MyGroupsSidebarItem/MyGroupsSidebarItem.test.tsx b/plugins/org/src/components/MyGroupsSidebarItem/MyGroupsSidebarItem.test.tsx index d290001eb8..bd212bf972 100644 --- a/plugins/org/src/components/MyGroupsSidebarItem/MyGroupsSidebarItem.test.tsx +++ b/plugins/org/src/components/MyGroupsSidebarItem/MyGroupsSidebarItem.test.tsx @@ -196,4 +196,92 @@ describe('MyGroupsSidebarItem Test', () => { expect(rendered.getByLabelText('My Squads')).toBeInTheDocument(); }); }); + + describe('When an additional filter is not provided', () => { + it('catalogApi.getEntities() should be called with the default filter', async () => { + const identityApi: Partial = { + getBackstageIdentity: async () => ({ + type: 'user', + userEntityRef: 'user:default/guest', + ownershipEntityRefs: ['user:default/guest'], + }), + }; + const mockCatalogApi: Partial = { + getEntities: jest.fn(), + }; + await renderInTestApp( + + + , + { + mountedRoutes: { + '/catalog/:namespace/:kind/:name': entityRouteRef, + }, + }, + ); + expect(mockCatalogApi.getEntities).toHaveBeenCalledWith({ + filter: [ + { + kind: 'group', + 'relations.hasMember': 'user:default/guest', + }, + ], + fields: ['metadata', 'kind'], + }); + }); + }); + + describe('When an additional filter is provided', () => { + it('catalogApi.getEntities() should be called with an additional filter item', async () => { + const identityApi: Partial = { + getBackstageIdentity: async () => ({ + type: 'user', + userEntityRef: 'user:default/guest', + ownershipEntityRefs: ['user:default/guest'], + }), + }; + const mockCatalogApi: Partial = { + getEntities: jest.fn(), + }; + await renderInTestApp( + + + , + { + mountedRoutes: { + '/catalog/:namespace/:kind/:name': entityRouteRef, + }, + }, + ); + expect(mockCatalogApi.getEntities).toHaveBeenCalledWith({ + filter: [ + { + kind: 'group', + 'relations.hasMember': 'user:default/guest', + 'spec.type': 'team', + }, + ], + fields: ['metadata', 'kind'], + }); + }); + }); }); diff --git a/plugins/org/src/components/MyGroupsSidebarItem/MyGroupsSidebarItem.tsx b/plugins/org/src/components/MyGroupsSidebarItem/MyGroupsSidebarItem.tsx index 5a3cc42628..fa8f8405ff 100644 --- a/plugins/org/src/components/MyGroupsSidebarItem/MyGroupsSidebarItem.tsx +++ b/plugins/org/src/components/MyGroupsSidebarItem/MyGroupsSidebarItem.tsx @@ -45,8 +45,9 @@ export const MyGroupsSidebarItem = (props: { singularTitle: string; pluralTitle: string; icon: IconComponent; + filter?: Record; }) => { - const { singularTitle, pluralTitle, icon } = props; + const { singularTitle, pluralTitle, icon, filter } = props; const identityApi = useApi(identityApiRef); const catalogApi: CatalogApi = useApi(catalogApiRef); @@ -56,7 +57,13 @@ export const MyGroupsSidebarItem = (props: { const profile = await identityApi.getBackstageIdentity(); const response = await catalogApi.getEntities({ - filter: [{ kind: 'group', 'relations.hasMember': profile.userEntityRef }], + filter: [ + { + kind: 'group', + 'relations.hasMember': profile.userEntityRef, + ...(filter ?? {}), + }, + ], fields: ['metadata', 'kind'], });