1

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

From the above documentation I’m trying to determine code and file owners of a particular file. I haven’t been able to find anything that gives this information within the GitHub documentation.

The closest I found was this, GitHub API v3: Determine if user is an Owner of an Organization

But that seems to be answering a slightly different question.

sf8193
  • 575
  • 1
  • 6
  • 25

3 Answers3

2

I've built an npm library called codeowners-api which does this . So its not python but in JS.

If you want to use REST you would need to fetch the codeowners file from the Repo in question using Github's get-file API.

After that you take the file and you iterate over the codeowners file until you find the match. You can look at my library's code as reference.

I've also created a chrome extension which gives the reviewer a filter button to see only their relevant files.

https://chrome.google.com/webstore/detail/codeowners/mklphhfajjbikchaodnibnjmeibbonhb

orepor
  • 905
  • 2
  • 13
  • 23
0

For those whoever get stuck in this situation and can't use that library^, I managed to find a work around where I use .search_issues() and then query repo:x+review:approved+is:open+is:pr, the review does not switch to approved until a Codeowner has approved the PR.

sf8193
  • 575
  • 1
  • 6
  • 25
0

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

  1. List requested reviewers for a pull request using the Pull Requests 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. So for each codeowning team that I got in Step 1, I used the Teams API to get lists of the team members: 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.