merge pullrequest under certain condition

I want to allow a merge of a pullrequest only under a certain condition.

I have written a script runner script for that:


import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.bitbucket.user.UserService

def userService = ComponentLocator.getComponent(UserService)
if (mergeRequest.pullRequest.pathsMatch("glob:**/spec/**") )
    mergeRequest.pullRequest.reviewers.findAll { it.approved }.any {
        userService.isUserInGroup(it.user, "electronics-lead")
    return true;


This script should do a special condition only when files in a folder with name "spec" were touched, modified or deleted.

Remark the else case with the `return true`. When the folder was touched some the additional condition jumps in, otherwise other scripts will run..

Is my approach above correct?



Where do I add this script?

I see following options:

a) "Script Merge Check" under "Require a number of approvers"

b) "Script Event Handlers" under "Auto merge of pull request"; here we have already a script.


2 answers

1 accepted

1 vote
Answer accepted

You can do this using the following script in a custom merge check. Go to Admin -> Script Merge Checks -> Custom merge check and add the following script:

import com.atlassian.bitbucket.user.UserService
import com.atlassian.sal.api.component.ComponentLocator

def userService = ComponentLocator.getComponent(UserService)

if (mergeRequest.pullRequest.pathsMatch("glob:**/spec/**")) {
    def group = "electronics-lead"
    def approved = mergeRequest.pullRequest.reviewers.any {
        it.approved && userService.isUserInGroup(it.user, group)

    if (! approved) {
        mergeRequest.veto("Pull request not approved", "Changes to spec folder must be approved by reviewer from $group group")

Hope this helps.

I don't find this "Custom merge check":

I have following under "Merge Checks"Picture002.jpg

You can't do this from the repository settings menu. You have to set it up from the system admin section of ScriptRunner due to security issues. You can read about them here.

Do you have appropriate access to do this? We have something in the pipeline that will make them available to project/repo admins in the future.

When I click on "Custom merge check" nothing pops up, no edditing field. There seems to be a bug...don't knowpic3.jpg

Which version of ScriptRunner and Bitbucket Server are you using? I'll try to reproduce then.

Are there any error messages in the browser console logs, when you click on the custom merge check? You can find out how to do this for example for Chrome here.

I get following error:

"NetworkError: 500 Internal Server Error - http://bitbucket/rest/scriptrunner-bitbucket/latest/mergechecks/com.onresolve.scriptrunner.canned.bitbucket.mergechecks.SimpleScriptedMergeCheck/params"


What version of Stash/Bitbucket are you using?

What version of ScriptRunner are you using?

4.3.14 is not compatible with Bitbucket Server 4.14.4. You should upgrade to ScriptRunner 5.0.6.

You can check version compatibility here:

Let us know how you get on after that.

How can I perform the installation? There is no automatic upgrade button in bitbucket. So I have to download the *.jar file.

How do I install this JAR file?

There are instructions here for manually upgrading this:

You can click download on the version here to get the JAR file.

Okay, I upgrade now. What a struggle...

Anyway, I added my script unter the admin pannel and "Sciript Merge Checks" and specified the repository.

However, I don't understand the "Custom Merge Check" button under the project setting of the repository. What can I do with this?

Can you take a screenshot of where you have added the script please?

That's all you should need to do. I was just saying previously that the custom merge check item is not available from the repository settings script merge checks menu. It's only available to global admins.

The thing is, only the administrator can create these kind of scripts.

I thought I can create the script and project admins can enable it on the repository they like to.

In script Merge Checks of the repository I can only choose between two scripts, which I find a little bit strange


This is for security reasons as I linked you to here.

You can write a script but not an inline script that the project admins can enable but it takes a bit of work. You can take a look at the scripts already available by unzipping the ScriptRunner JAR file and you can find one under: com/onresolve/scriptrunner/canned/bitbucket/mergechecks/RequiredApproversMergeCheck.groovy

The @PerRepoCheck enables this. The file then needs to be placed under one of your script roots, explained here.

We're working on exactly what you want in SRBITB-233, so keep an eye on that.

0 votes

Could you please clearly state what your requirement is.

You have mentioned that when files in the folder "spec" are modified you want to return true.

Does this mean you want to allow of prevent the merge?

You've not mentioned what your requirement for approval by one reviewer from the "electronics-lead" group has on preventing/allowing the merge.

I'll be able to give a more specific answer after that, along with some code to help you with this.

when a file in the folder named "spec" was modified, then the merge should only be done, when a reviewer from the group "electronics-lead" has approved the pull request, otherwhise no merge should be possible.

Does this clarify everything?

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Mar 14, 2019 in Bitbucket Pipelines

Building a Bitbucket Pipe as a casual coder :  #!/bin/bash source "$(dirname "$0")/" enable_debug extra_args="" if [[ "${DEBUG}" == "true" ]]; then extra_args="--verbose" fi # mandatory variables R...

280 views 0 12
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you