Iterate over all issues in an Epic

Ashley Kitson August 9, 2017

 

I have the following code, after much searching which will return a list of issues in an Epic .

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.fugue.Option
import com.atlassian.jira.issue.Issue

//Method to get the greenhopper bean instance
def getBean(String beanId)
{
def ghPlugin = ComponentAccessor.getPluginAccessor().getEnabledPlugin("com.pyxis.greenhopper.jira")
def descriptor = ghPlugin.getModuleDescriptor("greenhopper-launcher")
def applicationContext = descriptor.getModule().greenHopperCacheManager.applicationContext
def beanInstance = applicationContext.getBean(beanId)
return beanInstance;
}

//method to return list of issue keys in an Epic
def getEpicIssueKeys(Issue issue) {
def epic = (Option)Option.option(issue)
def epicLinkManagerImpl = getBean("epicLinkManagerImpl");
issuesInEpic = epicLinkManagerImpl.getIssuesInEpic(issue)
return issuesInEpic
}

if(issue.issueType.name=="Epic") {
return getEpicIssueKeys(issue)
}

return null

For an Epic issue, will deliver something like:

[PR-1173, PR-1174, PR-1175, PR-1191, PR-1190, PR-1193, PR-1192, PR-1195, 
PR-1194, PR-1197, PR-1196, PR-1177, PR-1199, PR-1176, PR-1198, PR-1179,
PR-1178, PR-1180, PR-1182, PR-1181, PR-1184, PR-1183, PR-1186, PR-1185,
PR-1188, PR-1187, PR-1201, PR-1189, PR-1200, INF-89, INF-90, PR-1227,
PR-1523, PR-1524]

Now I need to to be able to iterate over that list, retrieve the issue associated with the key and sum the value of a custom field, say 'issueValue' which is double type.  That's where I got lost.

Can anyone suggest/demonstrate how to do this?

3 answers

1 accepted

2 votes
Answer accepted
Joshua Yamdogo @ Adaptavist
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.
August 9, 2017

Hi Ashley,

I have another solution that seems to work. I created a scripted field named EpicSum. It looks to see if the issue is of type Epic. If it is, find all issues in that epic. Once it has those issues, it looks at the custom number field "NumField" and sums up the total value between all issues in the Epic.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter
enableCache = {-> false}
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser.class)
def searchProvider = ComponentAccessor.getComponent(SearchProvider.class)
def issueManager = ComponentAccessor.getIssueManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

if (issue.issueType.name == "Epic") {
// The search query
def query = jqlQueryParser.parseQuery("\"Epic Link\" in (${issue.key})")

def results = searchProvider.search(query, user, PagerFilter.getUnlimitedFilter())

Double sum = 0;

results.getIssues().each {documentIssue ->;
def issue = issueManager.getIssueObject(documentIssue.id)
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def customField = customFieldManager.getCustomFieldObjectByName("NumField")
def cfValue = issue.getCustomFieldValue(customField)?:0 as Double
sum += cfValue
}
return sum
}

 Result:

Screen Shot 2017-08-09 at 10.09.44 AM.png

0 votes
Ashley Kitson August 9, 2017

Hey Joshua

That looks almost just exactly what I want to do - I'll give it a try in the morning - many thanks

Ashley Kitson August 10, 2017

Hi Joshua

 

That nearly works.  I get

org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '0.00.0' with class 'java.lang.String' to class 'java.lang.Double'.  Helpfully no line number is given.

The custom field I am querying is itself a scripted custom field with a 'Number' template, returning a Double. Not sure if that is causing the problem.

Ashley Kitson August 10, 2017

if it helps others, the follwoing incantation worked to solve the casting issue

def cfRaw = issue.getCustomFieldValue(customField)?:0
Double cfValue = Double.valueOf(cfRaw)
sum += cfValue
0 votes
Aleksandr Zuevich
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.
August 9, 2017

Hi Ashley!

Now get CustomField object using CustomFieldManager and your custom field id (or type if you have only one field of this type https://polontech.com/blog/tip-of-the-week-how-to-get-a-customfield), organize a loop, get issues using IssueManager and for each issue get the value using customFieldObject.getValue(issue).

Ashley Kitson August 9, 2017

Hi Aleksandr

That's sort of where I got to, but can't figure out the incantation to get at the IssueManager and retrieve issues etc.  Can you provide an example please?

Aleksandr Zuevich
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.
August 9, 2017

ComponentAccessor.getIssueManager() to obtain the issue manager and issueManager.getIssueObject(yourIssueKey) to obtain the issue.

Suggest an answer

Log in or Sign up to answer