|
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 ParametersTo 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 SearchRequestSearchRequest 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 sourceYou 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. |
Labels
Except where otherwise noted, content in this space is licensed under a Creative Commons Attribution 2.5 Australia License.

Comments (4)
Jun 23, 2005
Tom Chiverton says:
You may want to also peek at JiraCreateSearch for a programers guide to working ...You may want to also peek at JiraCreateSearch for a programers guide to working with issues and their change logs returned from a search.
Apr 12, 2007
Ahmadin Badruddin says:
How do we do a custom field search in messagehandler within service? I coded as ...How do we do a custom field search in messagehandler within service? I coded as below, but unable to find any issue. If I use find issue screen with the exact string, I found the ticket.
User you = ((JiraAuthenticationContext) ComponentManager.getComponentInstanceOfType(JiraAuthenticationContext.class)).getUser();
SearchProvider sProvider = (SearchProvider) ComponentManager.getComponentInstanceOfType(SearchProvider.class);
SearchRequest sRequest = new SearchRequest(you);
CustomFieldManager cfm = ComponentManager.getInstance().getCustomFieldManager();
CustomField cf = cfm.getCustomFieldObject("customfield_10000");
IssueSearcher searcher = cf.getCustomFieldSearcher();
FieldValuesHolder fvh = new FieldValuesHolderImpl();
CustomFieldParams cfp = new CustomFieldParamsImpl();
List list = new ArrayList();
list.add("045632466");
cfp.put(null, list);
cfp.setCustomField(cf);
fvh.put("customfield_10000", cfp);
searcher.populateSearchRequest(sRequest, fvh);
List searched = null;
String orgSubject = subject;
try {
searched = sProvider.search(sRequest, you, PagerFilter.getUnlimitedFilter()).getIssues();
if (searched.size() > 0)
} catch (SearchException e)
Thanks.
Sep 11, 2008
Markku Halinen says:
Inside a messagehandler, User you = ((JiraAuthenticationContext) ComponentManag...Inside a messagehandler,
returns null. You need an actual User instance for the search to work. For example like this:
This works at least in JIRA 3.12
Cheers, Markku
Feb 14
Anonymous says:
Picking the first admin might work but not be the best solution...Picking the first admin might work but not be the best solution...
Add Comment