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

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

ScriptRunner: Iterating through an issue filter to calculate number of issue created by person Edited

I was asked to provide stats for all users submitted issue in the last year.

My first thought was export issues to Excel which seemed to disappear.

So I created a script to run in the console to do the math which worked but only up to 100 maxResult

def query = 'project = "Project name" AND created > "-365d" ORDER BY created ASC'

def searchReq = get("/rest/api/2/search")
.queryString("jql", query)
.queryString("maxResults", "1000") //only returns 100!
.asObject(Map)

Map searchResult = searchReq.body

def issuesByUser = [:]
def user = ''
def count = 0
searchResult.issues.each { Map issue ->
user = issue.fields.reporter.displayName
created = issue.fields.created
count += 1
//logger.info("${count} ${user} ${created}")
issuesByUser.putIfAbsent(user, 0)
issuesByUser.find{it.key == user}.value += 1

}
logger.info("Total ${count} issue")

def total = 0
issuesByUser.each {i ->
logger.info("${i.key} ${i.value}")
total += i.value
}
logger.info("Total ${total} issue")

I could use some help with that! 

Thank you

2 answers

1 accepted

2 votes
Answer accepted

Hi Alexy,

When using the Search for issues API if you want to return more than 100 issues you will need to use pagination by having multiple search queries which the extra issues.

This means for the second query and onwards you would an extra queryString paramater of startAt as documented in the API documentation page above as this would return the next 100 issues if you started from 100 and then 200 etc.

I hope this helps.

Regards,

Kristian

Thanks.

Just did that and got "Execution timed out after 60s."

def query = 'project = "Project name" AND created > "-365d" ORDER BY created ASC'

def issuesByUser = [:]
def user = ''
def count = 0
def pageSize = 100
def issuesGot = 0
Map searchResult = [:]
def totalIssues = 0

while (true) {
def searchReq = get("/rest/api/2/search")
.queryString("jql", query)
.queryString("maxResults", issuesGot+pageSize <= totalIssues ? pageSize : totalIssues - issuesGot - 1)
.queryString("startAt", issuesGot+1 )
// .queryString("fields", "Flagged")
.asObject(Map)
issuesGot += pageSize

totalIssues = searchReq.body.total
logger.info("Total issues ${totalIssues}")
//logger.info(searchReq.inspect())

searchResult = searchReq.body


searchResult.issues.each { Map issue ->
//print (issue.fields.reporter.displayName)
user = issue.fields.reporter.displayName
created = issue.fields.created
count += 1
//logger.info("${count} ${user} ${created}")
issuesByUser.putIfAbsent(user, 0)
issuesByUser.find{it.key == user}.value += 1

}
logger.info("Total ${count} issue")
if (count >= totalIssues) break
}
def total = 0
issuesByUser.each {i ->
logger.info("${i.key} ${i.value}")
total += i.value
}
logger.info("Total ${total} issue")

Hi Alexy,

I can confirm that we impose a timeout limit on scripts of 60 seconds in order to prevent long-running code as described in the documentation page here.

This means you will need to split your logic up into multiple scripts if your script is exceeding the 60-second timeout limit.

However, I notice from your script you have the line of // .queryString("fields", "Flagged") commented out which means as you are not restricting the fields you need on each issue that you are returning every field on the issue which would cause the search API to take a long time to return.

I would advise uncommenting this line and only returning the fields you need on each issue and if you are just counting the issues then just to return the summary field as doing this will cause the API to return faster and may help with the timeout limits that you are getting.

Regards,

Kristian

Like Hana Kučerová likes this

Thanks Kristian!

 

The flagged part was from the script example provided 

Can you please share a snippet where only few relevant fields returned? 

Thanks a ton 

Hi Alexy,

To return only the relevant fields you would need to change that line to be the fields you require, an example is below which shows how to return the summary and a custom field by its ID.

In this query string, you would need to specify the names of all system fields and any custom fields you wish to return.

queryString("fields", "summary,customfield_12345")

An example of using this is in the documentation page here.

However, in your code, it looks like just get the reporter field off the issues so you could try returning just this field off of the issues.

I hope this helps.

Regards,

Kristian

Thank you Kristian!

That worked so much faster this way with only few select fields requested!

Hi @Alexey Paveliev ,

I will try to help you with the code, but please, did you try Pie chart gadget? It should give you the same results without groovy scripting...

Hana Kučerová Community Leader Jan 22, 2021

Oh, I mostly forget on Issue statistics gadget - it can be used, when you want only simple table :-)

Thanks!

there are around 1200 issues submitted by too many people to fit in a chart

See code below that times out at issues over 1200 but luckily works up to 1000 issues 

Hana Kučerová Community Leader Jan 22, 2021

Hi @Alexey Paveliev ,

did you also check the Issue statictics gadget? I think it is exactly what you need - list of users with number of created issue

  • Statistic Type: Reporter
  • Show Resolved Issue Statistics: Yes

I got results for ~ 20 000 issues and 300 users without any problem.

I wasn't able to find it in cloud documentation, but is looks the same in the server :-).

Please, don't get me wrong, I don't want to force you and I love ScriptRunner, but I always prefer OOB functionality and I use SR only if really necessary :-).

Thanks for the suggestion. I was not able to find the report in my cloud instance though

Hana Kučerová Community Leader Jan 23, 2021

@Alexey Paveliev 

Please see here. In general, you need to navigate to one of your dashboards, which you are able to edit, open dialog for adding gadget using button in the right top corner, click on Load all gadgets to get all available gadgets, search for Issue Statistics, add this gadget, configure it and save.

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
TAGS
Community showcase
Published in Jira

Announcing the waitlist for Jira Work Management

Hey there Cloud Community members! We’re excited to give you the first glimpse of the new home for business teams on Jira — Jira Work Management. Jira Work Management is the next generation of J...

127 views 2 7
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you