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

Groovy - how to check if an issue is a sub-task

Graeme Johnson February 14, 2020

Hi

I am building a custom scripted validator in Groovy and within my script I need to identify whether the current issue is a type of sub-task.  Is there a way I can check this?

1 answer

Suggest an answer

Log in or Sign up to answer
2 votes
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 14, 2020

Hi @Graeme Johnson 

The sure is a way to check this and it's very easy:

if( issue.issueType.subTask){
//do something for sub-task issue types
} else {
//do something for parent issue types
}
Graeme Johnson February 14, 2020

Thanks, that worked a charm.  

Ajinkya Paradkar September 7, 2022

What if we want to check if current issue contains a particular Sub-Task named "abc" ?

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 7, 2022

You could try something like this:

def abcSubtaskFound = false
if(issue.subTaskObjects){
abcSubtaskFound = issue.subTaskObjects.any{it.issueType.name == 'abc'}
}

Or as a oneliner:

def abcSubtaskFound = issue.subTaskObjects && issue.subTaskObjects.any{it.issueType.name == 'abc'}
Pankaj May 15, 2023

if we want to check - Task issue type have any open subtask(open means issue not in any done status category).

then what should be the script?

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 15, 2023

Something like this should work

 

import com.atlassian.jira.issue.status.category.StatusCategory
issue.subTaskObjects.every{ it.status.statusCategory == StatusCategory.COMPLETE}
Pankaj May 15, 2023
we want to do for Task Issue Type while moving to close transition:
Error would be displayed if the task have any open subtasks. (open meaning issue not in any done status category)
Error Message: "All open subtasks needs to be closed"
------------x------------------x-------------x---------------------x---------------------
I have tried this one but, it is also giving an error when all subtasks status are  "Done".
import com.opensymphony.workflow.InvalidInputException;
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.status.category.StatusCategory

def issueType = issue.issueType.name == "Task"

if(issueType){

if (issue.subTaskObjects.every{ it.status.statusCategory != StatusCategory.COMPLETE})
 {
    InvalidInputException error = new InvalidInputException()
    error.addError("All open subtasks needs to be closed")
    throw error;

 }
}
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 16, 2023

I think you want either 

if(issue.subTaskObjects.every{ it.status.statusCategory == StatusCategory.COMPLETE}) return null
throw new InvalidInputException('All open subtasks need to be closed')

Or

if(issue.subTaskObjects.any{ it.status.statusCategory != StatusCategory.COMPLETE}){
throw new InvalidInputException('All open subtasks need to be closed')
}

But what you have right now will throw an error only if ALL (every) subtasks are open. If a single one is closed, the validation will pass.

TAGS
AUG Leaders

Atlassian Community Events