Hello, I have a script that runs when a JIRA button is pressed. I wanted to use threads to optimize the script but it gives me problems if I put in the threads the JQLs queries and loops, is there any problem with that? What alternative do we have to use threads?
This is tagged as Jira Cloud but looks like Data Center code, when using threads in Jira you first need to set up some thread locals etc, this can be done with JiraThreadLocalUtils
You’ll also want to set a logged in user to execute the query as, as there won’t be any logged in user by default in a new thread.
Wrapping your code like this should work:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.util.thread.JiraThreadLocalUtils
new Thread(JiraThreadLocalUtils.wrap {
ComponentAccessor.jiraAuthenticationContext.setLoggedInUser(
ComponentAccessor.userManager.getUserByName('username')
)
// Run your JQL here
ComponentAccessor.jiraAuthenticationContext.clearLoggedInUser()
}).start()
If you’re running ScriptRunner 7.11 or above, you could make use of HAPI to simplify, e.g:
Michael, the following seems to work for me. Note I’ve changed some of the strings so I can test more easily.
incompleteInSprint and similar ones related to sprints are special cases, as they have a different implementation depending on whether there is an “http context” or not, however, like I said, it seems to work for me.
One way to work with threads and loops in Jira scripts is to use filters. These can help you narrow down what you’re looking for without getting too bogged down in the details. You can create a custom filter to display only the issues that match specific criteria, like open or assigned to a particular user. If you’re looping through matters, just be mindful of performance—if you’re dealing with a large set, it can slow down the process. I’ve found that breaking up the loop into smaller parts and using the filter more strategically helps. It can be pretty handy when you’re automating workflows! quick filter can make all the difference in managing large projects.