All Sub-tasks Resolved

Hey I am simply trying to check if each sub-task is resolved. But it just keeps returning the keys of the sub-tasks.

I have the following, which is pretty much the same as https://docs.adaptavist.com/sr4js/6.27.0/features/workflows/conditions/built-in-conditions/all-sub-tasks-resolved

import com.atlassian.jira.component.ComponentAccessor
def issueManager = ComponentAccessor.issueManager
def issueKey = 'TEST-1'
def issue = issueManager.getIssueObject(issueKey)
def subTasks = issue.getSubTaskObjects()

subTasks.each {
    if (!it.resolution) {
        return false
    }
}

This just returns [TEST-2, TEST-3] in the results, when it actually should be false.

What am I missing?

Hi Marcus

The each closure is a groovy loop, similar to a java for loop.
It does not return the result of what you do inside the loop. It returns the list itself.

If you want to return some result based on logic you do on each item in the closure, you probably want the any closure

You should be able to do something like this:

Issues.getByKey('SSPA-1').subTaskObjects.any { !it.resolution }

Kind regards,
Matthew

1 Like

Heya mclark, thanks for your quick reply.
I thought I already tried this and was going crazy :clown_face:.

For anyone else struggling with this, I got it working with the following:

import com.atlassian.jira.component.ComponentAccessor
def issueManager = ComponentAccessor.issueManager
def issueKey = 'TEST-1'
def issue = issueManager.getIssueObject(issueKey)
def parentsubtasks = issue.parentObject.getSubTaskObjects()

parentsubtasks.every {it.getStatus().getName() == "Resolved"}

@mclark ah I have another question.

I can’t seem to get this to work within my post-function.

The post-function fires when a sub-task transitions to resolved, it should then check that every sub-task is resolved in the parent.

It seems like it counts the trigger sub-task as not resolved, and therefore returns false.

def parentsubtasks = issue.parentObject.getSubTaskObjects()

if (parentsubtasks.every {it.resolution}
    )
def parentsubtasks = issue.parentObject.getSubTaskObjects()

if (parentsubtasks.every {it.getStatus().getName() == "Resolved"}
    )

Both won’t work.

Nevermind, answered my own question! Haha.

Here’s my result, this checks if the sub-tasks are resolved and if the triggerissue contains sometext in it’s summary:

import com.atlassian.jira.component.ComponentAccessor
def issueManager = ComponentAccessor.issueManager
def issueKey = 'TEST-1'
def issue = issueManager.getIssueObject(issueKey)
def parentsubtasks = issue.parentObject.getSubTaskObjects()
def verifyparentsubstaksstatus = true
def verifyparentsubstakssummary = true

parentsubtasks.every { subtask ->
    if (!issue.getStatus().getName() == "Resolved") {
        verifyparentsubstaksstatus = false
        return
    }
}

if (!issue.summary.contains("sometext")) {
        verifyparentsubstakssummary = false
        return 
    }

if (verifyparentsubstaksstatus && verifyparentsubstakssummary)  {
    "Perfect!"
} else {
    "Something's not right"
}

You are using the triggering issue variable which holds “TEST-1” inside the every closure instead of the subtask closure scoped variable, so I am not sure that it is working how you want it to.

Also, you set a variable outside the closure from inside every loop. You can use the return of every as the variable value.

Maybe you want something like this:

import com.atlassian.jira.component.ComponentAccessor

def issueManager = ComponentAccessor.issueManager
def issueKey = 'TEST-2'
def issue = issueManager.getIssueObject(issueKey)

if(!issue.isSubTask()) {
    // if the triggering issue is not a subtask do nothing
    return
}

// get all sibling subtasks
def siblingSubtasks = issue.parentObject.getSubTaskObjects()

def verifySiblingSubstaskStatuses = siblingSubtasks.every { subtask ->
    //return true if every sibling subtask is in the Resolved state else return false
    subtask.status.name == "Resolved"
}

// return true if the triggering sub-task issue contains the word "sometext"
def triggeringIssueSummaryCheck = issue.summary.contains("sometext")


if (triggeringIssueSummaryCheck && verifySiblingSubstaskStatuses) {
 return "Perfect!"
} else {
    "Something's not right"
}

Also, if checking the issue.resolution is not working, it might be because that status transition to the "Resolution` might not set the resolution field.

You can always raise a support case here if it is still not right and we can work through it via support.

2 Likes

Yeah you’re right I got it properly working with your changes.

I also had to change siblingSubtasks to
def siblingSubtasks = issue.parentObject.getSubTaskObjects().findAll { it != issue }

Since it shouldn’t include the triggering sub-task.

Thanks a lot mclark.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.