简体   繁体   English

Github API GET 获得批准的拉取请求评论列表

[英]Github API GET Approved Pull Request Reviews List

I'm working on a Github Action with the Github API endpoint for the list of reviews but it returns the whole history, including dismissals and comments, but the only ones I need are the ones where the state is "APPROVED". I'm working on a Github Action with the Github API endpoint for the list of reviews but it returns the whole history, including dismissals and comments, but the only ones I need are the ones where the state is "APPROVED".

The issue is that if the PR has more than 100 review objects (100 being the max per page), I'm unable to find the approved objects which will be on the following page since it returns in chronological order.问题是,如果 PR 有超过 100 个评论对象(每页最多 100 个),我无法找到将在下一页上的已批准对象,因为它按时间顺序返回。

Is there any other way to get the pull request review approved state?有没有其他方法可以让拉取请求审查批准 state?

My code is the following:我的代码如下:

async function evaluateReviews() {
  const result = await octokit.request('GET /repos/{owner}/{repo}/pulls/{pull_number}/reviews', {
    owner: owner,
    repo: repo,
    pull_number: pullNumber
  });

  const numberOfApprovalsRequired = core.getInput('number-of-approvals');
  const reviews = result.data
  var approvals = 0

  reviews.forEach((item, i) => {
    if (item.state == "APPROVED") {
      approvals += 1;
    }
  });

  if (approvals >= numberOfApprovalsRequired) {
    addApprovedLabel();
  }
}

I'm posting the answer here so it might help someone with the same issue.我在这里发布答案,因此它可能会帮助遇到同样问题的人。

I opened a ticket on Github support, and they answered by saying that the endpoint to retrieve only approved pull request review is not available via REST API, however, I can get the count by using their GraphQL API. I opened a ticket on Github support, and they answered by saying that the endpoint to retrieve only approved pull request review is not available via REST API, however, I can get the count by using their GraphQL API.

I was able to get exactly what I needed by changing my previous request with a new one using GraphQL.通过使用 GraphQL 将之前的请求更改为新请求,我能够准确地获得所需的内容。

Here is the code.这是代码。

async function evaluateReviews() {
  try {
    const approvedResponse = await octokit.graphql(`
      query($name: String!, $owner: String!, $pull_number: Int!) {
        repository(name: $name, owner: $owner) {
          pullRequest(number: $pull_number) {
            reviews(states: APPROVED) {
              totalCount
            }
          }
        }
      }
      `, {
        "name": name,
        "owner": owner,
        "pull_number": pullNumber
      });
      const approvalsRequired = core.getInput('number-of-approvals');
      const approvals = approvedResponse.repository.pullRequest.reviews.totalCount
      if (approvals >= approvalsRequired) {
        addApprovedLabel();
      }
    } catch (error) {
      console.log(`ERROR: ${error}`);
    }
}

Reference:参考:

Pull Request Review Object 拉取请求审查 Object

Pull Request Review State Object 拉取请求审查 State Object

GitHub GraphQL Explorer GitHub GraphQL 浏览器

Example on GitHub GraphQL Explorer: GitHub GraphQL Explorer 示例:

{
  repository(name: "fetch", owner: "github") {
    pullRequest(number: 913) {
      reviews(states: APPROVED) {
        totalCount
      }
    }
  }
}

Response:回复:

{
  "data": {
    "repository": {
      "pullRequest": {
        "reviews": {
          "totalCount": 3
        }
      }
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM