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

How to update a jira issue from bitbucket on a pull request using scriptrunner?

Jeff Chan June 14, 2017

Hi, 

I'm trying to write an event handler in Scriptrunner for BitBucket such that on the PullRequestMergedEvent it should find all related issues and add the fixVersion to it based on the release branch the request was merged to. 

I've been able to write a cURL request which does what I want, just to test that I was creating the proper payload for the request.  Here is the cURL request I made which corresponds to what I'd like the script to do:

curl -u user:pass -X PUT --data '{"update":{"fixVersions":[{"add": {"name":"Release 10-7"}}]}}' -H "Content-Type: application/json" https://server/rest/api/2/issue/JA-30

Where I've subbed out the username/password and server url components of course.  

Now I'm trying to do the same thing in groovy, I'm using the sample in the Scriptrunner for Bitbucket for updating all related issues with a comment about the pull request (https://scriptrunner.adaptavist.com/latest/bitbucket/StashEventHandlers.html#_update_all_related_jira_issues_when_pull_request_opened) with modifications to run in the script runner console.

import com.atlassian.applinks.api.ApplicationLinkResponseHandler
import com.atlassian.applinks.api.application.jira.JiraApplicationType
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.sal.api.net.Response
import com.atlassian.sal.api.net.ResponseException
import groovy.json.JsonSlurper
import com.atlassian.sal.api.net.Request

// Stuff that I added for handling the stuff I want to do
import groovy.transform.BaseScript
import com.onresolve.scriptrunner.canned.bitbucket.util.BitbucketBaseScript
import com.atlassian.bitbucket.pull.PullRequestSearchRequest
import com.atlassian.bitbucket.util.PageRequestImpl
import com.atlassian.bitbucket.pull.PullRequestService
import com.atlassian.applinks.api.ApplicationLinkRequest
@BaseScript BitbucketBaseScript baseScript

// This part here is only to retrieve a pull request. 
// Will be replaced by the pull request merged event in practice
def prService = ComponentLocator.getComponent(PullRequestService)
def prsrBuilder = new PullRequestSearchRequest.Builder();
prsrBuilder.fromRepositoryId(50);
def pullRequestSearch = prsrBuilder.build();
def pageRequest = new PageRequestImpl(0, 25);
def page = prService.search(pullRequestSearch, pageRequest);
def prIter = page.getValues().iterator();
def pr = prIter.next();

// Start of real work
def keys = jiraIssueService.getIssuesForPullRequest(50, pr.id)*.key;
def jiraLink = getJiraAppLink();

// making the request
def releaseVersion = "Release 10-7";
def builder = new StringBuilder();
builder.append("{ \"update\": ");
builder.append("{ \"fixVersions\": ");
builder.append("[{ \"add\": ");
builder.append("\"name\": \"" + releaseVersion + "\" }}]");
builder.append("}");
builder.append("}");
def input = builder.toString();

def handler = new ApplicationLinkResponseHandler<Map>() {
    @Override
    Map credentialsRequired(Response response) throws ResponseException {
        return null
    }

    @Override
    Map handle(Response response) throws ResponseException {
        assert response.statusCode == 200
        new JsonSlurper().parseText(response.getResponseBodyAsString()) as Map 
    }
}
def applicationLinkRequestFactory = jiraLink.createAuthenticatedRequestFactory() 

// Just so I can see what's in the response
def response; // Making the requests for each issue key found. keys.each { String key -> ApplicationLinkRequest aRequest = applicationLinkRequestFactory.createRequest(Request.MethodType.POST, "/rest/api/2/issue/$key"); aRequest.addHeader("Content-Type", "application/json"); aRequest.setEntity(input); response = aRequest.execute(handler); } // Just some sample return value in the script runner console return "Making the request as: " + input + " " + response

 

I can run a GET request, commenting out the entity body part and changing the request type, but when I try to do a post, I always get an HTTP 405 response instead of a 200.  It seems like it doesn't do the analogous "-X PUT" part of the curl request.  Can someone shed any light on what I need to change with my request to make this work properly?

BTW we are using JIRA Server Software 7.2.7 and Bitbucket Server 4.13.  We have Scriptrunner installed on both.  

Thanks in advance!

1 answer

Suggest an answer

Log in or Sign up to answer
0 votes
Jeff Chan June 15, 2017

Looks like in transposing my cURL request to the sript form, I missed a brace in the composition of the update body so that was why it was failing!  D'oh!

Katy Kelly
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 15, 2017

Hi Jeff,

I'm sorry to hear you had some difficulties, but glad to see you were able to locate the problem. Did adding the missing brace resolve this for you? If not let us know and we'd be happy to help troubleshoot further. 

Cheers!

Katy

Adaptavist Product Support 

Jeff Chan June 15, 2017

yes, gotta what those brackets!!  

Katy Kelly
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 15, 2017

They can be tricky! Glad to hear that fixed the issue. Have a great day!

Katy

TAGS
AUG Leaders

Atlassian Community Events