How to run JQL query inside Groovy script?

Georgiy Senenkov
Contributor
February 6, 2013

Hello,

I would need to read information from the issues which are the JQL query result.

Could you please advise how to do it by Groovy script?

Cheers, Georgiy

2 answers

1 accepted

8 votes
Answer accepted
Henning Tietgens
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.
February 6, 2013

If you want to execute a JQL expression try this:

import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.user.util.UserUtil
import com.atlassian.jira.web.bean.PagerFilter

jqlSearch = "project = JIRA"
SearchService searchService = ComponentAccessor.getComponent(SearchService.class)
UserUtil userUtil = ComponentAccessor.getUserUtil()
User user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
IssueManager issueManager = ComponentAccessor.getIssueManager()

if (!user) {
    user = userUtil.getUserObject('jira_bot')
}

List<Issue> issues = null

SearchService.ParseResult parseResult =  searchService.parseQuery(user, jqlSearch)
if (parseResult.isValid()) {
    def searchResult = searchService.search(user, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())
    // Transform issues from DocumentIssueImpl to the "pure" form IssueImpl (some methods don't work with DocumentIssueImps)
    issues = searchResult.issues.collect {issueManager.getIssueObject(it.id)}
} else {
    log.error("Invalid JQL: " + jqlSearch);
}

jira_bot is a user which is used, if no current user is found, e.g. if you execute this script as a service.

Henning

Georgiy Senenkov
Contributor
February 6, 2013

Thank you!

Cheers, Georgiy

JamieA
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.
February 6, 2013
// Transform issues from DocumentIssueImpl to the "pure" form IssueImpl (some methods don't work with DocumentIssueImps)
    issues = searchResult.issues.collect {issueManager.getIssueObject(it.id)}

I would not do the above unless you really need to. This can be slow when fetching several thousands of issues. All the methods ON documentIssueImpl should work, although you might not be able to use it as a parameter in another method. But you should just fetch the MutableIssues or whatever that you need, rather than converting them all.

Georgiy Senenkov
Contributor
February 7, 2013

Hello,

you mentioned that I can execute script as the service. Could you please advise me how to set it as service?

My aim is to run script daily, write script results into some file and publish to some network drive if it's possible.

Thank you.

Regards, Georgiy

Henning Tietgens
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.
February 7, 2013

Just save the script as a .groovy file and add a groovy service (https://jamieechlin.atlassian.net/wiki/display/GRV/Script+Runner#ScriptRunner-Services) wich refers to this file.

Henning

Like John likes this
0 votes
Nelson Jimenez Manio November 14, 2016

The above gave me an error below:

 

016-11-15 10:56:55,046 WARN [common.UserScriptEndpoint]: Script console script failed: groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.ComponentManager.getSearchRequestService() is applicable for argument types: () values: [] at Script15.run(Script15.groovy:17)

JamieA
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.
November 14, 2016

I don't see any reference to "ComponentManager" in the code above.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events