Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Executing a Filter within a Jira Plugin

PaulC July 12, 2013
Hi, I have been trying to get the results of a Jira filter search into my plugin, the best I can do is turn it into a JQL statement like this: Filter = "Filter Name" then I run it throughthe JQL through the SearchService parseQuery method. This works but it feels like a bad hack. I have looked through the documents and JavaDocs but can't find the solution. Is there a better way? Thanks Paul

4 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

1 vote
Answer accepted
PaulC July 15, 2013
The first post from Andy got me on the way in the end. I created a method to create q Query object from a filter name: private Query getQuery(String filter) { JqlClauseBuilder subjectBuilder = JqlQueryBuilder.newClauseBuilder().savedFilter(filter); return subjectBuilder.buildQuery(); } This does the trick
1 vote
Wouter Hermeling October 16, 2014

The answer is in 

public SearchRequest retrieveOrMakeSearchRequest(final String projectOrFilterId, final Map<String, Object> params)
    {
        SearchRequest sr = null;
        final User user = authenticationContext.getLoggedInUser();
        if (projectOrFilterId.startsWith("filter-"))
        {
            Long filterId = new Long(projectOrFilterId.substring(7));
            sr = searchRequestService.getFilter(
                    new JiraServiceContextImpl(user, new SimpleErrorCollection()), filterId);
            if (sr != null)
            {
                params.put("searchRequest", sr);
            }
        }
        else if (projectOrFilterId.startsWith("project-"))
        {
            Long projectId = new Long(projectOrFilterId.substring(8));
            final Project project = projectManager.getProjectObj(projectId);
            if (project != null)
            {
                sr = makeProjectSearchRequest(project.getKey());
                params.put("project", project);
            }
        }
        else if(projectOrFilterId.startsWith("jql-"))
        {
            final String jql = projectOrFilterId.substring(4);
            sr = new SearchRequest();
            if (StringUtils.isNotBlank(jql))
            {
                final SearchService.ParseResult parseResult = searchService.parseQuery(user, jql);
                if (parseResult.isValid())
                {
                    sr = new SearchRequest(parseResult.getQuery());
                }
                else
                {
                    throw new IllegalArgumentException("Invalid JQL query specified for chart '" + jql + "'.");
                }
            }
            params.put("searchRequest", sr);
        }
        return sr;
    }
1 vote
Andy Brook [Plugin People]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 13, 2013

If all you have is a name, I 'think' you can locate the underlying query with:

List<SearchRequest> sr=ComponentManager.getInstance().getSearchRequestManager().findByNameIgnoreCase("name");

Query q=sr.iterator().next().getQuery()

Then inject that into the above?

abdul hafiz April 8, 2016

Thanks, I was looking for extracting jql from a filter. It worked for me.

0 votes
Andy Brook [Plugin People]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 12, 2013

Hi Paul,

Yo have two choices, either parse a JQL query, or build your own programatically. If you've got a JQL query that you are confident doesnt need to change, parsing the query is fine, saves you doing the timeconsuming conversion to programatically building the same thing in Java:

JIRA Source code is the thing you should look through, use of grep for something like JqlClauseBuilder should pay off. As ane example you codify a query something like:

JqlClauseBuilder subjectBuilder = JqlQueryBuilder.newClauseBuilder().defaultAnd();

JqlClauseBuilder clause = builder.project("ABC").status().notIn(new String[] {"resolved"});

Query q = clause.buildQuery();
SearchResults searchResults = ComponentAccessor.getComponent(SearchService.class).search(user, q,PagerFilter.getUnlimitedFilter());

Its usually not too hard to start with a JQL expression and break it down into sub blocks, which can be mirrored through query building via

clause.and().sub().defaultOr().this().that().theother().endsub();

It certainly more verbose and time consuming to code, but can be done. Some gotchas, dont call buildQuery() more than once, it appends query content internally and wont work.

In the end, what are solving your requirement/ what you are happier maintaining will be the right approach for you.

PaulC July 13, 2013

Hi Andy,

Thanks for the very detailed answer. Unfortunately it does not quite answer my query.

If I pass a name of a filter to a method, how do I get the results of of running that filter, without building a new piece of JQL to be parsed. I am currently producing a piece of JQL that says:

filter = "FilterName"

and running that.

So I build that JQL via the JqlClauseBuilder, is that the best way to do this, or can I directly access the Filter somehow?

Paul

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
AUG Leaders

Atlassian Community Events