How can I query about code owners for a PR?

I’d like to fetch the code owners associated with a pull request or at least the repository.

Is this possible through existing APIs or does this feature not exist? My backup plan is to write code owner evaluation logic myself, but that seems like it shouldn’t be necessary.

1 Like

Through the existing API, you can fetch pull request author, repo owner of the source of the pull request, assignees, users that merges the pull request, and get commits with committers and authors. What are you looking for specifically?

I want to find the code owners requested for review on a specific PR. It doesn’t seem possible through the API.

https://help.github.com/en/articles/about-code-owners

> Code owners are automatically requested for review when someone opens a pull request that modifies code that they own.

So you could query requested reviewers.

I wanted to identify if a requested reviewer was requested as a code owner.

Hi @chdsbd!

While it’s not possible to do this with a single API, you can create a script that allows you to crossreference the requested reviewers with the content of the CODEOWNERS file using the Get Contents API endpoint. Since the CODEOWNERS file has a limited number of locations that it can be located, it should be easy to find with a couple of calls.

Hope that helps!

1 Like

Thanks. That’s what I’ve settled on. The challenge I see is that a user could be in the code owners file but not necessarily be requested because of their ownership. Another user could request their review for code they don’t own.

@chdsbd, that’s very true. You’d probably have to do some advanced checking of the files in the PR and how they compare to the CODEOWNERS file as well to be sure. 

@that-pat, we have similiar request to check whether the approval is coming from code owner or not, does GitHub have any plan to expose one single API for code owner check?

Hi @payneChen o/

Since this OP, there have been some internal goings-on, with relation to CODEOWNERS data being available from our APIs.

Within the PullRequest object for GraphQL, we have the reviewDecision field, which you can see, here:

Continuing with GraphQL, we also have the PullRequestReviewDecision enum, which includes the status values for review requests:

As far as I’m aware (currently) we have no REST functionality, or a specific CODEOWNER endpoint as a single source for this information.

Very curious to know if the above solves for your use case. If it doesn’t, I would highly recommend submitting your feedback, through this form:

…which gets reviewed directly by our PMs.

Sincerely hope this helps! :bow:

It seems like reviewDecision should work for my use case, but there’s a bug with the field that’s preventing me from using it.

The reviewDecision field causes the GitHub GraphQL API to error when “Require review from Code Owners” is enabled and the pull request has a file matching CODEOWNERS. I have a ticket open with GitHub Support about this.

1 Like

Hey @chdsbd thanks so much for voicing your experience. Very curious to know how your exchange goes with that internal support ticket.

If anything seems relevant for the Community, would you mind posting back?

Anyone else reading is also empowered to @ mention me and I can see if there was traction that might be suited for a broader audience, than the 1:1 ticket mentioned above.

:bow: