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?
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
}
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
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Works as expected. Thanks once again!
And @Payne thanks for your suggestion too! Works just as well.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.