Attempting to move templateInfo to caller

Signed-off-by: Mark Foresta <Mark.Foresta691@gmail.com>
This commit is contained in:
Mark Foresta
2024-09-21 12:19:15 -04:00
committed by blam
parent 4bcccb1620
commit 2603968870
2 changed files with 38 additions and 19 deletions
@@ -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),
@@ -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'),