Script runner for Jira upgrade from 2.1.14 to 3.1.4

Hi Team,

 

We were using the JIRA 6.2.7  with the script runner plugin version 2.1.17 and testing the JIRA version upgrade to 6.4.8 with script runner 3.1.4, but couldnt get the new scripts while scanning from UI in Script JQL Functions section, tried putting the scripts in <jira_home/scripts>, <\jira-home\scripts\com\onresolve\scriptrunner\canned\jira\workflow\postfunctions> and also in <\jira-home\scripts\com\onresolve\jira\groovy\groovyrunner> but the script is not added while scanning, even couldnt find any errors regarding this in the JIRA log file.

Kindly let me know if I am doing it correctly.

Thanks

Fahad

1 answer

Hi Jamie,

Locating the script at jira-home\scripts\com\onresolve\jira\groovy\jql and implementing  com.onresolve.jira.groovy.jql.JqlQueryFunction also didnt help us to scan the scripted JQL function.

 

Please find the below script which we are using and kindly let me know if any more changes are required.

 

package com.onresolve.jira.groovy.jql
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.jql.operand.QueryLiteral
import com.atlassian.jira.jql.query.QueryCreationContext
import com.atlassian.jira.util.MessageSet
import com.atlassian.query.clause.TerminalClause
import com.atlassian.query.operand.FunctionOperand
import com.onresolve.jira.groovy.jql.AbstractScriptedJqlFunction
import org.apache.lucene.index.Term
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.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.user.util.UserUtil
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.ComponentManager
import com.atlassian.crowd.embedded.api.Group;
import com.atlassian.jira.user.ApplicationUser;

class ReportersInSubQueryAndAssigneeWasInCustomGroup  extends AbstractScriptedJqlFunction implements JqlQueryFunction {
    def String subquery
    def String customGroupFieldName
	def reporterliterals = []
	def reporterlist = []
	def groupliterals = []
	def groupList = []
	
    @Override
    MessageSet validate(User user, FunctionOperand operand, TerminalClause terminalClause) {
        def messageSet = super.validate(user, operand, terminalClause)
        subquery = operand.args[0]
        customGroupFieldName = operand.args[1]
        // if your function is slow, you might want to limit the results here
        messageSet.addMessageSet (validateSubquery(user, subquery))
        messageSet
    }
  
	 /**
     * Implement this method for using datatype = anything but IssueFunction
     * Delete it otherwise
     * @param queryCreationContext
     * @param operand
     * @param terminalClause
     * @return list of query literals
     */
    List&lt;QueryLiteral&gt; getValues(QueryCreationContext queryCreationContext, FunctionOperand operand, TerminalClause terminalClause) {
        ComponentManager componentManager = ComponentManager.getInstance()
		CustomFieldManager customFieldManager = componentManager.getCustomFieldManager()
		def groupManager = ComponentAccessor.getGroupManager()
		
		ApplicationUser appUser = ComponentAccessor.getJiraAuthenticationContext().getUser();
		User user = appUser.getDirectoryUser();
		String currUserFullName = user.getDisplayName();
		String currUserName = user.getName();      
		def List&lt;String&gt; currentUserGroups = groupManager.getGroupNamesForUser(currUserName);
		
		
		log.error (currUserFullName);
		log.error (currUserName);
		
		subquery = operand.args[0]
        customGroupFieldName = operand.args[1]  
		
        log.error "subquery : " + subquery
        log.error "Custom Group Field Name : " + customGroupFieldName        
		
		CustomField customField = customFieldManager.getCustomFieldObjectByName(customGroupFieldName);
        def List&lt;Issue&gt; issues = getIssues(subquery)
        
		def String groupName
		def String reporterName
		
		issues.each {Issue issue -&gt; 
		   if(!(issue.reporter == null)){
				  reporterName = issue.reporter.name
				  if(!(reporterName ==  reporterlist.find {it == reporterName } )){
					 reporterlist.add(reporterName)
				  }
				}
		   reporterName = ""	
		   
        }
		//groupList.each{ gName -&gt; groupliterals.add(new QueryLiteral(operand, gName as String))}
		currentUserGroups.each{ gName -&gt; groupliterals.add(new QueryLiteral(operand, "'"+ gName + "'" as String))}
		
		currentUserGroups.each{gName -&gt; log.error gName}
		
		reporterlist.each{ rName -&gt; reporterliterals.add(new QueryLiteral(operand, rName as String))}
		
        def foundCustomGroups = groupliterals.join(',')
		def foundReporters = reporterliterals.join(',')
		
		//log.error "Found Custom Groups : " + foundCustomGroups
        subquery = "reporter in (" + foundReporters + ")" + " AND '" + customGroupFieldName +"' IN ("+foundCustomGroups+")"
        
		issues = getIssues(subquery)
		
	    def literals = []
		issues.each {Issue issue -&gt;
                literals &lt;&lt; new QueryLiteral(operand, issue.id as Long)
		}
        
		literals
        
    }	
}

 

 

 

It needs the getDataType method, which you can see at the bottom of https://jamieechlin.atlassian.net/wiki/display/GRV/Breaking+Changes+for+Custom+JQL+Functions+in+3.0. In your case you want to return JiraDataTypes.ISSUE

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Jul 10, 2018 in Marketplace Apps

If you’re an Atlassian app developer, you’ll want to know about Atlas Camp!

This September 6-7, hundreds of Atlassian App developers will flock to Barcelona Spain to build skills, discover product roadmaps, meet face-to-face with the Atlassian team, and learn how to extend t...

179 views 0 4
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you