allow multiple kinds and namespaces

Signed-off-by: Juan Pablo Garcia Ripa <sarabadu@gmail.com>
This commit is contained in:
Juan Pablo Garcia Ripa
2022-05-18 10:04:09 +02:00
parent 449dcef98e
commit b08947bd5d
4 changed files with 68 additions and 11 deletions
+1 -1
View File
@@ -2,4 +2,4 @@
'@backstage/plugin-catalog': patch
---
Updates the `ìsComponentType` to allow an array of possible types
Updates the `isKind`, `ìsComponentType`, and `isNamespace` to allow an array of possible values
+4 -2
View File
@@ -376,10 +376,12 @@ export function isComponentType(
): (entity: Entity) => boolean;
// @public
export function isKind(kind: string): (entity: Entity) => boolean;
export function isKind(kinds: string | string[]): (entity: Entity) => boolean;
// @public
export function isNamespace(namespace: string): (entity: Entity) => boolean;
export function isNamespace(
namespaces: string | string[],
): (entity: Entity) => boolean;
// @public
export function isOrphan(entity: Entity): boolean;
@@ -15,7 +15,7 @@
*/
import { Entity } from '@backstage/catalog-model';
import { isComponentType } from '.';
import { isComponentType, isKind, isNamespace } from './conditions';
const serviceComponent: Entity = {
apiVersion: '',
@@ -38,6 +38,27 @@ const notComponent: Entity = {
spec: { type: 'service' },
};
const apiKind: Entity = {
apiVersion: '',
kind: 'api',
metadata: { name: 'api' },
spec: { type: 'api' },
};
const aNamespace: Entity = {
apiVersion: '',
kind: 'component',
metadata: { name: 'aService', namespace: 'a' },
spec: { type: 'service' },
};
const bNamespace: Entity = {
apiVersion: '',
kind: 'component',
metadata: { name: 'aService', namespace: 'b' },
spec: { type: 'service' },
};
describe('isComponentType', () => {
it('should false on non component kinds', () => {
const checkEntity = isComponentType('service');
@@ -57,3 +78,33 @@ describe('isComponentType', () => {
expect(checkEntity(websiteComponent)).toBeTruthy();
});
});
describe('isKind', () => {
it('should check for the intended kind', () => {
const checkEntity = isKind('component');
expect(checkEntity(notComponent)).not.toBeTruthy();
expect(checkEntity(serviceComponent)).toBeTruthy();
});
it('should check for multiple types', () => {
const checkEntity = isKind(['component', 'api']);
expect(checkEntity(serviceComponent)).toBeTruthy();
expect(checkEntity(apiKind)).toBeTruthy();
});
});
describe('isNamespace', () => {
it('should check for the intended type', () => {
const checkEntity = isNamespace('a');
expect(checkEntity(aNamespace)).toBeTruthy();
expect(checkEntity(bNamespace)).not.toBeTruthy();
});
it('should check for multiple types', () => {
const checkEntity = isNamespace(['a', 'b']);
expect(checkEntity(aNamespace)).toBeTruthy();
expect(checkEntity(bNamespace)).toBeTruthy();
});
});
@@ -22,12 +22,18 @@ function strCmp(a: string | undefined, b: string | undefined): boolean {
);
}
function strCmpAll(value: string | undefined, cmpValues: string | string[]) {
return typeof cmpValues === 'string'
? strCmp(value, cmpValues)
: cmpValues.some(cmpVal => strCmp(value, cmpVal));
}
/**
* For use in EntitySwitch.Case. Matches if the entity is of a given kind.
* @public
*/
export function isKind(kind: string) {
return (entity: Entity) => strCmp(entity.kind, kind);
export function isKind(kinds: string | string[]) {
return (entity: Entity) => strCmpAll(entity.kind, kinds);
}
/**
@@ -40,9 +46,7 @@ export function isComponentType(types: string | string[]) {
return false;
}
const componentEntity = entity as ComponentEntity;
return typeof types === 'string'
? strCmp(componentEntity.spec.type, types)
: types.some(type => strCmp(componentEntity.spec.type, type));
return strCmpAll(componentEntity.spec.type, types);
};
}
@@ -50,6 +54,6 @@ export function isComponentType(types: string | string[]) {
* For use in EntitySwitch.Case. Matches if the entity is in a given namespace.
* @public
*/
export function isNamespace(namespace: string) {
return (entity: Entity) => strCmp(entity.metadata?.namespace, namespace);
export function isNamespace(namespaces: string | string[]) {
return (entity: Entity) => strCmpAll(entity.metadata?.namespace, namespaces);
}