Merge pull request #11543 from Sarabadu/allow-multiple-types

allow multiple component types
This commit is contained in:
Patrik Oldsberg
2022-05-18 10:45:56 +02:00
committed by GitHub
4 changed files with 134 additions and 9 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-catalog': patch
---
Updates the `isKind`, `ìsComponentType`, and `isNamespace` to allow an array of possible values
+7 -3
View File
@@ -371,13 +371,17 @@ export interface HasSystemsCardProps {
}
// @public
export function isComponentType(type: string): (entity: Entity) => boolean;
export function isComponentType(
types: string | string[],
): (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;
@@ -0,0 +1,110 @@
/*
* Copyright 2022 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Entity } from '@backstage/catalog-model';
import { isComponentType, isKind, isNamespace } from './conditions';
const serviceComponent: Entity = {
apiVersion: '',
kind: 'component',
metadata: { name: 'aService' },
spec: { type: 'service' },
};
const websiteComponent: Entity = {
apiVersion: '',
kind: 'component',
metadata: { name: 'aService' },
spec: { type: 'website' },
};
const notComponent: Entity = {
apiVersion: '',
kind: 'not-component',
metadata: { name: 'aService' },
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');
expect(checkEntity(notComponent)).not.toBeTruthy();
});
it('should check for the intended type', () => {
const checkEntity = isComponentType('service');
expect(checkEntity(websiteComponent)).not.toBeTruthy();
expect(checkEntity(serviceComponent)).toBeTruthy();
});
it('should check for multiple types', () => {
const checkEntity = isComponentType(['service', 'website']);
expect(checkEntity(serviceComponent)).toBeTruthy();
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,25 +22,31 @@ 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);
}
/**
* For use in EntitySwitch.Case. Matches if the entity is a Component of a given spec.type.
* @public
*/
export function isComponentType(type: string) {
export function isComponentType(types: string | string[]) {
return (entity: Entity) => {
if (!strCmp(entity.kind, 'component')) {
return false;
}
const componentEntity = entity as ComponentEntity;
return strCmp(componentEntity.spec.type, type);
return strCmpAll(componentEntity.spec.type, types);
};
}
@@ -48,6 +54,6 @@ export function isComponentType(type: 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);
}