Just a heads up: On March 24, 2025, starting at 4:30pm CDT / 21:30 UTC, the site will be undergoing scheduled maintenance for a few hours. During this time, the site might be unavailable for a short while. Thanks for your patience.
×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.
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"
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
When I click on "Custom merge check" nothing pops up, no edditing field. There seems to be a bug...don't know
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.