This is my scenario:
I need to do a JQL and get the number of results (done), if this number is grater than X number do not allow the creation of an issue.
This is my current (example) code:
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.search.SearchException
import com.atlassian.jira.web.bean.PagerFilter
final String jqlSearch = 'project = "Sample Project"' // the jql query you want to search with
def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def searchService = ComponentAccessor.getComponentOfType(SearchService)
SearchService.ParseResult parseResult = searchService.parseQuery(user, jqlSearch)
def total_issues = 0
if (parseResult.isValid()) {
try {
def results = searchService.search(user, parseResult.query, PagerFilter.unlimitedFilter)
def issues = results.issues
//log.warn(results)
issues.each {
//log.warn(it.key)
total_issues++
}
return total_issues
} catch (SearchException e) {
e.printStackTrace()
}
} else {
print("Invalid query")
return null
}
if (total_issues >= 23){
return false
}
Right now is not working, I put this code on the 'Create' transition.
Hi daniel,
Why dont use SearchService.searchCountOverrideSecurity(ApplicationUser searcher, Query query) method?
Which indeed returns the number of issues that matches the given query.
You code should looks like something like this (I've just type the code here so probably I missed something)
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter
import com.opensymphony.workflow.InvalidInputException
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchService = ComponentAccessor.getComponent(SearchService)
def user = ComponentAccessor.getJiraAuthenticationContext().getUser()
// edit this query to suit
def query = jqlQueryParser.parseQuery("project = JRA and assignee = currentUser()")
def count = searchService.searchCountOverrideSecurity(user, query)
if(count >= 23 ) {
throw new InvalidInputException("Too Many Issues")
}
Regards
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.