Bitbucket Server API - Disable Merge button conditionally for certain branches

I need to require all reviewers to approve but I cannot use the global “All reviewers approve” Merge check. Teams need the freedom to handle their own reviews on feature/bug branches.
For certain branches, we have a custom plugin that does many other checks. In preUpdate of RepositoryMergeCheck, I can check if all reviewers approved and return RepositoryHookResult.rejected() but that puts up a warning that says Bitbucket Server could not check this pull request for conflicts.
If I don’t reject it on the page load and leave the Merge button clickable, I will still be able to stop the merge if all conditions aren’t met. I would like to avoid the daily questions about why the merge button is clickable.
Any ideas? Having a branch pattern on “All reviewers approve” would solve this.
Thanks.

2 Likes

Hi Edward

With Scriptrunner you could use a Custom Merge Check, and write a script like this:

    def approvedBy = mergeRequest.pullRequest.reviewers.findAll { it.approved }.collect { it.user }

    def allReviewersHaveApproved = mergeRequest.pullRequest.reviewers.every { reviewer -> 
        approvedBy.any { it.id == reviewer.user.id }
    }

    if (!allReviewersHaveApproved) {
        RepositoryHookResult.rejected("Reject message summary", "Detailed information about merge rejection.")
    }

A custom merge check can be applied on specific projects and/or repositories, but you can filter the branches in the script as well with something like :

if (mergeRequest.pullRequest.toRef.displayId in ['branch1', 'branch3', .. ]) {
...
}

Hope this helps! Let me know if you have any questions

We just added a feature to support this use case to our paid plugin (look for “Control Freak” in marketplace).

Here’s a screenshot: