JQL Syntax sample:

  • project = XXX AND issueFunction in UserInRole(” “, assignee, “Developer”) // note the space in the first parameter, this syntax is less efficient
  • issueFunction in UserInRole(” project = XXX “, assignee, “Developer”) //more efficient
  • issueFunction in UserInRole(” project = XXX “, reporter, “Developer”)

package com.onresolve.jira.groovy.jql

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.jql.operand.QueryLiteral
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.jql.query.RangeQueryFactory
import com.atlassian.jira.util.MessageSetImpl
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.query.operand.FunctionOperand
import com.atlassian.query.operator.Operator
import org.apache.log4j.Category
import com.atlassian.jira.util.MessageSet
import com.atlassian.crowd.embedded.api.User
import com.atlassian.query.clause.TerminalClause
import com.atlassian.jira.jql.query.QueryCreationContext
import org.apache.lucene.document.NumberTools
import org.apache.lucene.index.Term
import org.apache.lucene.search.Query
import org.apache.lucene.search.TermQuery
import com.onresolve.jira.groovy.jql.AbstractScriptedJqlFunction
import org.apache.lucene.search.BooleanClause
import org.apache.lucene.search.BooleanQuery
import org.apache.lucene.search.Query
import org.apache.lucene.search.TermQuery
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.security.JiraAuthenticationContext;
import com.atlassian.jira.security.roles.*
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.project.Project

class UserInRole extends AbstractScriptedJqlFunction implements JqlQueryFunction{
@Override
String getDescription() {
"Function to know if user is in any project role of the parameter"
}

@Override
List<Map> getArguments() {
[
[
"description": "Subquery",
"field": "assignee or reporter",
"role": "Role X"
]
]
}

@Override
String getFunctionName() {
"UserInRole"
}

def String subquery
//@Override
MessageSet validate(User user, FunctionOperand operand, TerminalClause terminalClause) {
def messageSet = new MessageSetImpl()
return messageSet
}

@Override
Query getQuery(QueryCreationContext queryCreationContext, FunctionOperand operand, TerminalClause terminalClause) {
JiraAuthenticationContext context = ComponentAccessor.getJiraAuthenticationContext();
ComponentManager componentManager=ComponentManager.getInstance()
ProjectManager projectManager = componentManager.getProjectManager()
ProjectRoleManager projectRoleManager = ComponentManager.getComponentInstanceOfType(ProjectRoleManager.class)
ApplicationUser applicationUser = context.getUser();
ProjectRoleActors projectRoleActors = null;
def booleanQuery = new BooleanQuery()
def typeuser
issues = getIssues(operand.args[0], applicationUser)
issues.each {Issue issue ->
try{
projectRoleActors = projectRoleManager.getProjectRoleActors(projectRoleManager.getProjectRole(operand.args[2]), issue.getProjectObject())
if (operand.args[1] == "assignee" )
typeuser = issue.getAssignee()
else
typeuser = issue.getReporter()
if ( projectRoleActors.getUsers().contains(typeuser) ) {
booleanQuery.add(new TermQuery(new Term("issue_id", issue.id as String)), BooleanClause.Occur.SHOULD)
}
}catch(NullPointerException NPE){

}
}

return booleanQuery
}
}

Posted by:.

11 replies on “JQL Custom Function UserInRole (with Groovy for JIRA)

  1. Unfortunately this doesnt work

    After installing the script and the scan successfully added it as a custom jql function

    issueFunction in UserInRole(” project = XXX “, reporter, “Developer”)

    Error in the JQL Query: Expecting ‘)’ or ‘,’ but got ‘project’. (line 1, character 31)

    😦

    Like

  2. Thank you very much, it works like a charm!

    For the user: Pay attention to put the correct name of the project role and also try to do a ‘good’ JQL query as the first parameter because it will check the role condition for every issue that it will find with that.

    Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s