簡體   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".

問題是,如果 PR 有超過 100 個評論對象(每頁最多 100 個),我無法找到將在下一頁上的已批准對象,因為它按時間順序返回。

有沒有其他方法可以讓拉取請求審查批准 state?

我的代碼如下:

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 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.

通過使用 GraphQL 將之前的請求更改為新請求,我能夠准確地獲得所需的內容。

這是代碼。

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}`);
    }
}

參考:

拉取請求審查 Object

拉取請求審查 State Object

GitHub GraphQL 瀏覽器

GitHub GraphQL Explorer 示例:

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

回復:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM