From 41d0b0a24a0db272583010fdf161d7b715afda95 Mon Sep 17 00:00:00 2001 From: Niklas Aronsson Date: Fri, 30 Dec 2022 11:55:08 +0100 Subject: [PATCH] Bazaar: Added 'isBazaarAvailable' helper The EntityBazaarInfoCard shows the bazaar data for components that have a bazaar item linked. The isBazaarAvailable helper makes it possible to only include the EntityBazaarInfoCard for components that actually have a bazaar project linked. Closes #15013 Signed-off-by: Niklas Aronsson --- .changeset/cool-kings-raise.md | 5 +++++ plugins/bazaar/README.md | 13 ++++++++----- plugins/bazaar/api-report.md | 10 ++++++++++ plugins/bazaar/src/api.ts | 21 +++++++++++++++++++++ plugins/bazaar/src/index.ts | 1 + 5 files changed, 45 insertions(+), 5 deletions(-) create mode 100644 .changeset/cool-kings-raise.md diff --git a/.changeset/cool-kings-raise.md b/.changeset/cool-kings-raise.md new file mode 100644 index 0000000000..18344b128c --- /dev/null +++ b/.changeset/cool-kings-raise.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-bazaar': patch +--- + +Added `isBazaarAvailable` helper to be used with the `EntitySwitch`. diff --git a/plugins/bazaar/README.md b/plugins/bazaar/README.md index 2ad141935e..218a5a886c 100644 --- a/plugins/bazaar/README.md +++ b/plugins/bazaar/README.md @@ -49,17 +49,20 @@ Add a **Bazaar icon** to the Sidebar to easily access the Bazaar. In `packages/a Add a **Bazaar card** to the overview tab on the `packages/app/src/components/catalog/EntityPage.tsx` add: ```diff -+ import { EntityBazaarInfoCard } from '@backstage/plugin-bazaar'; ++ import { EntityBazaarInfoCard, isBazaarAvailable } from '@backstage/plugin-bazaar'; const overviewContent = ( - -+ -+ -+ ++ ++ ++ ++ ++ ++ ++ {/* ...other entity-cards */} ``` diff --git a/plugins/bazaar/api-report.md b/plugins/bazaar/api-report.md index d7c463888a..afbb7b5504 100644 --- a/plugins/bazaar/api-report.md +++ b/plugins/bazaar/api-report.md @@ -5,7 +5,9 @@ ```ts /// +import { ApiHolder } from '@backstage/core-plugin-api'; import { BackstagePlugin } from '@backstage/core-plugin-api'; +import { Entity } from '@backstage/catalog-model'; import { RouteRef } from '@backstage/core-plugin-api'; // @public (undocumented) @@ -36,6 +38,14 @@ export const bazaarPlugin: BackstagePlugin< // @public (undocumented) export const EntityBazaarInfoCard: () => JSX.Element | null; +// @public (undocumented) +export const isBazaarAvailable: ( + entity: Entity, + context: { + apis: ApiHolder; + }, +) => Promise; + // @public (undocumented) export const SortView: () => JSX.Element; diff --git a/plugins/bazaar/src/api.ts b/plugins/bazaar/src/api.ts index 33455e69fc..a2e32e274a 100644 --- a/plugins/bazaar/src/api.ts +++ b/plugins/bazaar/src/api.ts @@ -14,7 +14,9 @@ * limitations under the License. */ +import { Entity, stringifyEntityRef } from '@backstage/catalog-model'; import { + ApiHolder, createApiRef, DiscoveryApi, FetchApi, @@ -46,6 +48,25 @@ export interface BazaarApi { deleteProject(id: number): Promise; } +/** @public */ +export const isBazaarAvailable = async ( + entity: Entity, + context: { apis: ApiHolder }, +): Promise => { + const bazaarClient = context.apis.get(bazaarApiRef); + if (bazaarClient === undefined) { + return false; + } + const entityRef = stringifyEntityRef({ + kind: entity.kind, + name: entity.metadata.name, + namespace: entity.metadata.namespace, + }); + const response = await bazaarClient.getProjectByRef(entityRef); + const project = await response.json(); + return project.data.length > 0; +}; + export class BazaarClient implements BazaarApi { private readonly identityApi: IdentityApi; private readonly discoveryApi: DiscoveryApi; diff --git a/plugins/bazaar/src/index.ts b/plugins/bazaar/src/index.ts index 0014c66b6d..c0378e60bf 100644 --- a/plugins/bazaar/src/index.ts +++ b/plugins/bazaar/src/index.ts @@ -15,6 +15,7 @@ */ export { bazaarPlugin, BazaarPage } from './plugin'; +export { isBazaarAvailable } from './api'; export { BazaarOverviewCard } from './components/BazaarOverviewCard'; export type { BazaarOverviewCardProps } from './components/BazaarOverviewCard'; export { EntityBazaarInfoCard } from './components/EntityBazaarInfoCard';