How to change the status of an epic when the stories chnge their status

I'm trying to run a script listener that updates the status of the epic (not the EpicStatus custom field) when one of the stories change its status.
The case is I don't know which event should I listen. I tried to use a generic event, but I can't figure out how to get its name and if I don't set any condition the event name, it runs a lot of times and breaks. How do I get the name of a generic event?
PS: I'm not sure my script works too.
PSS: Here's my script. I've found it on the internet and adapted for my needs.

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.link.IssueLinkManager;
import com.atlassian.jira.issue.link.IssueLink;
import com.atlassian.jira.issue.ModifiedValue;
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder;
import com.atlassian.jira.issue.customfields.option.Options;
import com.atlassian.jira.issue.customfields.option.Option;
import com.atlassian.jira.issue.fields.config.FieldConfig;
import com.atlassian.jira.issue.customfields.manager.OptionsManager;
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.bc.issue.IssueService.TransitionValidationResult;
import com.atlassian.jira.bc.issue.IssueService;
import com.atlassian.jira.issue.IssueInputParameters;
import com.atlassian.jira.user.ApplicationUser;
import com.opensymphony.workflow.loader.ActionDescriptor;
import com.atlassian.jira.workflow.JiraWorkflow;
import com.atlassian.jira.workflow.WorkflowManager;

ComponentManager componentManager = ComponentManager.getInstance();
def groupMan = ComponentAccessor.getGroupManager()
def authCon = ComponentAccessor.getJiraAuthenticationContext()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def changeHolder = new DefaultIssueChangeHolder();
IssueManager issueManager = ComponentAccessor.getIssueManager();
OptionsManager optionsManager = componentManager.getComponentInstanceOfType(OptionsManager.class);
IssueLinkManager issueLinkManager = ComponentAccessor.getIssueLinkManager()
String currentEpicStatus = "";

def curUser = authCon.getLoggedInUser()
def issue = event.issue

//this was a try of getting the name of the generic event, but it show the class name
/*
if (event == "In Progress (21)") {
log.warn "In Progress (21)"
} else {
log.warn event.toString()
}
*/

def epicLinkCf = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'Epic Link'}
if(!epicLinkCf) {log.warn "No Epic Link field"; return}
log.warn "Existing Epic Link: ${epicLinkCf.getValue(issue)}"

String epicIssue = epicLinkCf.getValue(issue)
Issue epic = issueManager.getIssueObject(epicIssue) // epicKey is passed into your script

// Check if Epic link is exist
if(!epic)
return true

def newEpicState = "Done"

log.warn "Epic: " + epic
List<IssueLink> allOutIssueLink = issueLinkManager.getOutwardLinks(epic.getId());
for (Iterator<IssueLink> outIterator = allOutIssueLink.iterator(); outIterator.hasNext();) {
IssueLink issueLink = (IssueLink) outIterator.next();
log.warn "child link type: " + issueLink.getIssueLinkType().getName()
Issue chIssue = issueLink.getDestinationObject();
// Check status of all issues from epic
if (issueLink.getIssueLinkType().getName() == "Epic-Story Link") {
log.warn "child state: " + chIssue.getStatus().getName()
if(chIssue.getStatus().getName() == "In Progress") {
newEpicState = "In Progress"
} else if (chIssue.getStatus().getName() != "Closed" && newEpicState != "In Progress") {
newEpicState = "To Do"
}
} else if (issueLink.getIssueLinkType().getName() == "Epic Link") {
currentEpicStatus = chIssue.getStatus().getName();
}
}

log.warn "Current epic status: " + currentEpicStatus

log.warn "New epic status: " + newEpicState

// Set new status if it necessary
if (currentEpicStatus != newEpicState) {

WorkflowManager workflowManager = ComponentAccessor.getWorkflowManager();
JiraWorkflow jiraWorkflow = workflowManager.getWorkflow(issue);
Collection<ActionDescriptor> cActionDesc = jiraWorkflow.getAllActions();
int actionId = 0;

for (ActionDescriptor res : cActionDesc) {
if (res.getName().equals(newEpicState)) {
actionId = res.getId();
break;
}
}

IssueService issueService = ComponentAccessor.getIssueService();
IssueService.IssueResult transResult;

IssueInputParameters issueInputParameters = issueService.newIssueInputParameters();
//issueInputParameters.setStatusId(newEpicState);

TransitionValidationResult validationResult = issueService.validateTransition(curUser, issue.getId(), actionId, issueInputParameters);

if (validationResult.isValid()) {
transResult = issueService.transition(curUser, validationResult);
}

log.warn "Epic status is updated!"
}

 

0 answers

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Thursday in Marketplace Apps

Tips on how to choose the best estimation method for your planning

Planning and grooming sessions all come with their own sets of rules. Team members meet to estimate stories or other work items, all according to an agreed-upon process. And with every session comes ...

71 views 0 11
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you