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

remote control custom jql

Marcin Pelzner May 10, 2018

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