From fbfddd21583a812e78766b202354cb88372a43f6 Mon Sep 17 00:00:00 2001 From: Colen Garoutte-Carson <49173979+Colengms@users.noreply.github.com> Date: Wed, 21 Aug 2024 14:32:58 -0700 Subject: [PATCH] Prevent AddComment action from adding redundant comments (#12611) --- .github/actions/AddComment/AddComment.js | 16 ++++++++++++++++ .github/actions/AddComment/AddComment.ts | 19 ++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/.github/actions/AddComment/AddComment.js b/.github/actions/AddComment/AddComment.js index ffcf3a1343..f621a4f7dd 100644 --- a/.github/actions/AddComment/AddComment.js +++ b/.github/actions/AddComment/AddComment.js @@ -31,6 +31,22 @@ class AddComment extends ActionBase_1.ActionBase { if (hydrated.open && this.validateIssue(hydrated) // TODO: Verify updated timestamp ) { + // Don't add a comment if already commented on by an action. + let foundActionComment = false; + for await (const commentBatch of issue.getComments()) { + for (const comment of commentBatch) { + if (comment.author.isGitHubApp) { + foundActionComment = true; + break; + } + } + if (foundActionComment) + break; + } + if (foundActionComment) { + (0, utils_1.safeLog)(`Issue ${hydrated.number} already commented on by an action. Ignoring.`); + continue; + } if (this.addComment) { (0, utils_1.safeLog)(`Posting comment on issue ${hydrated.number}`); await issue.postComment(this.addComment); diff --git a/.github/actions/AddComment/AddComment.ts b/.github/actions/AddComment/AddComment.ts index e21bf213a3..9dd36e1bb2 100644 --- a/.github/actions/AddComment/AddComment.ts +++ b/.github/actions/AddComment/AddComment.ts @@ -10,7 +10,7 @@ import { daysAgoToHumanReadbleDate, daysAgoToTimestamp, safeLog } from '../commo export class AddComment extends ActionBase { constructor( private github: GitHub, - private createdAfter: string, + private createdAfter: string | undefined, private afterDays: number, labels: string, private addComment: string, @@ -45,6 +45,23 @@ export class AddComment extends ActionBase { if (hydrated.open && this.validateIssue(hydrated) // TODO: Verify updated timestamp ) { + // Don't add a comment if already commented on by an action. + let foundActionComment = false; + for await (const commentBatch of issue.getComments()) { + for (const comment of commentBatch) { + if (comment.author.isGitHubApp) { + foundActionComment = true; + break; + } + } + if (foundActionComment) + break; + } + if (foundActionComment) { + safeLog(`Issue ${hydrated.number} already commented on by an action. Ignoring.`); + continue; + } + if (this.addComment) { safeLog(`Posting comment on issue ${hydrated.number}`); await issue.postComment(this.addComment);