I have 2 JSM instances connected with OAuth.
On the first instance, I have a text field which stores a hyperlink to another issue on the second JSM instance. I'm trying to write a script which automatically creates a reciprocal link between the 2 issues.
--------------------------------------------------------------------------------------------
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.link.RemoteIssueLinkManager
import com.atlassian.jira.issue.link.RemoteIssueLinkManager
import com.atlassian.jira.bc.user.search.UserSearchService
import com.atlassian.jira.issue.link.RemoteIssueLinkBuilder
import com.atlassian.jira.bc.issue.link.RemoteIssueLinkService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.applinks.api.ApplicationLinkService
import com.atlassian.applinks.api.application.jira.JiraApplicationType
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.sal.api.net.Response
import com.atlassian.sal.api.net.ResponseException
import com.atlassian.sal.api.net.ResponseHandler
import groovy.json.JsonBuilder
import static com.atlassian.sal.api.net.Request.MethodType.POST
import com.atlassian.jira.component.ComponentAccessor
def appLinkService = ComponentLocator.getComponent(ApplicationLinkService)
def appLink = appLinkService.getPrimaryApplicationLink(JiraApplicationType)
def applicationLinkRequestFactory = appLink.createAuthenticatedRequestFactory()
def user = Users.getByName("<admin username>")
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def customField = customFieldManager.getCustomFieldObject("customfield_<custom field id>")
// get hyperlink from current ticket
def remoteIssueLink = issue.getCustomFieldValue(customField) as String
def localIssueLink = ("<local link>" + issue.getKey())
def remoteIssueKey = remoteIssueLink.split("/").last()
// create post call for remote ticket
def post = ("/rest/api/2/issue/" + remoteIssueKey + "/remotelink")
// create input for remote post call
def input = new JsonBuilder([
object: [
url: localIssueLink,
title: issue
]
]).toString()
// define request
def request = applicationLinkRequestFactory.createRequest(POST, post)
.addHeader("Content-Type", "application/json")
.setEntity(input)
// make request and store response
def responseContentJson
request.execute(new ResponseHandler<Response>() {
@Override
void handle(Response response) throws ResponseException {
responseContentJson = response.responseBodyAsString
if ( response.statusCode != 201) {
issue.addComment("Fail: ${response.responseBodyAsString}")
}
}
})
// instantiate remote issue link builder
def linkBuilder = new RemoteIssueLinkBuilder()
// populate link builder with remote issue detals
linkBuilder.url(remoteIssueLink)
linkBuilder.title(remoteIssueKey)
linkBuilder.issueId(issue.id)
// create link
def remoteIssueLinkService = ComponentAccessor.getComponentOfType(RemoteIssueLinkService.class)
def validationResult = remoteIssueLinkService.validateCreate(user, linkBuilder.build())
remoteIssueLinkService.create(user, validationResult)
-----------------------------------------------------------------------------------------------
When I run this script I encounter a stack overflow. I'm really quite new to scriptrunner and the rest API. My understanding is poor and I'm struggling to understand whats going wrong.