Executing

The code below demonstrates how to execute a JQL query, which may be useful in a validator or post-function etc. This code can also be tested from the Script Console.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter

def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchProvider = ComponentAccessor.getComponent(SearchProvider)
def issueManager = ComponentAccessor.getIssueManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getUser()

// edit this query to suit
def query = jqlQueryParser.parseQuery("project = JRA and assignee = currentUser()")

def results = searchProvider.search(query, user, PagerFilter.getUnlimitedFilter())

log.debug("Total issues: ${results.total}")

results.getIssues().each {documentIssue ->
    log.debug(documentIssue.key)

    // if you need a mutable issue you can do:
    def issue = issueManager.getIssueObject(documentIssue.id)

    // do something to the issue...
    log.debug(issue.summary)
}

Creating and Sharing

This code demonstrates how to create, save, and share a saved filter:

import com.atlassian.jira.bc.JiraServiceContextImpl
import com.atlassian.jira.bc.filter.SearchRequestService
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.search.SearchRequest
import com.atlassian.jira.sharing.SharePermissionImpl
import com.atlassian.jira.sharing.SharedEntity
import com.atlassian.jira.sharing.type.ShareType
import com.atlassian.sal.api.component.ComponentLocator

def searchRequestService = ComponentLocator.getComponent(SearchRequestService)
def user = ComponentAccessor.jiraAuthenticationContext?.getLoggedInUser()
def searchService = ComponentAccessor.getComponent(SearchService)

def serviceContext = new JiraServiceContextImpl(user)

def parseResult = searchService.parseQuery(user, "project = JRA")
if (parseResult.isValid()) {

    // create the search request
    def query = parseResult.query
    def searchRequest = new SearchRequest(query, user, "My filter", "Some description")

    // set shares
    def sharePerm = new SharePermissionImpl(null, ShareType.Name.GROUP, "jira-administrators", null)
    searchRequest.setPermissions(new SharedEntity.SharePermissions([sharePerm] as Set))

    // store the search request
    searchRequestService.createFilter(serviceContext, searchRequest)
}

For how-to questions please ask on Atlassian Answers where there is a very active community. Adaptavist staff are also likely to respond there.

Ask a question about ScriptRunner for JIRA, for for Bitbucket Server, or for Confluence.