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!

2 Likes

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:

2 Likes

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.

2 Likes

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:

1 Like

@nethgato I’m pinging you because we’d really need to have code owner reviews exposed for the REST API too. Is there anything we can help with?

1 Like

Hi @jd o/

As-is, the best thing you could do, is to add your voice to those who would like to see similar data in REST, as we have in GraphQL. The most helpful path forward, would be to submit via our feedback form, here:

1 Like

This is the best way I’ve found to do this:

  1. List requested reviewers for a pull request using the following API: https://docs.github.com/en/github-ae@latest/rest/reference/pulls#list-requested-reviewers-for-a-pull-request. This will give you a list of usernames and a list of teams.
  2. Codeowners are essentially just teams or individuals assigned to watch over certain parts of the codebase. In my case, I wanted a list of usernames who are part of each codeowning team. For this I used the Teams API: https://docs.github.com/en/rest/reference/teams#list-team-members.

Caveats of this approach:

  • Step 1 of this approach lists out all of those whose reviews were requested on the PR. While Github automatically requests reviews from codeowning individuals and teams, one can manually request reviews as well. This step does not distinguish between automatic review requests (which suggest codeowner status) and manual review requests.