Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

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

Ankit Kumar May 6, 2024

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

1 answer

0 votes
Ulrich Kuhnhardt _IzymesCo_
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
May 7, 2024

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.

Ankit Kumar May 8, 2024

I am doing this because it was suggested by my team member.

Ankit Kumar May 8, 2024

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.





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")


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)






However, I am currently unable to do so.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events