From 306d0b4fddf2b0baf0cc29133eebb26f86ddcf18 Mon Sep 17 00:00:00 2001 From: kielosz Date: Wed, 1 Jun 2022 12:21:01 +0200 Subject: [PATCH 1/3] Add filter param to MyGroupsSidebarItem Signed-off-by: kielosz --- .changeset/spicy-forks-return.md | 5 ++ .../MyGroupsSidebarItem.test.tsx | 88 +++++++++++++++++++ .../MyGroupsSidebarItem.tsx | 11 ++- 3 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 .changeset/spicy-forks-return.md 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'], }); From 5a4ffaba0b48a04b95976d209eb26a10e260938f Mon Sep 17 00:00:00 2001 From: kielosz Date: Thu, 2 Jun 2022 11:16:56 +0200 Subject: [PATCH 2/3] Add api-report Signed-off-by: kielosz --- plugins/org/api-report.md | 1 + 1 file changed, 1 insertion(+) diff --git a/plugins/org/api-report.md b/plugins/org/api-report.md index 5f71be44bc..d020dfb438 100644 --- a/plugins/org/api-report.md +++ b/plugins/org/api-report.md @@ -50,6 +50,7 @@ export const MyGroupsSidebarItem: (props: { singularTitle: string; pluralTitle: string; icon: IconComponent; + filter?: Record; }) => JSX.Element | null; // @public (undocumented) From 9813ef27cecd3631f3283da6db74df9381c4ae23 Mon Sep 17 00:00:00 2001 From: kielosz Date: Thu, 2 Jun 2022 11:32:13 +0200 Subject: [PATCH 3/3] Add example Signed-off-by: kielosz --- .changeset/spicy-forks-return.md | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/.changeset/spicy-forks-return.md b/.changeset/spicy-forks-return.md index d23ce6995b..b36366639c 100644 --- a/.changeset/spicy-forks-return.md +++ b/.changeset/spicy-forks-return.md @@ -2,4 +2,25 @@ '@backstage/plugin-org': patch --- -Added the ability to use an additional `filter` when fetching groups in `MyGroupsSidebarItem` component +Added the ability to use an additional `filter` when fetching groups in `MyGroupsSidebarItem` component. Example: + +```diff +// app/src/components/Root/Root.tsx + + + //... + }> + {/* Global nav, not org-specific */} + //... + + + //... + + + +```