Prevent post-function script from running if assignee is manually set

- June 4, 2021

I have a post function script that auto-assigns users in the 'Operations' project role in  round robin fashion. It does so if ran in between the hours of 4AM-4PM UTC, taking the most recently created 'Defect' issue that has an assignee. The script will look back at this issue and look at the value in a custom user field [customfield_20321] called 'Original Assignee' (which was made to retain the user in the round robin queue that was first assigned...in case the assignee was changed to someone else). The next user in queue (at the next index after 'Original Assignee') is assigned to the issue, their user id is stored in 'Original  Assignee', and basically that's the cycle it keeps going in.

Script is as follows:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.security.roles.ProjectRoleManager

import java.time.ZoneOffset

def utcHour = new Date().toInstant().atOffset( ZoneOffset.ofTotalSeconds(0)).hour
if(utcHour >= 4 && utcHour < 16) {

// The role you want assignees to set from
final roleName = 'Operations'
final originalAssigneeCustomFieldName = "customfield_20321"

// If it is true, the assigned issues will be reassigned
final reassignedIssues = true

def issueManager = ComponentAccessor.issueManager
def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager)
def customFieldManager = ComponentAccessor.getCustomFieldManager()

def originalAssigneeCustomField = customFieldManager.getCustomFieldObject(originalAssigneeCustomFieldName)

// 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 {
def foundIssue = issueManager.getIssueObject(it)
return foundIssue.assignee && foundIssue.issueType.name == "Defect"
}

// If no issue fulfilling above criteria is found, new cert issue is assigned to the first user in rr queue
if (!lastIssueIdWithAssignee) {
issue.setAssignee(users.first())
def changeHolder = new DefaultIssueChangeHolder()
originalAssigneeCustomField.updateValue(null, issue, new ModifiedValue(null, users.first()), changeHolder)
return
}

// If issue is found, then assignee is based on the Original Assignee and Assignee fields:
// * assuming Original Assignee field is not empty, user in said field is taken, and issue assigns to
//the next user in queue
def lastIssue = issueManager.getIssueObject(lastIssueIdWithAssignee)

def lastAssignee = lastIssue.getCustomFieldValue(originalAssigneeCustomField) ?: lastIssue.assignee
def lastAssigneeIndex = users.indexOf(lastAssignee)
def nextAssignee = users[(lastAssigneeIndex + 1) % users.size()]

issue.setAssignee(nextAssignee)
def changeHolder = new DefaultIssueChangeHolder()
originalAssigneeCustomField.updateValue(null, issue, new ModifiedValue(null, nextAssignee), changeHolder)

}

This script works fine; I had a lot of extensive help on this countless time from the community. New requirement, though...

I want to give the user the ability to manually assign upon creation. If they do so, then this script shouldn't run at all.

I figured this condition  already covered that:

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

But when I assign the issue before creating, the script will bypass what I did, auto-assigning the first user in the round robin queue, and storing that user as the Original Assignee...which is not what I want.

1 answer

1 accepted

0 votes
Answer accepted
Renee Lyons June 7, 2021

Have you tried enclosing everything after your condition

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

in an else { } statement?

- June 7, 2021

@Renee Lyons 

I was actually about to update this post with that in mind and it worked! Thanks!

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.security.roles.ProjectRoleManager

import java.time.ZoneOffset

def issueManager = ComponentAccessor.issueManager

if (!!issue.assignee){
  return
} else {
  def utcHour = new Date().toInstant().atOffset( ZoneOffset.ofTotalSeconds(0)).hour
  if (utcHour >= 16 || utcHour < 4) {
    // The role you want assignees to set from
    final roleName = 'Utility'
    final originalAssigneeCustomFieldName = "customfield_20321"

    // If it is true, the assigned issues will be reassigned
    final reassignedIssues = true

    def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager)
    def customFieldManager = ComponentAccessor.getCustomFieldManager()

    def originalAssigneeCustomField = customFieldManager.getCustomFieldObject(originalAssigneeCustomFieldName)

    // 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 {
  def foundIssue = issueManager.getIssueObject(it)
  return foundIssue.assignee && foundIssue.issueType.name == "Defect"
  }

    // If no issue fulfilling above criteria is found, new cert issue is assigned to the first user in rr queue
    if (!lastIssueIdWithAssignee) {
      issue.setAssignee(users.first())
      def changeHolder = new DefaultIssueChangeHolder()
originalAssigneeCustomField.updateValue(null, issue, new ModifiedValue(null, users.first()), changeHolder)
      return
    }

// If issue is found, then assignee is based on the Original Assignee and Assignee fields:
// * assuming Original Assignee field is not empty, user in said field is taken, and issue assigns to
//the next user in queue
    def lastIssue = issueManager.getIssueObject(lastIssueIdWithAssignee)

    def lastAssignee = lastIssue.getCustomFieldValue(originalAssigneeCustomField) ?: lastIssue.assignee
    def lastAssigneeIndex = users.indexOf(lastAssignee)
    def nextAssignee = users[(lastAssigneeIndex + 1) % users.size()]

    issue.setAssignee(nextAssignee)
    def changeHolder = new DefaultIssueChangeHolder()
    originalAssigneeCustomField.updateValue(null, issue, new ModifiedValue(null, nextAssignee), changeHolder)
    }
  }

Suggest an answer

Log in or Sign up to answer