From 7b736657102e74c51201c4770f8160a4f3caf858 Mon Sep 17 00:00:00 2001 From: levivannoort Date: Sat, 28 Sep 2024 11:43:51 +0200 Subject: [PATCH] refactor: working generated code setup --- .../.amplicationrc.json | 22 ++++++-- plugins/backstage-catalog-entity/README.md | 21 +++++++- .../backstage-catalog-entity/src/constants.ts | 9 ++-- plugins/backstage-catalog-entity/src/index.ts | 52 ++++++++++++++----- .../src/static/template.yaml | 13 ++--- 5 files changed, 86 insertions(+), 31 deletions(-) diff --git a/plugins/backstage-catalog-entity/.amplicationrc.json b/plugins/backstage-catalog-entity/.amplicationrc.json index c06d999a..0c417c48 100644 --- a/plugins/backstage-catalog-entity/.amplicationrc.json +++ b/plugins/backstage-catalog-entity/.amplicationrc.json @@ -1,6 +1,22 @@ { - "settings": {}, - "systemSettings": { - "requireAuthenticationEntity": "false" + "settings": { + "labels": { + "vendor": "amplication" + }, + "annotations": { + "github.com/project-slug": "backstage/backstage", + "github.com/team-slug": "backstage/maintainers" + }, + "tags": [ + "Node.js" + ], + "spec": { + "type": "service", + "lifecycle": "production", + "owner": "development" } + }, + "systemSettings": { + "requireAuthenticationEntity": "false" + } } \ No newline at end of file diff --git a/plugins/backstage-catalog-entity/README.md b/plugins/backstage-catalog-entity/README.md index fb6d68a5..58d41c45 100644 --- a/plugins/backstage-catalog-entity/README.md +++ b/plugins/backstage-catalog-entity/README.md @@ -12,10 +12,29 @@ Documentation: - register: https://backstage.io/docs/features/software-catalog/#manually-register-components - documentation: https://backstage.io/docs/features/software-catalog/descriptor-format/ +- annotations: https://backstage.io/docs/features/software-catalog/well-known-annotations ## Configuration -If a configuration is required, add it here. +`example` + +```json +{ + "labels": { + "vendor": "amplication" + }, + "annotations": { + "github.com/project-slug": "backstage/backstage", + "github.com/team-slug": "backstage/maintainers" + }, + "tags": ["Node.js"], + "spec": { + "type": "service", + "lifecycle": "production", + "owner": "development" + } +} +``` ## Scripts diff --git a/plugins/backstage-catalog-entity/src/constants.ts b/plugins/backstage-catalog-entity/src/constants.ts index 9740cdaf..b5f978d7 100644 --- a/plugins/backstage-catalog-entity/src/constants.ts +++ b/plugins/backstage-catalog-entity/src/constants.ts @@ -9,15 +9,16 @@ export const specTypeKey = "${{ SPEC_TYPE }}"; export const specLifecycleKey = "${{ SPEC_LIFECYCLE }}"; export const specOwnerKey = "${{ SPEC_OWNER }}"; -export const workspaceIdKey = "${{ WORKSPACE_ID }}"; -export const projectIdKey = "${{ PROJECT_ID }}"; -export const serviceIdKey = "${{ SERVICE_ID }}"; +export const appUrlKey = "${{ APP_URL }}"; export const defaultNamePrefix = "Amplication"; export const defaultDescription = "Backend service which is auto-generated by Amplication."; export const defaultLabels = new Map([["vendor", "amplication"]]); -export const defaultAnnotations = new Map([["vendor", "amplication"]]); +export const defaultAnnotations = new Map([ + ["github.com/project-slug", "amplication"], +]); +export const defaultUrl = "https://app.amplication.com"; export const defaultTags = ["Node.js"]; export const defaultLifecycle = "production"; export const defaultOwner = "development"; diff --git a/plugins/backstage-catalog-entity/src/index.ts b/plugins/backstage-catalog-entity/src/index.ts index 034c1208..ec12df18 100644 --- a/plugins/backstage-catalog-entity/src/index.ts +++ b/plugins/backstage-catalog-entity/src/index.ts @@ -11,6 +11,7 @@ import { metadataLabelsKey, metadataAnnotationsKey, metadataTagsKey, + appUrlKey, specLifecycleKey, specOwnerKey, specTypeKey, @@ -18,6 +19,7 @@ import { defaultDescription, defaultLabels, defaultAnnotations, + defaultUrl, defaultTags, defaultLifecycle, defaultOwner, @@ -47,14 +49,15 @@ class BackstageCatalogEntityPlugin implements AmplicationPlugin { const serviceName = kebabCase(context.resourceInfo?.name); const serviceDescription = context.resourceInfo?.description; - const serviceType = context.resourceType.toLowerCase(); + const serviceType = "service"; + const serviceUrl = context.resourceInfo?.url; const settings = getPluginSettings(context.pluginInstallations); const staticPath = resolve(__dirname, "./static/"); const staticFiles = await context.utils.importStaticModules( staticPath, - "." + "./" ); const templateFileName = "template.yaml"; @@ -64,9 +67,9 @@ class BackstageCatalogEntityPlugin implements AmplicationPlugin { path.replace(templateFileName, destinationFileName) ); - let labels = ""; - let annotations = ""; - let tags = ""; + let labels = `labels:`; + let annotations = `annotations:`; + let tags = `tags:`; const indentation = " "; @@ -78,26 +81,45 @@ class BackstageCatalogEntityPlugin implements AmplicationPlugin { const l = settings.labels ?? defaultLabels; - l.forEach((value, name) => { - labels = `${labels}\n${indentation}${name}: "${value}"`; - }); + if (typeof l === "object" && l !== null) { + Object.entries(l).forEach(([name, value]) => { + labels = `${labels}\n${indentation}${name}: "${value}"`; + }); + } else { + context.logger.info(`"Labels should be an object:"`, l); + } const a = settings.annotations ?? defaultAnnotations; - a.forEach((value, name) => { - annotations = `${annotations}\n${indentation}${name}: "${value}"`; - }); + if (typeof a === "object" && a !== null) { + Object.entries(a).forEach(([name, value]) => { + annotations = `${annotations}\n${indentation}${name}: "${value}"`; + }); + } else { + context.logger.info(`"Annotations should be an object:"`, a); + } + // Handling tags (Array) const t = settings.tags ?? defaultTags; - t.forEach((tag) => { - tags = `${tags}\n${indentation}- "${tag}"`; - }); + if (Array.isArray(t)) { + t.forEach((tag) => { + tags = `${tags}\n${indentation}- "${tag}"`; + }); + } else { + context.logger.info(`"Tags should be an array:"`, t); + } + + const appUrl = serviceUrl ?? defaultUrl; const type = settings.spec?.type ?? serviceType; const lifecycle = settings.spec?.life_cycle ?? defaultLifecycle; const owner = settings.spec?.owner ?? defaultOwner; + staticFiles.replaceModulesPath((path) => + path.replace(templateFileName, destinationFileName) + ); + staticFiles.replaceModulesCode((_path, code) => code .replace(metadataNameKey, name) @@ -105,11 +127,13 @@ class BackstageCatalogEntityPlugin implements AmplicationPlugin { .replace(metadataLabelsKey, labels) .replace(metadataAnnotationsKey, annotations) .replace(metadataTagsKey, tags) + .replace(appUrlKey, appUrl) .replace(specTypeKey, type) .replace(specLifecycleKey, lifecycle) .replace(specOwnerKey, owner) ); + await modules.merge(staticFiles); context.logger.info(`Generated ${pluginDescription}...`); return modules; } diff --git a/plugins/backstage-catalog-entity/src/static/template.yaml b/plugins/backstage-catalog-entity/src/static/template.yaml index 12a6993b..dded982f 100644 --- a/plugins/backstage-catalog-entity/src/static/template.yaml +++ b/plugins/backstage-catalog-entity/src/static/template.yaml @@ -3,17 +3,12 @@ kind: Component metadata: name: ${{ METADATA_NAME }} description: ${{ METADATA_DESCRIPTION }} - labels: - ${{ METADATA_LABELS_CUSTOM }} - annotations: - ${{ METADATA_ANNOTATIONS }} - github.com/project-slug: [GitHub repo of the service] - tags: - ${{ METDATA_TAGS }} + ${{ METADATA_LABELS }} + ${{ METADATA_ANNOTATIONS }} + ${{ METADATA_TAGS }} links: - - url: https://app.amplication.com/${{ WORKSPACE_ID }}/${{ PROJECT_ID }}/${{ SERVICE_ID }} + - url: ${{ APP_URL }} title: Amplication Platform - icon: [Amplication icon - The icon field value is meant to be a semantic key that will map to a specific icon that may be provided by an icon library (e.g. material-ui icons)] spec: type: ${{ SPEC_TYPE }} lifecycle: ${{ SPEC_LIFECYCLE }}