Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Scriptrunner (Jira Cloud) Transition An Issue When Linked Issues, are Resolved.

Adam Dulberg
Contributor
November 13, 2018

Hello Community,

I would like to create a post-function script to transition an issue [issue-x] from status-y to status-z when all linked issues to [issue-x] via a [custom] link type, are resolved. Running Jira Cloud.

Not sure the type of link matters, figure I'd mention it.

There are dozens of posts along this topic, both here and on Adaptavist related blogs, however either they are outdated or seem to be geared towards hosted Jira. I've used the script console to poke around and attempt to pull bits and pieces from various posts, alas have failed. Being new to groovy and in the interest of learning, I would very much appreciate if someone could help me to generate this first script. I have access to the customfield ids and any other info required to plug in. 

Have a feeling this could help a lot of people who are in my position.

Thanks ahead.

2 answers

1 accepted

0 votes
Answer accepted
Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 19, 2018

Hi Adam,

Thank you for your question.

I have created an example script located here which you can run on the script console inside of Jira Cloud in order to get all of the linked issues for an issue and to transition if the issue if all the issues are in a specified closed status.

You will be able to use this script as a reference to see how you can get all linked issues and to transition the issue when they are all closed so that you can create the script which you require.

If this response has answered your question can you please mark it as accepted so that other users can see it is correct when searching for similar answers.

Regards,

Kristian

Adam Dulberg
Contributor
January 3, 2019

Hi Kristian,

 

Happy new year and thanks for getting back to me. Before the holidays I spent time with the sample query you shared. The script console gave me errors, notably at line 24 (minus the legal text). Due to that legal text I'm not sure what I am allowed to mention publicly, however, it looks like there are a few ways to handle iterating on the FindAll function which is where this gets caught up. 

I am available to chat offline to work out the nuances and return to the thread here with a resolution if that is preferred. 

 

Thanks again,

 

Adam

Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 4, 2019

Hi Adam,

Thank you for your response.

Can I please ask you to confirm if in the sample script shared if you updated lines 10, 13 and 17 to include the Issue Key, Status ID and transition ID by removing the text that contains the  < > angle brackets and by replacing it with just the value inside of the quotation marks.

If you are still encountering issues can you please post a screenshot of the error that you are receiving so that we can advise on this thread how to resolve the issue. 

Thanks

Kristian

Adam Dulberg
Contributor
November 22, 2019

Hey Kristian - nearly a year later and I've finally made my way back. I've accepted the answer. Thank you for the most part I didn't need to make any edits, yet I'm going to call out several things that folks should be aware of, when considering this action

  1. Line 13:

    // Specify the name of the version to extract from the issue
    def closedStatus = '<ClosedStatusHere>'

    There could be x number of tickets linked who follow various workflows utilizing different done statuses. Thus the Closed status I don't think works. I think we're looking for the "ClosedStatusCategory" [of the linked tickets]; "Done".

    1. Which would change line 31 ......fields.status.statusCategory.name
    2. And line 36 ......fields.status.statusCategory.name
  2. Tripped myself up on line 46 - I didn't initially declare the transitionID as a variable. My suggestion for the documentation is making it clear there is a variable:
    .body([transition: [id: "${transitionID}"]])

Lastly, used as a listener this is going to be an extremely expensive call. Unless I'm missing something, Issue Updated is the only event for us Cloud folks that we can use, and that event runs on everything. Folks should be advised to have conditions which will narrow the scope to prevent running this script on every issue updated event for a given project(s).

0 votes
Musku Nagaraju
Contributor
June 27, 2019

Hi @Kristian Walker _Adaptavist_ , / @Adam Dulberg ,

I am on cloud JIRA

I am trying to achieve the same what you discussed above, I have used your code but i am getting error. please have a look my attached screen short .

 

def issueKey = "<ETM-495>"

// Specify the name of the version to extract from the issue
def closedStatus = '<Cancelled>'

// Specify the transition to execute on the issue
// NOTE - The transition ID must represent a valid transition for the workflow that the issue uses.
def transitionID = '<11>'

// Specify a boolean to flag if all issue links are resolved
boolean allIssueLinksResolved;

// Get the issue object
def result = get('/rest/api/2/issue/' + issueKey)
.header('Content-Type', 'application/json')
.asObject(Map)

// Find the linked issues
def linkedIssues = result.body.fields.issuelinks

// if all issue links have the Done status set the flag to true
if(linkedIssues.findAll{closedStatus.contains(it.outwardIssue.fields.status.name)}){
allIssueLinksResolved = true
}

// If any of the linked issues do not have the done status set the flag to false
if (linkedIssues.findAll{!closedStatus.contains(it.outwardIssue.fields.status.name)}){
allIssueLinksResolved = false
}

// If all issue links are resolved transition the issue
if(allIssueLinksResolved == true){

// The rest call to transition the issue
def transitionIssue = post("/rest/api/2/issue/${issueKey}/transitions")
.header("Content-Type", "application/json")
.body([transition: [id: transitionID]])
.asObject(Map)

return "All issue links are to closed issues and the issue has been transitioned."
// If the issue contains unresolved linked issues return a message and do not transition the issue
}else if (allIssueLinksResolved == false){
return "The issue contain issue links which are not closed"

 

script error.PNG

Matthias Gaiser _K15t_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 27, 2019

Hi @Musku Nagaraju,

have a look at the error message again. It quite clearly expresses what the problem is: a missing curly brace '{'.

If you copied your whole script into here, you should add a curly brace at the end of your script.

Cheers,
Matthias.

Musku Nagaraju
Contributor
July 1, 2019

Hi @Matthias Gaiser _K15t_ ,

I have put curly brace, their is no error now but the workflow sync not working.

 

I have used this code in post function "run script"

 

scripr logs.PNG

Matthias Gaiser _K15t_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 1, 2019

Hi @Musku Nagaraju,

please have a closer look at the error message again - it tells you more information about the problem: Illegal character in path at index 89.

If you want to use a solution like Scriptrunner, I highly suggest to read error message, try to understand them and see if you can solve the problem.

If you don't feel equipped well enough for that, you can always look for other apps where you don't need to code or look for an Atlassian solution partner which will help you in the configuration.

Cheers,
Matthias.

Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 4, 2019

Hi Musku,

Thank you for your comment.

I have reviewed your script above and notice that in the issueKey,transitionID, and closedStatus variables that you are have defined your values inside of < and > tags and these tags should be removed as you just need to define your values as plain text inside of the speech marks for the variables. 

In the example script, the < > tags are just to show where you need to update the script and to specify values as a string and should be removed.

Also please note when reusing the sample script you must keep the block comment from the top of the script as the licensing comment which is required to reuse the script sample provided.

Regards,

Kristian

Jeff Santos
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
October 26, 2020

Hi @Kristian Walker _Adaptavist_ I am reviewing the script you provided above and line below has a warning. I am using JIRA Cloud. The reason for the interest in your script is because I would like transition an issue to status if when it is created it is a bug and has a link to an issues that is currently in development. 

Hopefully you can help shed some light.

def linkedIssues = result.body.fields.issuelinks

 issueLinks.PNG

Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 26, 2020

Hi Jeff,

Thank you for your response.

I can confirm that the yellow warning you are seeing is just a static type checking warning as described here and these warnings are just where the compiler does not fully understand the code and these are not errors that will stop the code from running.

This means that the code will likely still run and I would advise you to test running the code as I have it defined in my example in the Script Console in order to see if it works as expected.

If you are using the code elsewhere such as in a post function then you will need to log out the issue object to see what structure is used to store the field values in this object in order to check how to access the issue links field as my example is only designed to work on the script console where the issue is fetched by a call to the Get Issue API.

I hope this information helps.

Regards,

Kristian

Suggest an answer

Log in or Sign up to answer