On {JIRA_GOME}/scripts/ create this folders:
For example: ../jira_home/scripts/com/onresolve/jira/groovy/jql
Then add the groovy file with the new JQLFunction, exmaple: HasInactiveAssignee.groovy
The code:
package com.onresolve.jira.groovy.jql 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; class HasInactiveAssignee extends AbstractScriptedJqlFunction implements JqlQueryFunction{ @Override String getDescription() { "Function to show only the inactive users" } @Override List<Map> getArguments() { [ [ "description": "Subquery", "optional": false, ] ] } @Override String getFunctionName() { "hasInactiveAssignee" } 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) { //User user=ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser(); JiraAuthenticationContext context = ComponentAccessor.getJiraAuthenticationContext(); ApplicationUser applicationUser = context.getUser(); def booleanQuery = new BooleanQuery() issues = getIssues(operand.args[0], applicationUser) issues.each {Issue issue -> try{ def active = issue.assignee.isActive() if ( !active ) booleanQuery.add(new TermQuery(new Term("issue_id", issue.id as String)), BooleanClause.Occur.SHOULD) }catch(NullPointerException NPE){ } } return booleanQuery } }
Then Go to Addons –> ScriptRunner –> Script JQL Functions –> and then press “Scan”
Syntax: issueFunction in hasInactiveAssignee(‘project = XXX’)
That’s all!
This is great, thank you!
LikeLiked by 1 person
Its nice to me share the knowledge 🙂
LikeLike
Fantastic, this saved me a good bit of work.
LikeLike
Just as a quick FYI – I’m running JIRA 6.3.11 and Crowd 2.4.2 and within the Script Runner, Script JQL Functions area, I see the below warning:
use JiraAuthenticationContext.getUser(). Since v6.0 @ line 64, column 11.
However, it still seems to work.
Will this change for JIRA 7.0?
LikeLiked by 1 person
You can replace this line:
“User user=ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();”
for this one:
“User user=ComponentAccessor.getJiraAuthenticationContext().getUser();”
because the method is deprecated 😛 sorry
LikeLike
I had tried that, however it gave another error. Will this not work for folks using Crowd and JIRA?
[Static type checking] – Cannot assign value of type com.atlassian.jira.user.ApplicationUser to variable of type com.atlassian.crowd.embedded.api.User
@ line 64, column 11.
LikeLike
OK, I think is something related to use Crowd or not ( my script is for crowd users).
I am reading the documentation for the class in JIRA 7 and it seems work fine.
In other words:
getUser() is Deprecated.
Use getLoggedInUser(). Since JIRA v7.0 (but you can use in JIRA 6 also)
Regards
LikeLike
@IncompatibleReturnType(since=”7.0″,
was=”com.atlassian.crowd.embedded.api.User”)
ApplicationUser getLoggedInUser()
Returns the currently logged in User.
Warning: previous incarnations of this method returned com.atlassian.crowd.embedded.api.User. This method had previously been deprecated and has been reused to return an ApplicationUser instead. That class has different semantics for Object.equals(Object) and therefore does not extend the User class. This means that the 6.0 version is not binary or source compatible with earlier versions when this method is used.
Returns:
The logged in ApplicationUser, or null
LikeLike
Thanks!
LikeLike
Hi Carol, now I am testing the function for JIRA 7 and needs some changes in the source:
– Remove/Comment @Override in the validate() function
– Remove/Comment User user=ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
I am testing and now works fine with JIRA7
Regards!
LikeLike
We’ll be deploying JIRA 7 to our development environment within the next month so I’ll keep this in mind. Thank you so much!
LikeLike
Thank you! Saved my development time
LikeLike
Awesome! Took 5 minutes to implement. Thanks!
LikeLiked by 1 person
Nice! Best regards!
LikeLike
Here the Jira 8 code for the function JQL:
Syntax and example:
Source code compatible with Jira 8
LikeLiked by 1 person