diff --git a/packages/backend/src/plugins/badges.ts b/packages/backend/src/plugins/badges.ts index 4e2eee74e5..cea58d8737 100644 --- a/packages/backend/src/plugins/badges.ts +++ b/packages/backend/src/plugins/badges.ts @@ -14,7 +14,10 @@ * limitations under the License. */ -import { createRouter, badges } from '@backstage/plugin-badges-backend'; +import { + createRouter, + createDefaultBadges, +} from '@backstage/plugin-badges-backend'; import { PluginEnvironment } from '../types'; export default async function createPlugin({ @@ -22,5 +25,10 @@ export default async function createPlugin({ config, discovery, }: PluginEnvironment) { - return await createRouter({ logger, config, discovery, badges }); + return await createRouter({ + logger, + config, + discovery, + badges: createDefaultBadges(), + }); } diff --git a/plugins/badges-backend/README.md b/plugins/badges-backend/README.md index ac85267ca1..94008c4ba0 100644 --- a/plugins/badges-backend/README.md +++ b/plugins/badges-backend/README.md @@ -9,20 +9,6 @@ the badges, in svg. The list of all badges to offer are passed to the badges-backend `createRouter()`. -You may also add/redefine badges in the `app-config.yaml`, under a -`badges` key. Example: - -```yaml -badges: - docs: - kind: 'entity' - target: '_{entity_url}/docs' - label: 'docs' - message: '_{entity.metadata.name}' - color: 'navyblue' - style: for-the-badge -``` - ## Links - [Frontend part of the plugin](https://github.com/backstage/backstage/tree/master/plugins/badges) diff --git a/plugins/badges-backend/config.d.ts b/plugins/badges-backend/config.d.ts deleted file mode 100644 index 5870b650a0..0000000000 --- a/plugins/badges-backend/config.d.ts +++ /dev/null @@ -1,103 +0,0 @@ -/* - * Copyright 2021 Spotify AB - * - * 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 interface Config { - /** - * Define custom badges. By default, the badges are declared in - * code, and passed to the badges backend `createRouter`, which - * merges them with any additional badges defined in this - * configuration. - * - * The `label` and `message` fields may use templating to support - * dynamic content, based on context. Use same syntax as for - * javascript template literals, but with `_` instead of `$`, - * e.g. `_{context.variable.name}`. - * - */ - badges?: { - [badgeId: string]: { - /** - * (Optional) Badge kind. - * - * Restrict badge usage to a certain kind only. - * Useful when using templating for label and/or message if they - * use context data only available for a certain kind of badge. - * - * Context per badge kind. - * - * Entity badges: - * - * * `entity` The entity data is available as `entity` in the template. - * * `entity_url` The (frontend) URL to view the entity in Backstage. - * - * Default context for all badges: - * - * * `app.title` As configured or defaults to "Backstage". - * * `badge_url` The URL to the badge image. - * - */ - kind?: 'entity'; - - /** - * The badge label. - */ - label: string; - - /** - * The badge message. - */ - message: string; - - /** - * The message color. Default: `#36BAA2`. - */ - color?: string; - - /** - * The label color. Default: `gray`. - */ - labelColor?: string; - - /** - * Visual design of the badge. One of: 'plastic', 'flat', 'flat-square', - * 'for-the-badge' or 'social'. - * - * Default: 'flat' - * - */ - style?: 'plastic' | 'flat' | 'flat-square' | 'for-the-badge' | 'social'; - - /** - * Badge description, used as prefix on the alt text in the markdown code. - * - * Defaults to badge id. - * - */ - description?: string; - - /** - * Badge link URL, turns badge into a link in the markdown code. - * - * For `entity` badges, there is a `entity_url` in the context - * that could be appropriate to use here. - * - * Defaults to the `entity_url`, set to falsey value to disable the link. - * - */ - link?: string; - }; - }; -} diff --git a/plugins/badges-backend/package.json b/plugins/badges-backend/package.json index ef9e23e16f..f3bdef761c 100644 --- a/plugins/badges-backend/package.json +++ b/plugins/badges-backend/package.json @@ -48,8 +48,6 @@ "supertest": "^4.0.2" }, "files": [ - "dist", - "config.d.ts" - ], - "configSchema": "config.d.ts" + "dist" + ] } diff --git a/plugins/badges-backend/src/badges.ts b/plugins/badges-backend/src/badges.ts index c830fbcfa2..da1dad6bce 100644 --- a/plugins/badges-backend/src/badges.ts +++ b/plugins/badges-backend/src/badges.ts @@ -16,7 +16,7 @@ import { Badge } from './types'; -export const badges: Badge[] = [ +export const createDefaultBadges = (): Badge[] => [ { id: 'pingback', kind: 'entity', diff --git a/plugins/badges-backend/src/lib/BadgeBuilder/DefaultBadgeBuilder.ts b/plugins/badges-backend/src/lib/BadgeBuilder/DefaultBadgeBuilder.ts index 76d2645b51..c9c9021ddd 100644 --- a/plugins/badges-backend/src/lib/BadgeBuilder/DefaultBadgeBuilder.ts +++ b/plugins/badges-backend/src/lib/BadgeBuilder/DefaultBadgeBuilder.ts @@ -17,29 +17,31 @@ import { Logger } from 'winston'; import { makeBadge } from 'badge-maker'; import { BadgeBuilder, BadgeOptions } from './types'; -import { Badge, BadgeConfig, BadgeStyle, BadgeStyles } from '../../types'; +import { Badge, BadgeConfig, BadgeStyle, BADGE_STYLES } from '../../types'; import { interpolate } from '../../utils'; export class DefaultBadgeBuilder implements BadgeBuilder { - constructor( - private readonly logger: Logger, - private readonly config: BadgeConfig, - ) { - for (const [badgeId, badge] of Object.entries(config)) { - if (badge) { - badge.id = badgeId; + private readonly badges: BadgeConfig = {}; + + constructor(private readonly logger: Logger, initBadges: Badge[]) { + for (const badge of initBadges) { + if (!badge.id) { + logger.warning(`badge without "id": ${JSON.stringify(badge, null, 2)}`); + } else { + this.badges[badge.id] = badge; + logger.info(`register ${badge.kind || 'entity'} badge: "${badge.id}"`); } } } public async getAllBadgeConfigs(): Promise { - return Object.values(this.config); + return Object.values(this.badges); } public async getBadgeConfig(badgeId: string): Promise { return ( - this.config[badgeId] || - this.config.default || + this.badges[badgeId] || + this.badges.default || ({ label: 'Unknown badge ID', message: badgeId, @@ -60,7 +62,7 @@ export class DefaultBadgeBuilder implements BadgeBuilder { params.labelColor = badge.labelColor; } - if (BadgeStyles.includes(badge.style as BadgeStyle)) { + if (BADGE_STYLES.includes(badge.style as BadgeStyle)) { params.style = badge.style as BadgeStyle; } diff --git a/plugins/badges-backend/src/service/router.ts b/plugins/badges-backend/src/service/router.ts index 694d53aed0..31eb96ceb1 100644 --- a/plugins/badges-backend/src/service/router.ts +++ b/plugins/badges-backend/src/service/router.ts @@ -25,7 +25,7 @@ import { import { Entity } from '@backstage/catalog-model'; import { Config, JsonObject } from '@backstage/config'; import { BadgeBuilder, DefaultBadgeBuilder } from '../lib/BadgeBuilder'; -import { Badge, BadgeConfig, BadgeStyle, BadgeStyles } from '../types'; +import { Badge, BadgeStyle, BADGE_STYLES } from '../types'; export interface RouterOptions { badgeBuilder?: BadgeBuilder; @@ -42,22 +42,9 @@ export async function createRouter( const logger = options.logger.child({ plugin: 'badges' }); const title = options.config.getString('app.title') || 'Backstage'; const catalogUrl = `${options.config.getString('app.baseUrl')}/catalog`; - const badgesConfig = (options.config.getOptional('badges') ?? - {}) as BadgeConfig; - - for (const badge of options.badges || []) { - if (!badge.id) { - logger.warning(`badge without "id": ${JSON.stringify(badge, null, 2)}`); - } else if (!badgesConfig[badge.id]) { - badgesConfig[badge.id] = badge; - logger.info(`register builtin badge: ${badge.id}`); - } else { - logger.info(`builtin badge replaced from configuration: ${badge.id}`); - } - } - const badgeBuilder = - options.badgeBuilder || new DefaultBadgeBuilder(logger, badgesConfig); + options.badgeBuilder || + new DefaultBadgeBuilder(logger, options.badges || []); router.get('/entity/:namespace/:kind/:name/badge-specs', async (req, res) => { const entityUri = getEntityUri(req.params); @@ -122,7 +109,7 @@ export async function createRouter( format = 'application/json'; } - if (BadgeStyles.includes(req.query.style as BadgeStyle)) { + if (BADGE_STYLES.includes(req.query.style as BadgeStyle)) { badge.style = req.query.style as BadgeStyle; } diff --git a/plugins/badges-backend/src/types.ts b/plugins/badges-backend/src/types.ts index 00a3d40756..0943589422 100644 --- a/plugins/badges-backend/src/types.ts +++ b/plugins/badges-backend/src/types.ts @@ -14,14 +14,14 @@ * limitations under the License. */ -export const BadgeStyles = [ +export const BADGE_STYLES = [ 'plastic', 'flat', 'flat-square', 'for-the-badge', 'social', ] as const; -export type BadgeStyle = typeof BadgeStyles[number]; +export type BadgeStyle = typeof BADGE_STYLES[number]; export interface Badge { /** Unique name for the badge. */