This blog as about, How you can add the default reviewers in GitHub for the individual branches through “CODEOWNERS”.
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.
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…
- 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.
- 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.
- 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”
@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.
Lets suppose you want a particular owner for a particular file then.
it means any file with the extension .js is modified then js-owner will get added as a reviewer in PR.
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.
Teams can be specified as code owners as well. Teams should be identified in the format @org/team-name.
Here the all team members present in the cloud-engineers team will be individually able to review the changes made in .txt files.
@cloud-engineers owns any files in the build/logs directory at the root of the repository and any of its sub directories.
The `docs/*` pattern will match files like `docs/getting-started.md` but not further nested files like `docs/build-app/troubleshooting.md`.
@cloud-engineers owns any file in an apps directory anywhere in your repository.
In this example, @cloud-engineers owns any file in the `/docs` directory in the root of your repository and any of its subdirectories.
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
- 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