Groovy errors

Andrew Striletskyi February 19, 2018

I have a script. Main task of this script is to assign issues in Round Robin fashion . But this script not working and I can't understand why. Anyone can help?

 

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.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 groupsList = ComponentAccessor.getGroupManager() as GroupManager
def groupMembers = groupsList.getUsersInGroup("NetOps")

def userManager = ComponentAccessor.getUserManager() as UserManager
def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser.class)
def searchProvider = ComponentAccessor.getComponent(SearchProvider.class)
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 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
}


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)
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 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)
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, createIssueUpdateRequest())
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()
}
}

1 answer

0 votes
Alexey Matveev
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.
February 19, 2018

What are the errors?

Andrew Striletskyi February 19, 2018

For example in this line:

 issueManager.getIssueObject(results.issues.get(0).id)

I see an error that  getIssueObject not defined or type is not correct for this method

Alexey Matveev
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.
February 19, 2018

I guess, it is all static compilation. Moreover your code seems to me as broken. I am not sure it will work at all.

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.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 groupsList = ComponentAccessor.getGroupManager() as GroupManager
def groupMembers = groupsList.getUsersInGroup("NetOps")

def userManager = ComponentAccessor.getUserManager() as UserManager
def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser.class)
def searchProvider = ComponentAccessor.getComponent(SearchProvider.class)
def jqlQuery = "status != Closed and assignee=null ORDER BY created DESC"
def query = jqlQueryParser.parseQuery(jqlQuery)
def results = searchProvider.search(query,user,PagerFilter.getUnlimitedFilter())
Collection<ApplicationUser> agents = new ArrayList<ApplicationUser>()
PriorityQueue<Agent> agentPQ = new PriorityQueue<Agent>()


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

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
}


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)
def results = searchProvider.search(query, agent, PagerFilter.getUnlimitedFilter())
if(results.issues.size() == 0)
return 0;
else{
def changeLogManager = ComponentAccessor.changeHistoryManager
def assignedAt = changeLogManager.getChangeItemsForField(((Issue) 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 assignAgent(ApplicationUser admin,SearchResults results ,Collection<ApplicationUser> agents, JqlQueryParser jqlQueryParser, SearchProvider searchProvider, PriorityQueue<Agent> agentPQ)
{
def issueManager = ComponentAccessor.issueManager
MutableIssue mutableIssue = issueManager.getIssueObject(((Issue)results.issues.get(0)).id)
agents.forEach { agent ->
log.debug("agent = "+agent)
Agent a = new Agent()
def count = getIssueCountForAgent((ApplicationUser) agent, jqlQueryParser , searchProvider) as int

if(count > 0) {
a.lastAssignedTime = getLastAssignedTimeForAgent((ApplicationUser) agent, jqlQueryParser , searchProvider) as long
a.issueCount = count
}
else {
a.lastAssignedTime = 0
a.issueCount = 0
}
a.user = (ApplicationUser) agent
agentPQ.add(a)

}
mutableIssue.setAssignee(agentPQ.peek().user)
mutableIssue.store()
issueManager.updateIssue(admin, mutableIssue, createIssueUpdateRequest())
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()
}
}

 

There is a error only with createIssueUpdateRequest(). Because the method is not defined in your script

Andrew Striletskyi February 20, 2018

I fixed it , thank you) So, now the only problem is that the changes made by the script running as post-function is not reflecting in the assignee field.

Alexey Matveev
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.
February 20, 2018

Your script is too long. I would advise you to make it shorter and try to save a value in the assignee field. Make the short script work. After it you can fix your long script.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events