From fb02d2d94d95aaa6cdfed904fe2590f11bcce6ea Mon Sep 17 00:00:00 2001 From: Patrick Jungermann Date: Fri, 25 Mar 2022 20:43:32 +0100 Subject: [PATCH] feat: export `locationSpecToLocationEntity` Export function `locationSpecToLocationEntity` which helps with converting `LocationSpec` to `LocationEntityV1alpha1` and hereby, make it usable in other packages/plugins. Change signature to be easier to extend in the future. Signed-off-by: Patrick Jungermann --- .changeset/short-knives-wave.md | 5 +++++ plugins/catalog-backend/api-report.md | 7 +++++++ plugins/catalog-backend/src/index.ts | 7 ++++--- .../core/ConfigLocationEntityProvider.ts | 6 ++++-- .../src/modules/core/DefaultLocationStore.ts | 6 +++--- .../src/processing/ProcessorOutputCollector.ts | 8 ++++---- plugins/catalog-backend/src/util/conversion.ts | 12 ++++++++---- plugins/catalog-backend/src/util/index.ts | 17 +++++++++++++++++ 8 files changed, 52 insertions(+), 16 deletions(-) create mode 100644 .changeset/short-knives-wave.md create mode 100644 plugins/catalog-backend/src/util/index.ts diff --git a/.changeset/short-knives-wave.md b/.changeset/short-knives-wave.md new file mode 100644 index 0000000000..a7372618e2 --- /dev/null +++ b/.changeset/short-knives-wave.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-catalog-backend': minor +--- + +export `locationSpecToLocationEntity` diff --git a/plugins/catalog-backend/api-report.md b/plugins/catalog-backend/api-report.md index 0df10cf944..15c108f9f6 100644 --- a/plugins/catalog-backend/api-report.md +++ b/plugins/catalog-backend/api-report.md @@ -16,6 +16,7 @@ import { Entity } from '@backstage/catalog-model'; import { EntityPolicy } from '@backstage/catalog-model'; import { GetEntitiesRequest } from '@backstage/catalog-client'; import { JsonValue } from '@backstage/types'; +import { LocationEntityV1alpha1 } from '@backstage/catalog-model'; import { Logger } from 'winston'; import { Permission } from '@backstage/plugin-permission-common'; import { PermissionAuthorizer } from '@backstage/plugin-permission-common'; @@ -458,6 +459,12 @@ export type LocationSpec = { presence?: 'optional' | 'required'; }; +// @public (undocumented) +export function locationSpecToLocationEntity(opts: { + location: LocationSpec; + parentEntity?: Entity; +}): LocationEntityV1alpha1; + // @public (undocumented) export function parseEntityYaml( data: Buffer, diff --git a/plugins/catalog-backend/src/index.ts b/plugins/catalog-backend/src/index.ts index 437c1f4598..c5c653f0dd 100644 --- a/plugins/catalog-backend/src/index.ts +++ b/plugins/catalog-backend/src/index.ts @@ -24,7 +24,8 @@ export * from './api'; export * from './catalog'; export * from './ingestion'; export * from './modules'; -export * from './search'; -export * from './processing'; -export * from './service'; export * from './permissions'; +export * from './processing'; +export * from './search'; +export * from './service'; +export * from './util'; diff --git a/plugins/catalog-backend/src/modules/core/ConfigLocationEntityProvider.ts b/plugins/catalog-backend/src/modules/core/ConfigLocationEntityProvider.ts index 9529a5b608..69827b1380 100644 --- a/plugins/catalog-backend/src/modules/core/ConfigLocationEntityProvider.ts +++ b/plugins/catalog-backend/src/modules/core/ConfigLocationEntityProvider.ts @@ -60,8 +60,10 @@ export class ConfigLocationEntityProvider implements EntityProvider { const type = location.getString('type'); const target = location.getString('target'); const entity = locationSpecToLocationEntity({ - type, - target: type === 'file' ? path.resolve(target) : target, + location: { + type, + target: type === 'file' ? path.resolve(target) : target, + }, }); const locationKey = getEntityLocationRef(entity); return { entity, locationKey }; diff --git a/plugins/catalog-backend/src/modules/core/DefaultLocationStore.ts b/plugins/catalog-backend/src/modules/core/DefaultLocationStore.ts index 0d4bedabbb..33ab046104 100644 --- a/plugins/catalog-backend/src/modules/core/DefaultLocationStore.ts +++ b/plugins/catalog-backend/src/modules/core/DefaultLocationStore.ts @@ -58,7 +58,7 @@ export class DefaultLocationStore implements LocationStore, EntityProvider { return inner; }); - const entity = locationSpecToLocationEntity(location); + const entity = locationSpecToLocationEntity({ location }); await this.connection.applyMutation({ type: 'delta', added: [{ entity, locationKey: getEntityLocationRef(entity) }], @@ -100,7 +100,7 @@ export class DefaultLocationStore implements LocationStore, EntityProvider { await tx('locations').where({ id }).del(); return location; }); - const entity = locationSpecToLocationEntity(deleted); + const entity = locationSpecToLocationEntity({ location: deleted }); await this.connection.applyMutation({ type: 'delta', added: [], @@ -122,7 +122,7 @@ export class DefaultLocationStore implements LocationStore, EntityProvider { const locations = await this.locations(); const entities = locations.map(location => { - const entity = locationSpecToLocationEntity(location); + const entity = locationSpecToLocationEntity({ location }); return { entity, locationKey: getEntityLocationRef(entity) }; }); diff --git a/plugins/catalog-backend/src/processing/ProcessorOutputCollector.ts b/plugins/catalog-backend/src/processing/ProcessorOutputCollector.ts index cc3e1401bd..db7a050dce 100644 --- a/plugins/catalog-backend/src/processing/ProcessorOutputCollector.ts +++ b/plugins/catalog-backend/src/processing/ProcessorOutputCollector.ts @@ -106,10 +106,10 @@ export class ProcessorOutputCollector { this.deferredEntities.push({ entity, locationKey: location }); } else if (i.type === 'location') { - const entity = locationSpecToLocationEntity( - i.location, - this.parentEntity, - ); + const entity = locationSpecToLocationEntity({ + location: i.location, + parentEntity: this.parentEntity, + }); const locationKey = getEntityLocationRef(entity); this.deferredEntities.push({ entity, locationKey }); } else if (i.type === 'relation') { diff --git a/plugins/catalog-backend/src/util/conversion.ts b/plugins/catalog-backend/src/util/conversion.ts index 63771d1028..d61304a419 100644 --- a/plugins/catalog-backend/src/util/conversion.ts +++ b/plugins/catalog-backend/src/util/conversion.ts @@ -32,10 +32,14 @@ export function locationSpecToMetadataName(location: LocationSpec) { return `generated-${hash}`; } -export function locationSpecToLocationEntity( - location: LocationSpec, - parentEntity?: Entity, -): LocationEntityV1alpha1 { +/** @public */ +export function locationSpecToLocationEntity(opts: { + location: LocationSpec; + parentEntity?: Entity; +}): LocationEntityV1alpha1 { + const location = opts.location; + const parentEntity = opts.parentEntity; + let ownLocation: string; let originLocation: string; if (parentEntity) { diff --git a/plugins/catalog-backend/src/util/index.ts b/plugins/catalog-backend/src/util/index.ts new file mode 100644 index 0000000000..b3bd4d5316 --- /dev/null +++ b/plugins/catalog-backend/src/util/index.ts @@ -0,0 +1,17 @@ +/* + * 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. + */ + +export { locationSpecToLocationEntity } from './conversion';