Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,294,111
Community Members
 
Community Events
165
Community Groups

Round-robin assigning

I have a "simple" script for Round Robin assigning. And I have one problem:  changes made by the script running as post-function is not reflecting in the assignee field. How to fix it?)

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.user.util.UserManager;
import com.atlassian.jira.security.groups.GroupManager;
import com.atlassian.jira.issue.search.SearchResults;
import com.atlassian.jira.issue.search.SearchProvider;
import com.atlassian.jira.jql.parser.JqlQueryParser;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.event.type.EventDispatchOption;
import com.atlassian.jira.bc.issue.IssueService.IssueResult;
import com.atlassian.jira.bc.issue.IssueService;
import com.atlassian.jira.issue.IssueInputParameters;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.web.bean.PagerFilter;
import java.sql.Timestamp;

class Agent {
ApplicationUser user;
int issueCount;
long lastAssignedTime;
}

def groups = ComponentAccessor.getGroupManager() as GroupManager
def agents = groups.getUsersInGroup("jira-software-users")



def userManager = ComponentAccessor.getUserManager() as UserManager
def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser.class)
def searchProvider = ComponentAccessor.getComponent(SearchProvider.class)




def PriorityQueue<Agent> agentPQ = new PriorityQueue<>(agents.size(), new Comparator<Agent>() {
@Override
int compare(Agent o1, Agent o2) {
if(o1.issueCount == o2.issueCount){
if(o2.lastAssignedTime == o1.lastAssignedTime){
return o1.user.name.compareTo(o2.user.name)
}else{
return o1.lastAssignedTime.compareTo(o2.lastAssignedTime)
}
}else{
return o1.issueCount - o2.issueCount
}
}
})

def jqlQuery = "status != Closed and assignee=null ORDER BY created DESC"
def query = jqlQueryParser.parseQuery(jqlQuery)
def results = searchProvider.search(query, user, PagerFilter.getUnlimitedFilter())

assignAgent(user,results, agents, jqlQueryParser, searchProvider,agentPQ)


def assignAgent(ApplicationUser admin,SearchResults results ,Collection<ApplicationUser> agents, JqlQueryParser jqlQueryParser, SearchProvider searchProvider, PriorityQueue<Agent> agentPQ){
def issueManager = ComponentAccessor.issueManager
MutableIssue mutableIssue = issueManager.getIssueObject(results.issues.get(0).id)
agents.forEach{agent ->
log.debug("agent = " + agent)
Agent a = new Agent()

def count = getIssueCountForAgent(agent, jqlQueryParser , searchProvider)

if(count > 0){
a.lastAssignedTime = getLastAssignedTimeForAgent(agent, jqlQueryParser , searchProvider)
log.debug("Last assigned time for agent " + agent + "= " + a.lastAssignedTime)
a.issueCount = count

}else{
a.lastAssignedTime = 0
a.issueCount = 0
}
a.user = agent
agentPQ.add(a)
}
mutableIssue.setAssignee(agentPQ.peek().user)
mutableIssue.store()
issueManager.updateIssue(admin, mutableIssue, EventDispatchOption.ISSUE_ASSIGNED, true)
def issueService = ComponentAccessor.getIssueService()
IssueInputParameters params = ComponentAccessor.issueService.newIssueInputParameters()
params.assigneeId = agentPQ.peek().user.id
def validateUpdateResult = issueService.validateUpdate(admin,mutableIssue.id,params)
if(!validateUpdateResult.errorCollection.hasAnyErrors()){
log.debug("Assignee validated = " + validateUpdateResult.fieldValuesHolder.get("assigneeId"))
IssueService.IssueResult serviceResult=issueService.update(admin,validateUpdateResult)
log.debug(serviceResult)
}
else{
log.debug validateUpdateResult.errorCollection.errorMessages.toString()
}
}

def getLastAssignedTimeForAgent(ApplicationUser agent, JqlQueryParser jqlQueryParser, SearchProvider searchProvider ){
def jqlQuery = "assignee=\""+ agent.name + "\" and status!=Closed "+ " ORDER BY created DESC"
def query = jqlQueryParser.parseQuery(jqlQuery)
log.debug("JQL Query = " + jqlQuery)
def results = searchProvider.search(query, agent, PagerFilter.getUnlimitedFilter())
if(results.issues.size() == 0)
return 0;
else{
def changeLogManager = ComponentAccessor.changeHistoryManager
def assignedAt = changeLogManager.getChangeItemsForField(results.issues.get(0),"assignee").find {
it.toString == agent.name
}?.getCreated() as Timestamp
if(assignedAt){
Date d = new Date(assignedAt.getTime())
return d.getTime()
}else{
return 0
}
}
}


def getIssueCountForAgent(ApplicationUser agent ,JqlQueryParser jqlQueryParser, SearchProvider searchProvider ){
def jqlQuery = "assignee=\""+ agent.name + "\" and status!=Closed "+ " ORDER BY created DESC"
def query = jqlQueryParser.parseQuery(jqlQuery)
def results = searchProvider.search(query, agent, PagerFilter.getUnlimitedFilter())
return results.total
}

0 answers

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Posted in Jira Service Management

Jira Service Management Documentation Opportunities

Hello everyone, Hope everyone is safe! A few months ago we posted an article sharing all the new articles and documentation that we, the AMER Jira Service Management team created. As mentioned ...

253 views 0 6
Join discussion

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