Skip to content

Commit c1cab81

Browse files
heiskrCopilotCopilot
authored
Delete stale PR link-checker comment when links are clean (#61993)
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent 26dcde1 commit c1cab81

1 file changed

Lines changed: 30 additions & 8 deletions

File tree

src/links/scripts/check-links-pr.ts

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -177,21 +177,35 @@ async function commentOnPR(brokenLinks: BrokenLink[], actionUrl?: string) {
177177
const octokit = github()
178178
const comment = generatePRComment(brokenLinks, { actionUrl })
179179

180-
if (!comment) {
181-
console.log('No broken links to report')
182-
return
183-
}
184-
185-
// Find existing comment
180+
// Find any existing comment we previously posted (identified by the hidden marker)
181+
const marker = '<!-- link-checker-pr-comment -->'
186182
const { data: comments } = await octokit.rest.issues.listComments({
187183
owner,
188184
repo,
189185
issue_number: pullNumber,
190186
})
191-
192-
const marker = '<!-- link-checker-pr-comment -->'
193187
const existingComment = comments.find((c) => c.body?.includes(marker))
194188

189+
if (!comment) {
190+
console.log('No broken links to report')
191+
// Links are now clean: remove any stale comment from an earlier commit.
192+
// Best-effort: a concurrent run may have already deleted it (404), and
193+
// cleanup should never turn an otherwise-passing run into a failure.
194+
if (existingComment) {
195+
try {
196+
await octokit.rest.issues.deleteComment({
197+
owner,
198+
repo,
199+
comment_id: existingComment.id,
200+
})
201+
console.log(`Deleted stale PR comment: ${existingComment.id}`)
202+
} catch (err) {
203+
console.warn(`Could not delete stale PR comment ${existingComment.id}:`, err)
204+
}
205+
}
206+
return
207+
}
208+
195209
if (existingComment) {
196210
await octokit.rest.issues.updateComment({
197211
owner,
@@ -287,6 +301,14 @@ async function main() {
287301

288302
if (allBrokenLinks.length === 0 && allRedirectLinks.length === 0) {
289303
console.log(chalk.green('✅ All links valid!'))
304+
// Remove any stale comment posted on an earlier commit, now that links are clean
305+
if (process.env.SHOULD_COMMENT === 'true') {
306+
try {
307+
await commentOnPR([], process.env.ACTION_RUN_URL)
308+
} catch (err) {
309+
console.warn('Could not clean up stale PR comment:', err)
310+
}
311+
}
290312
process.exit(0)
291313
}
292314

0 commit comments

Comments
 (0)