Is it possible to restrict who can merge to a specific branch? I would like to only allow a couple of users to be able to merge into the master branch. Is this possible?
Hi Trent,
Yes you can do this with ScriptRunner for Bitbucket Server. Thanks for adding the correct tag to your question as well it helps us find these questions and respond to them swiftly.
You can do this by going to Admin -> Script Merge Checks -> Custom merge check and add the following code:
import com.atlassian.bitbucket.auth.AuthenticationContext import com.atlassian.bitbucket.scm.pull.MergeRequest import com.atlassian.sal.api.component.ComponentLocator def authenticationContext = ComponentLocator.getComponent(AuthenticationContext) def mergeRequest = mergeRequest as MergeRequest def pullRequest = mergeRequest.getPullRequest() def isMasterBranch = pullRequest.getToRef().getId() == "refs/heads/master" // add users who can merge to master here def allowedUserNames = ["user1", "user2", "user3"] def currentUser = authenticationContext.currentUser if (isMasterBranch && ! (currentUser.slug in allowedUserNames)) { mergeRequest.veto("Can not merge to master branch", "You are not allowed to merge to the master branch") }
I've indicated where you can add the users who can push to the master branch.
Let us know how you get on with that.
Adam
If you're on Bitbucket Server 4.5 or newer, you can do this without any add-ons using the "Branch Permissions" settings for your repo and setting the "Prevent all changes" restriction.
Screenshot here:
branch-perms.png
p.s. I invite you to try my add-on: Bit-Booster for Bitbucket Server
Thanks Adam! It is blocking merges correctly but not allowing the allowedUserNames to merge. Is it missing the else clause that allows the users in the list to have access?
Trent
It should already be doing that. If it is the master branch and the user is not in the list of allowed user names we don't allow the merge.
It seems like "currentUser.slug" is not in the allowedUserNames.
Try adding the following to that script and refresh the pull request page to see what users you have:
log.warn currentUser.slug log.warn allowedUserNames
I added that line to the bottom and had the user try again. I do not see anything on the pull request tab. What specifically am I looking for?
Thank you,
It looks like you're new here. Sign in or register to get started.