[Script Runner] Post function to set fixVersion and affectsVersion from CF

Błażej O_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 9, 2014

Hello.

I'm trying to alter the code from the script runner documentation:

https://jamieechlin.atlassian.net/wiki/display/GRV/Post+Functions#PostFunctions-SetAffectsVersions,FixVersionsorComponents

I have done something like this:

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.link.IssueLinkTypeManager
import com.atlassian.jira.web.action
import com.atlassian.jira.user
import com.atlassian.jira.project.version
import com.atlassian.core.ofbiz.CoreFactory
import webwork.dispatcher.ActionResult

import org.apache.log4j.Category
Category log = log
log.setLevel(org.apache.log4j.Level.DEBUG);

ComponentManager componentManager = ComponentManager.getInstance()
User remoteUser = componentManager.getJiraAuthenticationContext().getUser()
//MutableIssue issue = componentManager.getIssueManager().getIssueObject("DTT-1757")
MutableIssue issue = issue

def ver_affCF = customFieldManager.getCustomFieldObject('customfield_13302');//customfield containing string with affects version
def ver_aff = issue.getCustomFieldValue(ver_affCF);
def ver_fixCF = customFieldManager.getCustomFieldObject('customfield_13416');//customfield containing string with fix version
def ver_fix = issue.getCustomFieldValue(ver_fixCF);

Project project = issue.getProjectObject()
Version versionFX = componentManager.getVersionManager().getVersion(project.getId(), ver_fix)
Version versionAFCTS = componentManager.getVersionManager().getVersion(project.getId(), ver_aff)
//ProjectComponent component = componentManager.getProjectComponentManager().findByComponentName(project.getId(), "MyComponent")
issue.setFixVersions([versionFX])
issue.setAffectsVersions([versionAFCTS])
//issue.setComponents([component.getGenericValue()])
 
Map actionParams = EasyMap.build("issue", issue.getGenericValue(), "issueObject", issue, "remoteUser", remoteUser, "dispatchEvent", Boolean.FALSE);
ActionResult aResult = CoreFactory.getActionDispatcher().execute(ActionNames.ISSUE_UPDATE, actionParams);
// check aResult

However, even after adding import wich I think are correct (at least that's what I've found in https://docs.atlassian.com/jira/) I still get following errors:

2014-06-09 16:14:02,248 Thread-12981 ERROR bolszyca 973x252239x2 1bth0u4 10.1.3.34 /secure/CommentAssignIssue.jspa [onresolve.jira.groovy.GroovyRunner] The script failed : javax.script.ScriptException: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script4.groovy: 5: unable to resolve class com.atlassian.jira.web.action
 @ line 5, column 1.
   import com.atlassian.jira.web.action
   ^

Script4.groovy: 6: unable to resolve class com.atlassian.jira.user
 @ line 6, column 1.
   import com.atlassian.jira.user
   ^

Script4.groovy: 7: unable to resolve class com.atlassian.jira.project.version
 @ line 7, column 1.
   import com.atlassian.jira.project.version
   ^

Script4.groovy: 18: unable to resolve class User 
 @ line 18, column 6.
   User remoteUser = componentManager.getJiraAuthenticationContext().getUser()
        ^

Script4.groovy: 27: unable to resolve class Project 
 @ line 27, column 9.
   Project project = issue.getProjectObject()
           ^

Script4.groovy: 28: unable to resolve class Version 
 @ line 28, column 9.
   Version versionFX = componentManager.getVersionManager().getVersion(project.getId(), wersja_naprawa)
           ^

Script4.groovy: 29: unable to resolve class Version 
 @ line 29, column 9.
   Version versionAFCTS = componentManager.getVersionManager().getVersion(project.getId(), wersja_znaleziono)
           ^

7 errors

2014-06-09 16:14:02,248 Thread-12981 ERROR bolszyca 973x252239x2 1bth0u4 10.1.3.34 /secure/CommentAssignIssue.jspa [onresolve.jira.groovy.GroovyFunctionPlugin] Script post-function failed on HOSP-3615: C:\Program Files\Atlassian\JIRA\atlassian-jira\skryptyistyle\update_versions_components.groovy
javax.script.ScriptException: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script4.groovy: 5: unable to resolve class com.atlassian.jira.web.action
 @ line 5, column 1.
   import com.atlassian.jira.web.action
   ^

Script4.groovy: 6: unable to resolve class com.atlassian.jira.user
 @ line 6, column 1.
   import com.atlassian.jira.user
   ^

Script4.groovy: 7: unable to resolve class com.atlassian.jira.project.version
 @ line 7, column 1.
   import com.atlassian.jira.project.version
   ^

Script4.groovy: 18: unable to resolve class User 
 @ line 18, column 6.
   User remoteUser = componentManager.getJiraAuthenticationContext().getUser()
        ^

Script4.groovy: 27: unable to resolve class Project 
 @ line 27, column 9.
   Project project = issue.getProjectObject()
           ^

Script4.groovy: 28: unable to resolve class Version 
 @ line 28, column 9.
   Version versionFX = componentManager.getVersionManager().getVersion(project.getId(), wersja_naprawa)
           ^

Script4.groovy: 29: unable to resolve class Version 
 @ line 29, column 9.
   Version versionAFCTS = componentManager.getVersionManager().getVersion(project.getId(), wersja_znaleziono)
           ^

7 errors

	at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:117)
	at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:103)
	at javax.script.AbstractScriptEngine.eval(Unknown Source)
	at com.onresolve.jira.groovy.GroovyRunner.runFile(GroovyRunner.java:102)
	at com.onresolve.jira.groovy.GroovyRunner.run(GroovyRunner.java:62)
	at com.onresolve.jira.groovy.GroovyFunctionPlugin.execute(GroovyFunctionPlugin.java:38)
	at com.opensymphony.workflow.AbstractWorkflow.executeFunction(AbstractWorkflow.java:1050)
	at com.opensymphony.workflow.AbstractWorkflow.transitionWorkflow(AbstractWorkflow.java:1446)
	at com.opensymphony.workflow.AbstractWorkflow.doAction(AbstractWorkflow.java:564)
	at com.atlassian.jira.workflow.OSWorkflowManager.doWorkflowActionInsideTxn(OSWorkflowManager.java:954)
	at com.atlassian.jira.workflow.OSWorkflowManager.doWorkflowAction(OSWorkflowManager.java:909)
	at com.atlassian.jira.bc.issue.DefaultIssueService.transition(DefaultIssueService.java:450)
	at com.atlassian.jira.bc.issue.IssueService$transition.call(Unknown Source)
	at com.onresolve.jira.groovy.canned.utils.WorkflowUtils.actionIssue(WorkflowUtils.groovy:87)  <+2>
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
	at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
	at org.codehaus.groovy.runtime.callsite.StaticMetaMethodSite.invoke(StaticMetaMethodSite.java:43)
	at org.codehaus.groovy.runtime.callsite.StaticMetaMethodSite.call(StaticMetaMethodSite.java:88)
	at com.onresolve.jira.groovy.canned.workflow.postfunctions.FasttrackTransition.doTransition(FasttrackTransition.groovy:136)
	at com.onresolve.jira.groovy.canned.workflow.postfunctions.FasttrackTransition.this$2$doTransition(FasttrackTransition.groovy)  <+2>
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
	at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
	at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1054)
	at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodOnCurrentN(ScriptBytecodeAdapter.java:78)
	at com.onresolve.jira.groovy.canned.workflow.postfunctions.FasttrackTransition.this$dist$invoke$1(FasttrackTransition.groovy)
	at com.onresolve.jira.groovy.canned.workflow.postfunctions.FasttrackTransition$1.methodMissing(FasttrackTransition.groovy)  <+2>
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
	at groovy.lang.MetaClassImpl.invokeMissingMethod(MetaClassImpl.java:811)
	at groovy.lang.MetaClassImpl.invokePropertyOrMissing(MetaClassImpl.java:1103)
	at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1056)
	at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:884)
	at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:66)
	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:149)
	at com.onresolve.jira.groovy.canned.workflow.postfunctions.FasttrackTransition$1.run(FasttrackTransition.groovy:96)
	at java.lang.Thread.run(Unknown Source)
Caused by: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script4.groovy: 5: unable to resolve class com.atlassian.jira.web.action
 @ line 5, column 1.
   import com.atlassian.jira.web.action
   ^

 @ line 6, column 1.
   import com.atlassian.jira.user
   ^

Script4.groovy: 7: unable to resolve class com.atlassian.jira.project.version
 @ line 7, column 1.
   import com.atlassian.jira.project.version
   ^

Script4.groovy: 18: unable to resolve class User 
 @ line 18, column 6.
   User remoteUser = componentManager.getJiraAuthenticationContext().getUser()
        ^

Script4.groovy: 27: unable to resolve class Project 
 @ line 27, column 9.
   Project project = issue.getProjectObject()
           ^

Script4.groovy: 28: unable to resolve class Version 
 @ line 28, column 9.
   Version versionFX = componentManager.getVersionManager().getVersion(project.getId(), wersja_naprawa)
           ^

Script4.groovy: 29: unable to resolve class Version 
 @ line 29, column 9.
   Version versionAFCTS = componentManager.getVersionManager().getVersion(project.getId(), wersja_znaleziono)
           ^

7 errors

	at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:302)
	at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:858)
	at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:548)
	at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:497)
	at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:306)
	at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:287)
	at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:267)
	at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:214)
	at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.getScriptClass(GroovyScriptEngineImpl.java:337)
	at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:109)
	... 43 more

It seems like something is horribly wrong with imports. Unfortunately I'm out of ideas what may be wrong.

1 answer

1 accepted

3 votes
Answer accepted
Henning Tietgens
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 9, 2014

It's unlikely that an import ends with a lowercase word. Try this

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.project.Project
import com.atlassian.jira.project.version.Version
import com.atlassian.jira.project.version.VersionManager
import org.apache.log4j.Category

Category log = log
log.setLevel(org.apache.log4j.Level.DEBUG);

CustomFieldManager customFieldManager = ComponentAccessor.customFieldManager
VersionManager versionManager = ComponentAccessor.versionManager

MutableIssue issue = issue

def ver_affCF = customFieldManager.getCustomFieldObject('customfield_13302');//customfield containing string with affects version
def ver_aff = issue.getCustomFieldValue(ver_affCF);
def ver_fixCF = customFieldManager.getCustomFieldObject('customfield_13416');//customfield containing string with fix version
def ver_fix = issue.getCustomFieldValue(ver_fixCF);

Project project = issue.getProjectObject()
Version versionFX = versionManager.getVersion(project.getId(), ver_fix as String)
Version versionAFCTS = versionManager.getVersion(project.getId(), ver_aff as String)
issue.setFixVersions([versionFX])
issue.setAffectedVersions([versionAFCTS])

And place this postfunction at the beginning of the list of postfunctions.

JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 9, 2014

> It's unlikely that an import ends with a lowercase word

It is. And if you use "def" rather than the static typing you can get rid of most of the imports.

Błażej O_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 9, 2014

Thanks, I'll remember that :)

You guys are awesome :)

JIRA_USER March 15, 2017

Hi Henning Tietgens,

thanks for providing the information.

However, after I implemented the code in Create Transition/postfunction, I was able to get the value in Fix Version/s which was on the basis of Custom field. But seeing two different issues. Would you please update those for me please?

1) When I go to the Edit screen, the Fix Version/s is Null (value is not available) but is available in View Screen.

Note: Both View and Edit screens has same Fix VErsion/s field.

 

2) Would it be possible to auto populate the value of Custom field (Release VErsion = 1.1) in Fix Version/s in a create screen (before creating a defect)?

Please help.

Suggest an answer

Log in or Sign up to answer