It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Check if a branch exists as part of a Scriptrunner pre-receive hook?

RJ Bernau Dec 01, 2016

If I'm reading the stash documentation right (https://developer.atlassian.com/static/javadoc/stash/3.9.2/api/reference/com/atlassian/stash/repository/Repository.html) it appears that there is no way to get a list of branches or even commits associated with a repository?

1 answer

1 accepted

2 votes
Answer accepted
Adam Markham [Adaptavist] Community Leader Dec 01, 2016

You can get the branches and commits in a custom pre-receive hook like so:

import com.atlassian.stash.repository.Repository
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.stash.repository.RepositoryMetadataService
import com.atlassian.stash.repository.RepositoryBranchesRequest
import com.atlassian.stash.util.PagedIterable
import com.atlassian.stash.repository.Branch
import com.atlassian.stash.util.PageRequest
import com.atlassian.stash.repository.RefChange
import com.atlassian.stash.util.PageProvider
import com.atlassian.stash.util.Page

def repositoryMetadataService = ComponentLocator.getComponent(RepositoryMetadataService)

def repository = repository as Repository
def refChanges = refChanges as Collection<RefChange>

def request = new RepositoryBranchesRequest.Builder().repository(repository).build()

def branches = new PagedIterable<Branch>(new PageProvider<Branch>() {
    @Override
    Page<Branch> get(PageRequest pageRequest) {
        repositoryMetadataService.getBranches(request, pageRequest)
    }
})

def commits = refChanges.getCommits(repository)

Please note that we stopped supporting Stash some time ago in ScriptRunner for Bitbucket Sever, as Atlassian renamed all the packages when they rebranded to Bitbucket which made it very difficult to support both. I would recommend upgrading if you can to get the latest features.

Hope this helps.

Adam

RJ Bernau Dec 02, 2016

If it was up to me we would update in December when we go into a change freeze for the holidays, but we're scheduled to upgrade to whatever the latest stable version sometime in Q2 2017 sad Better late than never I guess. Thanks for the help.

Adam Markham [Adaptavist] Community Leader Dec 02, 2016

No problem. Hopefully you will benefit from the new features and bug fixes when you do. We have a page on updating your ScriptRunner scripts to be compatible with Bitbucket here: https://scriptrunner.adaptavist.com/latest/bitbucket/releases/UpgradingToBitbucketServer.html

RJ Bernau Dec 02, 2016

I'm getting an error when I try to save that script and I can't even find the PageProvider class in the stash API, is there somewhere else I should be looking for these things?


 

The script could not be compiled:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script119.groovy: 17: unable to resolve class PageProvider 
 @ line 17, column 69.
   h>(new PageProvider() {
                                 ^

Script119.groovy: 18: unable to resolve class Page 
 @ line 18, column 5.
       @Override
       ^

2 errors
Adam Markham [Adaptavist] Community Leader Dec 02, 2016

I've updated the example I think you should be able to run it now i've fixed the imports.

Suggest an answer

Log in or Sign up to answer
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Published in Bitbucket Pipelines

Building a Bitbucket Pipe as a casual coder

...ipe.sh :  #!/bin/bash source "$(dirname "$0")/common.sh" enable_debug extra_args="" if [[ "${DEBUG}" == "true" ]]; then extra_args="--verbose" fi # mandatory variables R...

1,865 views 1 19
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you