From b08947bd5d384e196af3120f9cfdfd22e1167640 Mon Sep 17 00:00:00 2001 From: Juan Pablo Garcia Ripa Date: Wed, 18 May 2022 10:04:09 +0200 Subject: [PATCH] allow multiple kinds and namespaces Signed-off-by: Juan Pablo Garcia Ripa --- .changeset/fluffy-candles-learn.md | 2 +- plugins/catalog/api-report.md | 6 ++- .../EntitySwitch/conditions.test.ts | 53 ++++++++++++++++++- .../src/components/EntitySwitch/conditions.ts | 18 ++++--- 4 files changed, 68 insertions(+), 11 deletions(-) diff --git a/.changeset/fluffy-candles-learn.md b/.changeset/fluffy-candles-learn.md index f3ed109071..6fe3f1e09b 100644 --- a/.changeset/fluffy-candles-learn.md +++ b/.changeset/fluffy-candles-learn.md @@ -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 diff --git a/plugins/catalog/api-report.md b/plugins/catalog/api-report.md index e95a8a638c..3d426a0c8b 100644 --- a/plugins/catalog/api-report.md +++ b/plugins/catalog/api-report.md @@ -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; diff --git a/plugins/catalog/src/components/EntitySwitch/conditions.test.ts b/plugins/catalog/src/components/EntitySwitch/conditions.test.ts index 05df2d6bc5..dfed546fd8 100644 --- a/plugins/catalog/src/components/EntitySwitch/conditions.test.ts +++ b/plugins/catalog/src/components/EntitySwitch/conditions.test.ts @@ -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(); + }); +}); diff --git a/plugins/catalog/src/components/EntitySwitch/conditions.ts b/plugins/catalog/src/components/EntitySwitch/conditions.ts index 314c05d14b..5f50fde5c9 100644 --- a/plugins/catalog/src/components/EntitySwitch/conditions.ts +++ b/plugins/catalog/src/components/EntitySwitch/conditions.ts @@ -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); }