From edbc03814a4c4a5be626968e8b02dde27824f215 Mon Sep 17 00:00:00 2001 From: Johan Haals Date: Fri, 11 Feb 2022 14:10:58 +0100 Subject: [PATCH 1/4] 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)}`, ); } } From d08b6d035d59a2c8aadec4127706768cbc5c5824 Mon Sep 17 00:00:00 2001 From: Johan Haals Date: Fri, 11 Feb 2022 14:16:53 +0100 Subject: [PATCH 2/4] add breaking notice Signed-off-by: Johan Haals --- .changeset/plenty-boxes-wink.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/plenty-boxes-wink.md b/.changeset/plenty-boxes-wink.md index af8df864b3..0011641938 100644 --- a/.changeset/plenty-boxes-wink.md +++ b/.changeset/plenty-boxes-wink.md @@ -2,4 +2,4 @@ '@backstage/catalog-model': minor --- -Remove deprecated `serializeEntityRef` which is replaced by `stringifyEntityRef`. +**BREAKING**: Remove deprecated `serializeEntityRef` which is replaced by `stringifyEntityRef`. From 854ef5ddfff9ebcf357ba8b6c39da719ca0054c3 Mon Sep 17 00:00:00 2001 From: Johan Haals Date: Fri, 11 Feb 2022 15:03:04 +0100 Subject: [PATCH 3/4] reuse format logic from serializeEntityRef Signed-off-by: Johan Haals --- .../src/components/EntityRefLink/format.ts | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/plugins/catalog-react/src/components/EntityRefLink/format.ts b/plugins/catalog-react/src/components/EntityRefLink/format.ts index d94a576677..4787474c68 100644 --- a/plugins/catalog-react/src/components/EntityRefLink/format.ts +++ b/plugins/catalog-react/src/components/EntityRefLink/format.ts @@ -18,7 +18,6 @@ import { Entity, EntityName, ENTITY_DEFAULT_NAMESPACE, - stringifyEntityRef, } from '@backstage/catalog-model'; export function formatEntityRefTitle( @@ -45,13 +44,9 @@ export function formatEntityRefTitle( } kind = kind.toLocaleLowerCase('en-US'); - - return `${stringifyEntityRef({ - kind: - defaultKind && defaultKind.toLocaleLowerCase('en-US') === kind - ? '' - : kind, - name, - namespace, - })}`; + kind = + defaultKind && defaultKind.toLocaleLowerCase('en-US') === kind + ? undefined + : kind; + return `${kind ? `${kind}:` : ''}${namespace ? `${namespace}/` : ''}${name}`; } From 215debc7e0a2a51313b725e029a5c1cdd56b0cf9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Adel=C3=B6w?= Date: Sat, 12 Feb 2022 10:20:50 +0100 Subject: [PATCH 4/4] fix tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fredrik Adelöw --- .../todo-backend/src/service/TodoReaderService.test.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/plugins/todo-backend/src/service/TodoReaderService.test.ts b/plugins/todo-backend/src/service/TodoReaderService.test.ts index 3d86a1726b..ce1c61d8a5 100644 --- a/plugins/todo-backend/src/service/TodoReaderService.test.ts +++ b/plugins/todo-backend/src/service/TodoReaderService.test.ts @@ -301,7 +301,7 @@ describe('TodoReaderService', () => { await expect(service.listTodos({ entity: entityName })).rejects.toEqual( expect.objectContaining({ name: 'NotFoundError', - message: 'Entity not found, Component:default/my-component', + message: 'Entity not found, component:default/my-component', }), ); expect(catalogClient.getEntityByName).toHaveBeenCalledWith(entityName, { @@ -321,7 +321,7 @@ describe('TodoReaderService', () => { expect.objectContaining({ name: 'InputError', message: - 'No entity location annotation found for Component:my-component', + 'No entity location annotation found for component:default/my-component', }), ); }); @@ -342,7 +342,7 @@ describe('TodoReaderService', () => { await expect(service.listTodos({ entity: entityName })).rejects.toEqual( expect.objectContaining({ name: 'InputError', - message: `Invalid entity location type for Component:my-component, got 'file'`, + message: `Invalid entity location type for component:default/my-component, got 'file'`, }), ); }); @@ -363,7 +363,7 @@ describe('TodoReaderService', () => { await expect(service.listTodos({ entity: entityName })).rejects.toEqual( expect.objectContaining({ name: 'InputError', - message: `Invalid entity source location type for Component:my-component, got 'file'`, + message: `Invalid entity source location type for component:default/my-component, got 'file'`, }), ); });