From 615f668d317afbce4466f98cc811afa218e31c06 Mon Sep 17 00:00:00 2001 From: Dominik Henneke Date: Tue, 12 Oct 2021 11:02:30 +0200 Subject: [PATCH] Use entity refs as a storage format instead of the custom one Signed-off-by: Dominik Henneke --- .changeset/cool-deers-cough.md | 2 ++ plugins/catalog-react/api-report.md | 4 +--- .../DefaultStarredEntitiesApi.test.ts | 6 +++--- .../DefaultStarredEntitiesApi.ts | 16 ++++++++-------- .../StarredEntitiesApi/StarredEntitiesApi.ts | 2 +- 5 files changed, 15 insertions(+), 15 deletions(-) diff --git a/.changeset/cool-deers-cough.md b/.changeset/cool-deers-cough.md index 6d3e923d7d..c2619d1fa2 100644 --- a/.changeset/cool-deers-cough.md +++ b/.changeset/cool-deers-cough.md @@ -5,3 +5,5 @@ Introduce a new `StarredEntitiesApi` that is used in the `useStarredEntities` hook. The `@backstage/plugin-catalog` installs a default implementation that is backed by the `StorageApi`, but one can also override the `starredEntitiesApiRef`. + +**BREAKING** All previously stored entities get lost with this update, because the storage format has been migrated from a custom string to an entity reference. diff --git a/plugins/catalog-react/api-report.md b/plugins/catalog-react/api-report.md index 12ae143d8a..ea4c51adda 100644 --- a/plugins/catalog-react/api-report.md +++ b/plugins/catalog-react/api-report.md @@ -133,9 +133,7 @@ export type DefaultEntityFilters = { text?: EntityTextFilter; }; -// Warning: (ae-missing-release-tag) "DefaultStarredEntitiesApi" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) -// -// @public (undocumented) +// @public export class DefaultStarredEntitiesApi implements StarredEntitiesApi { constructor(opts: { storageApi: StorageApi }); // (undocumented) diff --git a/plugins/catalog-react/src/apis/StarredEntitiesApi/DefaultStarredEntitiesApi.test.ts b/plugins/catalog-react/src/apis/StarredEntitiesApi/DefaultStarredEntitiesApi.test.ts index 1edcdf706b..eec484863f 100644 --- a/plugins/catalog-react/src/apis/StarredEntitiesApi/DefaultStarredEntitiesApi.test.ts +++ b/plugins/catalog-react/src/apis/StarredEntitiesApi/DefaultStarredEntitiesApi.test.ts @@ -53,7 +53,7 @@ describe('DefaultStarredEntitiesApi', () => { it('should unstar starred entity', async () => { const bucket = mockStorage.forBucket('settings'); - await bucket.set('starredEntities', ['entity:Component:default:mock']); + await bucket.set('starredEntities', ['component:default/mock']); expect(starredEntitiesApi.isStarred(mockEntity)).toBe(true); @@ -79,7 +79,7 @@ describe('DefaultStarredEntitiesApi', () => { }); const bucket = mockStorage.forBucket('settings'); - bucket.set('starredEntities', ['entity:Component:default:mock']).then(); + bucket.set('starredEntities', ['component:default/mock']).then(); }); }); @@ -90,7 +90,7 @@ describe('DefaultStarredEntitiesApi', () => { isStarred: expect.any(Function), }); expect(handler).toBeCalledWith({ - starredEntities: new Set(['entity:Component:default:mock']), + starredEntities: new Set(['component:default/mock']), isStarred: expect.any(Function), }); }); diff --git a/plugins/catalog-react/src/apis/StarredEntitiesApi/DefaultStarredEntitiesApi.ts b/plugins/catalog-react/src/apis/StarredEntitiesApi/DefaultStarredEntitiesApi.ts index 4c5ea63cf9..5783954fbc 100644 --- a/plugins/catalog-react/src/apis/StarredEntitiesApi/DefaultStarredEntitiesApi.ts +++ b/plugins/catalog-react/src/apis/StarredEntitiesApi/DefaultStarredEntitiesApi.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import { Entity } from '@backstage/catalog-model'; +import { Entity, stringifyEntityRef } from '@backstage/catalog-model'; import { Observable, StorageApi } from '@backstage/core-plugin-api'; import ObservableImpl from 'zen-observable'; import { @@ -22,11 +22,11 @@ import { StarredEntitiesApiObservable, } from './StarredEntitiesApi'; -const buildEntityKey = (component: Entity) => - `entity:${component.kind}:${component.metadata.namespace ?? 'default'}:${ - component.metadata.name - }`; - +/** + * Default implementation of the StarredEntitiesApi that is backed by the StorageApi. + * + * @public + */ export class DefaultStarredEntitiesApi implements StarredEntitiesApi { private readonly settingsStore: StorageApi; private starredEntities: Set; @@ -47,7 +47,7 @@ export class DefaultStarredEntitiesApi implements StarredEntitiesApi { } async toggleStarred(entity: Entity): Promise { - const entityKey = buildEntityKey(entity); + const entityKey = stringifyEntityRef(entity); if (this.starredEntities.has(entityKey)) { this.starredEntities.delete(entityKey); @@ -66,7 +66,7 @@ export class DefaultStarredEntitiesApi implements StarredEntitiesApi { } isStarred(entity: Entity): boolean { - const entityKey = buildEntityKey(entity); + const entityKey = stringifyEntityRef(entity); return this.starredEntities.has(entityKey); } diff --git a/plugins/catalog-react/src/apis/StarredEntitiesApi/StarredEntitiesApi.ts b/plugins/catalog-react/src/apis/StarredEntitiesApi/StarredEntitiesApi.ts index f64298227e..0c8ec52445 100644 --- a/plugins/catalog-react/src/apis/StarredEntitiesApi/StarredEntitiesApi.ts +++ b/plugins/catalog-react/src/apis/StarredEntitiesApi/StarredEntitiesApi.ts @@ -23,7 +23,7 @@ export const starredEntitiesApiRef: ApiRef = createApiRef({ export type StarredEntitiesApiObservable = { /** - * A set of starred entities + * A set of entity references that are starred */ starredEntities: Set;