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

Marcos Fernando Simon May 4, 2017

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!"
}

 

1 answer

0 votes
Craig Nodwell
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 18, 2022

Ouch this script isn't going to work too big too complicated for such a small task.  Did you get this working?  If not please ping me back and I'll assist

Suggest an answer

Log in or Sign up to answer