Set assignee to some specific user in post function script

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

Try this

ComponentManager.getUserUtil.getUser('ngs')

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 Community Champion Jul 02, 2012

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

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

Mizan Community Champion Jul 02, 2012

Try

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

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

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

i also used simplier view as

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

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

Mizan Community Champion Jul 02, 2012

WC .,... :)

0 vote
Mizan Community Champion Jul 02, 2012

Try importing below interface in your script

com.atlassian.jira.user.util.UserManager

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 Community Champion Jul 02, 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 Community Champion Jul 02, 2012

I guess you are not using quotes

Try this

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

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)

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

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

getUser is not static

neither
User usera = UserManager.getUser('ngs');
NOR
User usera = UserManager.getUserObject('ngs');
worked.

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 vote
Mizan Community Champion Jul 02, 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())

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 Community Champion Jul 03, 2012

You are getting the project object , try

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

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

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

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published Thursday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

348 views 0 6
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