How to limit query output in Groovy Scripted Field and show warning

Areg Vrtanesyan _Work_ May 9, 2017

I am using Scripted field to show the details about the JQL query stored in another field of current issue.

Looks like when user put issuetype = bug instance simply crashed because of 217K bug issue types being loaded to memory.

Now I am trying to put reasonable limit on search request and if it exceeding some number show warning.

I assume that it could be achieved by using new PagerFilter(1) instead of PagerFilter.getUnlimitedFilter() as described in multiple replies on https://community.atlassian.com/t5/Answers-Developer-Questions/Scriptrunner-post-function-Search-for-highest-number-in-custom/qaq-p/520750

Now I am trying to understand if I will use new PagerFilter(100) how I can determine if there are more than 100 results to show warning that JQL is possibly wrong?

Something like:

def pf = new PagerFilter(100)

def query = jqlQueryParser.parseQuery(cfJQLQueryVal)
def results = searchProvider.search(query, user, pf)

if ( pf.getPages().size() > 1 ) {

error ..

} else {

results.getIssues().each {rissues ->

....

}

}

https://docs.atlassian.com/jira/7.2.6/com/atlassian/jira/web/bean/PagerFilter.html

I am going to test this but any help will be nice ...

1 answer

0 votes
Jonny Carter
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.
May 15, 2017

You should be able to get the number of pages from results.pages.

if (results.pages.size() > 1) {
    //....add your error here
}

Keep in mind the other caveats about JQL in scripted fields:

https://scriptrunner.adaptavist.com/latest/jira/scripted-fields.html#_jql_searches_in_script_fields

Another thought is that you might be able to automatically restrict the JQL a bit more (e.g. limit it to the issue's project, to open issues, or some other important context) by adding some clauses to the user input JQL. You may also want to run the query through the SearchService's validateQuery method:

import com.atlassian.jira.bc.issue.search.SearchService
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

// get all the used invoice numbers so far
def query = ComponentAccessor.getComponent(JqlQueryParser).parseQuery(queryText)
def pagerFilter = new PagerFilter(100)
def searchProvider = ComponentAccessor.getComponent(SearchProvider)
def searchService = ComponentAccessor.getComponent(SearchService)
def loggedInUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def messages = searchService.validateQuery(loggedInUser, query)
if (!messages.hasAnyErrors()) {
    def results = searchProvider.search(query, loggedInUser, pagerFilter)
//...rest of your code here }

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events