Search code examples
groovyjirajql

Groovy Scripted field to display result of JIRA JQL


I want to get some pointer to write a simple JIRA groovy scripted field – the input is a JQL and the result is the result of the JQL. For example, if the JQL is "project = RS and fixVersion = 5.0", it will go ahead a list the issues returned from this JQL in the custom field display.


Solution

  • First I created a JIRA field called "Fixed Issues JQL", which supposed I will enter the value of "project = VOL and fixVersion = 6.0" in the JIRA. Then I create a second JIRA custom field , a groovy scripted field called "Fixed Issues List", which contain the following code:

    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
    import com.atlassian.jira.ComponentManager
    import com.atlassian.jira.issue.customfields.manager.OptionsManager
    
    SearchService searchService = ComponentAccessor.getComponent(SearchService.class)
    UserUtil userUtil = ComponentAccessor.getUserUtil()
    User user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
    IssueManager issueManager = ComponentAccessor.getIssueManager()
    
    def componentManager = ComponentManager.instance
    def optionsManager = ComponentManager.getComponentInstanceOfType(OptionsManager.class)
    def customFieldManager = componentManager.getCustomFieldManager()
    def cf = customFieldManager.getCustomFieldObjectByName("Fixed Issues JQL")
    def myJQL = issue.getCustomFieldValue(cf)   // has a value such as "project = VOL and fixVersion = 6.0"
    
    if (!user) {
        user = userUtil.getUserObject('kwhite')
    }
    
    List<Issue> issues = null
    
    SearchService.ParseResult parseResult =  searchService.parseQuery(user, myJQL)
    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: " + myJQL);
    }