From 078f46ea3269a61faaaab1d21290741c511d199e Mon Sep 17 00:00:00 2001 From: Dustin Brewer Date: Fri, 10 Mar 2023 15:45:48 +0000 Subject: [PATCH] Cleanup building FireHydrant service name Remove the reduntant check for the annotation, and utilize stringifyEntityRef as recommended Signed-off-by: Dustin Brewer --- .../ServiceDetailsCard/ServiceDetailsCard.test.tsx | 4 +++- plugins/firehydrant/src/components/hooks.test.ts | 2 +- plugins/firehydrant/src/components/hooks.ts | 9 +++------ 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/plugins/firehydrant/src/components/ServiceDetailsCard/ServiceDetailsCard.test.tsx b/plugins/firehydrant/src/components/ServiceDetailsCard/ServiceDetailsCard.test.tsx index 20dbb77e86..ce2b1f5391 100644 --- a/plugins/firehydrant/src/components/ServiceDetailsCard/ServiceDetailsCard.test.tsx +++ b/plugins/firehydrant/src/components/ServiceDetailsCard/ServiceDetailsCard.test.tsx @@ -30,7 +30,9 @@ const apis = TestApiRegistry.from([fireHydrantApiRef, mockFireHydrantApi]); jest.mock('@backstage/plugin-catalog-react', () => ({ useEntity: () => { - return { entity: { metadata: { name: 'service-example' } } }; + return { + entity: { kind: 'Component', metadata: { name: 'service-example' } }, + }; }, })); diff --git a/plugins/firehydrant/src/components/hooks.test.ts b/plugins/firehydrant/src/components/hooks.test.ts index 2fbf1e2d57..f35d6774ce 100644 --- a/plugins/firehydrant/src/components/hooks.test.ts +++ b/plugins/firehydrant/src/components/hooks.test.ts @@ -63,7 +63,7 @@ describe('firehydrant-hooks-getFireHydrantServiceName', () => { }); it('should return generated service name', () => { - const expected = 'Component:default/test-fh-name'; + const expected = 'component:default/test-fh-name'; const e: Entity = { apiVersion: 'backstage.io/v1alpha1', kind: 'Component', diff --git a/plugins/firehydrant/src/components/hooks.ts b/plugins/firehydrant/src/components/hooks.ts index d9145f13f0..c4055b1724 100644 --- a/plugins/firehydrant/src/components/hooks.ts +++ b/plugins/firehydrant/src/components/hooks.ts @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { Entity } from '@backstage/catalog-model'; +import { Entity, stringifyEntityRef } from '@backstage/catalog-model'; export const FIREHYDRANT_SERVICE_NAME_ANNOTATION = 'firehydrant.com/service-name'; @@ -21,8 +21,5 @@ export const FIREHYDRANT_SERVICE_NAME_ANNOTATION = export const isFireHydrantAvailable = (entity: Entity) => Boolean(entity.metadata.annotations?.[FIREHYDRANT_SERVICE_NAME_ANNOTATION]); export const getFireHydrantServiceName = (entity: Entity) => - isFireHydrantAvailable(entity) - ? entity?.metadata.annotations?.[FIREHYDRANT_SERVICE_NAME_ANNOTATION] ?? '' - : `${entity?.kind}:${entity?.metadata?.namespace ?? 'default'}/${ - entity?.metadata?.name - }`; + entity?.metadata.annotations?.[FIREHYDRANT_SERVICE_NAME_ANNOTATION] ?? + stringifyEntityRef(entity);