We decided to test all possible scenarios to identify how GitHub’s CODEOWNERS file can be configured and misconfigured. The bottom line is that what seems to be configured properly misbehaves with different settings. This blog post highlights these scenarios and provides tips to mitigate them.
We decided to test GitHub CODEOWNERS scenarios by combining branch protection rules, direct and indirect git permission assignments, and other misconfigurations. The common settings across all scenarios are:
Scenario Settings
If the “Require review from Code Owners” setting is enabled, after the user or Team with effective Write permission approved the pull request, merging is allowed.
Each of the assigned reviewers gets an email notification when assigned to review the Pull Request.
If the reviewer is not in the CODEOWNERS file or does not have the effective Write permission, the merge is blocked.
Scenario Settings
Any approval by any non-author user with a Write permission passes the required check to merge the code into the protected branch.
Everyone in the relevant paths in the CODEOWNERS file will get a notification to approve the Pull Request, but the action can be taken by a user not in the CODEOWNERS file.
Scenario Settings
Regardless of the “Require review from Code Owners” setting, any approval by any non-author user with a Write access will allow the code to be merged.
Scenario Settings
If “Require review from Code Owners” is enabled, the merging is blocked unless bypassed by an admin or a user is added to the empty Team.
This approach is a suitable alternative to archiving a stale repository. The considerations are highlighted in the considerations to protect stale code repositories.
Scenario Settings
Regardless of the “Require review from Code Owners” setting, any approval by any non-author user with a Write access will allow the code to be merged.
This approach is good in order to allow merges in paths where the file changes are less sensitive. For example, allow everyone to modify a folder with a markdown content that explains developers how to setup the service on their local machine.
Scenario Settings
Regardless of the “Require review from Code Owners” setting, even after the user in the Team “arnica-codeowners” approved the pull request, merging is blocked.
Additionally, since the Team is misconfigured, the members will not receive an email notification with the ask to review the Pull Request.
To fix this error, simply add the Write permission for this Team.
Scenario Settings
Same as Scenario #0a.
This is interesting as the Team is misconfigured, but the approval of the member with the Write access is counted as a valid check.
The downside of this misconfiguration is that the user with Write access did not receive an email notification to review the Pull Request.
To fix this error, simply add the permission Write for this Team.
Scenario Settings
Regardless of the “Require review from Code Owners” setting, even after the user in the Team “arnica-codeowners” approved the pull request, merging is blocked. It seems to be like scenario #4 above, where the owner is identified but has insufficient access to the repository.
Since the user is misconfigured, an email notification will not be sent. Managing developer permissions effectively, such as with Arnica's Developer Access Management, can prevent such issues.
To fix this error, simply add the permission Write for this user.
Scenario Settings
Similar to Scenario #0a.
Note that in this case the user received an email notification with the ask to review the Pull Request.
When a “mixed role” is configured, the more permissive permission takes place. There might be a misconfiguration in the way permissions are defined, and therefore, worth reviewing the root cause of this configuration.
Scenario Settings
Similar to Scenario #1.
Assign Teams instead users in CODEOWNERS files, so that if a reviewer leaves the organization, the file remains valid and enforced.
This part is a bit “sales-ish”, but if you got here, wouldn’t it be great if you could see all mentioned misconfigurations and fix them with a single click?
At Arnica, we identify misconfigured CODEOWNERS across all organizations for free forever, regardless how many users and repositories are scanned. The easy-button solution also fixes misconfigured CODEOWNERS. Here is a snapshot:
Here are the scenarios and test results in a single table:
We can summarize this even further using the following logic