diff --git a/.changeset/fluffy-candles-learn.md b/.changeset/fluffy-candles-learn.md new file mode 100644 index 0000000000..6fe3f1e09b --- /dev/null +++ b/.changeset/fluffy-candles-learn.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-catalog': patch +--- + +Updates the `isKind`, `ìsComponentType`, and `isNamespace` to allow an array of possible values diff --git a/plugins/catalog/api-report.md b/plugins/catalog/api-report.md index 556016941a..3d426a0c8b 100644 --- a/plugins/catalog/api-report.md +++ b/plugins/catalog/api-report.md @@ -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; diff --git a/plugins/catalog/src/components/EntitySwitch/conditions.test.ts b/plugins/catalog/src/components/EntitySwitch/conditions.test.ts new file mode 100644 index 0000000000..dfed546fd8 --- /dev/null +++ b/plugins/catalog/src/components/EntitySwitch/conditions.test.ts @@ -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(); + }); +}); diff --git a/plugins/catalog/src/components/EntitySwitch/conditions.ts b/plugins/catalog/src/components/EntitySwitch/conditions.ts index 907aa45afe..5f50fde5c9 100644 --- a/plugins/catalog/src/components/EntitySwitch/conditions.ts +++ b/plugins/catalog/src/components/EntitySwitch/conditions.ts @@ -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); }