From edbc03814a4c4a5be626968e8b02dde27824f215 Mon Sep 17 00:00:00 2001 From: Johan Haals Date: Fri, 11 Feb 2022 14:10:58 +0100 Subject: [PATCH] catalog-model: Remove serializeEntityRef Signed-off-by: Johan Haals --- .changeset/dry-lies-move.md | 6 ++ .changeset/plenty-boxes-wink.md | 5 ++ packages/catalog-model/api-report.md | 11 ---- packages/catalog-model/src/entity/index.ts | 1 - packages/catalog-model/src/entity/ref.test.ts | 65 +------------------ packages/catalog-model/src/entity/ref.ts | 48 -------------- .../src/components/EntityRefLink/format.ts | 6 +- .../src/service/TodoReaderService.ts | 10 +-- 8 files changed, 20 insertions(+), 132 deletions(-) create mode 100644 .changeset/dry-lies-move.md create mode 100644 .changeset/plenty-boxes-wink.md diff --git a/.changeset/dry-lies-move.md b/.changeset/dry-lies-move.md new file mode 100644 index 0000000000..b0eb1be6f7 --- /dev/null +++ b/.changeset/dry-lies-move.md @@ -0,0 +1,6 @@ +--- +'@backstage/plugin-catalog-react': patch +'@backstage/plugin-todo-backend': patch +--- + +Replace usage of `serializeEntityRef` with `stringifyEntityRef` diff --git a/.changeset/plenty-boxes-wink.md b/.changeset/plenty-boxes-wink.md new file mode 100644 index 0000000000..af8df864b3 --- /dev/null +++ b/.changeset/plenty-boxes-wink.md @@ -0,0 +1,5 @@ +--- +'@backstage/catalog-model': minor +--- + +Remove deprecated `serializeEntityRef` which is replaced by `stringifyEntityRef`. diff --git a/packages/catalog-model/api-report.md b/packages/catalog-model/api-report.md index 22b249e211..18e66813fb 100644 --- a/packages/catalog-model/api-report.md +++ b/packages/catalog-model/api-report.md @@ -489,17 +489,6 @@ export class SchemaValidEntityPolicy implements EntityPolicy { enforce(entity: Entity): Promise; } -// @public @deprecated -export function serializeEntityRef( - ref: - | Entity - | { - kind?: string; - namespace?: string; - name: string; - }, -): EntityRef; - // @public export const SOURCE_LOCATION_ANNOTATION = 'backstage.io/source-location'; diff --git a/packages/catalog-model/src/entity/index.ts b/packages/catalog-model/src/entity/index.ts index ce1e76a20b..ea2145ab1a 100644 --- a/packages/catalog-model/src/entity/index.ts +++ b/packages/catalog-model/src/entity/index.ts @@ -40,7 +40,6 @@ export { getEntityName, parseEntityName, parseEntityRef, - serializeEntityRef, stringifyEntityRef, } from './ref'; export type { EntityRefContext } from './ref'; diff --git a/packages/catalog-model/src/entity/ref.test.ts b/packages/catalog-model/src/entity/ref.test.ts index cf4bf0d869..f80b588d55 100644 --- a/packages/catalog-model/src/entity/ref.test.ts +++ b/packages/catalog-model/src/entity/ref.test.ts @@ -16,12 +16,7 @@ import { ENTITY_DEFAULT_NAMESPACE } from './constants'; import { Entity } from './Entity'; -import { - compareEntityToRef, - parseEntityName, - parseEntityRef, - serializeEntityRef, -} from './ref'; +import { compareEntityToRef, parseEntityName, parseEntityRef } from './ref'; describe('ref', () => { describe('parseEntityName', () => { @@ -329,64 +324,6 @@ describe('ref', () => { }); }); - describe('serializeEntityRef', () => { - it('handles partials', () => { - expect( - serializeEntityRef({ kind: 'a', namespace: 'b', name: 'c' }), - ).toEqual('a:b/c'); - expect(serializeEntityRef({ namespace: 'b', name: 'c' })).toEqual('b/c'); - expect(serializeEntityRef({ kind: 'a', name: 'c' })).toEqual('a:c'); - expect(serializeEntityRef({ name: 'c' })).toEqual('c'); - }); - - it('handles entities', () => { - const entityWithNamespace: Entity = { - apiVersion: 'a', - kind: 'b', - metadata: { - name: 'c', - namespace: 'd', - }, - }; - const entityWithoutNamespace: Entity = { - apiVersion: 'a', - kind: 'b', - metadata: { - name: 'c', - }, - }; - expect(serializeEntityRef(entityWithNamespace)).toEqual('b:d/c'); - expect(serializeEntityRef(entityWithoutNamespace)).toEqual('b:c'); - }); - - it('picks the least complex form', () => { - expect( - serializeEntityRef({ kind: 'a', namespace: 'b', name: 'c' }), - ).toEqual('a:b/c'); - expect(serializeEntityRef({ namespace: 'b', name: 'c' })).toEqual('b/c'); - expect(serializeEntityRef({ kind: 'a', name: 'c' })).toEqual('a:c'); - expect(serializeEntityRef({ name: 'c' })).toEqual('c'); - expect( - serializeEntityRef({ kind: 'a:x', namespace: 'b', name: 'c' }), - ).toEqual({ kind: 'a:x', namespace: 'b', name: 'c' }); - expect( - serializeEntityRef({ kind: 'a/x', namespace: 'b', name: 'c' }), - ).toEqual({ kind: 'a/x', namespace: 'b', name: 'c' }); - expect( - serializeEntityRef({ kind: 'a', namespace: 'b:x', name: 'c' }), - ).toEqual({ kind: 'a', namespace: 'b:x', name: 'c' }); - expect( - serializeEntityRef({ kind: 'a', namespace: 'b/x', name: 'c' }), - ).toEqual({ kind: 'a', namespace: 'b/x', name: 'c' }); - expect( - serializeEntityRef({ kind: 'a', namespace: 'b', name: 'c:x' }), - ).toEqual({ kind: 'a', namespace: 'b', name: 'c:x' }); - expect( - serializeEntityRef({ kind: 'a', namespace: 'b', name: 'c/x' }), - ).toEqual({ kind: 'a', namespace: 'b', name: 'c/x' }); - }); - }); - describe('compareEntityToRef', () => { const entityWithNamespace: Entity = { apiVersion: 'a', diff --git a/packages/catalog-model/src/entity/ref.ts b/packages/catalog-model/src/entity/ref.ts index 958f750e32..1996616077 100644 --- a/packages/catalog-model/src/entity/ref.ts +++ b/packages/catalog-model/src/entity/ref.ts @@ -184,54 +184,6 @@ export function parseEntityRef( }; } -/** - * Takes an entity reference or name, and outputs an entity reference on the - * most compact form possible. I.e. if the parts do not contain any - * special/reserved characters, it outputs the string form, otherwise it - * outputs the compound form. - * - * @public - * @deprecated Use `stringifyEntityRef` instead - * @param ref - The reference to serialize - * @returns The same reference on either string or compound form - */ -export function serializeEntityRef( - ref: - | Entity - | { - kind?: string; - namespace?: string; - name: string; - }, -): EntityRef { - let kind; - let namespace; - let name; - - if ('metadata' in ref) { - kind = ref.kind; - namespace = ref.metadata.namespace; - name = ref.metadata.name; - } else { - kind = ref.kind; - namespace = ref.namespace; - name = ref.name; - } - - if ( - kind?.includes(':') || - kind?.includes('/') || - namespace?.includes(':') || - namespace?.includes('/') || - name.includes(':') || - name.includes('/') - ) { - return { kind, namespace, name }; - } - - return `${kind ? `${kind}:` : ''}${namespace ? `${namespace}/` : ''}${name}`; -} - /** * Takes an entity or entity name/reference, and returns the string form of an * entity ref. diff --git a/plugins/catalog-react/src/components/EntityRefLink/format.ts b/plugins/catalog-react/src/components/EntityRefLink/format.ts index f3977ccc95..d94a576677 100644 --- a/plugins/catalog-react/src/components/EntityRefLink/format.ts +++ b/plugins/catalog-react/src/components/EntityRefLink/format.ts @@ -18,7 +18,7 @@ import { Entity, EntityName, ENTITY_DEFAULT_NAMESPACE, - serializeEntityRef, + stringifyEntityRef, } from '@backstage/catalog-model'; export function formatEntityRefTitle( @@ -46,10 +46,10 @@ export function formatEntityRefTitle( kind = kind.toLocaleLowerCase('en-US'); - return `${serializeEntityRef({ + return `${stringifyEntityRef({ kind: defaultKind && defaultKind.toLocaleLowerCase('en-US') === kind - ? undefined + ? '' : kind, name, namespace, diff --git a/plugins/todo-backend/src/service/TodoReaderService.ts b/plugins/todo-backend/src/service/TodoReaderService.ts index 8fbca4019f..c3cd1c189b 100644 --- a/plugins/todo-backend/src/service/TodoReaderService.ts +++ b/plugins/todo-backend/src/service/TodoReaderService.ts @@ -19,9 +19,9 @@ import { CatalogApi } from '@backstage/catalog-client'; import { LOCATION_ANNOTATION, SOURCE_LOCATION_ANNOTATION, - serializeEntityRef, Entity, parseLocationRef, + stringifyEntityRef, } from '@backstage/catalog-model'; import { TodoReader } from '../lib'; import { ListTodosRequest, ListTodosResponse, TodoService } from './types'; @@ -71,7 +71,7 @@ export class TodoReaderService implements TodoService { }); if (!entity) { throw new NotFoundError( - `Entity not found, ${serializeEntityRef(req.entity)}`, + `Entity not found, ${stringifyEntityRef(req.entity)}`, ); } @@ -133,7 +133,7 @@ export class TodoReaderService implements TodoService { const parsed = parseLocationRef(sourceLocation); if (parsed.type !== 'url') { throw new InputError( - `Invalid entity source location type for ${serializeEntityRef( + `Invalid entity source location type for ${stringifyEntityRef( entity, )}, got '${parsed.type}'`, ); @@ -146,7 +146,7 @@ export class TodoReaderService implements TodoService { const parsed = parseLocationRef(location); if (parsed.type !== 'url') { throw new InputError( - `Invalid entity location type for ${serializeEntityRef( + `Invalid entity location type for ${stringifyEntityRef( entity, )}, got '${parsed.type}'`, ); @@ -154,7 +154,7 @@ export class TodoReaderService implements TodoService { return parsed.target; } throw new InputError( - `No entity location annotation found for ${serializeEntityRef(entity)}`, + `No entity location annotation found for ${stringifyEntityRef(entity)}`, ); } }