From ffda02e12e505e459859dcc6b4ce1273b246a483 Mon Sep 17 00:00:00 2001 From: James Date: Thu, 19 Jun 2025 08:52:44 +0100 Subject: [PATCH] fix: delete comment if new comment is empty --- src/comment.ts | 16 +++++++++++++++- src/run.ts | 18 +++++++++++------- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/comment.ts b/src/comment.ts index f69efab9..8edd41d6 100644 --- a/src/comment.ts +++ b/src/comment.ts @@ -9,6 +9,8 @@ type Comment = { export type UpdateIfExistsType = 'create' | 'replace' | 'append' | 'recreate' +export const createCommentKey = (key: string): string => `` + export const addComment = async (github: GitHubContext, comment: Comment): Promise => { if (!github.issueNumber) { core.info(`Ignored non pull request event: ${github.eventName}`) @@ -31,7 +33,7 @@ export const addComment = async (github: GitHubContext, comment: Comment): Promi return } - const commentKey = `` + const commentKey = createCommentKey(comment.updateIfExistsKey) core.info(`Finding key ${commentKey} from comments in #${github.issueNumber}`) const existingComment = await findComment(github, commentKey) if (!existingComment) { @@ -107,3 +109,15 @@ const findComment = async (github: GitHubContext, key: string): Promise => { + const comment = await findComment(github, createCommentKey(updateIfExistsKey)) + + if (comment) { + await github.octokit.rest.issues.deleteComment({ + owner: github.owner, + repo: github.repo, + comment_id: comment.id, + }) + } +} diff --git a/src/run.ts b/src/run.ts index e1ff1faf..d4fe304e 100644 --- a/src/run.ts +++ b/src/run.ts @@ -2,7 +2,7 @@ import * as core from '@actions/core' import { GitHubContext } from './github.js' import { computeDiff, showColorDiff } from './diff.js' import { addLabels, removeLabels } from './label.js' -import { UpdateIfExistsType, addComment } from './comment.js' +import { UpdateIfExistsType, addComment, deleteCommentIfExists } from './comment.js' import { formatComment } from './format.js' type Inputs = { @@ -33,12 +33,16 @@ export const run = async (github: GitHubContext, inputs: Inputs): Promise 0) { - await addComment(github, { - body: `${inputs.commentHeader}\n\n${commentBody}\n\n${inputs.commentFooter}`, - updateIfExists: inputs.updateIfExists, - updateIfExistsKey: inputs.updateIfExistsKey, - }) + if (inputs.comment) { + if (commentBody.length === 0) { + await deleteCommentIfExists(github, inputs.updateIfExistsKey) + } else { + await addComment(github, { + body: `${inputs.commentHeader}\n\n${commentBody}\n\n${inputs.commentFooter}`, + updateIfExists: inputs.updateIfExists, + updateIfExistsKey: inputs.updateIfExistsKey, + }) + } } if (diffs.length > 0) {