Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How do I prevent creation of branches whose names that only differ by case

A. Truelsen August 6, 2019

I have scriptrunner installed so I believe a sufficiently clever pre-recieve hook could do the trick.

Any input? 

 

TIA

/anders

2 answers

1 accepted

1 vote
Answer accepted
Reece Lander _ScriptRunner - The Adaptavist Group_
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.
August 7, 2019

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

A. Truelsen August 8, 2019

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

Reece Lander _ScriptRunner - The Adaptavist Group_
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.
August 8, 2019

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

Like A. Truelsen likes this
0 votes
jredmond
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
August 6, 2019

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.

A. Truelsen August 6, 2019

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

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events