githubgithub-codeowners

Exclude a file or directory from CODEOWNERS


I have a file structure like so:

foo/file1.c
foo/file2.c
foo/freeforall.c

My foogroup on GitHub needs to review changes to everything in foo except freeforall.c which is a file that anyone can touch without restriction, so it should not automatically add anybody as a reviewer when changes are made to it.

(In reality, whoever modifies freeforall.c just asks someone else on their own team to review their change.)

The Question

How can I do this with GitHub CODEOWNERS? The file looks like this right now:

foo @MyOrg/foogroup

What I Tried

GitHub explicitly says that the ! syntax is not supported, so that won't work:

foo @MyOrg/foogroup
!foo/freeforall.c

And there are too many files in the foo directory to explicitly include them individually. I could move freeforall.c into a different directory, but really I don't want to be having GitHub's CODEOWNER feature dictating how I organize my components. I want freeforall.c to be in the foo directory, that's where it belongs!

I also considered creating an emptygroup to assign that file to, but soon realized that now I'd be requiring that a group with zero members must approve PRs to that file which obviously would cause problems. 😂

foo @MyOrg/foogroup
foo/freeforall.c @MyOrg/emptygroup

Solution

  • The CODEOWNERS file takes the last matching line into account. You can make definitions with empty owners to specify paths/files without an owner.

    Consequently, you can use the following lines to have all files in the foo directory owned by @MyOrg/foogroup except foo/freeforall.c:

    foo @MyOrg/foogroup
    foo/freeforall.c