Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Scriptrunner - Assign an issue to a random user in a project role

Edison Augusto Machado Junior March 9, 2023

I need to edit the script below to set the issue assignee according to a project role list randomly.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.roles.ProjectRoleManager
import com.atlassian.jira.issue.customfields.view.CustomFieldParams
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.customfields.view.CustomFieldParams
import com.atlassian.jira.issue.customfields.impl.CascadingSelectCFType
import com.atlassian.jira.issue.CustomFieldManager;
import org.apache.log4j.Category

log.setLevel(org.apache.log4j.Level.DEBUG)
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def field = customFieldManager.getCustomFieldObject("customfield_10115")
Object cfVal = issue.getCustomFieldValue(field)

HashMap<String, Option> hashMapEntries = (HashMap<String, Option>) cfVal
if (hashMapEntries != null) {
Option parent = hashMapEntries.get(CascadingSelectCFType.PARENT_KEY)
Option child = hashMapEntries.get(CascadingSelectCFType.CHILD_KEY)
def first = parent.toString()
def second = child.toString()

//-----------------------------------------------------------------------------------------------------------
if (second == "ACM"){
// The role you want assignees to set from
final roleName = 'SAPN1 - ACM'

final reassignedIssues = true
def issueManager = ComponentAccessor.issueManager
def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager)

// Get all of the users associated with the specified project role
def projectRole = projectRoleManager.getProjectRole(roleName)

// Sort the users of the project role using the user key
def users = projectRoleManager.getProjectRoleActors(projectRole, issue.projectObject).applicationUsers.toSorted { it.key }
log.debug("Users: " + users);

issue.setAssignee(users.first()) //Set the first user as assignee
}}

In the last line of this script the issue assignee is changed to project role "SAPN1 - ACM" first member, but I need to change the script to get a random user in this project role.

I tried to modify the last line as below but it did not work. 

issue.setAssignee(users.random()) //Set the first user as assignee

 Can someone help me with this script?

3 answers

0 votes
Ram Kumar Aravindakshan _Adaptavist_
Community Champion
March 11, 2023

Hi @Edison Augusto Machado Junior

We have a couple of working examples in the Adaptavist Library.

The first example is using a Round-Robin approach to assign the issue to the User.

And the second is using the Modulus value of the Issue key to assign the issue to the User.

I hope this helps to answer your question. :-)

Thank you and Kind regards,
Ram

0 votes
Edison Augusto Machado Junior March 10, 2023

.

0 votes
Michael Wohlgemuth
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.
March 9, 2023

Greetings @Edison Augusto Machado Junior .

Although i do not recommend assigning tickets randomly, this method should "randomize" your list:

users.shuffle()

Afterwards, you can access a "randomized" user by using what you did already, ie:

users.first()
Edison Augusto Machado Junior March 9, 2023

Hi Michael, thanks for your answer.

I tried to change the script according your suggest, but I get an error.

Error.png

I understood your recommendation and I agree with you, but my idea is assign the issue to a group of users according to a customfield value.

As you can see in my script, I created the variable "second" to get this customfield and a condition to assign the issue.

Michael Wohlgemuth
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.
March 10, 2023

Hey Edison,

i learned to ignore the type checking "errors" since half the time they are displayed as errors but work as intended.

But:

The shuffle method doesnt return an output afaik. So setting the assignee as you're trying here doesnt work.

I'd suggest just trying to output the values first to see if the general method works as you need it to. Try commenting out the setAsignee() method for now and try this:

users.shuffle()
log.warn(users.first())

Then you can run this a few times and check the logs if it works like intended. Then, if it does, go for:

users.shuffle()
issue.setAssignee(users.first())
Edison Augusto Machado Junior March 10, 2023

Hi Michael,

I changed the script as you suggest and I got the following error in log:

2023-03-10 08:49:33,131 ERROR [workflow.AbstractScriptWorkflowFunction]: Workflow script has failed on issue NA-94298 for user 'edison.machado'. View here: https://jira.na.novaagri.com.br/secure/admin/workflows/ViewWorkflowTransition.jspa?workflowMode=live&workflowName=Atualizacao+NA%3A+WF+Washout+v1&descriptorTab=postfunctions&workflowTransition=921&highlight=1 groovy.lang.MissingMethodException: No signature of method: java.util.ArrayList.shuffle() is applicable for argument types: () values: [] Possible solutions: size(), size(), sum()  at Script1313.run(Script1313.groovy:51)

 

Here is my script after edit:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.roles.ProjectRoleManager
import com.atlassian.jira.issue.customfields.view.CustomFieldParams
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.customfields.view.CustomFieldParams
import com.atlassian.jira.issue.customfields.impl.CascadingSelectCFType
import com.atlassian.jira.issue.CustomFieldManager;
import org.apache.log4j.Category

log.setLevel(org.apache.log4j.Level.DEBUG)
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def field = customFieldManager.getCustomFieldObject("customfield_10115")
Object cfVal = issue.getCustomFieldValue(field)

HashMap<String, Option> hashMapEntries = (HashMap<String, Option>) cfVal
if (hashMapEntries != null) {
Option parent = hashMapEntries.get(CascadingSelectCFType.PARENT_KEY)
Option child = hashMapEntries.get(CascadingSelectCFType.CHILD_KEY)
def first = parent.toString()
def second = child.toString()
//-----------------------------------------------------------------------------------------------------------
if (second == "ACM"){
// The role you want assignees to set from
final roleName = 'SAPN1 - ACM'

final reassignedIssues = true
def issueManager = ComponentAccessor.issueManager
def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager)

// Get all of the users associated with the specified project role
def projectRole = projectRoleManager.getProjectRole(roleName)

// Sort the users of the project role using the user key
def users = projectRoleManager.getProjectRoleActors(projectRole, issue.projectObject).applicationUsers.toSorted { it.key }

users.shuffle()
log.warn(users.first())

// log.debug("Usuários: " + users);


//issue.setAssignee(users.first())
}}
Michael Wohlgemuth
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.
March 10, 2023

Hm. Im not sure what happened there. Your project role isnt empty, is it?

I tried replicating the error and cannot. See my code and a screen of the expected output:

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.security.roles.ProjectRoleManager

def issueManager = ComponentAccessor.getComponent(IssueManager)
def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager)

// For testing purposes, i used an existing role on my local env
final roleName = "Administrators"
def projectRole = projectRoleManager.getProjectRole(roleName)

// this is not needed if the script is run in a worfklow postfunction since you have a issue object already
def issue = issueManager.getIssueByKeyIgnoreCase("BUS-7")

// this is copied 1to1 from your code
def users = projectRoleManager.getProjectRoleActors(projectRole, issue.projectObject).applicationUsers.toSorted { it.key }

// here you see the users in the role
log.warn("These users have been found in the role named ${roleName} for project ${issue.projectObject.getName()}:")
log.warn(users)

// shuffle the list around a bit
users.shuffle()

// see if it did get shaken
log.warn("Now lets see the list after shuffling:")
log.warn(users)
And thats how my logs look after running this code (the last line gets randomized each time i hit run):
getusersfromprojectrole.PNG

Suggest an answer

Log in or Sign up to answer