Auto transition epic if all linked issues are closed (Script Runner for JIRA)

Trying to set up a script listener with Script Runner for JIRA.

Have no experience with Groovy scripts and examples provided (on the plugin's page itself or wiki page here) do not apply.

What we need is a condition script that will (additionaly):

a) filter for issue type "epic" (introduced by JIRA Agile plugin)

b) filter for specific status (Development)

c) filter for (outward) issue link type "is Epic of" and get list of linked issues

d) filter for specific statuses (To Verify or Closed) of all linked issues found in c) above

Our intention is for this script listener to automatically transition all epics in Development that have all linked issues (using the Epic link type) Closed or To Verify to the next status in our custom workflow. The action (transition) is obvious to set, what we need help with is the condition that checks for all 4 things above.

Condition a) is superceded by c) - since only epics can have the outward "Epic-Story Link" link type but I still want it there in case we need to apply this to a different scenario with different issue types.

Any help or idea to the right direction is appreciated.

Thanks.

4 answers

Hi Daniel,

Below is a gist that has a few different examples using Script Runner:

1) Automatically transition an "Initiative" to "In Progress" if an Epic moves "In Progress". This can be useful if you have a hierarchy via links between Initiatives and Epics. Its coded in such a way that it doesn't matter what type of link is used. Place this on the "In Progress" status as a post-workflow action of an Epic.

2) Automatically transition an "Initiative" to "Closed" if an Epic moves to "Closed" and all other Epics are also in the "Closed" state. This is useful if you want to keep accurate cycle times of your hierarchy. Place this on the closed status as a post-workflow action  of the Epic.

3)Automatically transition an Epic to "Closed" if a Story moves to "Closed" and all other Stories/issues are in the "Closed" state. This should help you automate the closing of Epics based on the issues underneath. Place this on the closed status as a post-workflow action of the Story.

4) Not included in the script, but Automatically transition an Epic to "In Progress" if a Story moves in "In Progress". I use the JIRA misc workflows "transition linked issue" function and specify the transition id of the epic and "has Epic" issue type (since its a 1 to 1 mapping). I place this in the post-workflow of a Story. 

You can place this code inside of a custom post-script plugin on the Closed status of their respective

https://gist.github.com/jechlin/573aa77aadacfd5ed05f0adc5744727a (Note there are a few things hardcoded like the transition, doesn't validate the transition, issueLinkTypes. I would try and clean up a few of those.) I would also test this using the Script Console before putting it in the post-workflow function. I also place all of these after the re-index step in the post-workflow function. They are all done using the Post-groovy script -> Custom groovy script.

 

Thanks to @Jamie Echlin (Adaptavist) for starting off the gist and writing an awesome script runner plugin!

Hi Dorian Kersch,

As you mentioned , is there any updated code for this ? OR any link to see latest code .

Please could you provide it.

 

The gist I linked in my answer has all of the code for those steps. Unfortunately I haven't tested if it is compatible with Jira 7. I haven't had time to convert it by following https://scriptrunner.adaptavist.com/4.3.6/jira/releases/UpgradingToJira7.html but it shouldn't be hard. If you do convert it feel free to append it to the gist or comment back if it works.

Hi Dorian,

Thanks for reply .

I was trying "Auto-Close Epics if all the stories are closed." code.

But it's giving Type checking error : "closedStory" undeclared error in JIRA 7.1.8 .

And in below code 

if (story.statusObject.name == "closed")

Giving worning as shown

<span class="deprecationComment"> since 7.0

use <a href="com/atlassian/jira/issue/Issue.html#getStatus--">

<code>Issue.getStatus()</code></a>instead.</span>

Please help me how to fix it.

 

Hi Tukaram,

 

Currently not by a computer but looking at their documentation change story.statusObject.name to story.getStatus.

 

But getStatus returns a status so you may want to do story.getStatus.getSimpleStatus which returns a simple status. Within that simple status you have the ability to get the name by calling getName(). 

 

So maybe try changing it to story.getStatus.getSimpleStatus.getName

 

You may run into other issues as there are more deprecated methods. I recommend looking at an IDE or the documentation to help determine how to convert it. The link I provided about converting to Jira7 should help you convert other parts. If you do convert it please post it back so others can get the benefit.

 

Cheers,

Dorian

Thanks so much for your help and support.

In below code , you used hard code value 31 as parameter value  and  isEpicOfLinkType = 10300

Are these remain same ? if not please let me know how to find these values on my jira environment .

 

def updateEpic = {Issue epic ->
if (isEpicInProgress) {
def parameters = issueService.newIssueInputParameters()
def result = issueService.validateTransition(currentUser, epic.id, 31, parameters)
issueService.transition(currentUser, result)
}
}


issueLinkManager.getInwardLinks(issue.id).each { link ->
def linkTypeId = link.getLinkTypeId()
def isEpicOfLinkType = 10300
if (isEpicOfLinkType == linkTypeId) {
def epic = link.sourceObject
if (isEpicInProgress(epic)) {
if (isEpicClosable(epic)) {
updateEpic(epic)
}
}
}
}

Both those values are hardcoded, but may be the same. The below again is based on memory as I don't have a computer with a Jira instance in front of me.

To find the "31" open up the workflow to text view of your epic and look at the transition state to "Closed" or "Done". It will be next to the transition name (I'm making assumptions about how you configured epics). You will probably want to make sure it's global able to transition to that state or you will have to add more checks. 

The 10300 is the link type. If you go to your link type page in admin panel and hover over the link types it should return their values in the url. Epics have a unique one that may not be visible from that page. It can be found within the database or if you go to a workflow post function and select "transition linked issues" it should show the full drop-down. You can inspect element and find the right value associated to it. Hopefully that helps.

Hi Dorian,

As you mentioned above "story.getStatus().getSimpleStatus().getName()=="Closed"  working

But  "epic .getStatus().getSimpleStatus().getName()="Closed" epic one not working .

This was discussed during our roll out of JIRA Agile. If you have a proper solution I would appreciate it, if you can share this. :)

Is there any update on this? 

Can this be implemented using rules on workflow transitions/status' as opposed to script runner?

0 vote
Andreas Knecht Community Champion May 14, 2017

Hi Daniel (and everyone else),

I'm one of the founders of Code Barrel, and we make Automation for JIRA.  

With Automation you can achieve this fairly easily. Here's an example Automation rule:

close-epic.png

 

So what this does is:

  • Anytime an issue is transitioned
  • We check that the issue is a Story and was moved to 'Done'
  • Then we branch off for the Epic of that story
    • For this Epic we check that *all* its stories are now done
    • Then we transition the Epic to Done.

Hope this helps!

Cheers,

  Andreas

Hi Andreas,

 

I've tried both your version and my modified version below and unfortuantley it's not working. What's weird is that I have 2 tasks linked to the Epic. When I transition both from To Do to Done, nothing happens. But then when I transition 1 (either) of the tasks back to "To Do" from "Done" it updates the status of the Epic from To Do to Done.

Of course, it should be updating the status when the both tasks linked to the epic transition to Done.

 

2017-08-30 at 6.57 PM.png

I had this same need and set it up as Andreas did above and it worked. MAKE SURE your JQL where type = story (includes all ticket types you want to transition it on (not just story)).

 

Example:

Type in ("story","upgrade/ deploy","New Client Setup")

 

hope that helps.

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published Thursday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

77 views 0 5
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you