How to run JQL query inside Groovy script?

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

7 votes
Henning Tietgens Community Champion Feb 06, 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

Thank you!

Cheers, Georgiy

// 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.

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 Community Champion Feb 07, 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

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)

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

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published Thursday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

348 views 0 6
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you