Get count of subtasks with a certain field value

Mihai Mihai September 13, 2019

Hello,

We have a script that counts the number of issues returned by a JQL:

....

def getSubtaskIssuesFromJQL(String subtaskjql) {
def adminUser = ComponentAccessor.getUserManager().getUserByKey("admin") 
def searchService = ComponentAccessor.getComponent(SearchService)
def issueManager = ComponentAccessor.getIssueManager()

SearchService.ParseResult parseResult = searchService.parseQuery(adminUser, subtaskjql)
if (parseResult.isValid()) {
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
String subtaskcomment = ""
def commentManager = ComponentAccessor.getCommentManager()
def searchResult = searchService.search(adminUser, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())
subtaskcomment = "This feature request has " + searchResult.total + " subtasks"
return subtaskcomment
}
}

We would like to extend this by counting how many issue resulted from the query have values in a certain select list field.

 

Example: SelectList field has values test, test1, test2, test3

 

The query returns 15 issues. (the select list is mandatory so all of them will have a value)

Output would be: "The feature has 15 issues. 3 test, 3 test1, 5 test2 and 4 test3"

 

Thank you  for any suggestions.

 

 

 

1 answer

1 accepted

1 vote
Answer accepted
Ilya Turov
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.
September 13, 2019

didn't really test it, but feel like it should be working:

def getSubtaskIssuesFromJQL(String subtaskjql) {
def adminUser = ComponentAccessor.getUserManager().getUserByKey("admin")
def searchService = ComponentAccessor.getComponent(SearchService)
def customFieldObject = ComponentAccessor.customFieldManager.getCustomFieldObjectsByName("your select field name")[0]
def values = ["test", "test1", "test2", "test3"]
SearchService.ParseResult parseResult = searchService.parseQuery(adminUser, subtaskjql)
if (parseResult.isValid()) {
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
String subtaskcomment = ""
def searchResult = searchService.search(adminUser, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())
def issues = searchResult.issues
def valuesComment = values.collect { value ->
def issueCount = issues.count {it.getCustomFieldValue(customFieldObject)?.value == value}
"$issueCount $value"
}
subtaskcomment = "This feature request has " + searchResult.total + " subtasks. " + valuesComment.join(",")
return subtaskcomment
}
}
Mihai Mihai September 13, 2019

Thank you very much, that works great!

Like Ravi Sagar _Sparxsys_ likes this

Suggest an answer

Log in or Sign up to answer