I have scriptrunner installed so I believe a sufficiently clever pre-recieve hook could do the trick.
Any input?
TIA
/anders
Hello,
I'm Reece, an engineer from Adaptavist working on ScriptRunner for Bitbucket. ScriptRunner does include a built in pre hook for branch naming enforcement, however what you describe is slightly more specialised.
As you guessed, you can achieve the behaviour you desire with a custom pre hook script.
The following custom pre hook script will prevent pushing of a branch if another branch exists with a case insensitive match on it's name:
import com.atlassian.bitbucket.repository.RepositoryBranchesRequest
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.bitbucket.repository.RefService
import com.atlassian.bitbucket.repository.Branch
import com.atlassian.bitbucket.repository.RefChangeType
import com.atlassian.bitbucket.repository.BranchCallback
def refService = ComponentLocator.getComponent(RefService)
def repoBranchesRequest = new RepositoryBranchesRequest.Builder(repository).build()
def caseInsensitiveBranchRefsInRefChanges = refChanges
.findAll { it.type == RefChangeType.ADD }
.findAll { it.ref.id.startsWith("refs/heads/") }
.collect { it.ref.id.toLowerCase() }
.toSet()
def branchCallback = new BranchCallback() {
@Override
boolean onBranch(Branch branch) {
if (caseInsensitiveBranchRefsInRefChanges.contains(branch.id.toLowerCase())) {
def msg = "Branch (${branch.displayId}) already exists case insensitively in repository"
resultBuilder.veto(msg, msg)
return false
}
return true
}
}
refService.streamBranches(repoBranchesRequest, branchCallback)
resultBuilder.build()
To set this up, browse to Script Pre Hooks in Bitbucket administration and then create a new Custom script hook. Configure the hook with the triggers branch-create and push.
You can find more information about pre hooks in ScriptRunner for Bitbucket via our documentation: Pre receive hooks documentation
If you have any further questions please let me know.
Cheers,
Reece
Hi @Reece Lander _ScriptRunner - The Adaptavist Group_
This is just what I was looking for :-)
The inline script parser says 'the variable [resultBuilder] is undeclared' in the last line, but the script works like a charm.
Thanks
/anders
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Anders,
The warning about resultBuilder is a bug, the builder is in fact present for the script to interact with. We will get that bug addressed in the next release of ScriptRunner for Bitbucket.
Kind regards,
Reece
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
A pre-receive hook won't do anything to prevent users from creating badly-named branches on their own systems. All it will do is prevent users from pushing those badly-named branches to your Bitbucket Server instance.
If that's acceptable to you, then a pre-receive hook (or perhaps an update hook) should be plenty. Otherwise, you'll need to find another way to enforce your branch naming scheme.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @jredmond
I appreciate the fact that git is distributed and my control is limited to what happens server-side,
If my users repeatedly gets their branches rejected when they push, as a consequence of not following the standard (or even using the Create Branch button in Jira), I do believe they will learn eventually.
Any hints on what to write in my hook?
TIA
/Anders
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.