pr-comments

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Address PR Comments

处理PR评论

Pull all review comments from the current GitHub pull request and address each one by making the necessary code changes.
从当前GitHub拉取请求中提取所有评审评论,并通过做出必要的代码修改来处理每一条评论。

Step 1: Identify the PR

步骤1:识别PR

If the user provided a PR number as
$ARGUMENTS
, use that. Otherwise, detect the PR for the current branch:
gh pr view --json number,title,url,headRefName
If no PR is found, inform the user and stop.
如果用户提供了PR编号作为
$ARGUMENTS
,则使用该编号。否则,检测当前分支对应的PR:
gh pr view --json number,title,url,headRefName
如果未找到PR,通知用户并停止操作。

Step 2: Fetch all review comments

步骤2:获取所有评审评论

Get all review comments (file-level comments, not general PR comments) using:
gh api repos/{owner}/{repo}/pulls/{number}/comments --paginate --jq '.[] | {id, path, line, original_line, side, body, diff_hunk, subject_type, user: .user.login, created_at, in_reply_to_id}'
Also fetch review threads via GraphQL to understand resolved vs unresolved status (note:
gh pr view --json
does NOT support
reviewThreads
):
gh api graphql -f query='
{
  repository(owner: "{owner}", name: "{repo}") {
    pullRequest(number: {number}) {
      reviewThreads(first: 100) {
        nodes {
          id
          isResolved
          comments(first: 10) {
            nodes {
              databaseId
              path
              originalStartLine
              startLine
              body
              author { login }
            }
          }
        }
      }
    }
  }
}'
Filter the results to only unresolved threads (
isResolved == false
). The
id
field on each thread node is the GraphQL node ID needed to resolve the thread later.
使用以下命令获取所有评审评论(文件级评论,而非通用PR评论):
gh api repos/{owner}/{repo}/pulls/{number}/comments --paginate --jq '.[] | {id, path, line, original_line, side, body, diff_hunk, subject_type, user: .user.login, created_at, in_reply_to_id}'
同时通过GraphQL获取评审线程,以了解已解决和未解决状态(注意:
gh pr view --json
不支持
reviewThreads
):
gh api graphql -f query='
{
  repository(owner: "{owner}", name: "{repo}") {
    pullRequest(number: {number}) {
      reviewThreads(first: 100) {
        nodes {
          id
          isResolved
          comments(first: 10) {
            nodes {
              databaseId
              path
              originalStartLine
              startLine
              body
              author { login }
            }
          }
        }
      }
    }
  }
}'
过滤结果,仅保留未解决的线程(
isResolved == false
)。每个线程节点的
id
字段是后续解决线程所需的GraphQL节点ID。

Step 3: Filter and organize

步骤3:筛选与整理

  • Only address unresolved comment threads - skip any threads that are already resolved.
  • Skip reply comments (those with
    in_reply_to_id
    set) - only process top-level comments in each thread.
  • Group comments by file path for efficient processing.
  • Present a summary to the user showing each comment with:
    • File path and line number
    • Who wrote the comment
    • The comment body
    • The relevant code context (from diff_hunk)
  • 仅处理未解决的评论线程 - 跳过已解决的线程。
  • 跳过回复评论(带有
    in_reply_to_id
    的评论)- 仅处理每个线程中的顶级评论。
  • 按文件路径分组评论以提高处理效率。
  • 向用户展示摘要,每条评论包含:
    • 文件路径和行号
    • 评论作者
    • 评论内容
    • 相关代码上下文(来自diff_hunk)

Step 4: Address each comment

步骤4:处理每条评论

For each unresolved comment:
  1. Read the relevant file to understand the full context around the commented line.
  2. Analyze the comment to determine what change is being requested.
  3. Make the code change using the Edit tool.
  4. Resolve the comment thread on GitHub using the GraphQL API:
    gh api graphql -f query='mutation { resolveReviewThread(input: {threadId: "<thread_node_id>"}) { thread { isResolved } } }'
    To get the thread node ID, include
    id
    (the GraphQL node ID) when fetching review threads in Step 2.
  5. Briefly explain what you changed and why.
If a comment is unclear or requires a design decision, flag it to the user instead of guessing. Do NOT resolve these threads.
对于每条未解决的评论:
  1. 读取相关文件,了解评论行周围的完整上下文。
  2. 分析评论,确定所需的修改内容。
  3. 使用编辑工具进行代码修改
  4. 通过GraphQL API在GitHub上解决评论线程
    gh api graphql -f query='mutation { resolveReviewThread(input: {threadId: "<thread_node_id>"}) { thread { isResolved } } }'
    要获取线程节点ID,在步骤2获取评审线程时需包含
    id
    (GraphQL节点ID)。
  5. 简要说明所做的修改及原因。
如果评论内容不明确或需要进行设计决策,请标记给用户,不要自行猜测。不要解决这些线程。

Step 5: Summary

步骤5:总结

After addressing all comments, provide a summary:
  • List each comment and what was done to address it
  • Note any comments that were skipped or need human input
  • Remind the user to review the changes before committing
处理完所有评论后,提供总结:
  • 列出每条评论及对应的处理操作
  • 记录跳过或需要人工介入的评论
  • 提醒用户在提交前审查修改内容