Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root


1 badge earned


Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!


Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.


Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!


Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
Community Members
Community Events
Community Groups

remote control custom jql

Hi! :)

I have a working remote control custom jql code, which seems to be not quite efficient performance-wise.

Do anyone have any ideas on how to improve the code?

package com.onresolve.jira.groovy.jql

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
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.jira.util.MessageSetImpl
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.query.clause.TerminalClause
import com.atlassian.query.operand.FunctionOperand
import com.onresolve.scriptrunner.remote.RemoteControl
import groovy.util.logging.Log4j

import java.text.MessageFormat

class RemoteQueryJQLFunction extends AbstractScriptedJqlFunction implements JqlQueryFunction {

public static final String TEMPLATE_QUERY = "id in ({0})"
public static final String REMOTE_TEMPLATE_QUERY = "{0}"
public static final String APP_KEY_LOCAL = "XXX"
public static final String APP_KEY_REMOTE = "XXX"
public static final String INTEGRATION_USER_UNAME = "XXX"

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

String getDescription() {
"Get issues linked with issues in remote Jira instance specified by given query."

MessageSet validate(ApplicationUser user, FunctionOperand operand, TerminalClause terminalClause) {
MessageSet messageSet = new NumberOfArgumentsValidator(1, 1, getI18n()).validate(operand)

if (messageSet.hasAnyErrors()) {
return messageSet

def queryStr = MessageFormat.format(REMOTE_TEMPLATE_QUERY, operand.args.first())
messageSet = (MessageSet) RemoteControl.forAppLinkId(APP_KEY_REMOTE).exec{
ApplicationUser remoteUser = ComponentAccessor.getUserManager().getUserByName(INTEGRATION_USER_UNAME)
com.atlassian.query.Query query = ComponentAccessor.getComponent(JqlQueryParser).parseQuery(queryStr)
return ComponentAccessor.getComponent(SearchService).validateQuery(remoteUser, query)
}catch(Exception e){
return new MessageSetImpl().addErrorMessage(e.getMessage())
return messageSet

List<Map> getArguments() {
description: "Query to execute on remote Jira instance",
optional: false,

String getFunctionName() {

Query getQuery(QueryCreationContext queryCreationContext, FunctionOperand operand, TerminalClause terminalClause) {

String idList = ""
def queryStr = MessageFormat.format(REMOTE_TEMPLATE_QUERY, operand.args.first())
idList = (String) RemoteControl.forAppLinkId(APP_KEY_REMOTE).exec {
RemoteIssueLinkManager remoteIssueLinkManager = ComponentAccessor.getComponent(RemoteIssueLinkManager)
ApplicationUser remoteUser = ComponentAccessor.getUserManager().getUserByName(INTEGRATION_USER_UNAME)
SearchService remoteSearchService = ComponentAccessor.getComponent(SearchService)

com.atlassian.query.Query query = ComponentAccessor.getComponent(JqlQueryParser).parseQuery(queryStr)
List remoteIssues =, query, PagerFilter.getUnlimitedFilter()).getIssues()

String issuesIdList = ""
for (Issue issue : remoteIssues) {
List remoteLinks = remoteIssueLinkManager.getRemoteIssueLinksForIssue(issue)
for (RemoteIssueLink remoteLink : remoteLinks) {
String globalId = remoteLink.getGlobalId()
if (globalId?.startsWith("appId=" + APP_KEY_LOCAL)) {
issuesIdList += globalId.substring(globalId.indexOf("issueId=") + 8) + ","
issuesIdList = issuesIdList.substring(0, issuesIdList.length() == 0 ? 0 : issuesIdList.length()-1)
log.warn "id list: " + issuesIdList
return issuesIdList

if (idList == "" || idList == null) queryStr = MessageFormat.format(TEMPLATE_QUERY, "issue_id:")
else queryStr = MessageFormat.format(TEMPLATE_QUERY, idList)
log.warn queryStr
com.atlassian.query.Query query = queryParser.parseQuery(queryStr)
return luceneQueryBuilder.createLuceneQuery(queryCreationContext, query.whereClause)


0 answers

Suggest an answer

Log in or Sign up to answer
AUG Leaders

Atlassian Community Events