Default Reviewers through ‘CODEOWNERS’ file

Reading Time: 3 minutes

This blog as about, How you can add the default reviewers in GitHub for the individual branches through “CODEOWNERS”.

Problem Statement:

When someone with admin or owner permissions enables require reviews. We know whenever a developer raises a PR on a particular base branch. Which is protected branch(that could be any like master, develop). He needs to add the reviewers for the pr, so the code will get review by the reviewers and merge into the base branch. So the problem over is we need to add the reviewers manually. Adding reviewers manually is not a one-time task. How many times you raise the PR, you need to add the reviewers.

Solution:

Default Reviewers through ‘CODEOWNERS’ file

Through the code-owners file, we can add the reviewers for the particular branch at once. It means whenever the developer raises the PR on this branch, they don’t need to add the reviewers. The Reviewers will get assign automatically, we defined in the “CODEOWNERS“ file.

So let’s see how we actually going to achieve that…

  1. CODEOWNERS File location
    Create a new file called “CODEOWNERS“ in the root, docs/, or .github/ directory of the repository, in the branch where you would like to add the code-owners file. Each CODEOWNERS file assigns the code owners for a single branch in the repository. Thus, you can assign different code owners for different branches.
  2. CODEOWNERS file size
    CODEOWNERS files must be under 3 MB in size. If the file size gets over the limit which means that code owner information is not to be shown and the appropriate code owners will not be requested to review changes in a pull request.
  3. CODEOWNERS syntax
    Note: If any line in your CODEOWNERS file contains invalid syntax, the file will not be detected and will not be used to request reviews.

Lets see different cases can be used in “CODEOWNERS”

Case1:

@global-owner-1 and @global-owner-2 are the default reviewer for all the files present in the branch. Then you need to add the following line in the CODEOWNERS file

*       @global-owner1 @global-owner2

Here ‘*’ represent all the files present in the repo.
suppose you don’t need to apply for a review on some specific file then a CODEOWNERS file uses a pattern that follows most of the same rules used in gitignore files, with some exceptions.

Case2:

Lets suppose you want a particular owner for a particular file then.

*.js    @js-owner

it means any file with the extension .js is modified then js-owner will get added as a reviewer in PR.

Case3:

You can also use email addresses if you prefer. They’ll be # used to look up users just like we do for commit author emails.

*.go docs@example.com

Case4:

Teams can be specified as code owners as well. Teams should be identified in the format @org/team-name.

*.txt @knoldus-test/cloud-engineers

Here the all team members present in the cloud-engineers team will be individually able to review the changes made in .txt files.

Case5:

@cloud-engineers owns any files in the build/logs directory at the root of the repository and any of its sub directories.

/build/logs/ @cloud-engineers

Case6:

The `docs/*` pattern will match files like `docs/getting-started.md` but not further nested files like `docs/build-app/troubleshooting.md`.

docs/*  docs@example.com

Case7:

@cloud-engineers owns any file in an apps directory anywhere in your repository.

apps/ @cloud-engineers

Case8:

In this example, @cloud-engineers owns any file in the `/docs` directory in the root of your repository and any of its subdirectories.

/docs/ @doctocat

Case9:

In this example, @octocat owns any file in the `/apps` directory in the root of your repository except for the `/apps/github` subdirectory, as its owners are left empty.

/apps/ @octocat
/apps/github 

So, in this way we can add the default reviewer in GitHub . Secondly just remember one thing the default reviewers will get assign to that branch only where we add the “codeowners” file. We can add the different code-owners to the different branches through “CODEOWNERS”.

Let’s see one example

  1. I have a repo “ecr-demo” with the branch “default-reviewers”

2. Now we need to add default reviewer for this branch. Means if anyone raises PR on this branch without any reviewer. Then this PR get not automatically merge . There has to be the some reviewers present by default.

So for that we need we add “CODEOWNERS” file under .github folder

3. Commit the above file. Now if any raise PR to this branch.You can see “sakshigawande” as a reviewer automatically assigned. As shown

References:

https://docs.github.com/en/repositories/managing-your-repositorys-settings-and-features/customizing-your-repository/about-code-owners

Written by 

Sakshi Gawande is a software consultant at "KNOLDUS" having more than 2 years of experience. She's working as a DevOps engineer. She always wants to explore new things and solve problems herself. on personal, she likes dancing, painting, and traveling.

Leave a Reply