【问题标题】:How do we know a pull request is approved or rejected using API in github?我们如何知道使用 github 中的 API 批准或拒绝拉取请求?
【发布时间】:2017-10-24 20:48:58
【问题描述】:

我想知道 Github API 中是否有一个函数可以返回拉取请求的状态,无论是接受还是拒绝。有这样的功能吗?

【问题讨论】:

    标签: github github-api


    【解决方案1】:

    您可以get a single PR 并检查它的statemerged 属性。如果它被合并,则它被接受。如果它已关闭且未合并,它可能会被拒绝

    事实上,它可能不是拒绝,而是被创建者关闭。我不确定是否可以检查它是被另一个用户(rejected)还是它的创建者(denied)关闭。

    【讨论】:

    • 它确实提供了state,但只能是openclosed。不接受或拒绝或介于两者之间的任何事情。 /e: 刚刚看到你的编辑,这将是一个很好的方式来看看它是被接受还是拒绝。
    • @nbokmans 我已经更新了答案。感谢您指出这一点!
    • @nbokmans - 感谢您的快速输入。可以通过评论来说明这一点吗?链接help.github.com/articles/… 表明,使用评论,审阅者可以请求更改,这表明 PR 被拒绝更改。是否有可能在 API 中得到这个?
    • @Gowtham 是的,还有一个用于评论的 API。 developer.github.com/v3/pulls/reviews
    【解决方案2】:

    在官方Github API Documentation 中,它表明您可以发出一个 GET 请求,如果该字段已合并,则该请求具有 merge_at 字段。编辑:还有一个合并字段。

    片段: ... "merge_commit_sha": "e5bd3914e2e596debea16f433f57875b5b90bcd6", "merged": false, "mergeable": true, "merged_by": { "login": "octocat", "id": 1, "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 },

    【讨论】:

      【解决方案3】:

      我有一个类似的要求 - 在将 PR 合并到 master 之前知道它是否已被批准。我在 repo 上添加了一条规则,以确保每个 PR 都必须在合并之前获得批准。然后在使用 API 获取有关分支的详细信息时,有一个字段说明该分支是否可以合并或阻塞。我用它来管理我的应用程序中的东西。

       mergeable_state
      

      【讨论】:

        【解决方案4】:

        我也被这个问题所困扰。我很难在文档中找到相关信息。

        选项 A

        我采用的策略是在q 参数中使用issues search endpointreview 限定符。

        因此,就我而言,当我想查看分配给我但尚未审核的所有 PR 时,我会点击此端点:https://github.com/api/v3/search/issues?q=is:open+is:pr+review-requested:jordan-bonitatis+review:none

        其他review 限定符包括approvedchanges_requested,如下所述:https://docs.github.com/en/github/searching-for-information-on-github/searching-issues-and-pull-requests#search-by-pull-request-review-status-and-reviewer

        选项 B

        如果您想查看给定 PR 的审核状态,而不是像上面的示例那样获取按状态过滤的列表,您可以点击 pull requests reviews endpoint/repos/:owner/:repo/pulls/:number/reviews

        这将返回 PR 的评论列表,每个评论都有一个 state 键。

        请注意,给定 PR 可能有多个状态相互冲突的评论。比如,如果 teamMemberA 批准了它,但 teamMemberB 要求更改。您必须遍历整个列表并根据所有状态决定如何处理它。

        【讨论】:

          【解决方案5】:

          现在可以使用GraphQL

          具体来说,如果 mergeStateStatus 枚举为 BLOCKED,则拉取请求尚未获得批准,而对于任何其他状态,它都将获得批准。

          此枚举存在于 PullRequest 对象中。请注意,在发布时这是一个preview feature,因此必须有适当的标题才能工作。它也将在预览时使用GraphQL Explorer

          【讨论】:

            【解决方案6】:

            根据 repo 配置,您可以通过一种或另一种方式获得答案。以下示例使用GraphQL API

            案例1:合并前需要PR审核

            您可以在pullRequest 字段中查询给定存储库的reviewDecision

            reviewDecisionPullRequestReviewDecision 类型,一个枚举值APPROVEDCHANGES_REQUESTEDREVIEW_REQUIRED

            查询示例:

            {
              repository(name: "gatsby", owner: "gatsbyjs") {
                pullRequest(number: 30371) {
                  title
                  reviewDecision
                  url
                }
              }
            }
            

            回复:

            {
              "data": {
                "repository": {
                  "pullRequest": {
                    "title": "chore(gatsby): don't terminate dev server if graphql wasn't imported from gatsby",
                    "reviewDecision": "APPROVED",
                    "url": "https://github.com/gatsbyjs/gatsby/pull/30371"
                  }
                }
              }
            }
            

            案例 2:在合并之前不需要 PR 审查

            如果存储库设置未指定需要审核,则reviewDecision 将是null,无论是否获得批准。

            在这种情况下,您可以遍历评论并检查状态。 statePullRequestReviewState 类型,它是一个枚举,其值为 APPROVEDCHANGES_REQUESTEDCOMMENTEDDISMISSEDPENDING

            查询示例:

            {
              repository(name: "create-react-app", owner: "facebook") {
                pullRequest(number: 10003) {
                  title
                  reviewDecision
                  url
                  reviews(first: 100) {
                    nodes {
                      state
                      author {
                        login
                      }
                    }
                  }
                }
              }
            }
            

            回复:

            {
              "data": {
                "repository": {
                  "pullRequest": {
                    "title": "Update postcss packages",
                    "reviewDecision": null,
                    "url": "https://github.com/facebook/create-react-app/pull/10003",
                    "reviews": {
                      "nodes": [
                        {
                          "state": "APPROVED",
                          "author": {
                            "login": "jasonwilliams"
                          }
                        }
                      ]
                    }
                  }
                }
              }
            }
            

            可以过滤评论以获得批准:reviews(first: 100, states: APPROVED)

            请注意,如果审阅者批准并随后请求更改,则将返回两条评论。

            检查 PR statePullRequestState 类型)可能会产生误导:管理员用户可能绕过了合并更改所需的审核流程。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2023-02-07
              • 2016-10-21
              • 1970-01-01
              • 2022-07-24
              • 2022-10-02
              • 1970-01-01
              相关资源
              最近更新 更多