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-30Where 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!
In your description, you mentioned:-
I want to hide fields based upon the following fields-
- Edit Only
- Edit and Create
- Create Only
When you say Edit Only, Edit and Create and Create only are you referring to the Create and Edit Screens?
If yes, then you can use the underlyingIssue condition to determine the visibility of the fields. The underlyingIssue is only accessible once the issue has been created.
So if you want to do it for only the create screen, you and use the Behaviour Initialiser and add this condition:-
def field = getFieldByName('Some Field')
field.hidden = false
/*
This is for the Create Screen only
*/
if (!underlyingIssue) {
field.hidden = true
}
Below is a screenshot for your reference:-
If you want to add it for the edit screen only, you will need to use the Behaviour Initialiser with the sample code below:-
def field = getFieldByName('Some Field')
field.hidden = false
/*
This is for the Edit Screen only
*/
if (underlyingIssue) {
field.hidden = true
}
Below is a screenshot for your reference:-
For both, you will need to use the Server-Side Behaviour and set it according to the values of a particular field.
def field1 = getFieldById(fieldChanged)
def field1Value = field1.value.toString()
def field2 = getFieldByName('Some Field')
field2.hidden = false
if (field1Value == 'Some Value') {
field2.hidden = true
}
Below is a screenshot for your reference:-
Please note that the sample codes above are not 100% exact to your environment. Hence, you will need to make the required modifications.
I suggest you take a look at the Adaptavist Documentation to start.
I hope this helps to answer your question. :-)
Thank you and Kind regards,
Ram
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Great to hear that the solution worked for you. :)
Please accept the answer.
Thank you and Kind regards,
Ram
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Jyoti Kumari ,
Instead of writing behaviour it’s better to add different create and edit screen.
Thanks,
Vikrant Yadav
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello @Vikrant Yadav
Thank you for the suggestion, but I needed this to be implemented by script runner.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.