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

This widget could not be displayed.

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!

This widget could not be displayed.

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.

This widget could not be displayed.

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)

This widget could not be displayed.

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

This widget could not be displayed.

Thanks, I got it working.

This widget could not be displayed.

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
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Published Jul 25, 2018 in Marketplace Apps

Jira Cloud and Bitbucket Cloud Integration with Microsoft Teams

One of the newest products in the Microsoft family - Microsoft Teams,  is a chat-based hub for teamwork that integrates all the people, content, and tools your team needs to be more engaged and ...

733 views 0 3
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