This page last changed on Jun 18, 2009 by rosie@atlassian.com.

When developing plugins it is often desired to find issues that meet a certain criteria. Here is a code sample that constructs a SearchRequest and loops through all unresolved issues in project with id of 10000 and prints out their summary:

public class MyPlugin extends ...
{
    private final SearchProvider searchProvider;
    private final JiraAuthenticationContext authenticationContext;

    public MyPlugin(SearchProvider searchProvider, JiraAuthenticationContext authenticationContext)
    {
        this.searchProvider = searchProvider;
        this.authenticationContext = authenticationContext;
    }

    public void findIssues()
    {
        try
        {
            SearchRequest sr = new SearchRequest(authenticationContext.getUser());
            sr.addParameter(new ProjectParameter(new Long(10000)));
            sr.addParameter(new ResolutionParameter("-1"));
            SearchResults searchResults = searchProvider.search(sr, authenticationContext.getUser(), PagerFilter.getUnlimitedFilter());
            List issues = searchResults.getIssues();
            for (Iterator iterator = issues.iterator(); iterator.hasNext();)
            {
                Issue issue = (Issue) iterator.next();
                System.out.println("issue = " + issue.getSummary());
            }
        }
        catch (SearchException e)
        {
            e.printStackTrace();
        }
    }
}

The issues returned in the SearchResults are Issue objects and you can check out the linked javadocs for information on available methods.

Please note that the class declares its dependencies in the constructor (SearchProvider and JiraAuthenticationContext). When MyPlugin is instantiated it will be given the dependencies by JIRA. The process is explained in more detail here.

Note, that using a SearchRequest it is possible to construct the same type of queries as using the Issue Navigator. It is possible to add as many parameters to the search request as required. The summary of available parameters can be found in JIRA's API documentation.

Examples of using other Search Parameters

To search for all unresolved issues in a project with id 10000 that are assigned to the user with username "admin", create the search request as follows:

SearchRequest sr = new SearchRequest(getRemoteUser());
sr.addParameter(new ProjectParameter(new Long(10000)));
sr.addParameter(new ResolutionParameter("-1"));
sr.addParameter(new UserParameter(DocumentConstants.ISSUE_ASSIGNEE, "admin"));

Examples of populating CustomFieldParams into SearchRequest

SearchRequest sRequest = new SearchRequest(you);

CustomFieldManager cfm = ComponentManager.getInstance().getCustomFieldManager();
CustomField cf = cfm.getCustomFieldObject("customfield_10021");
IssueSearcher searcher = cf.getCustomFieldSearcher();
FieldValuesHolder fvh = new FieldValuesHolderImpl();
CustomFieldParams cfp = new CustomFieldParamsImpl();
List list = new ArrayList();
list.add("hello");
cfp.put(null, list);
cfp.setCustomField(cf);
fvh.put("customfield_10021", cfp);
searcher.populateSearchRequest(sRequest, fvh);

Figuring out what Search Parameters you need from the JIRA source

You can get a better understanding of how searching works in JIRA by looking at the source code. Each SearchableField in JIRA has associated searchers. The IssueSearchers are responsible for all of the field's searching functionality like taking the search criteria from the user and adding it to the SearchRequest.

For examples of which parameters a particular searcher uses look at the populateSearchRequest method of the searcher. For example look at the class FixForVersionsSearcher for more information on what SearchParameters you should be for fix for versions (you should use FixForParameter).

For custom fields, you choose an associated searcher on creation. You can find which searchers are available for a given CustomFieldType in the config file system-customfieldtypes-plugin.xml where all the system custom field types and custom field searchers are specified.

For example, we can see that the searcher for multicheckboxes is the checkboxsearcher (This uses MultiSelectSearcher). Examining the class MultiSelectSearcher, we can see that it returns the search parameter:

new GenericMultiValueParameter(field.getId(), cleanedValues)

This is the thing you need to add to your search request. "cleanedValues" is a List of values you want to search on.

Document generated by Confluence on Oct 06, 2009 00:31