Automated code review requests with code owners
Tags: github, productivity, git
Published: 2022-10-11 09:01:07

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](https://www.kutac.cz/pocitace-a-internety/automaticke-zadosti-o-code-review-s-codeowners)

---

Code owners are not a new feature, it was introduced on the [GitHub blog](https://github.blog/2017-07-06-introducing-code-owners/) 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](https://docs.gitlab.com/ee/user/project/code_owners.html).

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/35n5a95nifzmhn3vvq0b.jpg)

### 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/20uaal89u9d9levcs4s1.jpg)

## 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](https://git-scm.com/docs/gitignore#_pattern_format) of git documentation.

The final CODEOWNERS file might look like this including comments:

```bash
# 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
```

---

Cover image is taken from [GitHub blog](https://github.blog/2017-07-06-introducing-code-owners/).