API to check if a PR is approved by a reviewer

Hi,

How do I check if a PR has been approved ?
I am currently using Github Python API.
Created a repo object and get a PR object by doing:

pr=repo.get_pull(1602)

type(pr)
<class ‘github.PullRequest.PullRequest’>

I don’t see an ‘approved’ attribute. How do I check if a PR is approved?

Thanks,
Kalyan

Hey there @kalyan-nidumolu :wave:

Indeed, there is a state value returned from this endpoint:

Syntax is:

GET /repos/{owner}/{repo}/pulls/{pull_number}/reviews

…and note that in the default response, we have:

"state": "APPROVED",

Let us know how you go!

Hi Nethgato,

Thanks for the response. What do you mean by “default response” ?

when I look at:
https://api.github.com/repos///pulls//reviews

I only see an Array of reviews. For for each review, I see a state. If there is no approval, the state is “COMMENTED”. If the review has an approval, its state is “APPROVED”.

So I used:

def is_pr_approved(pr):
review_list = pr.get_reviews()
for review in review_list:
if review.state == “APPROVED”:
return True
return False

Instead of iterating reviews like this - I was hoping to see if the top level PR object has “approved” flag or something.

But yeah… the current solution also works works for me.

Hey @kalyan-nidumolu

The default response is simply what we display in our documentation that exposes the state attribute and it’s subsequent values.

For the code you’re using, I feel less comfortable commenting and suggesting any changes as solving for your use case is not explicitly something that GitHub staff can assist with.

I would say if you’re hoping for coding assistance, to post in our Software Development board.

But for assistance with our APIs themselves, that’s what I’m kicking around this board for =)

Hi @nethgato,

The code that I wrote was just for an example - to indicate that I currently don’t see that attribute in the response from https://api.github.com/repos/owner/repo/pulls/pr-num/reviews

The response is just a List of reviews. I don’t see any default attributes other than that.
The code, I just gave to explain the logic that I am currently using.

Thanks,
Kalyan

1 Like

Thanks for further explaining @kalyan-nidumolu :bow:

Do you happen to have an example PR in a public repository that you could share?