Here is a function with two input parameters to show, as result, a list of issues that it’s worklogged hours sum more than the input parameter two. It’s an easy function to know how create new JQL custom methods.

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;
import com.atlassian.jira.issue.worklog.Worklog;



class SumWorklogMoreThan extends AbstractScriptedJqlFunction implements JqlQueryFunction{
    @Override
    String getDescription() {
        "Function to show issues with worklog >= than hours in function parameter 2"
    }

    @Override
    List<Map> getArguments() {
        [
            [
                "description": "Subquery",
                "worklog": 0,
            ]
        ]
    }

    @Override
    String getFunctionName() {
        "sumWorklogMoreThan"
    }

    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 worklogManager = ComponentAccessor.getWorklogManager()
		 def worklogs = null;
		 def suma = 0;
		 
		 
		 def booleanQuery = new BooleanQuery()
		 issues = getIssues(operand.args[0], applicationUser)
		 issues.each {Issue issue ->
                        try{
								worklogs = worklogManager.getByIssue(issue)
								
								worklogs.each { Worklog work ->
									suma = suma + (work.getTimeSpent()/60)/60;
								}
								if ( suma >= Long.parseLong(operand.args[1]) )
									booleanQuery.add(new TermQuery(new Term("issue_id", issue.id as String)), BooleanClause.Occur.SHOULD)
									
								suma = 0;
                        }catch(NullPointerException NPE){
                           suma = 0;
                        }
                    }                                 
         
        return booleanQuery
    }
}

 

Posted by:.

One thought on “New example of JQL Custom function

  1. New source compatible with Jira 8:
    Syntax example:

    issuefunction in SumWorklogMoreThan("project = JIRA", 5)
    

    Source code:

    package com.onresolve.jira.groovy.jql
    		 
    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.issue.worklog.Worklog;
    
    
    
    class SumWorklogMoreThan extends AbstractScriptedJqlFunction implements JqlQueryFunction{
        @Override
        String getDescription() {
            "Function to show issues with worklog >= than hours in function parameter 2"
        }
    
        @Override
        List<Map> getArguments() {
            [
                ["description": "Subquery","optional": false],
                [    "worklog": 0,"optional": false]
                
            ]
        }
    
        @Override
        String getFunctionName() {
            "sumWorklogMoreThan"
        }
    
        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.getLoggedInUser();
    		 
    		 def worklogManager = ComponentAccessor.getWorklogManager()
    		 def worklogs = null;
    		 def suma = 0;
    		 
    		 
    		 //def booleanQuery = new BooleanQuery()
             //JIRA 8
     		 BooleanQuery.Builder boolQueryBuilder = new BooleanQuery.Builder(); 
    		 issues = getIssues(operand.args[0], applicationUser)
    		 issues.each {Issue issue ->
                            try{
    								worklogs = worklogManager.getByIssue(issue)
    								
    								worklogs.each { Worklog work ->
    									suma = suma + (work.getTimeSpent()/60)/60;
    								}
    								if ( suma >= Long.parseLong(operand.args[1]) )
    									boolQueryBuilder.add(new TermQuery(new Term("issue_id", issue.id as String)), BooleanClause.Occur.SHOULD)
    									
    								suma = 0;
                            }catch(NullPointerException NPE){
                               suma = 0;
                            }
                        }                                 
             
            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