Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know if a git pull request is approved via github api

Based on this api: https://docs.github.com/en/rest/reference/pulls#get-a-pull-request

could I use the state, mergable or rebaseable fields from the response to find out that some reviewer approved my PR.

I'm also specifically looking for usecase where if there is a minimum requirement of 2 approving reviews(as shown below in the image) and the pull request only has 1 approval, the state should still be non-approved and should return approved once there are at least 2 approving reviewers. Is there any field that I can use for this usecase?

enter image description here

like image 208
Prasad Shinde Avatar asked May 09 '19 21:05

Prasad Shinde


People also ask

How do you know if your pull request has been approved?

You can get a single PR and check it's state and merged properties. If it's merged, then it's accepted. If it's closed and not merged it may be rejected. In fact it may be not rejected but closed by a creator.

What happens when a pull request is approved?

Once the repository maintainer has approved a pull request, the developer's new updates in the forked repository are merged with the main project repository. The product is then updated with the new feature or bug fix, and can now be viewed by end users.

How do I check my status on GitHub?

You can see the overall state of the last commit to a branch on your repository's branches page or in your repository's list of pull requests. If status checks are required for a repository, the required status checks must pass before you can merge your branch into the protected branch.


Video Answer


2 Answers

There are two options to do that.

  1. You request all the reviews for the PR and maintain logic of approves/review requests by yourself. Link to docs
  2. Personally I use GitHub hooks and on each review submitted I check whether that was an APPROVE and then use this advanced query syntax to search whether this PR was approved or not.

Check this repository for complete code example (especially this function)

like image 127
Most Wanted Avatar answered Sep 29 '22 15:09

Most Wanted


Using the GitHub GraphQL API you can query reviewDecision on the pullRequest field for a given repository.

If 2 reviews are required and the PR received exactly 1 approval, reviewDecision will have the value REVIEW_REQUIRED.

reviewDecision is of type PullRequestReviewDecision, an enum with values of APPROVED, CHANGES_REQUESTED, and REVIEW_REQUIRED.

Example query:

{
  repository(name: "gatsby", owner: "gatsbyjs") {
    pullRequest(number: 30371) {
      title
      reviewDecision
      state
      reviews(first: 100) {
        nodes {
          state
          author {
            login
          }
        }
      }
    }
  }
}

Response:

{
  "data": {
    "repository": {
      "pullRequest": {
        "title": "chore(gatsby): don't terminate dev server if graphql wasn't imported from gatsby",
        "reviewDecision": "APPROVED",
        "state": "MERGED",
        "reviews": {
          "nodes": [
            {
              "state": "APPROVED",
              "author": {
                "login": "LekoArts"
              }
            }
          ]
        }
      }
    }
  }
}

Depending on your repo settings, it may be a bit convoluted to check if a PR has been approved (for instance reviewDecision returns null if reviews are not enforced and the PR has received one approval but another review has been requested and is pending). You may need to iterate through the reviews to find what you need.

state may also be useful. It's of type PullRequestReviewState, an enum with values of APPROVED, CHANGES_REQUESTED, COMMENTED, DISMISSED, and PENDING.

like image 30
Jelefra Avatar answered Sep 29 '22 15:09

Jelefra