Round Robin Issue Assignment (ScriptRunner for JIRA Cloud)

Overview
Assign issues to the members of a project in a ‘round robin’ manner. This script automatically assigns the next user in the list to each newly created issue.

Good to Know

  • This script has been designed to work for ScriptRunner for Jira Cloud
  • Script Listener based on Issue Created event
  • Create a project property that will be used to keep track of the round robin assignments. In the script example a project property called “round-robin” was used which contained value lastIndex.
  • Update the users array with the appropriate account IDs
  • By no means an expert at coding so apologise in advance for non-optimal code

Script

// Retrieve current value of round-robin project property
def result = get("/rest/api/2/project/<projectkey>/properties/round-robin")asString().body
def parsedJson = new groovy.json.JsonSlurper().parseText(result)
def lastIndex = parsedJson.value.lastIndex

// Define account IDs of users to be assigned issues
def users = ['accountID1','accountID2']

// Define issue key variable
def issueKey = issue.key;

// Update issue with next assignee based on round-robin index and users array
def assignIssue = put("rest/api/2/issue/${issueKey}")
        .header('Content-Type', 'application/json')
        .queryString("notifyUsers":false)
        .body([
        fields:[
                assignee: [id:users[lastIndex]]
        ]
])
        .asString()
if (assignIssue.status == 204) {
    // Update the index for the next user assignment
    lastIndex = (lastIndex + 1) % users.size()
    
    // Update the round-robin project property value
    def updateroundrobin = put("/rest/api/2/project/<projectkey>/properties/round-robin")
        .header('Content-Type', 'application/json')
        .header('Accept', 'application/json')
        .body(["lastIndex": lastIndex])
        .asString()
        
    return 'Success'
} else {
    return "${assignIssue.status}: ${assignIssue.body}"
}