Hello, I am creating a merge check that will block the merge button in case there is a ongoing build on master.
I am trying to do this in script runner but since its running in Groovy sandbox I can't perform http request to the bitbucket rest API and fetch the status.
How can I achieve this??
A custom Merge Check with this script should do the job :
````
import com.atlassian.bitbucket.hook.repository.RepositoryHookResult
import com.atlassian.bitbucket.build.BuildState
import com.onresolve.scriptrunner.canned.bitbucket.util.BitbucketPageUtils
import com.atlassian.bitbucket.build.status.RepositoryBuildStatus
import com.atlassian.bitbucket.repository.Ref
import com.atlassian.bitbucket.repository.RefService
import com.atlassian.bitbucket.repository.ResolveRefRequest
import com.atlassian.bitbucket.build.status.BuildStatusRepositorySearchRequest
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.bitbucket.build.status.RepositoryBuildStatusService
import com.atlassian.bitbucket.repository.Branch
import static com.atlassian.bitbucket.repository.StandardRefType.BRANCH
def repositoryBuildStatusService = ComponentLocator.getComponent(RepositoryBuildStatusService)
def refService = ComponentLocator.getComponent(RefService)
def resolveRefRequest = new ResolveRefRequest.Builder(mergeRequest.pullRequest.toRef.repository)
.type(BRANCH)
.refId(mergeRequest.pullRequest.toRef.id)
.build()
def branch = refService.resolveRef(resolveRefRequest) as Branch
log.info("Will scan for builds on branch ${branch}")
def buildSearchRequest = new BuildStatusRepositorySearchRequest.Builder(mergeRequest.pullRequest.toRef.repository)
.ref(branch as Ref)
.build()
Iterable<RepositoryBuildStatus> builds = BitbucketPageUtils.pagedIterable(repositoryBuildStatusService.&search, buildSearchRequest)
def buildInProgrsess = false
log.info("found ${builds.size()} builds")
builds.each {
if (it.state == BuildState.INPROGRESS) {
buildInProgrsess = true
log.info("Blocked by Build on commit ${it.commit} with status ${it.state}")
return
}
}
if (buildInProgrsess) {
RepositoryHookResult.rejected("Rejected because build is in progress on destination branch", "Detailed information about merge rejection.")
}
````
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.