From 2603968870bc2c879fadc8feec6172855eda43fe Mon Sep 17 00:00:00 2001 From: Mark Foresta Date: Sat, 21 Sep 2024 12:19:15 -0400 Subject: [PATCH] Attempting to move templateInfo to caller Signed-off-by: Mark Foresta --- .../src/scaffolder/dryrun/createDryRunner.ts | 35 +++++++++---------- .../scaffolder-backend/src/service/router.ts | 22 +++++++++++- 2 files changed, 38 insertions(+), 19 deletions(-) diff --git a/plugins/scaffolder-backend/src/scaffolder/dryrun/createDryRunner.ts b/plugins/scaffolder-backend/src/scaffolder/dryrun/createDryRunner.ts index a2cb19ce33..7d62310920 100644 --- a/plugins/scaffolder-backend/src/scaffolder/dryrun/createDryRunner.ts +++ b/plugins/scaffolder-backend/src/scaffolder/dryrun/createDryRunner.ts @@ -15,10 +15,10 @@ */ import { ScmIntegrations } from '@backstage/integration'; -import { TaskSpec } from '@backstage/plugin-scaffolder-common'; +import { TaskSpec, TemplateInfo } from '@backstage/plugin-scaffolder-common'; import { JsonObject } from '@backstage/types'; import { v4 as uuid } from 'uuid'; -import { pathToFileURL } from 'url'; +import { fileURLToPath, pathToFileURL } from 'url'; import { Logger } from 'winston'; import { createTemplateAction, @@ -38,11 +38,12 @@ import { BackstageCredentials, resolveSafeChildPath, } from '@backstage/backend-plugin-api'; -import type { EntityMeta, UserEntity } from '@backstage/catalog-model'; +import type { UserEntity } from '@backstage/catalog-model'; +import { template } from 'lodash'; interface DryRunInput { spec: TaskSpec; - templateMetadata: EntityMeta; + templateInfo: TemplateInfo; secrets?: TaskSecrets; directoryContents: SerializedFile[]; credentials: BackstageCredentials; @@ -95,12 +96,18 @@ export function createDryRunner(options: TemplateTesterCreateOptions) { ]), }); - const dryRunId = uuid(); + // Extracting contentsPath and dryRunId from the baseUrl + const baseUrl = input.templateInfo.baseUrl; + if (!baseUrl) { + throw new Error('baseUrl is required'); + } + const basePath = fileURLToPath(new URL(baseUrl)); + const contentsPath = basePath.replace('template.yaml', ''); + const dryRunId = contentsPath + .replace(options.workingDirectory, '') + .replace('dry-run-content-', ''); + const log = new Array<{ body: JsonObject }>(); - const contentsPath = resolveSafeChildPath( - options.workingDirectory, - `dry-run-content-${dryRunId}`, - ); try { await deserializeDirectoryContents(contentsPath, input.directoryContents); @@ -117,15 +124,7 @@ export function createDryRunner(options: TemplateTesterCreateOptions) { action: 'dry-run:extract', }, ], - templateInfo: { - entityRef: 'template:default/dry-run', - entity: { - metadata: input.templateMetadata, - }, - baseUrl: pathToFileURL( - resolveSafeChildPath(contentsPath, 'template.yaml'), - ).toString(), - }, + templateInfo: input.templateInfo, }, secrets: input.secrets, getInitiatorCredentials: () => Promise.resolve(input.credentials), diff --git a/plugins/scaffolder-backend/src/service/router.ts b/plugins/scaffolder-backend/src/service/router.ts index 23c3988b0d..c62a187e1b 100644 --- a/plugins/scaffolder-backend/src/service/router.ts +++ b/plugins/scaffolder-backend/src/service/router.ts @@ -94,6 +94,7 @@ import { PermissionsService, SchedulerService, UrlReaderService, + resolveSafeChildPath, } from '@backstage/backend-plugin-api'; import { IdentityApi, @@ -105,6 +106,8 @@ import { AutocompleteHandler, WorkspaceProvider, } from '@backstage/plugin-scaffolder-node/alpha'; +import { pathToFileURL } from 'url'; +import { v4 as uuid } from 'uuid'; /** * @@ -814,6 +817,23 @@ export async function createRouter( name: step.name ?? step.action, })); + const dryRunId = uuid(); + const contentsPath = resolveSafeChildPath( + workingDirectory, + `dry-run-content-${dryRunId}`, + ); + console.log('Mark: contentsPath', contentsPath); + + const templateInfo = { + entityRef: 'template:default/dry-run', + entity: { + metadata: template.metadata, + }, + baseUrl: pathToFileURL( + resolveSafeChildPath(contentsPath, 'template.yaml'), + ).toString(), + }; + const result = await dryRunner({ spec: { apiVersion: template.apiVersion, @@ -825,7 +845,7 @@ export async function createRouter( ref: userEntityRef, }, }, - templateMetadata: template.metadata, + templateInfo: templateInfo, directoryContents: (body.directoryContents ?? []).map(file => ({ path: file.path, content: Buffer.from(file.base64Content, 'base64'),