The Atlassian Community Forums are currently in read-only mode. We will be relaunching on a new platform on September 22 (read more here). We apologize for the extended downtime. For concerns or questions, please email communitymanagers@atlassian.com. See you on the other side, on the new Atlassian Community Forums! :)

×

Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 
  • Community
  • Q&A
  • Jira
  • Questions
  • Is it possible to to create in ticket a link to a page in confluence using scriptrunner in jira post

Is it possible to to create in ticket a link to a page in confluence using scriptrunner in jira post

Andrey Permyakov
Contributor
July 8, 2021

Is it possible to to create in ticket a link to a page in confluence using scriptrunner in jira post function?

The option described in the docking is not suitable, The page is needed in the wiki.

On the confluence side, I create a ticket backlink using {jira: key = $ {issue.key}} in the pagebody. But I do not understand how to create a link of the "Confluence page" type on the jira side.

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.event.issue.IssueEvent
import com.atlassian.jira.issue.link.*
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

def ApplicationLink getPrimaryConfluenceLink() {
def applicationLinkService = ComponentLocator.getComponent(ApplicationLinkService)
final ApplicationLink conflLink = applicationLinkService.getPrimaryApplicationLink(ConfluenceApplicationType)
conflLink
}

Issue Issue = issue

def confluenceLink = getPrimaryConfluenceLink()
assert confluenceLink

def authenticatedRequestFactory = confluenceLink.createImpersonatingAuthenticatedRequestFactory()

def pageTitle = issue.key + " Discussion" as String


def pageBody = """h3. ${issue.summary}

{jira: key = ${issue.key}}
{quote}${issue.description}{quote}

"""

def params = [
type : "page",
title: pageTitle,
space: [
key: "testovoe"
],

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 {

def webUrl = new JsonSlurper().parseText(response.responseBodyAsString)["_links"]["webui"]
def newurl = webUrl as String

def newweb = "https://my.confluenceserver.com" + newurl -"]"-"["
log.debug newweb

}
})

 

1 answer

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

0 votes
Answer accepted
PD Sheehan
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 Champions.
July 13, 2021

Normally, when you create a jira issue macro in a confluence page, it automatically creates a link back to the source confluence page in the jira issue as "mentioned in <pagename>". But that doesn't seem to happen automatically when creating using api this way.

Is this what you are trying to achieve?

Or are you asking how to include a link to another pre-existing page in the wiki pageBody?

If so, something like this should work:

def pageBody = """h3. ${issue.summary}

{jira: key = ${issue.key}}
{quote}${issue.description}{quote}

[Link To Page|Name of the page to link to]
"""
Andrey Permyakov
Contributor
July 13, 2021

Hello, Peter-Dave Sheehan 

The automatic link in jira to wikipage does not work in this case and it makes me sad.

I just need to create a link on the Jira side.

PD Sheehan
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 Champions.
July 13, 2021

I played around with this ... try to generate your pagebody jira macro with additional attributes

{jira: key=${issue.key}|serverId=xxx-xxx-xxx-xx|server=yyy}

Get the xxx-xxxx and yyy values from a macro that you insert manually. It will be the application id and name from your confluence applinks

When I specify both of those parameters, the "mention on" links is created automatically for me.

Like • Andrey Permyakov likes this
Andrey Permyakov
Contributor
July 13, 2021

It worked.

Thanks!