Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

merge pullrequest under certain condition

xion_admin June 6, 2017

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")
    }
}
else
{
    return true;
}

1)

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?

2)

 

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
adammarkham
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 7, 2017

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.

xion_admin June 7, 2017

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

I have following under "Merge Checks"Picture002.jpg

adammarkham
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 7, 2017

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.

xion_admin June 7, 2017

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

adammarkham
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 7, 2017

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.

xion_admin June 7, 2017

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?
    4.14.4

What version of ScriptRunner are you using?
    4.3.14 

adammarkham
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 7, 2017

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: https://marketplace.atlassian.com/plugins/com.onresolve.stash.groovy.groovyrunner/versions

Let us know how you get on after that.

xion_admin June 7, 2017

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?

adammarkham
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 7, 2017

There are instructions here for manually upgrading this: https://confluence.atlassian.com/upm/installing-add-ons-273875715.html

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

xion_admin June 14, 2017

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?

adammarkham
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 14, 2017

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.

xion_admin June 15, 2017

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

pic_1.jpg

adammarkham
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 15, 2017

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
adammarkham
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 6, 2017

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.

xion_admin June 7, 2017

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
TAGS
AUG Leaders

Atlassian Community Events