2

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.

Is there any other way to get the pull request review approved 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(); } } 
2
  • Hi rihurla, I'm wondering if using this URL would help: api.github.com/users/<username>/events?per_page=100, filtering the response to get only the event 'PullRequestReviewEvent' with the field "state": "approved" in the payload? Commented Mar 19, 2021 at 15:05
  • 1
    Thanks for your answer, I'll try to do that. 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 list by using their GraphQL API. I'll try both approaches and come back here, hopefully with this issue resolved. :) Commented Mar 20, 2021 at 11:17

1 Answer 1

4

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 was able to get exactly what I needed by changing my previous request with a new one using 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

Pull Request Review State Object

GitHub GraphQL Explorer

Example on GitHub GraphQL Explorer:

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

Response:

{ "data": { "repository": { "pullRequest": { "reviews": { "totalCount": 3 } } } } } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.