From 9027550b84fcd9239620c0f19f363d5efe86e314 Mon Sep 17 00:00:00 2001 From: Taylor Reece Date: Tue, 30 May 2023 11:09:59 -0500 Subject: [PATCH] Add option to remove temporary integration and component after a component:dev:test (#22) * Add option to remove temporary integration and component after a component:dev:test * PR Feedback --- package-lock.json | 4 ++-- package.json | 4 ++-- src/commands/components/dev/test.ts | 23 +++++++++++++++++++- src/commands/instances/create.ts | 3 ++- src/commands/instances/update.ts | 14 ++++++++++-- src/utils/component/deleteByKey.ts | 33 +++++++++++++++++++++++++++++ src/utils/integration/invoke.ts | 17 +++++++++++++++ 7 files changed, 90 insertions(+), 8 deletions(-) create mode 100644 src/utils/component/deleteByKey.ts diff --git a/package-lock.json b/package-lock.json index b9f7f37..1ae0d4a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@prismatic-io/prism", - "version": "4.6.3", + "version": "4.6.4", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@prismatic-io/prism", - "version": "4.6.3", + "version": "4.6.4", "license": "MIT", "dependencies": { "@msgpack/msgpack": "2.3.0", diff --git a/package.json b/package.json index 5e16c50..214a32b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@prismatic-io/prism", - "version": "4.6.3", + "version": "4.6.4", "description": "Build, deploy, and support integrations in Prismatic from the comfort of your command line", "keywords": [ "prismatic", @@ -163,4 +163,4 @@ } } } -} \ No newline at end of file +} diff --git a/src/commands/components/dev/test.ts b/src/commands/components/dev/test.ts index 69572e4..c2fcbba 100644 --- a/src/commands/components/dev/test.ts +++ b/src/commands/components/dev/test.ts @@ -20,7 +20,10 @@ import { ComponentTestInfo, } from "../../../utils/integration/definition"; import { importDefinition } from "../../../utils/integration/import"; -import { runIntegrationFlow } from "../../../utils/integration/invoke"; +import { + deleteIntegration, + runIntegrationFlow, +} from "../../../utils/integration/invoke"; import { pollForActiveConfigVarState } from "../../../utils/integration/query"; import { Expression } from "../../../utils/integration/export"; import { exists } from "../../../fs"; @@ -30,6 +33,7 @@ import { printFinalStepResults, writeFinalStepResults, } from "../../../utils/execution/stepResults"; +import { deleteComponentByKey } from "../../../utils/component/deleteByKey"; const setTimeoutPromise = promisify(setTimeout); @@ -168,6 +172,12 @@ export default class TestCommand extends Command { default: false, description: "Print the results of the action to stdout", }), + "clean-up": Flags.boolean({ + required: false, + default: false, + description: + "Clean up the integration and temporary component after running the action", + }), }; async run() { @@ -177,6 +187,7 @@ export default class TestCommand extends Command { build, "output-file": outputFile, "print-results": printResults, + "clean-up": cleanUp, }, } = await this.parse(TestCommand); @@ -379,6 +390,16 @@ export default class TestCommand extends Command { await printFinalStepResults(executionId); } + if (cleanUp) { + CliUx.ux.action.start(`Cleaning up test Integration (${integrationId})`); + await deleteIntegration(integrationId); + CliUx.ux.action.stop(); + + CliUx.ux.action.start(`Cleaning up test component (${definition.key})`); + await deleteComponentByKey(definition.key); + CliUx.ux.action.stop(); + } + CliUx.ux.action.stop(); } } diff --git a/src/commands/instances/create.ts b/src/commands/instances/create.ts index bfc5694..b91d05b 100644 --- a/src/commands/instances/create.ts +++ b/src/commands/instances/create.ts @@ -13,7 +13,8 @@ export default class CreateCommand extends Command { integration: Flags.string({ char: "i", required: true, - description: "ID of the integration or a specific integration version ID this is an instance of", + description: + "ID of the integration or a specific integration version ID this is an instance of", }), customer: Flags.string({ char: "c", diff --git a/src/commands/instances/update.ts b/src/commands/instances/update.ts index 320da81..2835abd 100644 --- a/src/commands/instances/update.ts +++ b/src/commands/instances/update.ts @@ -36,9 +36,19 @@ export default class UpdateCommand extends Command { const result = await gqlRequest({ document: gql` - mutation updateInstance($id: ID!, $name: String, $description: String, $version: ID! ) { + mutation updateInstance( + $id: ID! + $name: String + $description: String + $version: ID! + ) { updateInstance( - input: { id: $id, name: $name, description: $description, integration: $version} + input: { + id: $id + name: $name + description: $description + integration: $version + } ) { instance { id diff --git a/src/utils/component/deleteByKey.ts b/src/utils/component/deleteByKey.ts new file mode 100644 index 0000000..87f7816 --- /dev/null +++ b/src/utils/component/deleteByKey.ts @@ -0,0 +1,33 @@ +import { gql, gqlRequest } from "../../graphql"; + +export const deleteComponentByKey = async (key: string) => { + // Fetch a component by key + const result = await gqlRequest({ + document: gql` + query component($key: String!) { + components(key: $key, public: false) { + nodes { + id + } + } + } + `, + variables: { + key, + }, + }); + // Delete the component by ID + await gqlRequest({ + document: gql` + mutation deleteComponent($id: ID!) { + deleteComponent(input: { id: $id }) { + errors { + field + messages + } + } + } + `, + variables: { id: result.components.nodes[0].id }, + }); +}; diff --git a/src/utils/integration/invoke.ts b/src/utils/integration/invoke.ts index d483512..adf85d5 100644 --- a/src/utils/integration/invoke.ts +++ b/src/utils/integration/invoke.ts @@ -49,6 +49,23 @@ export const getIntegrationFlow = async ( return flows[0].id; }; +/** Delete a specified integration by ID */ +export const deleteIntegration = async (integrationId: string) => { + await gqlRequest({ + document: gql` + mutation deleteIntegration($id: ID!) { + deleteIntegration(input: { id: $id }) { + errors { + field + messages + } + } + } + `, + variables: { id: integrationId }, + }); +}; + interface IntegrationFlowRunProps { integrationId: string; flowId?: string;