Bitbucket auto merge pull requests when conditions are met

emrah.gullu November 23, 2018

Hello,

I've tried this solution "https://community.atlassian.com/t5/Bitbucket-questions/Bitbucket-auto-merge-pull-requests-when-conditions-are-met/qaq-p/212032"  but i have several errors.

By testing I have figured out that a merge, triggered from adaptavist ScriptRunner, can only happen if the pull request "conditions" are met.
In my case:
✓ Requires 1 approvers
✓ Requires all tasks to be resolved 
✓ Requires a minimum of 1 successful builds 

I am using the adaptavist ScriptRunner for Bitbucket version 5.4.41 and bitbucket server

The script is as follows:

import com.atlassian.bitbucket.build.BuildStatusServiceimport com.atlassian.bitbucket.event.pull.PullRequestApprovedEventimport com.atlassian.bitbucket.integration.jira.JiraIssueServiceimport com.atlassian.bitbucket.pull.PullRequestimport com.atlassian.bitbucket.pull.PullRequestMergeRequestimport com.atlassian.bitbucket.pull.PullRequestServiceimport com.atlassian.bitbucket.user.SecurityServiceimport com.atlassian.bitbucket.util.Operationimport com.atlassian.sal.api.component.ComponentLocatorimport com.atlassian.sal.api.net.Requestimport com.atlassian.sal.api.net.Responseimport com.atlassian.sal.api.net.ResponseExceptionimport com.atlassian.sal.api.net.ReturningResponseHandlerimport com.onresolve.scriptrunner.canned.bitbucket.util.BitbucketBaseScriptimport com.onresolve.scriptrunner.runner.customisers.WithPluginimport groovy.transform.BaseScript
//✓ Requires 1 approvers//✓ Requires all tasks to be resolved//✓ Requires a minimum of 1 successful build
@BaseScript BitbucketBaseScript baseScript
def buildStatusService = ComponentLocator.getComponent(BuildStatusService)def jiraIssueService = ComponentLocator.getComponent(JiraIssueService)def securityService = ComponentLocator.getComponent(SecurityService)def pullRequestService = ComponentLocator.getComponent(PullRequestService)
def pullRequests = []def repository = null
if (event instanceof PullRequestApprovedEvent) { def event = event as PullRequestApprovedEvent
pullRequests.add(event.pullRequest)} // we need to gather the pull requests for each event using above approach
// go through each pull request and check conditionspullRequests.each { pullRequest -> gt; // require 1 approver if (pullRequest.participants.find { it.approved }) { def summaries = buildStatusService.getSummaries(pullRequest.getCommits()*.id)
def successfulBuild = summaries.values().find { it.successfulCount > 0 }
// require 1 successful build if (successfulBuild) {
def issueKeys = jiraIssueService.getIssuesForPullRequest(repository, pullRequest.id)*.key
def jiraLink = getJiraAppLink()
def authenticatedRequestFactory = jiraLink.createImpersonatingAuthenticatedRequestFactory()
def allIssuesResolved = issueKeys.every { String key -> def response = authenticatedRequestFactory .createRequest(Request.MethodType.GET, "/rest/api/2/issue/$key?fields=status") .addHeader("Content-Type", "application/json") .executeAndReturn(new ReturningResponseHandler<Response, Map>() { @Override Map handle(Response response) throws ResponseException { if (response.statusCode != HttpURLConnection.HTTP_OK) { throw new Exception(response.getResponseBodyAsString()) }
return response.getEntity(Map) } })
response.fields.status.name == "Done" }
// require all issues to be resolved - this doesn't handle subtasks or linked issues if (allIssuesResolved) { mergePullRequest(securityService, pullRequest, pullRequestService) } } }}
private Object mergePullRequest(SecurityService securityService, PullRequest pullRequest, pullRequestService) { securityService.impersonating(pullRequest.author.user, "Merging pull-request on behalf of author").call(new Operation<Object, RuntimeException>() { @SuppressWarnings("ConstantConditions") @Override public Object perform() { pullRequestService.merge(new PullRequestMergeRequest.Builder(pullRequest).message("Automatically merged").build()) } })}

Thanks and Regards

 

0 answers

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events