列出所有未解决的拉取请求评论

Sha*_*tin 5 github github-api octokit octokit-js

我们使用@octokit/rest客户端来获取拉取请求的所有评论:

client.pulls
  .listComments({
    owner,
    repo,
    pull_number: 34,
    per_page: 100,
  })
  .then((result) => {
    console.log(result.data.length);
    console.log(result.data[0]);
  });
Run Code Online (Sandbox Code Playgroud)

如果我们可以只列出未解决的评论怎么办?数据中似乎没有属性表明某人是否已解决该评论。

Gle*_*as' 8

使用GitHub 的 v4 GraphQL APIGraphQL API Explorer(用于探索/构建我的查询)、GitHub CLI(用于简化调用 GraphQL API)和jq(用于过滤结果);我提出了以下解决方案(编写为 bash 脚本以简化调用,但如果也从代码调用 GraphQL API,则同样的原则也适用):

# Usage:
#   Retrieve unresolved review comments
#     ./pr-unresolved-review-comments myorganisation myrepo 1337
#
#   Count of unresolved review comment threads
#     ./pr-unresolved-review-comments myorganisation myrepo 1337 | jq length

gh api graphql -f owner="$1" -f repo="$2" -F pr="$3" -f query='
  query FetchReviewComments($owner: String!, $repo: String!, $pr: Int!) {
    repository(owner: $owner, name: $repo) {
      pullRequest(number: $pr) {
        url
        reviewDecision
        reviewThreads(first: 100) {
          edges {
            node {
              isResolved
              isOutdated
              isCollapsed
              comments(first: 100) {
                totalCount
                nodes {
                  author {
                    login
                  }
                  body
                  url
                }
              }
            }
          }
        }
      }
    }
  }
' | jq '.data.repository.pullRequest.reviewThreads.edges | map(select(.node.isResolved == false))'
Run Code Online (Sandbox Code Playgroud)

作为 GraphQL 查询的一部分包含的特定字段是可定制的,但这似乎给了我我需要的东西。

运行上述脚本的输出将如下所示:

[
  {
    "node": {
      "isResolved": false,
      "isOutdated": true,
      "isCollapsed": false,
      "comments": {
        "totalCount": 7,
        "nodes": [
          {
            "author": {
              "login": "0xdevalias"
            },
            "body": "This is an unresolved comment that is important!",
            "url": "https://github.com/myorganisation/myrepo/pull/1337#discussion_r559294516"
          },
          ..snip..
        ]
      }
    }
  },
  ..snip..
]
Run Code Online (Sandbox Code Playgroud)

要列出针对存储库的所有开放 PR 的未解决评论,可以使用以下脚本:

# Usage:
#   Retrieve all unresolved review comments for a repo
#     ./pr-unresolved-review-comments myorganisation myrepo

gh api graphql -f owner="$1" -f repo="$2" -f query='
  query FetchReviewComments($owner: String!, $repo: String!) {
    repository(owner: $owner, name: $repo) {
      pullRequests(first: 30, states: OPEN) {
        edges {
          node {
            url
            reviewDecision
            reviewThreads(first: 100) {
              edges {
                node {
                  isResolved
                  isOutdated
                  isCollapsed
                  comments(first: 100) {
                    totalCount
                    nodes {
                      author {
                        login
                      }
                      body
                      url
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
'  | jq '.data.repository.pullRequests.edges[].node.reviewThreads.edges | map(select(.node.isResolved == false))'

Run Code Online (Sandbox Code Playgroud)


Mad*_*hat 5

GitHub.com上,并非拉取请求下的所有评论都有状态resolved/unresolved,只有评论中的评论才具有状态。octokit使用的GitHubs v3 API不提供任何此类状态作为评论评论响应的一部分GET并且示例响应如下:

    [
      {
        "url": "https://api.github.com/repos/octocat/Hello-World/pulls/comments/1",
        "id": 10,
        "node_id": "MDI0OlB1bGxSZXF1ZXN0UmV2aWV3Q29tbWVudDEw",
        "pull_request_review_id": 42,
        "diff_hunk": "@@ -16,33 +16,40 @@ public class Connection : IConnection...",
        "path": "file1.txt",
        "position": 1,
        "original_position": 4,
        "commit_id": "6dcb09b5b57875f334f61aebed695e2e4193db5e",
        "original_commit_id": "9c48853fa3dc5c1c3d6f1f1cd1f2743e72652840",
        "in_reply_to_id": 8,
        "user": {
          "login": "octocat",
          "id": 1,
          "node_id": "MDQ6VXNlcjE=",
          "avatar_url": "https://github.com/images/error/octocat_happy.gif",
          "gravatar_id": "",
          "url": "https://api.github.com/users/octocat",
          "html_url": "https://github.com/octocat",
          "followers_url": "https://api.github.com/users/octocat/followers",
          "following_url": "https://api.github.com/users/octocat/following{/other_user}",
          "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
          "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
          "subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
          "organizations_url": "https://api.github.com/users/octocat/orgs",
          "repos_url": "https://api.github.com/users/octocat/repos",
          "events_url": "https://api.github.com/users/octocat/events{/privacy}",
          "received_events_url": "https://api.github.com/users/octocat/received_events",
          "type": "User",
          "site_admin": false
        },
        "body": "Great stuff",
        "created_at": "2011-04-14T16:00:49Z",
        "updated_at": "2011-04-14T16:00:49Z",
        "html_url": "https://github.com/octocat/Hello-World/pull/1#discussion-diff-1",
        "pull_request_url": "https://api.github.com/repos/octocat/Hello-World/pulls/1",
        "_links": {
          "self": {
            "href": "https://api.github.com/repos/octocat/Hello-World/pulls/comments/1"
          },
          "html": {
            "href": "https://github.com/octocat/Hello-World/pull/1#discussion-diff-1"
          },
          "pull_request": {
            "href": "https://api.github.com/repos/octocat/Hello-World/pulls/1"
          }
        }
      }
    ]
Run Code Online (Sandbox Code Playgroud)

所以本质上,我认为 GitHub 的 v3 API 或 Octokit 目前无法满足您的要求。您可以在 GitHub v3 API 的文档中查看更多相关信息