Pagination on Jira Cloud

Some Jira Cloud endpoint resources return paged results. This script is an example of how to handle these paged results.

import java.math.BigDecimal
import java.math.RoundingMode

// Specify what API to call
String url = "/rest/api/2/search"

Integer startAt = 0
Integer resultsPerPage = 50

// Make the API call
HttpResponse response = get(url)
        .queryString("maxResults", resultsPerPage)
        .queryString("startAt", startAt)
        .asObject(Map)

assert response.status == 200

Map result = response.body

Integer totalIssues = (Integer) result.total
// confirm pagination is working
assert resultsPerPage == result.maxResults

// Save the search results as a Map
List issues = result.issues as List

// calculate total pages we expect, minus the one we're currently on
Integer totalPages = (totalIssues / resultsPerPage ).setScale(0, RoundingMode.UP).intValueExact()
Integer currentPage = 1
logger.info("Expecting ${totalPages} pages")
logger.info("Got page ${currentPage}")

while (currentPage < totalPages) {
    startAt = resultsPerPage *  currentPage
    response = get(url)
        .queryString("maxResults", resultsPerPage)
        .queryString("startAt", startAt)
        .asObject(Map)
        
    assert response.status == 200
    
    result = response.body
    
    logger.info("Got page ${currentPage + 1}")
        
    issues.addAll(result.issues)

    currentPage += 1
}

logger.info("Got ${issues.size()} issues")

return issues
1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.