diff --git a/.changeset/eight-shrimps-call.md b/.changeset/eight-shrimps-call.md new file mode 100644 index 0000000000..2e19681f7a --- /dev/null +++ b/.changeset/eight-shrimps-call.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-catalog-react': patch +--- + +humanizeEntityRef function can now be forced to include default namespace diff --git a/plugins/catalog-react/api-report.md b/plugins/catalog-react/api-report.md index 1ae82f702b..ee43a0a5c1 100644 --- a/plugins/catalog-react/api-report.md +++ b/plugins/catalog-react/api-report.md @@ -436,6 +436,7 @@ export function humanizeEntityRef( entityRef: Entity | CompoundEntityRef, opts?: { defaultKind?: string; + defaultNamespace?: string | false; }, ): string; diff --git a/plugins/catalog-react/src/components/EntityRefLink/humanize.test.ts b/plugins/catalog-react/src/components/EntityRefLink/humanize.test.ts index d41d3c6635..80ce2100f6 100644 --- a/plugins/catalog-react/src/components/EntityRefLink/humanize.test.ts +++ b/plugins/catalog-react/src/components/EntityRefLink/humanize.test.ts @@ -34,6 +34,23 @@ describe('humanizeEntityRef', () => { expect(title).toEqual('component:software'); }); + it('formats entity in default namespace without skipping default namespace', () => { + const entity = { + apiVersion: 'v1', + kind: 'Component', + metadata: { + name: 'software', + }, + spec: { + owner: 'guest', + type: 'service', + lifecycle: 'production', + }, + }; + const title = humanizeEntityRef(entity, { defaultNamespace: false }); + expect(title).toEqual('component:default/software'); + }); + it('formats entity in other namespace', () => { const entity = { apiVersion: 'v1', @@ -52,6 +69,24 @@ describe('humanizeEntityRef', () => { expect(title).toEqual('component:test/software'); }); + it('formats entity in other namespace and hides this namespace', () => { + const entity = { + apiVersion: 'v1', + kind: 'Component', + metadata: { + name: 'software', + namespace: 'test', + }, + spec: { + owner: 'guest', + type: 'service', + lifecycle: 'production', + }, + }; + const title = humanizeEntityRef(entity, { defaultNamespace: 'test' }); + expect(title).toEqual('component:software'); + }); + it('formats entity and hides default kind', () => { const entity = { apiVersion: 'v1', @@ -70,6 +105,27 @@ describe('humanizeEntityRef', () => { expect(title).toEqual('test/software'); }); + it('formats entity and hides default kind and hiding namespace', () => { + const entity = { + apiVersion: 'v1', + kind: 'Component', + metadata: { + name: 'software', + namespace: 'test', + }, + spec: { + owner: 'guest', + type: 'service', + lifecycle: 'production', + }, + }; + const title = humanizeEntityRef(entity, { + defaultKind: 'Component', + defaultNamespace: 'test', + }); + expect(title).toEqual('software'); + }); + it('formats entity name in default namespace', () => { const entityName = { kind: 'Component', @@ -80,6 +136,16 @@ describe('humanizeEntityRef', () => { expect(title).toEqual('component:software'); }); + it('formats entity name in default namespace and does not skip default namespace', () => { + const entityName = { + kind: 'Component', + namespace: 'default', + name: 'software', + }; + const title = humanizeEntityRef(entityName, { defaultNamespace: false }); + expect(title).toEqual('component:default/software'); + }); + it('formats entity name in other namespace', () => { const entityName = { kind: 'Component', @@ -91,6 +157,19 @@ describe('humanizeEntityRef', () => { expect(title).toEqual('component:test/software'); }); + it('formats entity name in other namespace with skipping this namespace', () => { + const entityName = { + kind: 'Component', + namespace: 'test', + name: 'software', + }; + + const title = humanizeEntityRef(entityName, { + defaultNamespace: 'test', + }); + expect(title).toEqual('component:software'); + }); + it('renders link for entity name and hides default kind', () => { const entityName = { kind: 'Component', @@ -103,4 +182,31 @@ describe('humanizeEntityRef', () => { }); expect(title).toEqual('test/software'); }); + + it('renders link for entity name and hides default kind with skipping namespace', () => { + const entityName = { + kind: 'Component', + namespace: 'test', + name: 'software', + }; + + const title = humanizeEntityRef(entityName, { + defaultKind: 'component', + defaultNamespace: 'test', + }); + expect(title).toEqual('software'); + }); + + it('formats entity name in default namespace without skip of default namespace', () => { + const entityName = { + kind: 'Component', + namespace: 'default', + name: 'software', + }; + + const title = humanizeEntityRef(entityName, { + defaultNamespace: false, + }); + expect(title).toEqual('component:default/software'); + }); }); diff --git a/plugins/catalog-react/src/components/EntityRefLink/humanize.ts b/plugins/catalog-react/src/components/EntityRefLink/humanize.ts index 2f72a7ee3b..bfc8e2af84 100644 --- a/plugins/catalog-react/src/components/EntityRefLink/humanize.ts +++ b/plugins/catalog-react/src/components/EntityRefLink/humanize.ts @@ -20,10 +20,17 @@ import { DEFAULT_NAMESPACE, } from '@backstage/catalog-model'; -/** @public */ +/** + * @param defaultNamespace - if set to false then namespace is never omitted, + * if set to string which matches namespace of entity then omitted + * + * @public */ export function humanizeEntityRef( entityRef: Entity | CompoundEntityRef, - opts?: { defaultKind?: string }, + opts?: { + defaultKind?: string; + defaultNamespace?: string | false; + }, ) { const defaultKind = opts?.defaultKind; let kind; @@ -40,7 +47,14 @@ export function humanizeEntityRef( name = entityRef.name; } - if (namespace === DEFAULT_NAMESPACE) { + if (namespace === undefined || namespace === '') { + namespace = DEFAULT_NAMESPACE; + } + if (opts?.defaultNamespace !== undefined) { + if (opts?.defaultNamespace === namespace) { + namespace = undefined; + } + } else if (namespace === DEFAULT_NAMESPACE) { namespace = undefined; }