Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Set assignee based on created time and timezone

-
Contributor
May 24, 2021

Hi all,

I have a post-function script that I worked on from a previous post that assigns users upon issue creation in round robin fashion (based on the most recently created issue that's of a certain issue type and has an assignee). The 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 com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.UpdateIssueRequest

import java.sql.Timestamp

// The role you want assignees to set from
final roleName = 'Utility'
final originalAssigneeCustomFieldName = "customfield_20511"

// 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: 1) Is a 'Defect' and 2) 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 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)

I had extensive help on this script and it works completely as expected, no problem with it at all...but there's something I want to add and I'm not sure how to do it.

I want this script to only take effect in between 4AM - 4PM UTC.

How would I go about adding that condition?

1 answer

1 accepted

2 votes
Answer accepted
PD Sheehan
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.
May 25, 2021

You could try with something like this

 

//your original imports here
import java.time.ZoneOffset
def utcHour = new Date().toInstant().atOffset( ZoneOffset.ofTotalSeconds(0)).hour
if(utcHour >=4 && utcHour < 16){
//the rest of your script here
}
Payne
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.
May 25, 2021

Or reverse the logic and return early, rather than having a lot of code in an if block, i.e.

if(utcHour < 4 || utcHour >=16)
{
    return
}

-
Contributor
May 25, 2021

@PD Sheehan 

Works as expected. Thanks once again!

 

And @Payne thanks for your suggestion too! Works just as well.

Suggest an answer

Log in or Sign up to answer