It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Link a jira issue to confleuence created page [ScriptRunner]

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

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()
}

 

Suggest an answer

Log in or Sign up to answer
TAGS

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you