Just a heads up: On March 24, 2025, starting at 4:30pm CDT / 19:30 UTC, the site will be undergoing scheduled maintenance for a few hours. During this time, the site might be unavailable for a short while. Thanks for your patience.

×
Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

JQL search in Scriptrunner - limited results.

Damian Wodzinski
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.
October 26, 2021

Hi there,

Currently I am strugling with one thing in scriptRunner cloud jira, I am working on report, we want to check, how many components are covered by our tests.

The whole script is almost ready, but I can't get all issues with components field. Currently we have above 300 tests, but /rest/api/3/search limits results to only 100 items. How to change that?

def searchReq = get('/rest/api/3/search')
.queryString('jql', query)
.queryString('fields', 'components')
.asObject(Map)


assert searchReq.status == 200
Map searchResult = searchReq.body
int count = 0;
searchResult.issues.each { Map issue ->
count++
def components = issue.fields.components.name
}

println "${searchResult.total} iterations: ${count}"

So after running this simple script, I get 332 searchResult size but the loop goes only 50 times. Trying to change maxResults parameter allow me to increase iteration number up to 100 only.

2 answers

1 accepted

3 votes
Answer accepted
Damian Wodzinski
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.
October 26, 2021 edited

Resolved it. So if you are new in this whole REST API like me, there is solution:

def searchReq = get('/rest/api/3/search')
.queryString('jql', query)
.queryString('fields', 'components')
.asObject(Map)

assert searchReq.status == 200

int searchResultTotal = (int) searchReq.body.total
int loopRunsCounter = searchResultTotal.intdiv(50)

int count = 0;
int componentsCount = 0
int startAt = 0

for (int i = 0; i <= loopRunsCounter; i++){
searchReq = get('/rest/api/3/search')
.queryString('jql', query)
.queryString('fields', 'components')
.queryString('startAt', startAt)
.asObject(Map)

Map searchResult = searchReq.body
startAt += 50

searchResult.issues.each { Map issue ->
count++
def components = issue.fields.components.name
for (Iterator<org.json.JSONObject> a = automatedComponents.iterator(); a.hasNext(); ){
def arr = a.next()
components.each{
if(arr.getString("name").equals(it)){
componentsCount++
a.remove()
}
}
}
}
}

So basically what you need to do, is:
a) determine how many pages of data will you get
b) loop through data, using "startAt" parameter, default maxResults is set to 50.

Unfortunatelly there is no method, to determine how many results will you get, that's why I am using search REST two times.

0 votes
Tomas Arguinzones Yahoo
Contributor
January 12, 2023

@Damian Wodzinski  

Thank you...your solution works great!

Suggest an answer

Log in or Sign up to answer