Merge pull request #12991 from tunkl/feature/humanize_optional_namespace

Adds possibility to not skip default namespace
This commit is contained in:
Patrik Oldsberg
2022-09-12 22:08:42 +02:00
committed by GitHub
4 changed files with 129 additions and 3 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-catalog-react': patch
---
humanizeEntityRef function can now be forced to include default namespace
+1
View File
@@ -436,6 +436,7 @@ export function humanizeEntityRef(
entityRef: Entity | CompoundEntityRef,
opts?: {
defaultKind?: string;
defaultNamespace?: string | false;
},
): string;
@@ -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');
});
});
@@ -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;
}