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?
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:
Hey Joshua
That looks almost just exactly what I want to do - I'll give it a try in the morning - many thanks
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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).
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
ComponentAccessor.getIssueManager() to obtain the issue manager and issueManager.getIssueObject(yourIssueKey) to obtain the issue.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.