We have two Jira instances. We need to do a jql query based on a filter to get a list of keys from instance 1 and then get their remote link keys in instance 2 where key matches RMI-*.
Is a custom jql function the way to approach this?
Hi @Admin User welcome to the community. If you already have Scriptrunner installed then there already is a jql function for this. Have a look here for review.
Hi, thanks for the response. Unless I am missing something linkedIssuesOfRemote("filter='ABC 1.0.1'") or something like it isn't possible.
I need to jql query instance 2 with the filter and end up with a list of issues from instance that are remotely linked to the jql.
So I don't need to just find the issue with links, I need to find the remote link keys.
Are your instances Federated?
I don't believe so. But I'm thinking a custom jql that runs against a remote host which returns a list of keys allows us to query each individual key against the rest API to get remote link keys should be possible.So in Jira A:ScriptRunner uses ApplicationLinkService to perform a rest call doing a JQL query against Jira B. The results will be parsed to produce a list of keys.We then iterate through those list of keys querying the rest API for remote link keys starting with ABC-* and produce a list of keys in Jira A.With a custom JQL we can hopefully run a JQL with a Jira B filter to get Jira A keys.Following code is partly done and tested in the script console but need to complete getting the remote links from the initial query and then figure out how to run this into a custom JQL.
import groovy.json.JsonSlurperimport com.atlassian.applinks.api.ApplicationLinkServiceimport com.atlassian.jira.component.ComponentAccessorimport com.atlassian.applinks.api.application.jira.JiraApplicationTypeimport com.atlassian.sal.api.component.ComponentLocatorimport com.atlassian.sal.api.net.Responseimport com.atlassian.sal.api.net.ResponseExceptionimport org.apache.log4j.Levelimport static com.atlassian.sal.api.net.Request.MethodType.GETimport com.atlassian.applinks.api.ApplicationLinkResponseHandlerlog.setLevel(Level.DEBUG)def appLinkService = ComponentLocator.getComponent(ApplicationLinkService)def remoteJiraUrl = "https://jira.com"def applicationLinkService = ComponentAccessor.getComponent(ApplicationLinkService)def applicationLink = applicationLinkService.getApplicationLinks(JiraApplicationType).find {it.displayUrl.toString() == remoteJiraUrl}// Check if the application link was foundif (applicationLink) {log.debug "Application link found for ${remoteJiraUrl}: ${applicationLink.getDisplayUrl()}"} else {log.debug "Application link not found for ${remoteJiraUrl}"}applicationLinkRequestFactory = applicationLink.createAuthenticatedRequestFactory()// Define the remote host URL and JQL filterjqlFilter = "filter = 'ABC 1.0.1'"// Connect to the remote host and run the JQL querydef runJQLQueryOnRemote(String host, String jql, int start = 0, int limit = 50) {def apiUrl = "/rest/api/latest/search?jql=${URLEncoder.encode(jqlFilter, "UTF-8")}&fields=key?startAt=${start}&maxResults=${limit}"log.debug apiUrldef request = applicationLinkRequestFactory.createRequest(GET, apiUrl)def handler = new ApplicationLinkResponseHandler<Map>() {@OverrideMap credentialsRequired(Response response) throws ResponseException {null}@OverrideMap handle(Response response) throws ResponseException {assert response.statusCode == 200new JsonSlurper().parseText(response.getResponseBodyAsString()) as Map}} def sessionDetails = request.execute(handler)return sessionDetails}// Get the list of issues from the remote hostdef issues = []def start = 0def limit = 200def totalResults = 0def remoteIssues = runJQLQueryOnRemote(remoteJiraUrl, jqlFilter, start, limit)remoteIssues.issues.each {issues.add it.key}
It looks like you're new here. Sign in or register to get started.