ScriptRunner Jira Cloud : Assign users to tickets but keep the workload even

Marcelo Ulloa
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.
November 23, 2023

I have a script in ScriptRunner which works for me but assigns users randomly

import org.slf4j.LoggerFactory
import java.time.DayOfWeek
import java.time.LocalDate
import java.util.Random

def logger = LoggerFactory.getLogger(this.class)

def today = LocalDate.now().dayOfWeek

def assigneeAccountIds = getAssigneeAccountIdsForDay(today)

if (assigneeAccountIds) {
def randomIndex = (new Random() as Random).nextInt((assigneeAccountIds as List).size())
def assigneeAccountId = (assigneeAccountIds as List).get(randomIndex)

def updateResponse = put("/rest/api/3/issue/${issue.key}")
.header("Content-Type", "application/json")
.body([
fields: [
assignee: [
accountId: assigneeAccountId
]
]
])
.asString()

logger.info("Ticket ${issue.key} asignado al usuario con accountId: ${assigneeAccountId}")
} else {
logger.warn("No hay una tarea especificada para el dia: ${today}")
}

def getAssigneeAccountIdsForDay(DayOfWeek dayOfWeek) {
switch (dayOfWeek) {
case DayOfWeek.MONDAY:
return ["ID1","ID2","ID3"]
case DayOfWeek.TUESDAY:
return ["ID1","ID2","ID3"]
case DayOfWeek.WEDNESDAY:
return ["ID1","ID2","ID3"]
case DayOfWeek.THURSDAY:
return ["ID1","ID2","ID3"]
case DayOfWeek.FRIDAY:
return ["ID1","ID2","ID3"]
case DayOfWeek.SATURDAY:
return ["ID1","ID2","ID3"]
case DayOfWeek.SUNDAY:
return ["ID1","ID2","ID3"]
default:
return []
}
}

But I need to assign the users uniformly, currently in a random way there are users who have 10 tickers and another 2 tickets

I made this change, but instead of splitting the assignment it just assigns me to the same user

Do they know of a way to assign users evenly so that one user does not have 10 tickets and another 2?

import org.slf4j.LoggerFactory
import java.time.DayOfWeek
import java.time.LocalDate

def logger = LoggerFactory.getLogger(this.class)

def today = LocalDate.now().dayOfWeek


def counter = 0

def assigneeAccountIds = getAssigneeAccountIdsForDay(today)

if (assigneeAccountIds) {

def nextIndex = (counter++ % (assigneeAccountIds as List).size())
def assigneeAccountId = (assigneeAccountIds as List)[nextIndex]

def updateResponse = put("/rest/api/3/issue/${issue.key}")
.header("Content-Type", "application/json")
.body([
fields: [
assignee: [
accountId: assigneeAccountId
]
]
])
.asString()

logger.info("Ticket ${issue.key} asignado al usuario con accountId: ${assigneeAccountId}")
} else {
logger.warn("No hay una tarea especificada para el día: ${today}")
}

def getAssigneeAccountIdsForDay(DayOfWeek dayOfWeek) {
switch (dayOfWeek) {
case DayOfWeek.MONDAY:
return ["ID1","ID2","ID3"]
case DayOfWeek.TUESDAY:
return ["ID1","ID2","ID3"]
case DayOfWeek.WEDNESDAY:
return ["ID1","ID2","ID3"]
case DayOfWeek.THURSDAY:
return ["ID1","ID2","ID3"]
case DayOfWeek.FRIDAY:
return ["ID1","ID2","ID3"]
case DayOfWeek.SATURDAY:
return ["ID1","ID2","ID3"]
case DayOfWeek.SUNDAY:
return ["ID1","ID2","ID3"]
default:
return []
}
}

I use this in "Script Listeners" with the Issue Create event

1 answer

1 accepted

1 vote
Answer accepted
Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 24, 2023

Hi Marcelo,

To be able to assign user issues in an even manner, you would need to make sure you assign the issue to the next element in the array of user IDs each time. 

One way to do this would be to use a project property inside of Jira cloud, as you could then update this each time an issue is assigned with the element number from the array for the user it was assigned to.

This means each time the script is run, it could start off by getting this value out and incrementing it by 1 to always get the correct value.

I can confirm I have some example code here which can be run on the script console which shows how to get and set a project property, and you can use it as a guide to help with this requirement. 

Note the first time the project property is updated it will be created, so you can run just this code to create it before you run your script with this logic in.

I hope this information helps.

Regards,

Kristian

Marcelo Ulloa
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.
November 27, 2023

Could you share the example with me, since I do not have the permissions on the link. Regards

Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 27, 2023

Hi Marcello,

Sorry for the permissions issue.

You should now be able to see the code publically on the link at https://bitbucket.org/Adaptavist/workspace/snippets/7qXyxX

Regards,

Kristian

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
PREMIUM
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events