From fd6b6fd5fededfb654cc23226263e724e2056860 Mon Sep 17 00:00:00 2001 From: corymhall <43035978+corymhall@users.noreply.github.com> Date: Mon, 30 Oct 2023 13:06:07 -0400 Subject: [PATCH] feat: fail action if there are destructive changes --- .projenrc.ts | 2 +- action.yml | 2 +- src/action.ts | 3 +++ src/diff.ts | 9 +++++++++ 4 files changed, 14 insertions(+), 2 deletions(-) diff --git a/.projenrc.ts b/.projenrc.ts index 5be2935..2dc8c43 100644 --- a/.projenrc.ts +++ b/.projenrc.ts @@ -73,5 +73,5 @@ jestConfig?.patch(JsonPatch.add('/transform', { '^.+\\.(t|j)sx?$': new Transform('@swc/jest'), })); const actionYml = project.tryFindObjectFile('action.yml'); -actionYml?.addOverride('runs.using', 'node18'); +actionYml?.addOverride('runs.using', 'node20'); project.synth(); diff --git a/action.yml b/action.yml index d489e83..f79c8c2 100644 --- a/action.yml +++ b/action.yml @@ -3,7 +3,7 @@ name: cdk-diff-action description: The CDK Diff GitHub Action allows you to run CDK diff as part of your CI/CD workflow. runs: - using: node18 + using: node20 main: dist/index.js author: Cory Hall inputs: diff --git a/src/action.ts b/src/action.ts index 83af964..f16ced0 100644 --- a/src/action.ts +++ b/src/action.ts @@ -26,6 +26,9 @@ export async function run() { const processor = new StageProcessor(stages, inputs.allowedDestroyTypes); await processor.processStages(); await processor.commentStages(comments); + if (processor.hasDestructiveChanges && inputs.failOnDestructiveChanges) { + throw new Error('There are destructive changes! See PR comment for details.'); + } return; } diff --git a/src/diff.ts b/src/diff.ts index be12c88..d1a022f 100644 --- a/src/diff.ts +++ b/src/diff.ts @@ -137,6 +137,15 @@ export class StageProcessor { } } + public get hasDestructiveChanges(): boolean { + for (const comments of Object.values(this.stageComments)) { + if (comments.destructiveChanges) { + return true; + } + } + return false; + } + private async diffStack(stack: StackInfo): Promise<{comment: string[]; changes: number}> { const stackDiff = new StackDiff(stack, this.allowedDestroyTypes); const { diff, destructiveChanges } = await stackDiff.diffStack();