Set epic status to done in workflow post function with scriptrunner

I want to set the epic status to Done when an epic arrives in the final state of our workflow. The idea is to add a script on a post function on our 'Conclude' transition. So far I have come up with this:

 

import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.customfields.manager.OptionsManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.fields.config.FieldConfig;
import com.atlassian.jira.issue.customfields.option.Options;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.Issue; import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.event.type.*;
import com.atlassian.crowd.embedded.api.User;
 
ComponentManager componentManager = ComponentManager.getInstance();
IssueManager issueManager = componentManager.getIssueManager(); CustomFieldManager customFieldManager = componentManager.getCustomFieldManager();
OptionsManager optionsManager = componentAccessor.getOptionsManager();
User user = componentManager.getJiraAuthenticationContext().getLoggedInUser();
 
// This is to be able to test in Script Console
issue = issueManager.getIssueObject("SCRSAN-22");
 
if (issue.issueType.name == "Epic")
{
    CustomField customField = customFieldManager.getCustomFieldObjectByName("Epic Status");
    FieldConfig fieldConfig = customField.getRelevantConfig(issue);
    Options options = optionsManager.getOptions(fieldConfig);
    MutableIssue issueToUpdate = (MutableIssue) issue;
    return options;
 
    //If I ever get to set the Epic Status, write to issue ;)
    //issueToUpdate.setCustomFieldValue(customField, "Done");
    //issueManager.updateIssue(user, issueToUpdate, EventDispatchOption.ISSUE_UPDATED, false);
}

 

 

This is not working yet and I am wondering if I am on the right track or not. Do I really need to do all the imports and find the right Option to set to the customfield Epic Status. Or can it be done in a simpler way?

6 answers

1 accepted

I got this working with the following

import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.customfields.manager.OptionsManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.fields.config.FieldConfig;
import com.atlassian.jira.issue.customfields.option.Options;
import com.atlassian.jira.issue.customfields.option.Option;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.event.type.*;
import com.atlassian.crowd.embedded.api.User;
ComponentManager componentManager = ComponentManager.getInstance();
IssueManager issueManager = componentManager.getIssueManager(); CustomFieldManager customFieldManager = componentManager.getCustomFieldManager();
OptionsManager optionsManager = ComponentManager.getComponentInstanceOfType(OptionsManager.class);
User user = componentManager.getJiraAuthenticationContext().getLoggedInUser();
if (issue.issueType.name == "Epic")
{
    CustomField epicStatus = customFieldManager.getCustomFieldObjectByName("Epic Status");
    FieldConfig epicStatusFieldConfig = epicStatus.getRelevantConfig(issue);
    Options epicStatusOptions = optionsManager.getOptions(epicStatusFieldConfig);
    Option epicStatusDoneOption = epicStatusOptions.getOptionForValue("Done", null);
    MutableIssue issueToUpdate = (MutableIssue) issue;
    issueToUpdate.setCustomFieldValue(epicStatus, epicStatusDoneOption);
    //The updateIssue is actually not needed
    //issueManager.updateIssue(user, issueToUpdate, EventDispatchOption.ISSUE_UPDATED, false);
}

easy peasy!

0 vote

Can you use the {code} macro so that's readable? The hard-coded issue ID stands out as a problem. > Do I really need to do all the imports and find the right Option to set to the customfield Epic Status Pretty much, yes.

OK, I updated to use the code macro. OK, good to know I am on the right path (with all my imports and all). Please excuse me if I start asking rookie questions from here, as that's basically what I am in this SciptRunner/JIRA API stuff. The hard-coded issue ID was in there to be able to test/debug in the Script Console. That way I can see what is happening and don't have to go through workflow modifications. Is there a 'better' way to write/debug these scripts? Anyway, it seem I to need access the optionsManager to find the "Done" option. Seem to have found the following in a post by you from 2012. Will see if I can get that in there and find the right option to set to the customfield. OptionsManager optionsManager = ComponentManager.getComponentInstanceOfType(OptionsManager.class)

0 vote

Yes, if it's a select list you are on the right track there.

Thanks, I got it working.

0 vote
Nicholas Muldoon Community Champion Mar 13, 2016

Hi Peter,

I had exactly this issue at Twitter. I've detailed how we addressed this in Understanding Cycle Time of JIRA Epics.

May be of use in your scenario.

Regards,
Nicholas Muldoon
Arijea 

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 May 30, 2018 in Marketplace Apps

Three tips for boosting your board's efficiency with Story Maps

Trello is one of the most effective tools for driving your sprints. It's customizable for every Agile team and product owners and Scrum masters (SM) love it. However, Agile teams often struggle with:...

827 views 2 9
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