Just a heads up: On March 24, 2025, starting at 4:30pm CDT / 21:30 UTC, the site will be undergoing scheduled maintenance for a few hours. During this time, the site might be unavailable for a short while. Thanks for your patience.
×I am new with groovy script and I am having difficulties in writing a script.
The idea is auto assign newly created tickets based on Issue Type, as follows:
Can anyone help on how to start with the script to do this? Thanks.
Hello, You should write a post function for the create issue transition. The script will be somthing like this
if (issue.issueTypeObject.name == "Epic" || issue.issueTypeObject.name == "Story") {
issue.setAssignee(<product owner>);
}
What are Product owner and Tech Lead? These are roles for the project?
Hi Alexey,
Thank you for responding.
Yes, Product owner and Tech lead are the project roles that we have set.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You can find the users in Tech role like this
ProjectRoleManager projectRoleManager = ComponentAccessor.getComponentOfType(ProjectRoleManager.class) as ProjectRoleManager // name of role here ProjectRole devsRole = projectRoleManager.getProjectRole("Tech Lead") ProjectRoleActors actors = projectRoleManager.getProjectRoleActors(devsRole, issue.getProjectObject()) // if there is only one member of the role or you only want the first you could do: if (actors.size() == 0) {
//users not found
} else {
techlead = actors.getUsers().toList()?.first()
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Alexey,
Thank you very much for the answer .
I have run the below code, but it does not seemed to work :
Could you please help to advise on this :
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.UpdateIssueRequest
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.bc.projectroles.ProjectRoleService;
import com.atlassian.jira.project.Project;
import com.atlassian.jira.project.ProjectManager;
import com.atlassian.jira.security.roles.ProjectRole;
import com.atlassian.jira.security.roles.ProjectRoleActors;
import com.atlassian.jira.security.roles.ProjectRoleManager;
import com.atlassian.jira.security.roles.RoleActor;
ProjectRoleManager projectRoleManager = ComponentAccessor.getComponentOfType(ProjectRoleManager.class) as ProjectRoleManager
// name of role here
ProjectRole devsRole = projectRoleManager.getProjectRole("PRODUCT OWNERS")
ProjectRoleActors actors = projectRoleManager.getProjectRoleActors(devsRole, issue.getProjectObject())
// if there is only one member of the role or you only want the first you could do:
if (actors.size() == 0) {
//users not found
} else {
ProductOwners = actors.getUsers().toList()?.first()
}
if (issue.issueTypeObject.name == "Bug") {
issue.setAssignee(devsRole); (what do i need to pass here)
}
Error: 2017-11-19 03:42:29,346 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: null, actionId: 1, file: <inline script>
java.lang.IllegalArgumentException: ProjectRole can not be null
at com.atlassian.jira.security.roles.DefaultProjectRoleManager.getProjectRoleActors(DefaultProjectRoleManager.java:110)
at com.atlassian.jira.security.roles.ProjectRoleManager$getProjectRoleActors$0.call(Unknown Source)
at Script21.run(Script21.groovy:25)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
First of al Il, checked the script. Kindly change
if (actors.size() == 0) {
to
if (actors.getUsers().size() == 0) {
You get NullPointerException because the role PRODUCT OWNERS does not exist. You should add it first in cog -> system -> Project roles. Then you should add a user to the role in your project. After it everything must work.
In issue.setAssignee(user) you should pass the user key of your default user. For example,
issue.setAssignee("admin");
You can read more about project roles here
https://confluence.atlassian.com/adminjiracloud/managing-project-roles-776636382.html
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
The Project role is already Created. I have changed the script still I am getting the same error. I am not sure what i am doing wrong here. The error log is giving the same error
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
setAssignee needs a User object, not a string.
Alexey's code gets a user from a project role, so that should fix this problem
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, Nic is right.
You should write the line like this
issue.setAssignee(ComponentAccessor.getUserManager().getUserByKey("t_chhesh"));
or
issue.setAssignee(ComponentAccessor.getUserManager().getUserByKey(actors.getUsers().toList()?.first()));
If the role is called Product owners then it must be
projectRoleManager.getProjectRole("Product owners").
It is case sensitive.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
if I want the first member under product owner to be assigned . can use this below code?
if (actors.getUsers().size() == 0) {
}
else{
issue.setAssignee(ComponentAccessor.getUserManager().getUserByKey(actors.getUsers().toList()?.first()));
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Yes, you can use it
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
This gives me the user in the Product Owner role.
ProjectRoleManager projectRoleManager = ComponentAccessor.getComponentOfType(ProjectRoleManager.class) as ProjectRoleManager
ProjectRole devsRole = projectRoleManager.getProjectRole("Product Owners")
ProjectRoleActors actors = projectRoleManager.getProjectRoleActors(devsRole, issue.getProjectObject())
Now , to auto assign the ticket to the Product Owner when the issue type is bug:
if (issue.issueTypeObject.name == "Bug") {
issue.setAssignee(); ---What do I pass here?
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
this
issue.setAssignee(actors.getUsers().toList()?.first())
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Alexey,
Thank you much for your support. It worked now.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You can do it like this
ProjectRole techLeadRole = projectRoleManager.getProjectRole("Tech Lead")
ProjectRoleActors actorsTechLead = projectRoleManager.getProjectRoleActors(techLeadRole, issue.getProjectObject())
if (actorsTechLead.size() == 0) {
if you are here then no Tech Lead user then assign Product owner here
}
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.