Skip to content

Commit

Permalink
feat: Do not post comments when there are no changes (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
shapiy authored Jul 26, 2024
1 parent 3fd89bb commit de3ee1d
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 20 deletions.
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

0 comments on commit de3ee1d

Please sign in to comment.