How to Merge the Pull Request when the JIRA ticket is closed
Asked by Ankit Kumar:

How to Merge the Pull Request in bitbucket when the JIRA ticket is closed


Answers:
author: Ulrich Kuhnhardt _IzymesCo_
body: Hello Ankit,

could you please elaborate on the above scenario?

For example, why do you need to merge a pull request in a Jira issue?

Keen to learn about your use case.

comments: author: Ankit Kumar
body: I am doing this because it was suggested by my team member.


author: Ankit Kumar
body: To automatically merge the corresponding pull request upon closing the associated JIRA issue, I have two options:

**Option 1: Implement a custom script as a Post-function on the issue closure transition.**

**Option 2: Set up a Custom Listener script that listens to Issue Closed events.**

<br />

<br />

<br />

<br />

i am using **Option 1: Implement a custom script as a Post-function on the issue closure transition.**   

import com.atlassian.jira.component.ComponentAccessor  
import java.net.HttpURLConnection  
import java.net.URL  
import java.util.Base64

// Function to merge the pull request  
void mergePullRequest(String projectKey, String repositorySlug, String pullRequestId, String username, String password) {  
try {  
def baseUrl = "_____________________________"  
def mergePullRequestUrl = "${baseUrl}/rest/api/latest/projects/${projectKey}/repos/${repositorySlug}/pull-requests/${pullRequestId}/merge"

def authString = "${username}:${password}".bytes.encodeBase64().toString()

// Send POST request to merge the pull request  
def mergeRequest = new URL(mergePullRequestUrl).openConnection() as HttpURLConnection  
mergeRequest.setRequestProperty('Authorization', "Basic ${authString}")  
mergeRequest.setRequestProperty('Content-Type', 'application/json')  
mergeRequest.setRequestMethod("POST")  
mergeRequest.doOutput = true  
mergeRequest.outputStream.write('{}'.bytes()) // Empty JSON body for merge

if (mergeRequest.responseCode == 200) {  
println("Pull request PR-${pullRequestId} merged successfully")  
} else {  
println("Failed to merge pull request (status: ${mergeRequest.responseCode})")  
println("Response Message: ${mergeRequest.responseMessage}")  
}  
} catch (Exception e) {  
println("Error merging pull request: ${e}")  
e.printStackTrace()  
}  
}

// Get the current issue object  
def issue = issue  
//def issue = issueManager.getIssueObject("TFA-87")

<br />

def repositorySlug = "test-1"  
def projectKey = "TPFA"

// Extract PullRequest ID from the "Pull Request ID" custom field  
def customFieldName = "Pull Request Id"  
def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName(customFieldName)

if (!customField) {  
throw new RuntimeException("Custom field '${customFieldName}' not found.")  
}

def pullRequestId = issue.getCustomFieldValue(customField)  
if (!pullRequestId) {  
println("Custom field '${customFieldName}' is empty for the issue.")  
return  
}

println("Pull Request ID: ${pullRequestId}")

def username = "_______"  
def password = "_______"

mergePullRequest(projectKey, repositorySlug, pullRequestId, username, password)  

<br />

<br />

<br />

<br />

<br />

**However, I am currently unable to do so.**   


Tags: bitbucket-data-center

data-center