Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Pagination control in ScriptRunner

yevheniih January 13, 2025

Hey everyone! Hope I can get some help here.
So I run this pretty simple script for my custom field.

 

def commentsList = issue.fields?.comment?.comments
def count = 0
def textToCheck = "wix.wixanswers.com"
if (commentsList) {
for (comment in commentsList) {
if (comment.body.contains(textToCheck)) {
count += 1
}
}
return count
} else {
return 0
}
The issue here is that jira limit me to 100 max result. I am pretty sure it's due to pagination.

My question is, what should I add to the script to make it go through all comments in the issue and not just stop on page 1 of comments.
Any help would be much appreciated.
Thanks!
Screenshot 2025-01-13 at 11.08.46.png

2 answers

2 accepted

0 votes
Answer accepted
Marcelo Ulloa
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.
January 13, 2025

Hello,

I leave you the script as it runs constantly, it only stops due to the maximum ScriptRunner time or you will not find anything else

 

def commentsList = []
def startAt = 0
def maxResults = 100
def textToCheck = "wix.wixanswers.com"
def count = 0
while (true) {
    def response = get("/rest/api/3/issue/${issue.key}/comment?startAt=${startAt}&maxResults=${maxResults}")
        .header('Content-Type', 'application/json')
        .asObject(Map)
    if (response.status != 200) {
        logger.error("Error al obtener los comentarios: ${response.status}")
        break
    }
    def comments = response.body?.comments ?: []

    commentsList.addAll(comments)

    if (comments.size() < maxResults) {
        break
    }
    startAt += maxResults
}
commentsList.each { comment ->
    if (comment.body.contains(textToCheck)) {
        count++
    }
}

return count

 

yevheniih January 13, 2025

Thanks! I pretty much ended up with the same solution))
Thanks for your input!
Problem has been solved!

0 votes
Answer accepted
Charlie Misonne
Community Champion
January 13, 2025

I don't think that 100 limit applies here.

You are only counting items containing "wix.wixanswers.com" --> 95

But did you already check how many items are in commentsList?

You can use:

commentsList.size()

 

yevheniih January 13, 2025

Hey Charlie! Thanks for your reply.
I confirmed that the value of 100 is max number of comments jira shows per page.
I actually used commentsList.size() and break to cycle through pages. 

def textToCheck = "wix.wixanswers.com"
def count = 0
def startAt = 0
def maxResults = 100

// This will store all comments from the issue
def allComments = []

// Fetch comments in a paginated way
while (true) {
// Retrieve comments with pagination
def response = get("/rest/api/2/issue/${issue.key}/comment")
.queryString("startAt", startAt)
.queryString("maxResults", maxResults)
.asObject(Map)

def commentsList = response.body?.comments
if (commentsList) {
allComments.addAll(commentsList)

// If the number of comments retrieved is less than maxResults, we are done
if (commentsList.size() < maxResults) {
break
} else {
// Move to the next page
startAt += maxResults
}
} else {
break
}
}

// Now, allComments contains all comments, let's check for the text
allComments.each { comment ->
if (comment.body.contains(textToCheck)) {
count += 1
}
}

return count

 

This one worked perfectly.

Thanks!

Like Charlie Misonne likes this
Charlie Misonne
Community Champion
January 14, 2025

Ok, glad you found a solution!

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
PREMIUM
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events