Adaptavist ScriptRunner: BuildStatusService not resolved in Merge Check script

Hi

I'm trying to check the build status of the last commit in a pull request but can't get the BuildStatusService to get resolved using ComponentLocator.getComponent(BuildStatusService). It always returns null.

This is my script which runs as Merge Check script.

import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.bitbucket.build.BuildStatusService
import com.atlassian.bitbucket.build.BuildState
import com.atlassian.bitbucket.build.BuildStatus
import com.atlassian.bitbucket.util.Page
import com.onresolve.scriptrunner.canned.bitbucket.util.BitbucketBaseScript
import groovy.transform.BaseScript

@BaseScript BitbucketBaseScript baseScript

def commitId = mergeRequest.pullRequest.fromRef.latestCommit

def buildStatusService = ComponentLocator.getComponent(BuildStatusService)
String debugMessage = "commitId=" + commitId + ", buildStatusService=" + buildStatusService
mergeRequest.veto("Pull request can't be merged", "Pull request can't be merged: " + debugMessage)

I copied the basic code from this link, but the result that I'm getting on the pull request 'Merge' button is:

Pull request can't be merged: commitId=c933d15fd5222d5d79e16b58c76de2a5874c6173, buildStatusService=null

I'm running on Atlassian Bitbucket Server v4.8.3. Any ideas why this does not work?

Thx, Andi

1 answer

0 vote

You should be able to get the BuildStatusService by using:

import com.atlassian.bitbucket.build.BuildStatusService
import com.onresolve.scriptrunner.runner.ScriptRunnerImpl
import com.onresolve.scriptrunner.runner.customisers.WithPlugin

@WithPlugin("com.atlassian.bitbucket.server.bitbucket-build")

def buildStatusService = ScriptRunnerImpl.getOsgiService(BuildStatusService)

As it's listed as an OSGI service in the bitbucket-build plugin you need to use @WithPlugin and ScriptRunnerImpl.getOsgiService

Thanks a lot for the info! Using this approach it works perfectly.

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Posted Jun 12, 2018 in Bitbucket

Do you use any Atlassian products for your personal projects?

After spinning my wheels trying to get organized enough to write a book for National Novel Writing Month (NaNoWriMo) I took my affinity for Atlassian products from my work life and decided to tr...

27,820 views 26 12
Join discussion

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you