ScriptRunner : Groovy Script- Assign issue based on issue type

 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:

  • Epics, User Stories and Improvements get automatically assigned to the Product Owner
  • Tasks and Bugs get automatically assigned to the Tech Lead (if there is no Tech Lead assigned for a project, it should default back to Product Owner).

Can anyone help on how to start with the script to do this? Thanks.

 

1 answer

1 accepted

0 vote
Alexey Matveev Community Champion Nov 16, 2017

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.

Alexey Matveev Community Champion Nov 16, 2017

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()
}

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)

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

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

 

Script.PNG

setAssignee needs a User object, not a string.

Alexey's code gets a user from a project role, so that should fix this problem

Alexey Matveev Community Champion Nov 19, 2017

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.

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()));

}

Alexey Matveev Community Champion Nov 19, 2017

Yes, you can use it

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?
}

Alexey Matveev Community Champion Nov 19, 2017

this

issue.setAssignee(actors.getUsers().toList()?.first())

Hi Alexey,

 

Thank you much for your support. It worked now. 

  • Tasks and Bugs get automatically assigned to the Tech Lead (if there is no Tech Lead assigned for a project, it should default back to Product Owner). -> Is there a way we can achieve this-Highlighted in Bold
Alexey Matveev Community Champion Nov 19, 2017

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

}

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 yesterday 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...

71 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