Hi Community,
I need help in reviewing and correcting my below code. I am passing the code in Conditional execution section of Transition Issues post-function (using JMWE app) in the Child Workflow.
My use-case is:
- I am running a JIRA automation at the time of creating a Parent issue (Initiative issue type) which clones/ creates Children (Portfolio Epic issue type) based on the custom field value selection.
- There are 30 options in the custom field to create 30 children at one go and user can also add more child manually by updating Parent Link field (present on Child issue) after creation of Parent.
So, I want that:
- If each of the Child is transitioned to next status, then Parent should check all the statuses of its Children and transition to the next status based on Children status.
Parent-Child Transition Logic:
a) When all Children of a Parent (linked via Parent Link field), transition to Prioritized, then Parent will also transition to Prioritized.
b) When some Children transition to Prioritized and other transition to either Not-Prioritized or Cancelled, then Parent should only move to Prioritized, irrespective to those Children moved to Not-Prioritized or Cancelled.
c) When all Children are transitioned to Not-Prioritized, then Parent should move to Not-Prioritized.
d) When some Children are transitioned to Not-Prioritized and other are moved to Cancelled, then Parent should only move to Not-Prioritized, irrespective to those Children moved to Cancelled. (Here, No Children have moved to Prioritized)
e) When all Children are moved to Cancelled, then Parent should move to Cancelled. (Here, No Children have moved to Prioritized and Not-Prioritized).
Problem:
- Parent doesn't transitions to the next status if it is linked to more than 5 Children.
- Parent doesn't transitions if I fix 1) and then I test the same scenario with 3 Children or so.
Configuration:
Parent and Child Workflow are same and a Parent can transition from Approved status to either Prioritized, Not-Prioritized, or Cancelled based on transition of Children. I am using below code in Conditional Execution in Transition Issue post-function in Child Workflow from Approved status to Prioritized:
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.workflow.TransitionOptions
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.Issue
Integer actionId = XXXX // The transition ID for Prioritized
MutableIssue parent = issue.portfolioParent as MutableIssue
IssueService issueService = ComponentAccessor.getIssueService()
ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
TransitionOptions transitionOptions = new TransitionOptions.Builder()
.skipConditions()
.skipPermissions()
.skipValidators()
.build()
IssueService.TransitionValidationResult result = issueService.validateTransition(currentUser,
parent.getId(),
actionId,
issueService.newIssueInputParameters(),
transitionOptions)
//return true
def otherSubtasks = parent.getPortfolioChildIssues().findAll{it.issueType.id == "XXXXX" && it != issue
}
if (otherSubtasks.every{it.getStatus().getName() == "Prioritized"} && otherSubtasks.every{it.getStatus().getName() != "Approved"} ){
//return true
log.info("entered1")
issueService.transition(currentUser, result)
}
else if ( otherSubtasks.any{it.getStatus().getName() == "Prioritized"} )
{ log.info("entered2")
if(otherSubtasks.every{it.getStatus().getName() != "Approved"}){
log.info("entered3")
issueService.transition(currentUser, result)
}
}
else if ( otherSubtasks.any{it.getStatus().getName() == "Prioritized"} )
{ log.info("entered4")
if(otherSubtasks.every{it.getStatus().getName() != "Approved"}){
log.info("entered5")
if(otherSubtasks.any{it.getStatus().getName() == "Cancelled"}){
log.info("entered6")}
else if(otherSubtasks.any{it.getStatus().getName() == "Not-Prioritized"}){
log.info("entered7")
issueService.transition(currentUser, result)
}
else if(otherSubtasks.any{it.getStatus().getName() == "Prioritized"}){
log.info("entered8")
issueService.transition(currentUser, result)
}
}
}
else if ( otherSubtasks.any{it.getStatus().getName() == "Prioritized"} )
{ log.info("entered9")
if(otherSubtasks.every{it.getStatus().getName() != "Approved"}){
log.info("entered10")
if(otherSubtasks.any{it.getStatus().getName() == "Cancelled"}){
log.info("entered11")}
issueService.transition(currentUser, result)
}
}
else if ( otherSubtasks.any{it.getStatus().getName() == "Prioritized"} )
{ log.info("entered12")
if(otherSubtasks.every{it.getStatus().getName() != "Approved"}){
log.info("entered13")
if(otherSubtasks.every{it.getStatus().getName() == "Not-Prioritized"}){
log.info("entered14")}
issueService.transition(currentUser, result)
}
}
else if ( otherSubtasks.any{it.getStatus().getName() == "Prioritized"} )
{ log.info("entered15")
if(otherSubtasks.every{it.getStatus().getName() != "Approved"}){
log.info("entered16")
if(otherSubtasks.any{it.getStatus().getName() == "Not-Prioritized"}){
log.info("entered17")
if(otherSubtasks.any{it.getStatus().getName() == "Cancelled"}){
log.info("entered18")
issueService.transition(currentUser, result)
}
}
}
}
else if ( otherSubtasks.any{it.getStatus().getName() == "Prioritized"} )
{ log.info("entered19")
if(otherSubtasks.every{it.getStatus().getName() != "Approved"}){
log.info("entered20")
if(otherSubtasks.any{it.getStatus().getName() == "Not-Prioritized"}){
log.info("entered21")
issueService.transition(currentUser, result)
}
}
}
else if ( otherSubtasks.every{it.getStatus().getName() == "Not-Prioritized"} )
{
log.info("entered22")
if(otherSubtasks.every{it.getStatus().getName() != "Approved"}){
log.info("entered23")
issueService.transition(currentUser, result)
}
}
else if ( otherSubtasks.every{it.getStatus().getName() == "Cancelled"} )
{ log.info("entered24")
if(otherSubtasks.every{it.getStatus().getName() != "Approved"}){
log.info("entered25")
issueService.transition(currentUser, result)
}
}
else if ( otherSubtasks.any{it.getStatus().getName() == "Cancelled"} )
{ log.info("entered26")
if(otherSubtasks.every{it.getStatus().getName() != "Approved"}){
log.info("entered27")
if(otherSubtasks.any{it.getStatus().getName() == "Not-Prioritized"}){
log.info("entered28")
issueService.transition(currentUser, result)
}
}
}
else if ( otherSubtasks.any{it.getStatus().getName() == "Cancelled"} )
{ log.info("entered29")
if(otherSubtasks.every{it.getStatus().getName() != "Approved"}){
log.info("entered30")
if(otherSubtasks.every{it.getStatus().getName() == "Prioritized"}){
log.info("entered31")
issueService.transition(currentUser, result)
}
}
}
else if ( otherSubtasks.any{it.getStatus().getName() == "Prioritized"} )
{ log.info("entered32")
if(otherSubtasks.every{it.getStatus().getName() != "Approved"}){
log.info("entered33")
if(otherSubtasks.every{it.getStatus().getName() == "Not-Prioritized"}){
log.info("entered34")
issueService.transition(currentUser, result)
}
}
}
else if ( otherSubtasks.any{it.getStatus().getName() == "Prioritized"} )
{ log.info("entered35")
if(otherSubtasks.every{it.getStatus().getName() != "Approved"}){
log.info("entered36")
if(otherSubtasks.every{it.getStatus().getName() == "Cancelled"}){
log.info("entered37")
issueService.transition(currentUser, result)
}
}
}
Please guide further on how can I achieve the above transition logic using the mentioned Post-Function. It would be a great help and learning for me using groovy.
Thanks