Iterate over all issues in an Epic

 

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

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 vote
Aleksandr Zuevich Community Champion Aug 09, 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).

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 Community Champion Aug 09, 2017

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

Hey Joshua

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

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.

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

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published 50m ago in Jira Software

How large do you think Jira Software can grow?

Hi Atlassian Community! My name is Shana, and I’m on the Jira Software team. One of the many reasons this Community exists is to connect you to others on similar product journeys or with comparabl...

40 views 3 3
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you