Link a jira issue to confleuence created page [ScriptRunner]

Mahdi Challouf December 3, 2019

The task is to run a script that is able to create a new specific confluence page under a defined space after a Jira issue creation.

The script below do this stuff but there is no reference in Jira issue for this confluence page can someone help me how can I add the issue links reference in Jira.

Here is the code 

import com.atlassian.applinks.api.ApplicationLink
import com.atlassian.applinks.api.ApplicationLinkService
import com.atlassian.applinks.api.application.confluence.ConfluenceApplicationType
import com.atlassian.jira.issue.Issue
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.sal.api.net.Request
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 groovy.json.JsonSlurper
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import groovy.xml.MarkupBuilder

/**
 * Retrieve the primary confluence application link
 * @return confluence app link
 */
def ApplicationLink getPrimaryConfluenceLink() {
    def applicationLinkService = ComponentLocator.getComponent(ApplicationLinkService.class)
    final ApplicationLink conflLink = applicationLinkService.getPrimaryApplicationLink(ConfluenceApplicationType.class)
    conflLink
}


def confluenceLink = getPrimaryConfluenceLink()
assert confluenceLink // must have a working app link set up

def authenticatedRequestFactory = confluenceLink.createImpersonatingAuthenticatedRequestFactory()
def writer = new StringWriter()
def xml = new MarkupBuilder(writer)

// set the page title - this should be unique in the space or page creation will fail
def pageTitle =issue.key
def pageBody = """h3. ${}

{quote}${issue.description}{quote}
h3. Risk Identification
|Risk|Risk Category|
|empty|empty|
h3. Qualitive rating
|Probability|Impact|Risk Score|Risk ranging|Risk response|
|cell B1|cell B2|cell B3|cell B4|cell B5|
h3. Risk response
|Risaue response|Trigger|Risk Owner|
|cell B1|cell B2|cell B3|

"""

def params = [
    type : "page",
    title: pageTitle,
    space: [
        key: "RS" // set the space key - or calculate it from the project or something
    ],
    body : [
        storage: [
            value         : pageBody,
            representation: "wiki"
        ],
    ],
]

authenticatedRequestFactory
    .createRequest(Request.MethodType.POST, "rest/api/content")
    .addHeader("Content-Type", "application/json")
    .setRequestBody(new JsonBuilder(params).toString())
    .execute(new ResponseHandler<Response>() {
        @Override
        void handle(Response response) throws ResponseException {
            if (response.statusCode != HttpURLConnection.HTTP_OK) {
                throw new Exception(response.getResponseBodyAsString())
            } else {
                def webUrl = new JsonSlurper().parseText(response.responseBodyAsString)["_links"]["webui"]
            }
        }
    })
 

 

1 answer

0 votes
Marc Minten _EVS_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 4, 2019

I think something like this should do the trick (copied code fragments):

import com.atlassian.jira.issue.link.RemoteIssueLinkBuilder
import com.atlassian.jira.issue.link.RemoteIssueLink
import com.atlassian.jira.bc.issue.link.RemoteIssueLinkService

// get the page info (replace last line in your script)
Object page = new JsonSlurper().parseText(response)
String pageId = page["id"].toString()
String pageTitle = page["title"].toString()
String pageUrl = "${confluenceLink.getRpcUrl()}/pages/viewpage.action?pageId=${pageId}"

// build the link
RemoteIssueLinkBuilder linkBuilder = new RemoteIssueLinkBuilder()

linkBuilder.issueId(issue.getId())
linkBuilder.applicationName(confluenceLink.getName())
linkBuilder.applicationType(RemoteIssueLink.APPLICATION_TYPE_CONFLUENCE)
linkBuilder.globalId("appId=${conflLink.getId().get()}&pageId=${pageId}")
linkBuilder.relationship("conluencePages")
linkBuilder.title(pageTitle)
linkBuilder.url(pageUrl)
RemoteIssueLink link = linkBuilder.build()

RemoteIssueLinkService remoteIssueLinkService = ComponentAccessor.getComponentOfType(RemoteIssueLinkService.class)
ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

def validationResult = remoteIssueLinkService.validateCreate(user, link)
if (validationResult.isValid()) {
def linkResult = remoteIssueLinkService.create(user, validationResult)
return linkResult.getRemoteIssueLink()
}

 

YaliZhang November 16, 2022
Object page = new JsonSlurper().parseText(response)

I get an error,the variable [response] is undeclared. I want to ask how to get the variable response

Suggest an answer

Log in or Sign up to answer