Are you in the loop? Keep up with the latest by making sure you're subscribed to Community Announcements. Just click Watch and select Articles.

×
Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

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
AUG Leaders

Atlassian Community Events