【问题标题】:List all unresolved pull request comments列出所有未解决的拉取请求评论
【发布时间】:2019-09-06 21:30:46
【问题描述】:

我们正在使用@octokit/rest 客户端获取拉取请求中的所有 cmets:

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

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

【问题讨论】:

  • 也许您可以使用this 在 PR 中获得评论,然后对于每个待定评论使用 this 获得 cmets。尚未对此进行测试。
  • 要获得待审核的评论,我认为您可以检查 PR 的 stateCHANGES_REQUESTED。再次没有完全实施。
  • @DivyaMamgai 你提到的state 是PR。 OP 需要的是 PR 审查中 cmets 的状态。
  • @MadhuBhat 是的,这是正确的。它只是一个假设,如果特定的审核请求仍在CHANGES_REQUESTED 上,那么该特定审核的某些 cmets 仍未解决。
  • @DivyaMamgai 但即使审核请求的所有 cmets 都已解决,PR 的状态仍保持为 CHANGES_REQUESTED。它的变化只是它被批准或驳回。但无论如何,OP 需要根据状态列出 cmets,而不是它们是否存在。

标签: github github-api octokit octokit-js


【解决方案1】:

GitHub.com 上,并非所有拉取请求下的 cmets 都具有 resolved/unresolved 状态,但只有 cmets 作为审查的一部分。并且 octokit 使用的 GitHubs 的 v3 API 没有提供任何此类状态作为 GET 对评论 cmets 的回复的一部分,以及同样是:

[
  {
    "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"
      }
    }
  }
]

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

【讨论】:

  • 这也是我的感觉。我可能需要做大量的编程工作才能过滤掉已解析的 cmets。
【解决方案2】:

我可以通过list-review-comments-on-a-pull-request 获得已解决/未解决的 cmets。

试试/repos/{owner}/{repo}/pulls/{pull_number}/comments?

【讨论】:

  • 您使用什么字段来确定 cmets 是否已通过此方法解析/未解析?
【解决方案3】:

使用GitHub's 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

OWNER=$1
REPO=$2
PR=$3

gh api graphql -f query="
  query FetchReviewComments {
    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))'

您作为 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..
]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-18
    • 2020-01-05
    • 2018-07-09
    • 2020-04-21
    • 2019-12-19
    • 2021-09-25
    • 2017-08-02
    • 2016-03-09
    相关资源
    最近更新 更多