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?
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!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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 ;)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Yes, if it's a select list you are on the right track there.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.