Skip to content

Commit

Permalink
feat: ignore diff for certain stages (#16)
Browse files Browse the repository at this point in the history
Fixes #
  • Loading branch information
corymhall authored Nov 17, 2023
1 parent b3534f5 commit e24b0ba
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 10 deletions.
10 changes: 10 additions & 0 deletions .projenrc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ const project = new GitHubActionTypeScriptProject({
required: false,
default: 'true',
},
noDiffForStages: {
description: 'List of stages to ignore and not show a diff for',
required: false,
default: '',
},
noFailOnDestructiveChanges: {
description: '',
required: false,
default: 'List of stages where breaking changes will not fail the build',
},
},
runs: {
using: RunsUsing.NODE_16, // overwrite to node20
Expand Down
8 changes: 8 additions & 0 deletions action.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 11 additions & 4 deletions dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export async function run() {
allowedDestroyTypes: getInput('allowedDestroyTypes').split(','),
failOnDestructiveChanges: getBooleanInput('failOnDestructiveChanges'),
githubToken: getInput('githubToken'),
noDiffForStages: getInput('noDiffForStages').split(','),
noFailOnDestructiveChanges: getInput('noFailOnDestructiveChanges').split(','),
};
const octokit = github.getOctokit(inputs.githubToken);
const context = github.context;
Expand Down
14 changes: 14 additions & 0 deletions src/inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,18 @@ export interface Inputs {
* @default true
*/
failOnDestructiveChanges: boolean;

/**
* List of stages to ignore and not show a diff for
*
* @default - show diff for all stages
*/
noDiffForStages: string[];

/**
* List of stages where breaking changes will not fail the build
*
* @default - breaking changes on any stage will fail the build
*/
noFailOnDestructiveChanges: string[];
}
9 changes: 7 additions & 2 deletions src/stage-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,15 @@ export class StageProcessor {
* Process all of the stages. Once this has been run
* the comment can be created with `commentStages()`
*/
public async processStages() {
public async processStages(ignoreDestructiveChanges: string[] = []) {
for (const stage of this.stages) {
for (const stack of stage.stacks) {
try {
const { comment, changes } = await this.diffStack(stack);
this.stageComments[stage.name].comment.push(...comment);
this.stageComments[stage.name].destructiveChanges += changes;
if (!ignoreDestructiveChanges.includes(stage.name)) {
this.stageComments[stage.name].destructiveChanges += changes;
}
} catch (e: any) {
console.error('Error processing stages: ', e);
throw e;
Expand Down Expand Up @@ -144,16 +146,19 @@ export class StageProcessor {
'',
]);
if (changes.destructiveChanges.length) {
output.push('');
output.push('> [!WARNING]\n> ***Destructive Changes*** :bangbang:'),
changes.destructiveChanges.forEach(change => {
output.push(
`> **Stack: ${change.stackName} - Resource: ${change.logicalId} - Impact:** ***${change.impact}***`,
);
output.push('');
});
}
const writable = new StringWritable({});
formatDifferences(writable, diff);

output.push('');
output.push('```shell');
output.push(writable.data);
output.push('```');
Expand Down
6 changes: 3 additions & 3 deletions test/assembly.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import * as path from 'path';
import mockfs from 'mock-fs';
import mock from 'mock-fs';
import { AssemblyManifestReader } from '../src/assembly';

describe('cloud assembly manifest reader', () => {
const manifestFile = 'cdk.out/manifest.json';
const lookupRoleArn = 'arn:${AWS::Partition}:iam::123456789012:role/cdk-hnb659fds-lookup-role-123456789012-us-east-1';
beforeEach(() => {
mockfs({
mock({
['cdk.out']: {
['assembly-SomeStage']: {
['manifest.json']: JSON.stringify({
Expand Down Expand Up @@ -62,7 +62,7 @@ describe('cloud assembly manifest reader', () => {
});

afterEach(() => {
mockfs.restore();
mock.restore();
});

test('can read manifest from file', () => {
Expand Down
32 changes: 32 additions & 0 deletions test/stage-processor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,36 @@ describe('StageProcessor', () => {
expect(p.Stage1.destructiveChanges).toEqual(1);
});

test('stage with destructive changes-ignored', async () => {
cfnMock.on(GetTemplateCommand)
.resolves({
TemplateBody: JSON.stringify(stackInfo.content),
});
const processor = new StageProcessor([
{
name: 'Stage1',
stacks: [{
name: 'my-stack',
content: {
Resources: {
MyRole: {
Type: 'AWS::IAM::Role',
Properties: {
RoleName: 'MyNewCustomName2',
},
},
},
},
}],
},
], []);
await processor.processStages(['Stage1']);
const p = (processor as any).stageComments;
expect(p).toEqual({
Stage1: expect.any(Object),
});
expect(p.Stage1.comment.length).not.toEqual(0);
expect(p.Stage1.destructiveChanges).toEqual(0);
});

});

0 comments on commit e24b0ba

Please sign in to comment.