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

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

All you wanted to know about customer support KPIs

When working in customer support, it’s crucial to calculate, analyze and monitor specific numbers, called KPIs (Key Performance Indicators). They go hand-in-hand with customer satisfaction, problem d...

132 views 1 5
Read article

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