使用GitHub Actions删除PR评论时出现404错误求助
解决GitHub Action删除PR评论时的404错误
问题核心原因
你碰到的404错误,本质是PR评论分两种类型,你用了错误的API端点去删除:
- Issue评论:属于PR关联的issue的普通评论,对应API端点是
/repos/{owner}/{repo}/issues/comments/{comment_id} - PR Review评论:属于PR代码审查时的行内评论,对应API端点是
/repos/{owner}/{repo}/pulls/comments/{comment_id}
gh pr view --json comments导出的评论列表会同时包含这两种评论,但你大概率统一用了issue评论的端点去删除Review评论,导致API找不到对应资源返回404。
解决方案
1. 先确认评论类型
打开导出的comments.json,查看每个评论的url字段:
- 若url包含
pulls/comments,属于Review评论 - 若包含
issues/comments,属于Issue评论
2. 修改github-script脚本,适配不同评论类型
下面是修正后的脚本,会自动识别评论类型并调用对应API端点删除:
const fs = require('fs'); const comments = JSON.parse(fs.readFileSync('comments.json', 'utf8')); for (const comment of comments) { // 匹配删除条件:作者为author-name或内容是foo if (comment.author.login === 'author-name' || comment.body === 'foo') { let deleteUrl; // 根据评论的原始url判断类型,直接复用构造删除请求 if (comment.url.includes('/pulls/comments/') || comment.url.includes('/issues/comments/')) { deleteUrl = comment.url; } else { console.log(`跳过未知类型评论:${comment.url}`); continue; } try { await github.rest.request('DELETE ' + deleteUrl); console.log(`成功删除评论:${comment.url}`); } catch (error) { console.error(`删除失败:${comment.url},错误:${error.message}`); } } }
3. 检查Token权限
确保GitHub Action使用的GITHUB_TOKEN(或自定义token)拥有足够权限:
- 私有仓库需要
repo权限 - 需明确赋予读写issues和pull requests的权限,可在Workflow里添加:
permissions: issues: write pull-requests: write
额外排查点
- 确认评论是否已被删除:重复删除同一评论会返回404
- 核对仓库路径:确保URL里的
MyOrganization/MyRepo和实际仓库完全一致
内容的提问来源于stack exchange,提问作者pkaramol
相关产品推荐
相关产品推荐

