New custom JQL function for JIRA 7.x to filter the TOP ROWS of a Query! Very useful! By default filters the TOP 10 rows. The JQL function is writed in Groovy and deployed with Adaptavist Scriptrunner.

Syntax example: (to extract the top 20 rows of a project called JIRA)

issuefunction in TopRows( “project = JIRA ORDER BY Key DESC”, 20)

How to deploy in the Server? See the post How to add new custom JQL Function with ScriptRunner

The code:


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 TopRows extends AbstractScriptedJqlFunction implements JqlQueryFunction{

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

@Override
String getDescription() {
"Function to extract the TOP X rows of the result"
}

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

@Override
String getFunctionName() {
"TopRows"
}

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)
int top = (Integer.parseInt(operand.args[1]))
int i = 1;
while ( i <= top ) {
booleanQuery.add(new TermQuery(new Term("issue_id", issues[i-1].id as String)), BooleanClause.Occur.SHOULD)
i++;
}
return booleanQuery
}
}

 

Advertisements
Posted by:.

5 replies on “TopRows JQL Function for JIRA 7.2 + Scriptrunner

  1. This has saved our Jira – we have a project where sometimes we have 150 people pulling up to 15,000 issues from our to do column, so this has made managing that board a lifesaver as they pull from the top. Thank you very much for posting this.

    Liked by 1 person

    1. Many thanks for the review! Remember to change a little bit the code (because if the resultset is of less of the limit parameter, the JQL query will fail ) 😉 Best regards!

      Like

      1. Here is what I’ve figured would work to correct the failing JQL when number of issues returned are fewer than the limit;

        top = (issues.size() < top) ? issues.size() : top

        before the while query

        Liked by 1 person

  2. New version of the script for Jira 8.
    Syntax example:

    issuefunction in TopRows("project = JIRA", "30")
    

    Source 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
    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 TopRows extends AbstractScriptedJqlFunction implements JqlQueryFunction{
    	
    	def queryParser = ComponentAccessor.getComponent(JqlQueryParser)
        def luceneQueryBuilder = ComponentAccessor.getComponent(LuceneQueryBuilder)
        def searchService = ComponentAccessor.getComponent(SearchService)
    	
        @Override
        String getDescription() {
            "Function to extract the TOP X rows of the result"
        }
    
        @Override
        List<Map> getArguments() {
            [
                [ "description": "Subquery", "optional": false],
                [ "top": "10", "optional": false]
            ]
        }
    
        @Override
        String getFunctionName() {
            "TopRows"
        }
    
        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()
             //JIRA 8
     		 BooleanQuery.Builder boolQueryBuilder = new BooleanQuery.Builder();   
    		 
             issues = getIssues(operand.args[0], applicationUser)
    		 int top = (Integer.parseInt(operand.args[1]))
    		 int i = 1;
    		 while ( i <= top ) {
    			 boolQueryBuilder.add(new TermQuery(new Term("issue_id", issues[i-1].id as String)), BooleanClause.Occur.SHOULD)
    			 i++;
    		 }
    		 return boolQueryBuilder.build();
        }
    }
    
    

    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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s