It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

ScriptRunner : Groovy Script- Assign issue based on issue type

Shivani Chhetri Nov 16, 2017

 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

1 vote
Answer accepted
Alexey Matveev [cPrime] Community Leader 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?

Shivani Chhetri Nov 16, 2017

Hi Alexey,

 

Thank you for responding.

Yes, Product owner and Tech lead are the project roles that we have set.

Alexey Matveev [cPrime] Community Leader 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()
}
Shivani Chhetri Nov 19, 2017 • edited

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)

Alexey Matveev [cPrime] Community Leader Nov 19, 2017 • edited

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

Shivani Chhetri Nov 19, 2017 • edited

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

Nic Brough [Adaptavist] Community Leader Nov 19, 2017

setAssignee needs a User object, not a string.

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

Shivani Chhetri Nov 19, 2017 • edited

-

Alexey Matveev [cPrime] Community Leader 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.

Shivani Chhetri Nov 19, 2017

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 [cPrime] Community Leader Nov 19, 2017

Yes, you can use it

Shivani Chhetri Nov 19, 2017

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 [cPrime] Community Leader Nov 19, 2017

this

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

Shivani Chhetri Nov 19, 2017 • edited

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 [cPrime] Community Leader 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
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Published in Marketplace Apps & Integrations

Webinar alert! An IT Admin's Guide to Securing Collaboration at Scale

Hello, Atlassian Community! My name is Dave Meyer and I'm a Principal Product Manager at Atlassian. I wanted to give this community a heads up about an upcoming Webinar that might be of interest...

186 views 2 7
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you