How to add comment after workflow transition, from a different user that performs the action, with groovyrunner?

Hi everybody,

starting from this question

https://answers.atlassian.com/questions/5401/jira-automatically-add-a-comment-on-transition

i would like to add a comment automatically where the commenter is not the user who performs the workflow action.

I have made some experiments with the following code

import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.comments.CommentManager;
  
// gets a reference to the IssueManager and CustomFieldManager manager classes
ComponentManager componentManager = ComponentManager.getInstance()
CustomFieldManager customFieldManager = ComponentManager.getInstance().getCustomFieldManager();
CommentManager commentManager = componentManager.getCommentManager();  
// gets a reference to the desired issue and custom field
 
CustomField customField_email = customFieldManager.getCustomFieldObjectByName( "Email" );
// retrieves the custom field value object from the issue
def email = issue.getCustomFieldValue( customField_email );

commentManager.create(restuser, issue, transientVars.context.caller, "Email has been sent to " + email, false);

The username of the the user is "restuser" but i'm getting the following error

2013-02-06 16:41:19,237 http-bio-8080-exec-13 ERROR tsol 1001x821x1 1jr5p0a 10.0.9.115 /secure/CreateIssueDetails.jspa [onresolve.jira.groovy.GroovyRunner] The script failed : javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: restuser for class: Script2
2013-02-06 16:41:19,237 http-bio-8080-exec-13 ERROR tsol 1001x821x1 1jr5p0a 10.0.9.115 /secure/CreateIssueDetails.jspa [onresolve.jira.groovy.GroovyFunctionPlugin] Error executing post-function
javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: restuser for class: Script2
	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:97)
	at com.onresolve.jira.groovy.GroovyRunner.run(GroovyRunner.java:57)
	at com.onresolve.jira.groovy.GroovyFunctionPlugin.execute(GroovyFunctionPlugin.java:35)
	at com.opensymphony.workflow.AbstractWorkflow.executeFunction(AbstractWorkflow.java:1050)
	at com.opensymphony.workflow.AbstractWorkflow.transitionWorkflow(AbstractWorkflow.java:1446)
	at com.opensymphony.workflow.AbstractWorkflow.initialize(AbstractWorkflow.java:615)
	at com.atlassian.jira.workflow.OSWorkflowManager.createIssue(OSWorkflowManager.java:807)
	at com.atlassian.jira.issue.managers.DefaultIssueManager.createIssue(DefaultIssueManager.java:488)
	at com.atlassian.jira.issue.managers.DefaultIssueManager.createIssue(DefaultIssueManager.java:426)
	at com.atlassian.jira.bc.issue.DefaultIssueService.create(DefaultIssueService.java:182)
	at com.atlassian.jira.web.action.issue.CreateIssueDetails.createIssue(CreateIssueDetails.java:109)
	at com.atlassian.jira.web.action.issue.CreateIssueDetails.doExecute(CreateIssueDetails.java:85)  <+1> (ActionSupport.java:165)
	at com.atlassian.jira.action.JiraActionSupport.execute(JiraActionSupport.java:82)  <+7> (DefaultInterceptorChain.java:39) (NestedInterceptorChain.java:31) (ChainedInterceptor.java:16) (DefaultInterceptorChain.java:35) (GenericDispatcher.java:221) (GenericDispatcher.java:150) (JiraWebworkActionDispatcher.java:153)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)  <+14> (ApplicationFilterChain.java:305) (ApplicationFilterChain.java:210) (ChainedFilterStepRunner.java:78) (ApplicationFilterChain.java:243) (ApplicationFilterChain.java:210) (XContentTypeOptionsNoSniffFilter.java:22) (AbstractHttpFilter.java:31) (ApplicationFilterChain.java:243) (ApplicationFilterChain.java:210) (HeaderSanitisingFilter.java:44) (ApplicationFilterChain.java:243) (ApplicationFilterChain.java:210) (IteratingFilterChain.java:46) (DelegatingPluginFilter.java:66)
	at com.atlassian.labs.botkiller.BotKillerFilter.doFilter(BotKillerFilter.java:36)  <+3> (DelegatingPluginFilter.java:74) (IteratingFilterChain.java:42) (DelegatingPluginFilter.java:66)
	at com.atlassian.jira.tzdetect.IncludeResourcesFilter.doFilter(IncludeResourcesFilter.java:39)  <+3> (DelegatingPluginFilter.java:74) (IteratingFilterChain.java:42) (DelegatingPluginFilter.java:66)
	at com.atlassian.jira.baseurl.IncludeResourcesFilter.doFilter(IncludeResourcesFilter.java:40)  <+8> (AbstractHttpFilter.java:31) (DelegatingPluginFilter.java:74) (IteratingFilterChain.java:42) (DelegatingPluginFilter.java:66) (ContextFilter.java:25) (DelegatingPluginFilter.java:74) (IteratingFilterChain.java:42) (DelegatingPluginFilter.java:66)
	at com.atlassian.mywork.client.filter.ServingRequestsFilter.doFilter(ServingRequestsFilter.java:37)  <+20> (DelegatingPluginFilter.java:74) (IteratingFilterChain.java:42) (ServletFilterModuleContainerFilter.java:77) (ServletFilterModuleContainerFilter.java:63) (ApplicationFilterChain.java:243) (ApplicationFilterChain.java:210) (AccessLogFilter.java:103) (AccessLogFilter.java:87) (ApplicationFilterChain.java:243) (ApplicationFilterChain.java:210) (XsrfTokenAdditionRequestFilter.java:54) (ApplicationFilterChain.java:243) (ApplicationFilterChain.java:210) (SiteMeshFilter.java:129) (SiteMeshFilter.java:77) (SitemeshPageFilter.java:124) (ApplicationFilterChain.java:243) (ApplicationFilterChain.java:210) (IteratingFilterChain.java:46) (DelegatingPluginFilter.java:66)
	at com.atlassian.labs.remoteapps.modules.permissions.ApiScopingFilter.doFilter(ApiScopingFilter.java:60)  <+41> (DelegatingPluginFilter.java:74) (IteratingFilterChain.java:42) (ServletFilterModuleContainerFilter.java:77) (ServletFilterModuleContainerFilter.java:63) (ApplicationFilterChain.java:243) (ApplicationFilterChain.java:210) (SecurityFilter.java:234) (ApplicationFilterChain.java:243) (ApplicationFilterChain.java:210) (TrustedApplicationsFilter.java:98) (ApplicationFilterChain.java:243) (ApplicationFilterChain.java:210) (BaseLoginFilter.java:169) (JiraLoginFilter.java:70) (ApplicationFilterChain.java:243) (ApplicationFilterChain.java:210) (IteratingFilterChain.java:46) (DelegatingPluginFilter.java:66) (OAuthFilter.java:55) (DelegatingPluginFilter.java:74) (IteratingFilterChain.java:42) (ServletFilterModuleContainerFilter.java:77) (ServletFilterModuleContainerFilter.java:63) (ApplicationFilterChain.java:243) (ApplicationFilterChain.java:210) (ProfilingFilter.java:99) (JIRAProfilingFilter.java:19) (ApplicationFilterChain.java:243) (ApplicationFilterChain.java:210) (AbstractJohnsonFilter.java:71) (ApplicationFilterChain.java:243) (ApplicationFilterChain.java:210) (UrlRewriteFilter.java:738) (ApplicationFilterChain.java:243) (ApplicationFilterChain.java:210) (GzipFilter.java:80) (GzipFilter.java:51) (ApplicationFilterChain.java:243) (ApplicationFilterChain.java:210) (IteratingFilterChain.java:46) (DelegatingPluginFilter.java:66)
	at com.atlassian.labs.remoteapps.modules.oauth.OAuth2LOFilter.doFilter(OAuth2LOFilter.java:70)  <+3> (DelegatingPluginFilter.java:74) (IteratingFilterChain.java:42) (DelegatingPluginFilter.java:66)
	at com.atlassian.labs.remoteapps.util.http.bigpipe.RequestIdSettingFilter.doFilter(RequestIdSettingFilter.java:22)  <+43> (DelegatingPluginFilter.java:74) (IteratingFilterChain.java:42) (DelegatingPluginFilter.java:66) (JWDSendRedirectFilter.java:25) (DelegatingPluginFilter.java:74) (IteratingFilterChain.java:42) (ServletFilterModuleContainerFilter.java:77) (ServletFilterModuleContainerFilter.java:63) (ApplicationFilterChain.java:243) (ApplicationFilterChain.java:210) (ChainedFilterStepRunner.java:78) (ApplicationFilterChain.java:243) (ApplicationFilterChain.java:210) (AbstractCachingFilter.java:33) (AbstractHttpFilter.java:31) (ApplicationFilterChain.java:243) (ApplicationFilterChain.java:210) (AbstractEncodingFilter.java:41) (AbstractHttpFilter.java:31) (PathMatchingEncodingFilter.java:49) (AbstractHttpFilter.java:31) (ApplicationFilterChain.java:243) (ApplicationFilterChain.java:210) (JiraStartupChecklistFilter.java:74) (ApplicationFilterChain.java:243) (ApplicationFilterChain.java:210) (MultiTenantServletFilter.java:91) (ApplicationFilterChain.java:243) (ApplicationFilterChain.java:210) (ChainedFilterStepRunner.java:78) (ApplicationFilterChain.java:243) (ApplicationFilterChain.java:210) (StandardWrapperValve.java:225) (StandardContextValve.java:123) (AuthenticatorBase.java:472) (StandardHostValve.java:168) (ErrorReportValve.java:98) (StandardEngineValve.java:118) (AccessLogValve.java:927) (CoyoteAdapter.java:407) (AbstractHttp11Processor.java:1001) (AbstractProtocol.java:585) (JIoEndpoint.java:312)
	at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
	at java.lang.Thread.run(Unknown Source)
Caused by: javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: restuser for class: Script2
	at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:318)
	at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:111)
	... 169 more
Caused by: groovy.lang.MissingPropertyException: No such property: restuser for class: Script2
	at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:50)
	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:231)
	at Script2.run(Script2.groovy:19)
	at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:315)
	... 170 more

If i use the script without providing a username, script works fine.

Thanks in advance,

Kostas

1 answer

1 accepted

0 vote

> commentManager.create(restuser, issue, transientVars.context.caller, "Email has been sent to " + email, false);

This is wrong. If restuser is not a variable it should be quoted.

You need to pick one of the "create" methods from here: https://developer.atlassian.com/static/javadoc/jira/5.2/reference/com/atlassian/jira/issue/comments/CommentManager.html

Check the ordering of your parameters, none have the author as the first argument.

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 Thursday in Jira

5 ways you can make the most of Jira Software and Bitbucket Cloud

As part of the Bitbucket product team I'm always interested in better understanding what kind of impact the use of our tools have on the way you work. In a recent study we conducted of software devel...

84 views 0 5
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