It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Custom JQL function: membersOfProjectRole

  Hello

I am looking for a way to test "assignee in membersOfProjectRole(PROJECTKEY)" as standard "membersOf" is restricted to JIRA groups.

I have found a pending request: https://jira.atlassian.com/browse/JRASERVER-21711

And some example code at https://mraddon.blog/2016/02/17/jql-custom-function-userinrole-with-groovy-for-jira/ and https://mraddon.blog/2017/01/03/lastcommentcontains-jql-function-for-jira-7-2-scriptrunner/

May you please provide updated code of this function for JIRA 7.6/7.7 ?

@Adaptavist SuppWould it be possible for this JQL function to be included in plugin itself ?

Thank you in advance for your help

2 answers

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
import com.atlassian.jira.issue.comments.CommentManager

import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.jql.query.LuceneQueryBuilder
import com.atlassian.jira.jql.query.QueryCreationContext
import com.atlassian.jira.jql.validator.NumberOfArgumentsValidator
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.util.MessageSet
import com.atlassian.query.clause.TerminalClause
import com.atlassian.query.operand.FunctionOperand
import groovy.util.logging.Log4j
import org.apache.lucene.search.Query

import java.text.MessageFormat



class LastCommentContains extends AbstractScriptedJqlFunction implements JqlQueryFunction{

def queryParser = ComponentAccessor.getComponent(JqlQueryParser)
def luceneQueryBuilder = ComponentAccessor.getComponent(LuceneQueryBuilder)
def searchService = ComponentAccessor.getComponent(SearchService)

@Override
String getDescription() {
"Function to validate if the last comment contains certain text"
}

@Override
List<Map> getArguments() {
[
[ "description": "Subquery", "optional": false],
[ "contains": "phrase", "optional": false]
]
}

@Override
String getFunctionName() {
"LastCommentContains"
}

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();
ApplicationUser applicationUser = context.getLoggedInUser();
def booleanQuery = new BooleanQuery()
issues = getIssues(operand.args[0], applicationUser)
def comments
def lastcommen
String lastcomment
String texttofind
issues.each {Issue issue ->
try{
comments = ComponentAccessor.commentManager.getComments(issue)
if (comments) {
lastcommen = comments.last();
lastcomment = lastcommen.body.toLowerCase();
texttofind = operand.args[1].toLowerCase()
if ( lastcomment.contains(texttofind) ) {
booleanQuery.add(new TermQuery(new Term("issue_id", issue.id as String)), BooleanClause.Occur.SHOULD)
}
}


}catch(NullPointerException NPE){

}
}

return booleanQuery
}
}

Invocation: issuefunction in lastCommentContains("project = 'XXX'","text_to_search") 

Best regards!

The code for UserInRole JQL function:

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","optional": false],
["field": "assignee","optional": false],
["role": "Product Owner","optional": false]
]

}

@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();
ProjectManager projectManager = ComponentAccessor.getProjectManager()
ProjectRoleManager projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager.class)
//ProjectRoleManager projectRoleManager = ComponentAccessor.getProjectRoleManager()
ApplicationUser applicationUser = context.getLoggedInUser();
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
}
}

Usage:  issuefunction in UserInRole("project='xxx'","assignee","Administrators")

@Raul MrAddon can you provide updated code of this function for JIRA 8 ?

Like Raul MrAddon likes this

Ok Dmitry I will try!

New source code for Jira 8

Syntax example:

issuefunction in UserInRole("project = JIRA",assignee,Administrators)

Here the code:

package com.onresolve.jira.groovy.jql

import com.atlassian.jira.issue.Issue
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.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","optional": false],
["field": "assignee","optional": false],
["role": "Product Owner","optional": false]
]

}

@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();
ProjectManager projectManager = ComponentAccessor.getProjectManager()
ProjectRoleManager projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager.class)
ApplicationUser applicationUser = context.getLoggedInUser();
ProjectRoleActors projectRoleActors = null;

//JIRA 8
BooleanQuery.Builder boolQueryBuilder = new BooleanQuery.Builder();

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) ) {


boolQueryBuilder.add(new TermQuery(new Term("issue_id", issue.id as String)), BooleanClause.Occur.SHOULD)
}
}catch(NullPointerException NPE){

}
}

return boolQueryBuilder.build();
}

}

Thanks a lot MrAddon for these lines of code for JIRA 8 :-) It's working now like a charm :-)

Like Raul Pelaez likes this

Raul - noob here. Can you provide a step by step guide to enabling your custom issuefunction in UserInRole solution? I am on Scriptrunner 5.9.0-p5 and Jira Data Center 8.5.3

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Next-gen

Keyboard shortcuts have arrived for next-gen projects!

...ollected feedback from users around the lack of shortcuts, and we’re here to address that: In next-gen projects, I miss the keyboard shortcuts badly. This is particularly true on the Board, but also i...

278 views 2 5
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you