Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,362,144
Community Members
 
Community Events
168
Community Groups

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

1 vote
Answer 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);
}
JamieA Rising Star Jul 19, 2015

easy peasy!

I was unable to get the example working (most likely due to newer versions of the tools).

I found an alternative solution on the Adaptavist pages that is much shorter and that works like a charm:

 

import com.atlassian.jira.component.ComponentAccessor
def cfSelect = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Favourite Fruit")
def cfConfig = cfSelect.getRelevantConfig(issue)
def value = ComponentAccessor.optionsManager.getOptions(cfConfig)?.find {
it.toString() == 'Lemons'
}
issue.setCustomFieldValue(cfSelect, value)

 

Replace "Favourite Fruit" with the actual field name in JIRA.

Replace 'Lemons' with the actual custom field select value.

Like Vladislav likes this

Thanks Lars!

Everybody please note that on adaptavist docs there is a typo in the code 

https://docs.adaptavist.com/sfj/set-a-select-list-custom-field-value-11468840.html

Don't forget to fix it as mentioned by Lars - then code works like a charm ;)

code-typo.png

0 votes

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 

Thanks, I got it working.

0 votes
JamieA Rising Star Jul 19, 2015

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

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 votes
JamieA Rising Star Jul 18, 2015

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.

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events