Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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

Have you tried enclosing everything after your condition

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

in an else { } statement?

@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
TAGS

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you