Auto-merge pull request if approved and build successful
Asked by cwoody:

I'm currently working in bitbucket 7.20, I trying to use ScriptRunner to have pull requests auto-merged if there are 2 approvals and the build is successful. I've been able to get the approvals, but haven't been able to get the build status back for a pull request.

Here is the code I'm trying to use:

import com.atlassian.bitbucket.build.BuildStatusService  
import com.onresolve.scriptrunner.runner.ScriptRunnerImpl  
import com.onresolve.scriptrunner.runner.customisers.WithPlugin  
import com.atlassian.bitbucket.event.pull.PullRequestParticipantApprovedEvent  
import com.atlassian.bitbucket.integration.jira.JiraIssueService  
import com.atlassian.bitbucket.pull.PullRequest  
import com.atlassian.bitbucket.pull.PullRequestMergeRequest  
import com.atlassian.bitbucket.pull.PullRequestService  
import com.atlassian.bitbucket.user.SecurityService  
import com.atlassian.bitbucket.util.Operation  
import com.atlassian.sal.api.component.ComponentLocator  
import com.atlassian.sal.api.net.Request  
import com.atlassian.sal.api.net.Response  
import com.atlassian.sal.api.net.ResponseException  
import com.atlassian.sal.api.net.ReturningResponseHandler  
import com.onresolve.scriptrunner.canned.bitbucket.util.BitbucketBaseScript  
import com.onresolve.scriptrunner.runner.customisers.WithPlugin  
import groovy.transform.BaseScript  
import com.atlassian.bitbucket.scm.pull.MergeRequest

@BaseScript BitbucketBaseScript baseScript

<br />

def buildStatusService = ComponentLocator.getComponent(BuildStatusService)  
def securityService = ComponentLocator.getComponent(SecurityService)  
def pullRequestService = ComponentLocator.getComponent(PullRequestService)

def pullRequests = \[\]  
def repository = null  
def execute_branch = \["feature/OSC-1818-Test-Branch"\]  
def summaries = buildStatusService.getSummaries(event.pullRequest.getCommits()\*.id)  
def successfulBuild = summaries.values().find {it.successfulCount \> 0}

This code won't compile once it is saved. It throws the following errors:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:  
Script22.groovy: 32: \[Static type checking\] - You tried to call a method which is not allowed: java.util.Map #values()  
@ line 32, column 23.  
def successfulBuild = summaries.values().find {it.successfulCount \> 0}  
\^

Script22.groovy: 32: \[Static type checking\] - You tried to call a method which is not allowed: com.atlassian.bitbucket.build.BuildSummary#getSuccessfulCount()  
@ line 32, column 48.  
ld = summaries.values().find {it.success  
\^

Script22.groovy: 31: \[Static type checking\] - Property is not allowed: java.lang.Iterable #id  
@ line 31, column 49.  
ildStatusService.getSummaries(event.pull

Any assistance would be appreciated.


Answers:
author: Robert Giddings [Adaptavist]
body: Hi [@cwoody](/t5/user/viewprofilepage/user-id/4737226) ,

Thank you for your post.

Please can you confirm if you are a Project or Repository Admin? As currently some Bitbucket objects are not available for use directly by Project or Repository Admins.

Does your script work, if you are able to get a Bitbucket Administrator to add it to the global ScriptRunner config?

Kind regards,

Robert Giddings,

Product Manager, ScriptRunner for Bitbucket

comments: author: cwoody
body: I am a project admin. I'll need to get with an administrator. I'll post as soon as I have an answer.


author: cwoody
body: [@Robert Giddings \[Adaptavist\]](/t5/user/viewprofilepage/user-id/1196283) I'm try to work with an administrator. What exactly needs added to the global ScriptRunner config?


author: Robert Giddings [Adaptavist]
body: Hi [@cwoody](/t5/user/viewprofilepage/user-id/4737226) ,

Sorry for the late reply!

To answer your last question -

Whatever ScriptRunner object you were trying to create in ScriptRunner at the Project level (I assume it is a Listener?), you need to ask a global administrator to create the same type of ScriptRunner object in the global admin area of Bitbucket and use your Script.

Your global admin's have more permissions allowing them to run less restrictive code, which is what is currently causing your Script errors.

Please let me know if that helps answer your question? Or if you need more help?

Kind regards,

Robert Giddings,

Product Manager, ScriptRunner for Bitbucket


Tags: bitbucket-server

com.atlassian.bitbucket.build.buildstatusservice

scriptrunner