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

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

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
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published Thursday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

243 views 0 6
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