How to use Jira automation to close epic of story "and" sub tasks
Asked by Chris S_:

Hello community,

About closing epics when child issues are closed, what branch rule can cover "both" Epic-\>story and also Epic-\>sub task?

This rule works if stories (or bugs) are 'completed':

![story.png](https://community.atlassian.com/t5/image/serverpage/image-id/343569i85732C13697404D4/image-size/large?v=v2&px=999 "story.png")

And this works for closing an epic when the child issue is a sub task:

![subtask.png](https://community.atlassian.com/t5/image/serverpage/image-id/343570iF3AFC7F94B141F82/image-size/large?v=v2&px=999 "subtask.png")

These are two different branch rules, 1) For Epic (parent) and 2) For Parent.

Though how can an Epic with, for example, 1 story child and 1 sub-task be closed? (where the sub task is directly of the epic, not of the story)

* The 'For Epic (parent)' branch does not seem to recognize sub tasks as a direct child type.

The desired result is epic is transitioned to complete, when either the story or the sub task is transitioned to complete.

Thank you,

Chris


Answers:
author: Bill Sheboy
body: Hi [@Chris S_](/t5/user/viewprofilepage/user-id/1136175)

For Jira Data Center, there is no branch that spans both types as there are different linkages for epic -\> story and story -\> sub-task.

You may address this using an if / else condition and branches inside. For example:

* trigger: issue transitioned to Complete
* if / else condition:
  * field condition: issue type equals story
  * branch: to epic parent
    * related issues condition: check if all sibling stories are Complete
    * action: transition the epic parent
* else / if
  * field condition: issue type equals sub-task
  * branch: to parent (i.e., the story)
    * related issues condition: check if all sibling sub-tasks are Complete
    * action: transition the story

With Jira Cloud, a rule like this is simpler (in theory) as the parent field is used for the relationships of both.

Also...do you want this to be recursive, in that the completion of a sub-task could lead to completion of the grandparent epic?

Kind regards,  
Bill

comments: author: Chris S_
body: Hi [@Bill Sheboy](/t5/user/viewprofilepage/user-id/1685313), Let's say an epic has 1 story and also 1 sub-task (direct child of the epic), if the story is marked complete first, then the epic should not transition, since the sub-task is still open. However, once the sub-task is also transitioned to complete, the epic should now automatically transition to complete. The challenge is how to validate both conditions before transitioning the epic.

thank you,

Chris


author: Bill Sheboy
body: Hi [@Chris S_](/t5/user/viewprofilepage/user-id/1136175)

Sorry for the delay in responding as I have been offline for a few weeks.

First, I believe sub-tasks are children of Story, Task, and Bug issue types, and not of Epic ones. Let's assume you meant the Epic has one or more Stories, each with zero-to-many Sub-tasks.

To validate all of them are completed before transitioning the epic, you would need to gather the keys of all of them first and test them. I believe that can be done using the Lookup Issues action and the Related Issues Condition. Please validate this with testing as I am using Jira Cloud, and not Server / Data Center version.

For example, let's assume the trigger issue is a Story:

* trigger: issue transitioned to complete
* condition: issue type is Story
* action: lookup issues, to gather the other children of the Epic with JQL

```
"Epic Link" = {{triggerIssue.key}}
```

* action: create a variable to parse out the child and grandchild keys of the Epic
  * name: varKeyList
  * smart value:

```
{{#lookupIssues}},{{key}}{{#subtasks}},{{key}}{{/}}{{/}}
```

* Related Issues Condition: using the variable, create JQL to gather all of the Story-level and Subtask-level issues, testing no issues are still incomplete (i.e., none found)

```
key IN ( {{varKeyList.substringAfter(",")}} ) AND statusCategory != Done
```

* branch: to the epic parent
  * action: transition the issue

Kind regards,  
Bill


Tags: automation

data-center

jira-data-center

jira-server