Skip to content

Commit

Permalink
chore: adding debug logs
Browse files Browse the repository at this point in the history
  • Loading branch information
corymhall committed Oct 30, 2023
1 parent 72862f8 commit 1e1cb91
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 67 deletions.
104 changes: 70 additions & 34 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.

46 changes: 32 additions & 14 deletions src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,38 @@ export async function run() {
const octokit = github.getOctokit(inputs.githubToken);
const context = github.context;

const assembly = AssemblyManifestReader.fromPath('cdk.out');
let stages = assembly.stages;
if (!stages.length) {
stages = [{
name: 'DefaultStage',
stacks: assembly.stacks,
}];
}
const comments = new Comments(octokit, context);
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.');
try {
const assembly = AssemblyManifestReader.fromPath('cdk.out');
let stages = assembly.stages;
if (!stages.length) {
stages = [{
name: 'DefaultStage',
stacks: assembly.stacks,
}];
}
const comments = new Comments(octokit, context);
const processor = new StageProcessor(stages, inputs.allowedDestroyTypes);
try {
await processor.processStages();
} catch (e: any) {
console.error('Error running process stages: ', e);
throw e;
}

try {
await processor.commentStages(comments);

} catch (e: any) {
console.error('Error commenting stages: ', e);
throw e;
}

if (processor.hasDestructiveChanges && inputs.failOnDestructiveChanges) {
throw new Error('There are destructive changes! See PR comment for details.');
}
} catch (e: any) {
console.error('Error performing diff: ', e);
throw e;
}
return;
}
Expand Down
53 changes: 35 additions & 18 deletions src/diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,21 @@ export class StackDiff {
const cmd = new GetTemplateCommand({
StackName: this.stack.name,
});
const res = await this.client.send(cmd);
const newTemplate = res.TemplateBody ?
JSON.parse(res.TemplateBody) : {};
const diff = diffTemplate(newTemplate, JSON.parse(this.stack.content));
const changes = this.evaluateDiff(this.stack.name, diff);
return {
diff,
destructiveChanges: changes,
};
try {
const res = await this.client.send(cmd);
const newTemplate = res.TemplateBody ?
JSON.parse(res.TemplateBody) : {};
const diff = diffTemplate(newTemplate, JSON.parse(this.stack.content));
const changes = this.evaluateDiff(this.stack.name, diff);
return {
diff,
destructiveChanges: changes,
};

} catch (e: any) {
console.error('Error getting remote template: ', e);
throw e;
}
}

private evaluateDiff(templateId: string, templateDiff: TemplateDiff): DestructiveChange[] {
Expand Down Expand Up @@ -130,9 +136,14 @@ export class StageProcessor {
public async processStages() {
for (const stage of this.stages) {
for (const stack of stage.stacks) {
const { comment, changes } = await this.diffStack(stack);
this.stageComments[stage.name].comment.push(...comment);
this.stageComments[stage.name].destructiveChanges += changes;
try {
const { comment, changes } = await this.diffStack(stack);
this.stageComments[stage.name].comment.push(...comment);
this.stageComments[stage.name].destructiveChanges += changes;
} catch (e: any) {
console.error('Error processing stages: ', e);
throw e;
}
}
}
}
Expand All @@ -147,12 +158,18 @@ export class StageProcessor {
}

private async diffStack(stack: StackInfo): Promise<{comment: string[]; changes: number}> {
const stackDiff = new StackDiff(stack, this.allowedDestroyTypes);
const { diff, destructiveChanges } = await stackDiff.diffStack();
return {
comment: this.formatStackComment(stack.name, diff, destructiveChanges),
changes: destructiveChanges.length,
};
try {
const stackDiff = new StackDiff(stack, this.allowedDestroyTypes);
const { diff, destructiveChanges } = await stackDiff.diffStack();
return {
comment: this.formatStackComment(stack.name, diff, destructiveChanges),
changes: destructiveChanges.length,
};

} catch (e: any) {
console.error('Error performing stack diff: ', e);
throw e;
}
}

private formatStackComment(stackName: string, diff: TemplateDiff, changes: DestructiveChange[]): string[] {
Expand Down

0 comments on commit 1e1cb91

Please sign in to comment.