From c3271dbe0922d3b2caaf7edd03513da2b379a3c1 Mon Sep 17 00:00:00 2001 From: Mike Landi Date: Thu, 30 May 2024 17:02:48 -0400 Subject: [PATCH] feat(launch): update the launch blueprint to load options from repository contents --- .../blueprints/launch-blueprint/package.json | 2 +- .../launch-blueprint/src/blueprint.ts | 77 ++++++++++++------- .../launch-blueprint/src/defaults.json | 1 - 3 files changed, 50 insertions(+), 30 deletions(-) diff --git a/packages/blueprints/launch-blueprint/package.json b/packages/blueprints/launch-blueprint/package.json index 2064d7999..bed73e5a6 100644 --- a/packages/blueprints/launch-blueprint/package.json +++ b/packages/blueprints/launch-blueprint/package.json @@ -72,7 +72,7 @@ "publishConfig": { "access": "public" }, - "version": "0.4.29", + "version": "1.0.4", "types": "lib/index.d.ts", "publishingSpace": "blueprints", "displayName": "Launch with CodeCatalyst", diff --git a/packages/blueprints/launch-blueprint/src/blueprint.ts b/packages/blueprints/launch-blueprint/src/blueprint.ts index e89742cb5..05f9b73c2 100644 --- a/packages/blueprints/launch-blueprint/src/blueprint.ts +++ b/packages/blueprints/launch-blueprint/src/blueprint.ts @@ -4,7 +4,14 @@ import path from 'path'; import { EnvironmentDefinition, AccountConnection, Role, Environment } from '@amazon-codecatalyst/blueprint-component.environments'; import { SourceRepository } from '@amazon-codecatalyst/blueprint-component.source-repositories'; import { ConnectionDefinition, InputVariable, WorkflowDefinition, WorkflowEnvironment } from '@amazon-codecatalyst/blueprint-component.workflows'; -import { DynamicKVInput, Blueprint as ParentBlueprint, Options as ParentOptions, Selector, Tuple } from '@amazon-codecatalyst/blueprints.blueprint'; +import { + KVSchema, + OptionsSchema, + OptionsSchemaDefinition, + Blueprint as ParentBlueprint, + Options as ParentOptions, + Selector, +} from '@amazon-codecatalyst/blueprints.blueprint'; import * as yaml from 'yaml'; // eslint-disable-next-line import/no-extraneous-dependencies import defaults from './defaults.json'; @@ -18,20 +25,20 @@ export interface Options extends ParentOptions { sourceRepository: string; /** - * This is the branch to clone from sourceRepository. - * @validationRegex /^.*$/ - * @hidden + * This is the name of the destination repository to store your cloned copy of the code. */ - sourceBranch?: string; + destinationRepositoryName: Selector; /** - * This is the name of the destination repository to store your cloned copy of the code. + * This is the branch to clone from sourceRepository. * @validationRegex /^.*$/ + * @hidden */ - destinationRepositoryName: Selector; + sourceBranch?: string; /** * @showName readOnly + * @deprecated environments will be removed in a future release and can be embedded in parameters: https://github.com/aws/codecatalyst-blueprints/pull/565 */ environments: EnvironmentDefinition<{ /** @@ -50,19 +57,9 @@ export interface Options extends ParentOptions { }>; }>[]; - /** - * @readOnly - * @deprecated use `paremeters` property instead - */ - options: Tuple<[string, string]>[]; - - /** - * @readOnly - */ - parameters: DynamicKVInput[]; + parameters: OptionsSchemaDefinition<'launch-options', KVSchema, KVSchema>; } -const OPTIONS_PREFIX = 'LAUNCH_OPTIONS_'; const GIT_CLONE_TIMEOUT = 30_000; /** @@ -86,9 +83,8 @@ export class Blueprint extends ParentBlueprint { }; const options = Object.assign(typeCheck, options_); - const repository = new SourceRepository(this, { - title: options.destinationRepositoryName, + title: options.destinationRepositoryName ?? 'launch-with-codecatalyst', }); // create environments @@ -124,6 +120,13 @@ export class Blueprint extends ParentBlueprint { fs.cpSync(pathToRepository, this.state.repository.path, { recursive: true }); + //register options to blueprint + const embeddedOptionsPath = path.join(this.state.repository.path, '.codecatalyst', 'launch-options.yaml'); + if (fs.existsSync(embeddedOptionsPath)) { + const embeddedOptions = yaml.parse(fs.readFileSync(embeddedOptionsPath).toString()) as { options: KVSchema }; + new OptionsSchema(this, 'launch-options', embeddedOptions.options); + } + //map options and environments to workflows const workflowPath = path.join(this.state.repository.path, '.codecatalyst', 'workflows'); if (fs.existsSync(workflowPath)) { @@ -156,14 +159,10 @@ export class Blueprint extends ParentBlueprint { //set variables with options where applicable const variables = action.Inputs?.Variables as InputVariable[] | undefined; for (const variable of variables ?? []) { - if (variable?.Name?.startsWith(OPTIONS_PREFIX)) { - const optionName = (variable.Name as string).replace(OPTIONS_PREFIX, ''); - const specifiedValue = - this.state.options.parameters.find(parameter => parameter.key == optionName)?.value ?? - this.state.options.options.find(option => option[0] == optionName)?.[1]; - if (specifiedValue) { - variable.Value = specifiedValue.toString(); - } + const optionName = variable.Name as string; + const specifiedValue = this.state.options.parameters?.find(parameter => parameter.key == optionName)?.value; + if (specifiedValue) { + variable.Value = specifiedValue.toString(); } } @@ -184,5 +183,27 @@ export class Blueprint extends ParentBlueprint { ]; } } + + //set parameter overrides + if (action.Identifier?.startsWith('aws/cfn-deploy@')) { + const overrides = action.Configuration?.['parameter-overrides']; + if (overrides) { + const parameters: { key: string; value: string }[] = overrides.split(',').map((p: string) => { + const tuple = p.split('='); + return { key: tuple[0], value: tuple[1] }; + }); + + let newOverrides = ''; + for (const parameter of parameters) { + const override = this.state.options.parameters?.find(option => option.key == parameter.key)?.value; + newOverrides += `${parameter.key}=${override ?? parameter.value},`; + } + + //remove trailing comma and overwrite + if (newOverrides) { + action.Configuration['parameter-overrides'] = newOverrides.substring(0, newOverrides.length - 1); + } + } + } } } diff --git a/packages/blueprints/launch-blueprint/src/defaults.json b/packages/blueprints/launch-blueprint/src/defaults.json index 4c9744337..9dca65830 100644 --- a/packages/blueprints/launch-blueprint/src/defaults.json +++ b/packages/blueprints/launch-blueprint/src/defaults.json @@ -2,6 +2,5 @@ "sourceRepository": "https://github.com/aws-solutions/qnabot-on-aws", "destinationRepositoryName": "launch-with-codecatalyst", "environments": [], - "options": [], "parameters": [] }