badges-backend: drop support for defining badges in app config.

Signed-off-by: Andreas Stenius <andreas.stenius@svenskaspel.se>
This commit is contained in:
Andreas Stenius
2021-03-02 12:13:10 +01:00
committed by Fredrik Adelöw
parent d80bdf0324
commit 6a68e79217
8 changed files with 33 additions and 155 deletions
+10 -2
View File
@@ -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(),
});
}
-14
View File
@@ -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)
-103
View File
@@ -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;
};
};
}
+2 -4
View File
@@ -48,8 +48,6 @@
"supertest": "^4.0.2"
},
"files": [
"dist",
"config.d.ts"
],
"configSchema": "config.d.ts"
"dist"
]
}
+1 -1
View File
@@ -16,7 +16,7 @@
import { Badge } from './types';
export const badges: Badge[] = [
export const createDefaultBadges = (): Badge[] => [
{
id: 'pingback',
kind: 'entity',
@@ -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<Badge[]> {
return Object.values(this.config);
return Object.values(this.badges);
}
public async getBadgeConfig(badgeId: string): Promise<Badge> {
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;
}
+4 -17
View File
@@ -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;
}
+2 -2
View File
@@ -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. */