• Data Center / Server πŸ—„

  • Cloud ☁️

Want to apply Code Owners just for specific branches? πŸ—„

Destination branches: Inclusion

Per default, a Code Owner file affects pull requests for all destination branch names. You can make a Code Owner file to only apply pull requests with specific destination branches. For that you need to configure the destination_branch_pattern option in your Code Owner file, where you can configure a pattern for matching destination branches in pull requests.

For example, if you want to activate code owners only for pull requests for the destination branch master :

CODEOWNERS.destination_branch_pattern master
CODE

If you also want to have pull requests with release branches covered by Code Owners, also add this next line:

CODEOWNERS.destination_branch_pattern release/*
CODE

Source branches: exclusion (since 5.1)

Per default, a Code Owners file affects pull requests for all source branches. You can make a Code Owner file to be excluded for pull requests with specific source branches.
For that configure the source_branch_exclusion_pattern option in the Code Owners file.

For example, if you want to ignore code owners for pull requests created by a bot, which creates pull requests with source branches named e.g. like renovate/update-X :

CODEOWNERS.source_branch_exclusion_pattern renovate/*
CODE

Used pattern format is similar to the Atlassian branch permission patterns.

Want to have different CODEOWNERS file in subdirectories? (for monorepos) πŸ—„

Per default, only the Code Owners file at the top level of the repository is considered.

However, if your team has different projects in the same repository it can make sense to have a Code Owners file for different subdirectories. For that enable the CODEOWNERS.toplevel.subdirectory_overrides in the top-level Code Owners file. With that, any Code Owners file in a subdirectory determines the rules for that directory and it’s children. It replaces all rules set by the parent directories Code Owners file.


Here is an example:
In the top-level CODEOWNERS file:

CODEOWNERS.toplevel.subdirectory_overrides  enable
CODE

And then you can add Code Owner files for the subdirectories:

project-a/CODEOWNERS:

# Rules for Project A
* @@ProjectAOwners
src/*.java  @@JavaExperts
src/*.js    @@JavaScriptExperts
CODE

project-b/CODEOWNERS:

# Rules for Project B
* @@ProjectAOwners
src/*.cs  @@CSharpExperts
CODE

project-b/user-dos/CODEOWNERS:

# Rules for Project B's user documentation
* @@DocumentationTeam
*.md  @@DocumentationTeam
/images/* @@Designers
CODE

Why does the app not add a Code Owner as reviewer to the updated pull request? πŸ—„

  • Is the Code Owners app active in Repo settings?

  • The Code Owners app will check on rescope events from source branch of PR, if new Code Owners need to be added to the PR reviewer list and will do so.

    • But there is one exception since version 3 of the app:

      • If previously the new Code Owners got manually removed as reviewers from this PR, then you will need to add them manually as reviewers, in case a merge check requires their approval or if they should just review the PR.
        Note: If on PR creation some reviewers are assigned, but not all matching Code Owners are part of it, then the missing Code Owners are also treated as "manually removed". And if all reviewers are removed on PR creation, all Code Owners are re-add, because the app can not distinguish this case from a PR created via REST endpoint due to technical reasons.

What triggers the app to update the code owners of a pull request?

  • Changes on source branch πŸ—„ & ☁️

  • Changes on destination branch (not always visible in pull request activities) (only before version 3.1)

  • (starting with version 4.1) manually trigger Code Owners in pull request menu. πŸ—„

How to Remove Extra Code Owners Comment on Pull Request ? πŸ—„

By default, Code Owners adds a comment with the code owners and reason for their inclusion. Like any comment, this sends out a notification.

If you do not need this comment and want fewer notifications you can disable it by adding following line in the Code Owners file. It must be in the top-level Code Owner file.

CODEOWNERS.toplevel.create_pull_request_comment disable
CODE

How to create WIP/draft pull requests, where CODEOWNERS should not be added when pull request is created? πŸ—„

Currently, the app has no automated support for that.
But you can use the following manual workaround on pull request creation.

  1. In pull request creation wizard:

    1. Manually remove the CODEOWNERS as reviewers in the pull request creation wizard.

      1. Hint: Instead of clicking on cross on every reviewer, just place the cursor at the end of the list and remove them with pressing Delete key.

    2. Add at least one reviewer, which should review the draft pull request. Warning: If there is no reviewer at all, the app will re-add the CODEOWNERS after pull request creation.

    3. Create pull request

  2. As soon as the pull request is ready for final review:

    1. (starting with version 4.1) manually trigger Code Owners in pull request menu.

    2. Or add the removed CODEOWNERS manually as reviewers to the pull request.

      1. Check the skipped section in the comment the app creates on pull request creation, to get list of CODEOWNERS for this pull request.

How to require approval by Merge check for groups, but not for single users? πŸ—„

Create a merge check for that specific group or groups. It will not affect extra single users added to a code owner rule:

*.java  @@JavaDevs @SingleUser
Check(@@JavaDevs >= 2) # Will require merge checks from JavaDevs, but not from the @SingleUser
CODE

Need different Merge checks for different owner rules? (since 4.0) πŸ—„

  1. Define for each rule a separate Code Owners group:

    @@@rule_x @@Team
    CODE
  2. Use group in corresponding owner rule:

    *.x @@rule_x
    CODE
  3. Add a group based merge check for each rule:

    Check(@@rule_x >= 2)
    CODE

Is there an REST API for changing the Merge Checks settings? πŸ—„

Code Owners does not have a long-term stable REST API to enable/disable the merge checks. It uses the Bitbucket settings API for its merge check settings. You can call that API to configure the merge checks.
The endpoint for the settings are:

  • project-level: $YOUR_BITBUCKET_SERVER/rest/api/latest/projects/$PROJECT/settings/hooks/ch.mibex.bitbucket.codeowners%3Acodeowners-merge-check/enabled

  • repo-level: $YOUR_BITBUCKET_SERVER/rest/api/latest/projects/$PROJECT/repos/$REPO/settings/hooks/ch.mibex.bitbucket.codeowners%3Acodeowners-merge-check/enabled

To enable and change the Merge Check setting, use the project or repo-level endpoint and send a PUT request with the settings:

curl --request PUT $YOUR_BITBUCKET_SERVER/rest/api/latest/projects/$PROJECT/repos/$REPO/settings/hooks/ch.mibex.bitbucket.codeowners%3Acodeowners-merge-check/enabled \
--user '$ADMIN_USER:$PASSWORD' \
--header 'Content-Type: application/json' \
--data '{"approvals":"2","approvalsPerGroup":"", "disableMinApprovalsForRepoAdmin":""}'
CODE

An empty string is used for the default value.

To disable the Merge Checks settings, use a DELETE request:

curl --request DELETE $YOUR_BITBUCKET_SERVER/rest/api/latest/projects/$PROJECT/repos/$REPO/settings/hooks/ch.mibex.bitbucket.codeowners%3Acodeowners-merge-check/enabled \
--user '$ADMIN_USER:$PASSWORD'
CODE

To reset a repo-level setting back to inherited, use this delete request:

curl --request DELETE $YOUR_BITBUCKET_SERVER/rest/api/latest/projects/$PROJECT/repos/$REPO/settings/hooks/ch.mibex.bitbucket.codeowners%3Acodeowners-merge-check \
--user '$ADMIN_USER:$PASSWORD'
CODE

Want to disable Code Owners app for a specific repository or fork? (since 4.0) πŸ—„

Repository level:

  1. Go to Repository settings

  2. Click on ADD-ONS -> Code Owners

    1. Disable App for repo (since 5.1)

    2. Or if you just want do disable the auto-assignment, use option: Auto-assignment of Code Owners for current repository.

      1. No reviewers will be assigned to new PRs or updated PRs.

  3. Got to Merge Checks hooks of repository

    1. Disable Code Owners min approvals Merge Checks in Merge Checks hooks

      1. All Code Owners Merge Checks are ignored.

Project level (since 5.2):

  1. Go to Project settings

  2. Click on ADD-ONS -> Code Owners

    1. Enabled/disable app for project.

Why does the app add Code Owners to pull request for "unchanged" files/already merged files? (Bitbucket 6 only) πŸ—„

Bitbucket 6 uses a 3-way diff in the pull request view, which hides some changes of the commits, if the same changes are already in the destination branch.
If you use the Compare diff view to watch the diff for the source and destination branch, you will see that the files actually got changed in the source branch. The same effect can be observed when you view the diff in PR for the individual commits.
Bitbucket 7 uses the 2-way diff also in the pull request view, like in the Compare diff view.
And Code Owners also uses this 2-way diff to calculate the Code Owners.

Background:
Release notes 6.10: Section: 2-way diff for pull requests
Release notes 7.0: Section: A modified behavior for diffs

Is there Code Owners app for Bitbucket Cloud? ☁️

Yes, there is a Bitbucket Cloud edition of Code Owners.

How does escaping for Code Owners app work? πŸ—„ + ☁️

To use spaces in user, group names and glob expressions, put them in double quotes.

"test/file name with spaces.txt" @"Paul Winter" @@"Java Developers"
CODE

If your glob expression starts with a #, you need to escape # with a back slash: \

\#filewithFancyPrefix.txt @"Paul Winter"
CODE

How can I disable the Code Owners app on all projects? πŸ—„

The Code Owners app has an api to enable or disable the application for a given project. The following scripts will go through all existing project, and it will disable the Code Owners app for each of them (including the DevSensei auto-merge feature). In order for it to work, you need to fill in the information in lines 8, 10 and 12.

#!/usr/bin/env bash
# Disable Code Owners app for all Bitbucket DC projects, so that you can opt-in one-by-one.
set -e

# Requires curl and https://stedolan.github.io/jq/

# Admin user with Project admin permission
ADMIN_USER=
# Access token for above user with Project admin permission, see https://confluence.atlassian.com/bitbucketserver/http-access-tokens-939515499.html
ADMIN_ACCESS_TOKEN=
# Your bitbucket domain, e.g https://git.company.com
BITBUCKET_URL=

HTTP_AUTH="curl --user $ADMIN_USER:$ADMIN_ACCESS_TOKEN"

$HTTP_AUTH $BITBUCKET_URL/rest/api/latest/projects\?limit=1 | jq --raw-output '.values[] | .key' > projects.txt

# Code Owners
cat projects.txt | while read -r line ; do $HTTP_AUTH -X PUT $BITBUCKET_URL/rest/codeowners/1.0/projects/$line/settings/app-active/disabled ; done
# DevSensei Auto-merge
cat projects.txt | while read -r line ; do echo 'false' | $HTTP_AUTH -X PUT $BITBUCKET_URL/rest/codeowners/1.0/app-settings/projects/$line/settings/enable-devsensei ; done


echo "Go to $BITBUCKET_URL/projects and then to corresponding `Project settings -> Code Owners` to enable Code Owners app again in your some of the projects."
BASH