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

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

Avatar

1 badge earned

Collect

Participate in fun challenges

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

Challenges
Coins

Gift kudos to your peers

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

Recognition
Ribbon

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!

Leaderboard

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
4,552,417
Community Members
 
Community Events
184
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.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.link.RemoteIssueLink
import com.atlassian.jira.issue.link.RemoteIssueLinkManager
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 org.apache.lucene.search.Query

import java.text.MessageFormat

@Log4j
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)

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

@Override
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())
try{
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
}

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

@Override
String getFunctionName() {
"filterByRemoteIssueLink"
}

@Override
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 = remoteSearchService.search(remoteUser, 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
TAGS
AUG Leaders

Atlassian Community Events