Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync fork #73

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/auto-approve.yml

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

2 changes: 1 addition & 1 deletion .projenrc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const project = new GitHubActionTypeScriptProject({
},
autoApproveOptions: {
label: 'auto-approve',
allowedUsernames: ['corymhall'],
allowedUsernames: ['corymhall', 'shapiy'],
},
actionMetadata: {
author: 'Cory Hall',
Expand Down
42 changes: 34 additions & 8 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.

12 changes: 12 additions & 0 deletions src/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,16 @@ export class Comments {
issue_number: this.issueNumber,
});
}

/**
* Delete the comment
*
* @param commentId the id of the comment to delete
*/
public async deleteComment(commentId: number) {
await this.octokit.rest.issues.deleteComment({
...this.context.repo,
comment_id: commentId,
});
}
}
39 changes: 29 additions & 10 deletions src/stage-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ interface StageComment {
* The number of destructive changes in this stage
*/
destructiveChanges: number;

/**
* The number of all changes in this stage
*/
totalChanges: number;
}

/**
Expand All @@ -37,12 +42,14 @@ interface StageComment {
*/
export class StageProcessor {
private readonly stageComments: { [stageName: string]: StageComment } = {};

constructor(
private readonly stages: StageInfo[],
private readonly allowedDestroyTypes: string[],
) {
this.stages.forEach(stage => {
this.stageComments[stage.name] = {
totalChanges: 0,
destructiveChanges: 0,
stackComments: stage.stacks.reduce((prev, curr) => {
prev[curr.name] = [];
Expand Down Expand Up @@ -74,6 +81,7 @@ export class StageProcessor {
try {
const { comment, changes } = await this.diffStack(stack);
this.stageComments[stage.name].stackComments[stack.name].push(...comment);
this.stageComments[stage.name].totalChanges += changes;
if (!ignoreDestructiveChanges.includes(stage.name)) {
this.stageComments[stage.name].destructiveChanges += changes;
}
Expand All @@ -85,7 +93,7 @@ export class StageProcessor {
}
}

private async commentStacks(comments: Comments) {
private async commentStacks(comments: Comments, totalChanges: number) {
for (const [stageName, stage] of Object.entries(this.stageComments)) {
for (const [stackName, comment] of Object.entries(stage.stackComments)) {
const hash = md5Hash(JSON.stringify({
Expand All @@ -97,7 +105,12 @@ export class StageProcessor {
throw new Error(`Comment for stack ${stackName} is too long, please report this as a bug https://github.com/corymhall/cdk-diff-action/issues/new`);
}
const previous = await comments.findPrevious(hash);
if (previous) {
if (totalChanges === 0) {
if (previous) {
await comments.deleteComment(previous);
}
// Do not post a comment if there were no changes.
} else if (previous) {
await comments.updateComment(previous, hash, stackComment);
} else {
await comments.createComment(hash, stackComment);
Expand All @@ -106,14 +119,18 @@ export class StageProcessor {
}
}

private async commentStage(comments: Comments, hash: string, comment: string[]) {
private async commentStage(comments: Comments, hash: string, comment: string[], totalChanges: number) {
const previous = await comments.findPrevious(hash);
if (previous) {
if (totalChanges === 0) {
if (previous) {
await comments.deleteComment(previous);
}
// Do not post a comment if there were no changes.
} else if (previous) {
await comments.updateComment(previous, hash, comment);
} else {
await comments.createComment(hash, comment);
}

}

/**
Expand All @@ -125,10 +142,11 @@ export class StageProcessor {
public async commentStages(comments: Comments) {
for (const [stageName, info] of Object.entries(this.stageComments)) {
const stageComment = this.getCommentForStage(stageName);
let totalChanges = this.stageComments[stageName].totalChanges;
if (stageComment.join('\n').length > MAX_COMMENT_LENGTH) {
await this.commentStacks(comments);
await this.commentStacks(comments, totalChanges);
} else {
await this.commentStage(comments, info.hash, stageComment);
await this.commentStage(comments, info.hash, stageComment, totalChanges);
}
}
}
Expand Down Expand Up @@ -179,9 +197,9 @@ export class StageProcessor {
return output;
}
output.push(...[
`#### Diff for stack: ${stackName} - `+
`***${changes.createdResources} to add, ${changes.updatedResources} to update, ${changes.removedResources} to destroy*** `+
emoji,
`#### Diff for stack: ${stackName} - ` +
`***${changes.createdResources} to add, ${changes.updatedResources} to update, ${changes.removedResources} to destroy*** ` +
emoji,
'<details><summary>Details</summary>',
'',
]);
Expand Down Expand Up @@ -243,6 +261,7 @@ export class StageProcessor {
class StringWritable extends Writable {
public data: string;
private _decoder: StringDecoder;

constructor(options: WritableOptions) {
super(options);
this._decoder = new StringDecoder();
Expand Down
5 changes: 4 additions & 1 deletion test/stage-processor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ import { StageProcessor } from '../src/stage-processor';
let findPreviousMock = jest.fn();
let updateCommentMock = jest.fn();
let createCommentMock = jest.fn();
let deleteCommentMock = jest.fn();
jest.mock('../src/comment', () => {
return {
Comments: jest.fn().mockImplementation(() => {
return {
findPrevious: findPreviousMock,
updateComment: updateCommentMock,
createComment: createCommentMock,
deleteComment: deleteCommentMock,
};
}),
};
Expand Down Expand Up @@ -237,7 +239,8 @@ describe('StageProcessor', () => {
await processor.commentStages(new Comments({} as any, {} as any));
expect(findPreviousMock).toHaveBeenCalledTimes(10);
expect(createCommentMock).toHaveBeenCalledTimes(0);
expect(updateCommentMock).toHaveBeenCalledTimes(10);
expect(updateCommentMock).toHaveBeenCalledTimes(0);
expect(deleteCommentMock).toHaveBeenCalledTimes(10);
});
});

Expand Down
6 changes: 3 additions & 3 deletions yarn.lock

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

Loading