Skip to content

Commit 984acbd

Browse files
Feat/always latest delete comment (#184)
* feat: add deleteComment function to GitHub client and update comment handling logic
1 parent 90437bf commit 984acbd

File tree

6 files changed

+51
-9
lines changed

6 files changed

+51
-9
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,15 +223,15 @@ Requires a `GITHUB_TOKEN` with issue or pull request write permission.
223223

224224
### Comment Management Inputs
225225

226-
`--update-comment` An existing tagged comment is found, the new report is
226+
`update-comment` An existing tagged comment is found, the new report is
227227
appended to it. Otherwise, a new comment is created.
228228

229-
`--overwrite-comment` An existing tagged comment is found, that comment's entire
229+
`overwrite-comment` An existing tagged comment is found, that comment's entire
230230
content is replaced with the new report. Otherwise, a new comment is created.
231231

232-
`--always-latest-comment` Create a new comment if the existing comment is not the latest in the thread. Use with `--update-comment` or `--overwrite-comment`. Default is false
232+
`always-latest-comment` Create a new comment if the existing comment is not the latest in the thread. Use with `update-comment` or `overwrite-comment`. Default is false
233233

234-
`--comment-tag` A unique identifier for comments posted. Used to find and
234+
`comment-tag` A unique identifier for comments posted. Used to find and
235235
update/overwrite existing comments.
236236

237237
For example, the following command creates or updates a comment tagged with the

badges/coverage.svg

Lines changed: 1 addition & 1 deletion
Loading

dist/index.js

Lines changed: 19 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/client/github/issues.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,26 @@ export async function listComments(
8686
})
8787
return response.data
8888
}
89+
90+
/**
91+
* Deletes a comment on a specified pull request on GitHub.
92+
*
93+
* @param comment_id - The ID number of the comment
94+
* @param owner - The owner of the repository (organization or user).
95+
* @param repo - The name of the repository.
96+
* @param issue_number - The pull request number to which the comment will be added.
97+
*/
98+
export async function deleteComment(
99+
comment_id: number,
100+
owner: string,
101+
repo: string,
102+
issue_number: number
103+
): Promise<void> {
104+
const octokit = await createGitHubClient()
105+
await octokit.issues.deleteComment({
106+
comment_id,
107+
owner,
108+
repo,
109+
issue_number
110+
})
111+
}

src/github/handler.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import { context } from '@actions/github'
33
import {
44
updateComment,
55
listComments,
6-
addCommentToIssue
6+
addCommentToIssue,
7+
deleteComment
78
} from '../client/github'
89
import { CtrfReport, Inputs } from '../types'
910
import { generateViews, annotateFailed } from './core'
@@ -130,6 +131,7 @@ export async function handleComment(
130131

131132
if (updateConfig.alwaysLatestComment && existingComment && !isLatest) {
132133
await addCommentToIssue(owner, repo, issue_number, `${body}\n${marker}`)
134+
await deleteComment(existingComment.id, owner, repo, issue_number)
133135
return
134136
}
135137

@@ -208,7 +210,6 @@ async function postOrUpdatePRComment(
208210
'For forked PRs, you should use the pull_request_target event instead of pull_request.'
209211
)
210212
} else if (error instanceof Error) {
211-
// Log other errors
212213
core.warning(`Failed to post PR comment: ${error.message}`)
213214
}
214215
}

0 commit comments

Comments
 (0)