Skip to main content
Example scripts
arrow icon
To homepage
Jira
Data centre icon
Data Center

Round Robin Assign Issue to Users in a Certain Project Role

Created 1 year ago, Updated 2 month(s) ago
App in script
ScriptRunner For Jira
ScriptRunner For Jira
by Adaptavist
Compatibility
compatibility bullet
Jira (8.0 - 8.14)
compatibility bullet
ScriptRunner For Jira (6.18.0)
Language |
groovy
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.roles.ProjectRoleManager

// The role you want assignees to set from
final roleName = 'Project Role'

// If it is true, the assigned issues will be reassigned
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 }

// There are no users in the specific project role
if (!users) {
    log.info ("No users for project role $roleName")
    return
}

if (!reassignedIssues && issue.assignee) {
    log.info ('The issue is already assigned')
    return
}

// Find the latest created issue id that has an assignee
def lastIssueIdWithAssignee = issueManager.getIssueIdsForProject(issue.projectObject.id)
    .sort()
    .reverse()
    .find { issueManager.getIssueObject(it).assignee }

if (!lastIssueIdWithAssignee) {
    issue.setAssignee(users.first())
    return
}

def lastAssignee = issueManager.getIssueObject(lastIssueIdWithAssignee).assignee
def lastAssigneeIndex = users.indexOf(lastAssignee)
def nextAssignee = users[(lastAssigneeIndex + 1) % users.size()]

issue.setAssignee(nextAssignee)
Having an issue with this script?
Report it here