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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,551,722
Community Members
 
Community Events
184
Community Groups

Scriptrunner condition for sibling status

Hi, I'm looking to create a condition on a transition that is dependent on whether or not siblings of a certain issue type are in a particular status. 

I've found a solution from 2016 but code has since been deprecated.

def parent = issue.parentObject
def otherSubtasks = parent.subTaskObjects.findAll { it.issueTypeObject.name in ["A", "B"]}
return otherSubtasks.every {
    it.statusObject.name == "Resolved"
}

 Now we are supposed to use getIssueType() and getStatus(). 

Two questions:

  1. How do I use getIssueType with the "otherSubtasks" array?
  2. I'm not sure what the code looks like for the getStatus replacement.

I know this is wrong but wanted to show I tried:

import com.atlassian.jira.issue.Issue

def parent = issue.parentObject
def otherSubtasks = parent.subTaskObjects.findAll { issue.issueType.name == "Estimate"}
return otherSubtasks.every {
otherSubtasks.getStatus() = "Final"
}

1 answer

1 accepted

0 votes
Answer accepted
Ivan Tovbin
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.
Sep 28, 2018

Hi Amy,

You've almost nailed it. Here's the correct way:

import com.atlassian.jira.issue.Issue

Issue parent = issue.getParentObject()
List<Issue> otherSubtasks = parent.getSubTaskObjects().findAll{it.getIssueType().getName().equals("Estimate")}

return otherSubtasks.every{it.getStatus().getName().equals("Final")}

Hi Ivan, thanks for your help! I've tried this but getting an error:

[Static type checking] - Cannot assign value of type java.util.Collection <com.atlassian.jira.issue.Issue> to variable of type java.util.List <Issue>

Ivan Tovbin
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.
Sep 29, 2018

You can pretty much ignore this error. It'll work just fine as is. Thing is static type checking in Scriptrunner is not always accurate. 

However if you wanna get rid of this error simply change "List" to "Collection" in the code.

Ok, thanks for that. I'm getting this error and I think it's because the siblings are not subtasks? I mistakenly assumed that any child was a subtask but these siblings are tasks which is why this if failing:

Cannot invoke method getSubTaskObjects() on null object
 at Script748.run(Script748.groovy:4)

 I definitely have siblings of that issue type within the same Epic so I'm not sure why it would be null unless it has to do with them not being subtasks.

Ivan Tovbin
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.
Oct 01, 2018

Indeed, 

This error occurs because the issue this script is executed in is NOT a subtask and as such does not have a "parent". That's why the parent here is null, hence the error.

Now that we have this out of the way, I think I can still help you with this, but first could you clarify what exactly do you mean by "siblings"?

Sure. I have the following hierarchy:

Epic

  -Estimate issue type (task)

  -Intake issue type (task)

  -Job issue type (task)

My objective is to prevent the Job task from being put into IN PROGRESS state if the Estimate and Intake are not in the DONE state. So I've set this condition on the Job issue type, though it might be better practice to put is as a validator.

Ivan Tovbin
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.
Oct 01, 2018 • edited Oct 02, 2018

Condition is the correct approach here. Make sure you put it on your transition that leads to "In Progress" status.

The code is as follows:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue

import org.slf4j.Logger
import org.slf4j.LoggerFactory

Logger log = LoggerFactory.getLogger("LOG")

CustomFieldManager cfMgr = ComponentAccessor.getCustomFieldManager()
def epicLinkMgr = getBean("epicLinkManagerImpl")
Issue epic = issue.getCustomFieldValue(cfMgr.getCustomFieldObjectByName("Epic Link")) as Issue
log.error("Epic issue: ${epic.getKey()}")
List<String> issueTypeList = Arrays.asList("Estimate", "Intake")
List<Issue> issuesInEpic = epicLinkMgr.getIssuesInEpic(epic).findAll{issueTypeList.contains(it.getIssueType().getName())}
log.error("Issues to check: ${issuesInEpic.toString()}")
log.error("Issue statuses: ${issuesInEpic.collect{it.getStatus().getName()}.toString()}")

return issuesInEpic.every{it.getStatus().getName().equals("Done")}

def getBean(String beanId){
def ghPlugin = ComponentAccessor.getPluginAccessor().getEnabledPlugin("com.pyxis.greenhopper.jira")
def descriptor = ghPlugin.getModuleDescriptor("greenhopper-launcher")
def applicationContext = descriptor.getModule().greenHopperCacheManager.applicationContext
def beanInstance = applicationContext.getBean(beanId)
return beanInstance;
}

Unfortunately, still allowing me to transition to In Progress despite both Estimate and Intake not sitting in Done. Is there any way to find out what's allowing that?

Ivan Tovbin
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.
Oct 01, 2018

Just to clarify, your Intake and Estimate issues are connected to an epic via "epic link" field and not a simple issue link, right?

Yes, here's a screen grab showing that we have both the Estimate and the Intake tasks within "Issues in Epic" as well as the new task that I'm trying to prevent the "OK to Begin" transition: https://screencast.com/t/GmGb0MsI

I put the script on a Condition on the "OK to Begin" transition within the Task workflow as shown here: https://screencast.com/t/JVh3ojC3nP.

However, it's still showing as an option when the other two are not Done: https://screencast.com/t/bwQzf9KFx 

Ivan Tovbin
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.
Oct 02, 2018

That's odd. I've added some logging to the code above to be able to see what exactly is going on there. Could you please update your code with it and send me a screenshot of your script logs.? To quickly get to them, open your conditions tab and click on the last green ckeckmark (or red X) in your scripted condition. Make sure the code is executed after the update before your check the logs.

Ok, here's the log:

The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.

2018-10-02 14:52:40,266 ERROR [LOG]: Epic issue: AMGNLUS-58
2018-10-02 14:52:40,294 ERROR [LOG]: Issues to check: [AMGNLUS-59, AMGNLUS-60]
2018-10-02 14:52:40,295 ERROR [LOG]: Issue statuses: [IN PROGRESS, TO DO]
Ivan Tovbin
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.
Oct 02, 2018

Ok, so we know that we are getting the correct issues and we are getting their statuses right. The problem seems to be in 'return' statement. Try chaning it to:

return issuesInEpic.every{it.getStatus().getName().equalsIgnoreCase("Done")}

Thanks for your help, Ivan. But unfortunately, it's still allowing me to push this task. Can you clarify what part of this script disables the transition?

Ivan Tovbin
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.
Oct 02, 2018

The script should return logical true for the transition to become available or logical false otherwise. The 'return' statement does that in this case.

'every' closure returns true if the condition inside the closure is true for every element of the 'issuesInEpic' list. 

Interesting: I just tried running it as a validator and it seems to work as intended. 

Marked this as answered but want to clarify that this worked as a Validator rather than a Condition for me.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events