From 384dddd0b493aa935eb973970d6a64e88071a206 Mon Sep 17 00:00:00 2001 From: Alex Rybchenko Date: Tue, 21 Jun 2022 17:43:54 +0200 Subject: [PATCH] moved entity checkers to catalog-model Signed-off-by: Alex Rybchenko --- .../src/entity/conditions.test.ts | 172 ++++++++++++++++++ .../catalog-model/src/entity/conditions.ts | 76 ++++++++ packages/catalog-model/src/entity/index.ts | 1 + .../src/search/DefaultCatalogCollator.ts | 8 +- plugins/catalog-backend/src/search/util.ts | 10 +- .../src/components/EntitySwitch/conditions.ts | 61 +------ 6 files changed, 253 insertions(+), 75 deletions(-) create mode 100644 packages/catalog-model/src/entity/conditions.test.ts create mode 100644 packages/catalog-model/src/entity/conditions.ts diff --git a/packages/catalog-model/src/entity/conditions.test.ts b/packages/catalog-model/src/entity/conditions.test.ts new file mode 100644 index 0000000000..046f1c60e4 --- /dev/null +++ b/packages/catalog-model/src/entity/conditions.test.ts @@ -0,0 +1,172 @@ +/* + * 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 './Entity'; +import { + isApiEntity, + isComponentEntity, + isDomainEntity, + isGroupEntity, + isLocationEntity, + isResourceEntity, + isSystemEntity, + isUserEntity, +} from './conditions'; + +const apiEntity: Entity = { + apiVersion: '', + kind: 'api', + metadata: { name: 'api' }, +}; +const componentEntity: Entity = { + apiVersion: '', + kind: 'component', + metadata: { name: 'Component' }, +}; +const domainEntity: Entity = { + apiVersion: '', + kind: 'domain', + metadata: { name: 'Domain' }, +}; +const groupEntity: Entity = { + apiVersion: '', + kind: 'group', + metadata: { name: 'Group' }, +}; +const locationEntity: Entity = { + apiVersion: '', + kind: 'location', + metadata: { name: 'Location' }, +}; +const resourceEntity: Entity = { + apiVersion: '', + kind: 'resource', + metadata: { name: 'Resource' }, +}; +const systemEntity: Entity = { + apiVersion: '', + kind: 'system', + metadata: { name: 'System' }, +}; +const userEntity: Entity = { + apiVersion: '', + kind: 'user', + metadata: { name: 'User' }, +}; + +describe('isApiEntity', () => { + it('should check for the intended type', () => { + expect(isApiEntity(componentEntity)).not.toBeTruthy(); + expect(isApiEntity(domainEntity)).not.toBeTruthy(); + expect(isApiEntity(groupEntity)).not.toBeTruthy(); + expect(isApiEntity(locationEntity)).not.toBeTruthy(); + expect(isApiEntity(resourceEntity)).not.toBeTruthy(); + expect(isApiEntity(systemEntity)).not.toBeTruthy(); + expect(isApiEntity(userEntity)).not.toBeTruthy(); + expect(isApiEntity(apiEntity)).toBeTruthy(); + }); +}); + +describe('isComponentEntity', () => { + it('should check for the intended type', () => { + expect(isComponentEntity(apiEntity)).not.toBeTruthy(); + expect(isComponentEntity(domainEntity)).not.toBeTruthy(); + expect(isComponentEntity(groupEntity)).not.toBeTruthy(); + expect(isComponentEntity(locationEntity)).not.toBeTruthy(); + expect(isComponentEntity(resourceEntity)).not.toBeTruthy(); + expect(isComponentEntity(systemEntity)).not.toBeTruthy(); + expect(isComponentEntity(userEntity)).not.toBeTruthy(); + expect(isComponentEntity(componentEntity)).toBeTruthy(); + }); +}); + +describe('isDomainEntity', () => { + it('should check for the intended type', () => { + expect(isDomainEntity(apiEntity)).not.toBeTruthy(); + expect(isDomainEntity(componentEntity)).not.toBeTruthy(); + expect(isDomainEntity(groupEntity)).not.toBeTruthy(); + expect(isDomainEntity(locationEntity)).not.toBeTruthy(); + expect(isDomainEntity(resourceEntity)).not.toBeTruthy(); + expect(isDomainEntity(systemEntity)).not.toBeTruthy(); + expect(isDomainEntity(userEntity)).not.toBeTruthy(); + expect(isDomainEntity(domainEntity)).toBeTruthy(); + }); +}); + +describe('isGroupEntity', () => { + it('should check for the intended type', () => { + expect(isGroupEntity(apiEntity)).not.toBeTruthy(); + expect(isGroupEntity(componentEntity)).not.toBeTruthy(); + expect(isGroupEntity(domainEntity)).not.toBeTruthy(); + expect(isGroupEntity(locationEntity)).not.toBeTruthy(); + expect(isGroupEntity(resourceEntity)).not.toBeTruthy(); + expect(isGroupEntity(systemEntity)).not.toBeTruthy(); + expect(isGroupEntity(userEntity)).not.toBeTruthy(); + expect(isGroupEntity(groupEntity)).toBeTruthy(); + }); +}); + +describe('isLocationEntity', () => { + it('should check for the intended type', () => { + expect(isLocationEntity(apiEntity)).not.toBeTruthy(); + expect(isLocationEntity(componentEntity)).not.toBeTruthy(); + expect(isLocationEntity(domainEntity)).not.toBeTruthy(); + expect(isLocationEntity(groupEntity)).not.toBeTruthy(); + expect(isLocationEntity(resourceEntity)).not.toBeTruthy(); + expect(isLocationEntity(systemEntity)).not.toBeTruthy(); + expect(isLocationEntity(userEntity)).not.toBeTruthy(); + expect(isLocationEntity(locationEntity)).toBeTruthy(); + }); +}); + +describe('isResourceEntity', () => { + it('should check for the intended type', () => { + expect(isResourceEntity(apiEntity)).not.toBeTruthy(); + expect(isResourceEntity(componentEntity)).not.toBeTruthy(); + expect(isResourceEntity(domainEntity)).not.toBeTruthy(); + expect(isResourceEntity(groupEntity)).not.toBeTruthy(); + expect(isResourceEntity(locationEntity)).not.toBeTruthy(); + expect(isResourceEntity(systemEntity)).not.toBeTruthy(); + expect(isResourceEntity(userEntity)).not.toBeTruthy(); + expect(isResourceEntity(resourceEntity)).toBeTruthy(); + }); +}); + +describe('isSystemEntity', () => { + it('should check for the intended type', () => { + expect(isSystemEntity(apiEntity)).not.toBeTruthy(); + expect(isSystemEntity(componentEntity)).not.toBeTruthy(); + expect(isSystemEntity(domainEntity)).not.toBeTruthy(); + expect(isSystemEntity(groupEntity)).not.toBeTruthy(); + expect(isSystemEntity(locationEntity)).not.toBeTruthy(); + expect(isSystemEntity(resourceEntity)).not.toBeTruthy(); + expect(isSystemEntity(userEntity)).not.toBeTruthy(); + expect(isSystemEntity(systemEntity)).toBeTruthy(); + }); +}); + +describe('isUserEntity', () => { + it('should check for the intended type', () => { + expect(isUserEntity(apiEntity)).not.toBeTruthy(); + expect(isUserEntity(componentEntity)).not.toBeTruthy(); + expect(isUserEntity(domainEntity)).not.toBeTruthy(); + expect(isUserEntity(groupEntity)).not.toBeTruthy(); + expect(isUserEntity(locationEntity)).not.toBeTruthy(); + expect(isUserEntity(resourceEntity)).not.toBeTruthy(); + expect(isUserEntity(systemEntity)).not.toBeTruthy(); + expect(isUserEntity(userEntity)).toBeTruthy(); + }); +}); diff --git a/packages/catalog-model/src/entity/conditions.ts b/packages/catalog-model/src/entity/conditions.ts new file mode 100644 index 0000000000..d761695b5e --- /dev/null +++ b/packages/catalog-model/src/entity/conditions.ts @@ -0,0 +1,76 @@ +/* + * Copyright 2020 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 { + ApiEntity, + ComponentEntity, + DomainEntity, + GroupEntity, + LocationEntity, + ResourceEntity, + SystemEntity, + UserEntity, +} from '../kinds'; +import { Entity } from './Entity'; + +/** + * @public + */ +export function isApiEntity(entity: Entity): entity is ApiEntity { + return entity.kind.toLocaleUpperCase('en-US') === 'API'; +} +/** + * @public + */ +export function isComponentEntity(entity: Entity): entity is ComponentEntity { + return entity.kind.toLocaleUpperCase('en-US') === 'COMPONENT'; +} +/** + * @public + */ +export function isDomainEntity(entity: Entity): entity is DomainEntity { + return entity.kind.toLocaleUpperCase('en-US') === 'DOMAIN'; +} +/** + * @public + */ +export function isGroupEntity(entity: Entity): entity is GroupEntity { + return entity.kind.toLocaleUpperCase('en-US') === 'GROUP'; +} +/** + * @public + */ +export function isLocationEntity(entity: Entity): entity is LocationEntity { + return entity.kind.toLocaleUpperCase('en-US') === 'LOCATION'; +} +/** + * @public + */ +export function isResourceEntity(entity: Entity): entity is ResourceEntity { + return entity.kind.toLocaleUpperCase('en-US') === 'RESOURCE'; +} +/** + * @public + */ +export function isSystemEntity(entity: Entity): entity is SystemEntity { + return entity.kind.toLocaleUpperCase('en-US') === 'SYSTEM'; +} +/** + * @public + */ +export function isUserEntity(entity: Entity): entity is UserEntity { + return entity.kind.toLocaleUpperCase('en-US') === 'USER'; +} diff --git a/packages/catalog-model/src/entity/index.ts b/packages/catalog-model/src/entity/index.ts index 45ee673167..039529bf3e 100644 --- a/packages/catalog-model/src/entity/index.ts +++ b/packages/catalog-model/src/entity/index.ts @@ -14,6 +14,7 @@ * limitations under the License. */ +export * from './conditions'; export { DEFAULT_NAMESPACE, ANNOTATION_EDIT_URL, diff --git a/plugins/catalog-backend/src/search/DefaultCatalogCollator.ts b/plugins/catalog-backend/src/search/DefaultCatalogCollator.ts index f895ded85e..70ec0ee948 100644 --- a/plugins/catalog-backend/src/search/DefaultCatalogCollator.ts +++ b/plugins/catalog-backend/src/search/DefaultCatalogCollator.ts @@ -20,8 +20,8 @@ import { } from '@backstage/backend-common'; import { Entity, + isUserEntity, stringifyEntityRef, - UserEntity, } from '@backstage/catalog-model'; import { Config } from '@backstage/config'; import { @@ -93,13 +93,9 @@ export class DefaultCatalogCollator { return formatted.toLowerCase(); } - private isUserEntity(entity: Entity): entity is UserEntity { - return entity.kind.toLocaleUpperCase('en-US') === 'USER'; - } - private getDocumentText(entity: Entity): string { let documentText = entity.metadata.description || ''; - if (this.isUserEntity(entity)) { + if (isUserEntity(entity)) { if (entity.spec?.profile?.displayName && documentText) { // combine displayName and description const displayName = entity.spec?.profile?.displayName; diff --git a/plugins/catalog-backend/src/search/util.ts b/plugins/catalog-backend/src/search/util.ts index b5df2a28a7..fd54d397c9 100644 --- a/plugins/catalog-backend/src/search/util.ts +++ b/plugins/catalog-backend/src/search/util.ts @@ -14,15 +14,7 @@ * limitations under the License. */ -import { Entity, UserEntity, GroupEntity } from '@backstage/catalog-model'; - -function isUserEntity(entity: Entity): entity is UserEntity { - return entity.kind.toLocaleUpperCase('en-US') === 'USER'; -} - -function isGroupEntity(entity: Entity): entity is GroupEntity { - return entity.kind.toLocaleUpperCase('en-US') === 'GROUP'; -} +import { Entity, isUserEntity, isGroupEntity } from '@backstage/catalog-model'; export function getDocumentText(entity: Entity): string { const documentTexts: string[] = []; diff --git a/plugins/catalog/src/components/EntitySwitch/conditions.ts b/plugins/catalog/src/components/EntitySwitch/conditions.ts index b6837627c0..86fd38a900 100644 --- a/plugins/catalog/src/components/EntitySwitch/conditions.ts +++ b/plugins/catalog/src/components/EntitySwitch/conditions.ts @@ -14,17 +14,7 @@ * limitations under the License. */ -import { - ApiEntity, - ComponentEntity, - DomainEntity, - Entity, - GroupEntity, - LocationEntity, - ResourceEntity, - SystemEntity, - UserEntity, -} from '@backstage/catalog-model'; +import { ComponentEntity, Entity } from '@backstage/catalog-model'; function strCmp(a: string | undefined, b: string | undefined): boolean { return Boolean( @@ -67,52 +57,3 @@ export function isComponentType(types: string | string[]) { export function isNamespace(namespaces: string | string[]) { return (entity: Entity) => strCmpAll(entity.metadata?.namespace, namespaces); } - -/** - * @public - */ -export function isApiEntity(entity: Entity): entity is ApiEntity { - return entity.kind.toLocaleUpperCase('en-US') === 'API'; -} -/** - * @public - */ -export function isComponentEntity(entity: Entity): entity is ComponentEntity { - return entity.kind.toLocaleUpperCase('en-US') === 'COMPONENT'; -} -/** - * @public - */ -export function isDomainEntity(entity: Entity): entity is DomainEntity { - return entity.kind.toLocaleUpperCase('en-US') === 'DOMAIN'; -} -/** - * @public - */ -export function isGroupEntity(entity: Entity): entity is GroupEntity { - return entity.kind.toLocaleUpperCase('en-US') === 'GROUP'; -} -/** - * @public - */ -export function isLocationEntity(entity: Entity): entity is LocationEntity { - return entity.kind.toLocaleUpperCase('en-US') === 'LOCATION'; -} -/** - * @public - */ -export function isResourceEntity(entity: Entity): entity is ResourceEntity { - return entity.kind.toLocaleUpperCase('en-US') === 'RESOURCE'; -} -/** - * @public - */ -export function isSystemEntity(entity: Entity): entity is SystemEntity { - return entity.kind.toLocaleUpperCase('en-US') === 'SYSTEM'; -} -/** - * @public - */ -export function isUserEntity(entity: Entity): entity is UserEntity { - return entity.kind.toLocaleUpperCase('en-US') === 'USER'; -}