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

How to run multiple JQL queries in ScriptRunner with date variable Edited

Trying to automate quarterly reporting. This requires running multiple JQL queries in scriptrunner console that output results, then transfer results to external file. Wondering if anyone had an idea how to create date variables that could be put into queries that would have months. EX of code:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter
import java.sql.Date
import java.sql.Timestamp
import groovy.time.TimeCategory


// Search service
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchService = ComponentAccessor.getComponent(SearchService)
def issueManager = ComponentAccessor.getIssueManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

//def startDate = new Date(currentDateValue.find())
//def endDate = startDate.setMonth
//def format = "yyyy-MM-dd"
//def startDate = new Date()

// JQL queries
def query = jqlQueryParser.parseQuery("project in (OPX) AND issuetype = Incident AND created >= 2019-09-01")
def query1 = jqlQueryParser.parseQuery("project in (OPX) AND issuetype = Task")

// Searches
def search = searchService.search(user, query, PagerFilter.getUnlimitedFilter())
def search1 = searchService.search(user, query1, PagerFilter.getUnlimitedFilter())

// Logs
search.results.each { documentIssue ->
log.debug(documentIssue.key)
}

"Total issues INFRA: ${search.total} / Total issues CMS: ${search1.total}"

1 answer

1 accepted

0 votes
Answer accepted
Ivan Tovbin Community Leader Oct 24, 2019

Hi Eric,

Frankly I didn't quite get what exactly you are trying to achieve, however I'd start with optimizing your code a little bit:

import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.search.SearchResults
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.query.Query

ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
String date = "2019-09-01"
String jql = "project in (OPX) AND issuetype = Incident AND created >= ${date}"

List<MutableIssue> issues = getIssuesFromJQL(jql, user)

List<MutableIssue> getIssuesFromJQL(String jqlQuery, ApplicationUser user) {
IssueManager issueManager = ComponentAccessor.getIssueManager()
JqlQueryParser jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser.class)
SearchService searchService = ComponentAccessor.getComponent(SearchService.class)
Query query = jqlQueryParser.parseQuery(jqlQuery)
SearchResults searchResults = searchService.search(user, query, PagerFilter.getUnlimitedFilter())
List<MutableIssue> mutableIssueList = searchResults.getIssues().collect { issue -> issueManager.getIssueObject(issue.getId()) }
return mutableIssueList
}

The Script needs to run multiple queries and then append the issue totals to a file. Is this possible?

Ivan Tovbin Community Leader Oct 25, 2019

I suppose you could create a collection of queries and then iterate through it in a cycle and run a search using each element (query).

If you could help appending script output to a file that would be great. File directory in ScriptRunner console not working. ex: 

File file = new File("/Documents/script_output/sr_output.txt")

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted in Jira

Demo Den Ep. 7: New Jira Cloud Reports

Learn how to use two new reports for next-gen projects in Jira Cloud:  Cumulative flow diagram and Sprint burndown chart. Ivan Teong, Product Manager, Jira Software, demos the Cumulative ...

237 views 1 2
Join discussion

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