Merge user fields into one field to avoid OR filter

Sascha Werheid January 23, 2013

Hi,

We use different fields for users who have interests in an issue. The reporter, the assignee and a costume Multi User Picker field named "Stakeholders".

Now we build filter for all issues currentUser has an interest in.

To filter this we need JQL

(reporter = currentUser() OR Stakeholder = currentUser() OR assignee = currentUser())

But with the OR in our JQL query it is impossible for our customers to use the basic filters and they have no idea to extend our filter.

Now we want to merge (by a plugin, or jelly script?) the reporter, the assignee and the stakeholder into one hidden field, which we can use to filter without or.

Is this possible? has anyone tired something like that and has a hint for use how we get started?

2 answers

1 accepted

2 votes
Answer accepted
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 23, 2013

I've done something similar for a totally different reason.

As you say, you create a multi-user-picker field, and don't put it on any view screens (it still needs to have the right context for the project and not be hidden in the field context). You then find/write a listener that picks up changes on the fields and rebuilds that hidden field in the background.

Another option, probably a lot more neat, would be to create a derived or scripted field (again, mostly hidden). If you look at the Jira Toolkit plugin, you'll see that has a field for "participants" which is "commenters, reporter and assignee". That's easy to change to "people listed in other user picker fields". I believe you can do it with a scripted field as well, if you have the script-runner plugin.

JamieA
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.
January 23, 2013

A calculated field of some type is easy too, only disadvantage is that you need to do a full reindex, and it wastes a bit of storage index space, compared to writing your own jql function.

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 23, 2013

Yes, a JQL function is another excellent solution, I should have thought of that.

JamieA
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.
January 23, 2013

Go on then, give me a vote up, I just gave you one!

1 vote
JamieA
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.
January 23, 2013

Several different options you could use. You could create your own jql function that does this using script runner.

Then you could run a query like:

issueFunction in isStakeholder() and project = "Some Project"

It's not too hard, the code is below. You just paste it in, give your function a description, and that should be it.

package com.onresolve.jira.groovy.jql

import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.index.DocumentConstants
import com.atlassian.jira.issue.search.constants.SystemSearchConstants
import com.atlassian.jira.jql.query.QueryCreationContext
import com.atlassian.jira.jql.validator.NumberOfArgumentsValidator
import com.atlassian.jira.util.MessageSet
import com.atlassian.query.clause.TerminalClause
import com.atlassian.query.operand.FunctionOperand
import org.apache.lucene.index.Term
import org.apache.lucene.search.BooleanClause
import org.apache.lucene.search.BooleanQuery
import org.apache.lucene.search.Query
import org.apache.lucene.search.TermQuery

class StakeHolderFunction extends AbstractScriptedJqlFunction {

    private static final String STAKEHOLDER = "Stakeholder"
    def customFieldManager = ComponentAccessor.getCustomFieldManager()

    @Override
    MessageSet validate(User user, FunctionOperand operand, TerminalClause terminalClause) {

        def messageSet = new NumberOfArgumentsValidator(0, 0, getI18n()).validate(operand)

        if (! customFieldManager.getCustomFieldObjectByName(STAKEHOLDER)) {
            messageSet.addErrorMessage("Could not find $STAKEHOLDER field")
        }

        // todo: other validation
        messageSet
    }

    Query getQuery(QueryCreationContext queryCreationContext, FunctionOperand operand, TerminalClause terminalClause) {

        def stakeholder = customFieldManager.getCustomFieldObjectByName("Stakeholder")

        def booleanQuery = new BooleanQuery()
        def currentUser = queryCreationContext.user?.name
        booleanQuery.add(new TermQuery(new Term(DocumentConstants.ISSUE_AUTHOR, currentUser)), BooleanClause.Occur.SHOULD)
        booleanQuery.add(new TermQuery(new Term(DocumentConstants.ISSUE_ASSIGNEE, currentUser)), BooleanClause.Occur.SHOULD)
        booleanQuery.add(new TermQuery(new Term(stakeholder.getId(), currentUser)), BooleanClause.Occur.SHOULD)
        booleanQuery
    }
}

Suggest an answer

Log in or Sign up to answer