Set assignee to some specific user in post function script

Georgiy Senenkov July 2, 2012

Hello,

I use JIRA 5.0 and I need to set assignee to particular user when issue is resolved in specific project. I cannot use existing post functions to set assignee because workflow is not unique and used for other projects. I think post function script should serve very well for this purpose. I found an example how to set user to specific one at

https://answers.atlassian.com/questions/44757/groovy-issue-assignment-is-not-creating-an-active-link-on-the-name

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.util.ImportUtils
import com.atlassian.jira.user.util.DefaultUserManager
import com.atlassian.crowd.embedded.api.User

MutableIssue issue = issue

UserManager userManager = UserManager.getInstance();
 
User usera = userManager.getUser('my_user_name');
 
issue.setAssignee(usera);

but it does not work for JIRA5.0 and returns the following error

Script9.groovy: 20: unable to resolve class UserManager
@ line 20, column 13.
UserManager userManager = UserManager.getInstance()
^

1 error

I guess getInstance() method does not exist in JIRA5.0.

Could you please advise what method I can use to set user to particular one?

Thanky you in advance.

Regards, Georgiy

4 answers

1 accepted

1 vote
Answer accepted
Jobin Kuruvilla [Adaptavist]
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.
July 2, 2012

Try this

ComponentManager.getUserUtil.getUser('ngs')

Georgiy Senenkov July 2, 2012

I tried

User usera = ComponentManager.getUserUtil.getUser('ngs')

but it returned error

2012-07-03 14:46:57,813 http-80-5 ERROR admin 886x1216x1 13ft31r 10.161.201.37 /secure/CommentAssignIssue.jspa [onresolve.jira.groovy.GroovyFunctionPlugin] Error executing post-function
javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: getUserUtil for class: com.atlassian.jira.ComponentManager

Mizan
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.
July 2, 2012

Try

ComponentManager.getInstance().getUserUtil.getUser('ngs')

Mizan
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.
July 2, 2012

Hmm syntax errors . i am sorry , use the below code

ComponentManager.getInstance().getUserUtil().getUser('ngs')

Jobin Kuruvilla [Adaptavist]
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.
July 2, 2012

Are you sure you modified the script? The error seems to suggest the old code?

Georgiy Senenkov July 2, 2012

the same error :(

2012-07-03 15:04:37,658 http-80-4 ERROR admin 904x1287x1 13ft31r 10.161.201.37 /secure/CommentAssignIssue.jspa [onresolve.jira.groovy.GroovyRunner] The script failed : javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: getUserUtil for class: com.atlassian.jira.ComponentManager
2012-07-03 15:04:37,658 http-80-4 ERROR admin 904x1287x1 13ft31r 10.161.201.37 /secure/CommentAssignIssue.jspa [onresolve.jira.groovy.GroovyFunctionPlugin] Error executing post-function
javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: getUserUtil for class: com.atlassian.jira.ComponentManager

Georgiy Senenkov July 2, 2012

i also used simplier view as

componentManager = ComponentManager.getInstance()
userUtil= componentManager.getUserUtil()
usera = userUtil.getUser('ngs')

Georgiy Senenkov July 2, 2012

Yes, that's works!!!! :) Thank you very much!!!

Mizan
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.
July 2, 2012

WC .,... :)

0 votes
Mizan
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.
July 2, 2012

For the project try the below code , even apply some logging to check whether you get the project object

Project project=ComponentManager.getInstance().getProjectManager().getProjectObj(issue.getProjectObject().getId())

Georgiy Senenkov July 3, 2012

I used

project=ComponentManager.getInstance().getProjectManager().getProjectObj(issue.getProjectObject().getId())

"Project" returned error that class is unknown. I added print

System.out.println("Project is:" +project);

and it returns "Project is:Project: MYPROJECT"

so, now i need to parse the string to get the project name.

I guess "split" can be used.

Mizan
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.
July 3, 2012

You are getting the project object , try

System.out.println("Project is:" +project.getName());

Georgiy Senenkov July 3, 2012

That's what I was looking for! Thank you very much!

Amir Vegdani May 14, 2015

Hi Georgiy, Can you please advise what is the final script that you are using. Thanks

0 votes
Georgiy Senenkov July 2, 2012

Could you please point how I can read project from the issue? as I need to set assignee to particular user only in specific project.

Thank you.

Regards, Georgiy

0 votes
Mizan
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.
July 2, 2012

Try importing below interface in your script

com.atlassian.jira.user.util.UserManager

Georgiy Senenkov July 2, 2012

Thank you! It helped to resolve the error mentioned in the mail above, but now I get another error

2012-07-03 13:43:03,686 http-80-2 ERROR admin 823x910x1 13ft31r 10.161.201.37 /secure/CommentAssignIssue.jspa [onresolve.jira.groovy.GroovyRunner] The script failed : javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: static com.atlassian.jira.user.util.UserManager.getInstance() is applicable for argument types: () values: []
2012-07-03 13:43:03,686 http-80-2 ERROR admin 823x910x1 13ft31r 10.161.201.37 /secure/CommentAssignIssue.jspa [onresolve.jira.groovy.GroovyFunctionPlugin] Error executing post-function
javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: static com.atlassian.jira.user.util.UserManager.getInstance() is applicable for argument types: () values: []
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(AbstractScriptEngine.java:195)

which I guess should not appear. Do you know what might be wrong, please?

Regards, Georgiy

Mizan
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.
July 2, 2012

Try the below code

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.util.ImportUtils
import com.atlassian.jira.user.util.UserManager
import com.atlassian.crowd.embedded.api.User
 
MutableIssue issue = issue
 
User usera = UserManager.getUser('my_user_name');
  
issue.setAssignee(usera);

Mizan
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.
July 2, 2012

I guess you are not using quotes

Try this

User usera = UserManager.getUser('ngs'); OR
User usera = UserManager.getUserObject('ngs');
Georgiy Senenkov July 2, 2012

It didn't help. The error is still there :(

2012-07-03 14:05:58,099 http-80-2 ERROR admin 845x1060x1 13ft31r 10.161.201.37 /secure/CommentAssignIssue.jspa [onresolve.jira.groovy.GroovyRunner] The script failed : javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: static com.atlassian.jira.user.util.UserManager.getUser() is applicable for argument types: (java.lang.String) values: [ngs]
Possible solutions: getAt(java.lang.String)
2012-07-03 14:05:58,099 http-80-2 ERROR admin 845x1060x1 13ft31r 10.161.201.37 /secure/CommentAssignIssue.jspa [onresolve.jira.groovy.GroovyFunctionPlugin] Error executing post-function
javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: static com.atlassian.jira.user.util.UserManager.getUser() is applicable for argument types: (java.lang.String) values: [ngs]
Possible solutions: getAt(java.lang.String)

Georgiy Senenkov July 2, 2012

actually I used quotes. My line was exactly the same as suggested

User usera = UserManager.getUser('ngs');

Jobin Kuruvilla [Adaptavist]
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.
July 2, 2012

getUser is not static

Georgiy Senenkov July 2, 2012
neither
User usera = UserManager.getUser('ngs');
NOR
User usera = UserManager.getUserObject('ngs');
worked.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events