DEV Community

Cover image for Automated code review requests with code owners
Pavel Kutáč
Pavel Kutáč

Posted on

Automated code review requests with code owners

Tests, linters, build. All those and much more are very often automated in CI/CD. But you can go further and automate assigning developers to do a code review of PR. And you can specify different code owners of different parts of code as well.


🇨🇿 V češtině si lze článek přečíst na kutac.cz


Code owners are not a new feature, it was introduced on the GitHub blog already in the year 2017. But for small teams or individuals, this might not be so useful. So I learned about it just now.

Gitlab supports code owners too, but only on a paid plan. So I wasn't able to test it. However, it should be the same, according to their documentation.

What are Code owners and why you want to define them?

With code owners, you can define individual developer or the whole team, who is responsible for the code. When a new PR is created, it will automatically assign code owners to do a review, if some files owned by them are changed.

And later, in branch settings, you can specify Required review from code owners rule. And no one will be able to merge the code, until some owner approves PR.

Branch rule requires approval from Code owner

The team as a code owner

You can specify also the whole team as code owner. Then the whole team is assigned to do a code review. But not an individual developer. To assign individual dev, you have to turn on this feature in team settings.

You can also select a member of the team, which is never assigned to do a review. In our team, it is the main architect. He is still code owner, because the team is code owner. But is not assigned to review.

Unless you specify him directly as code owner of some file. If that file is changed, he is assigned as reviewer. But not as a team member, but as code owner.

Setting of code review per team

Define code owners

The config of code owners is in .github/CODEOWNERS file. Each line contains a file pattern followed by one or more developers or teams as owners. Later match takes precedence over the earlier rule.

File pattern is the same as in .gitignore file and is documented in Pattern format section of git documentation.

The final CODEOWNERS file might look like this including comments:

# Global rule set for everything. And special cases exclude later
* @mycompany/be-code-reviewers

# Pipeline files are owned by Octocat
.github/workflows/ @octocat

# Index and htaccess.
# This rules override previous one. Need to specify all of them if required.
/index.php @mycompany/be-code-reviewers @octocat
/.htaccess @mycompany/be-code-reviewers @octocat

# Dependencies
composer.json @arxeiss
composer.lock @arxeiss
/vendor/ @arxeiss

# You can also remove code owner if needed.
/CHANGELOG.md
/version.txt
Enter fullscreen mode Exit fullscreen mode

Cover image is taken from GitHub blog.

Top comments (0)