Just a heads up: On March 24, 2025, starting at 4:30pm CDT / 19:30 UTC, the site will be undergoing scheduled maintenance for a few hours. During this time, the site might be unavailable for a short while. Thanks for your patience.

×
Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How to link Jira issue with confluence page with scriptrunner

Dachi_Peradze November 11, 2019

Hello everyone

i am using Jira and confluence cloud.
i have post function which creates Confluence page, and it is code written in Scriptrunner.
(https://library.adaptavist.com/entity/create-a-confluence-page-for-each-subtask-of-an-issue)
nextly i want to automatically link this issue to newly created confluence page .

anny suggestions?

2 answers

1 accepted

1 vote
Answer accepted
Derek Fields _RightStar_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 12, 2019

When I do this in my cloud environment, I get the following macro code:

<ac:structured-macro ac:name="jira" ac:schema-version="1" ac:macro-id="ab120ae6-a585-4b2f-a713-4d23b355dc17">
<ac:parameter ac:name="server">System JIRA</ac:parameter>
<ac:parameter ac:name="columns">key,summary,type,created,updated,due,assignee,reporter,priority,status,resolution</ac:parameter>
<ac:parameter ac:name="serverId">f4829001-734e-3f21-9592-1239fa2ab525</ac:parameter>
<ac:parameter ac:name="key">SVCD1-26</ac:parameter>
</ac:structured-macro>

This is the code that you need to put into the Scriptrunner page creation code. You will need to update the text pieces, such as the Issue Key

Dachi Peradze November 12, 2019

I worked Derek ! Thank you very much!
I Put  my Cloud my serverID in the serverID parameter and it worked.
thank you very much again!

Derek Fields _RightStar_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 12, 2019

Would you accept the answer? This will let others know that the question has been answered.

Like Dachi Peradze likes this
Dachi Peradze November 12, 2019

Yes! I voted up for answer already . Thank you again 

Shubham Burghate
Contributor
June 29, 2021

HI Darek,

 

Could yuo please help me to place this macro in code :

def issueKey = 'Test-60'

def result = get("/rest/api/2/issue/${issueKey}")
.header('Content-Type', 'application/json')
.asObject(Map)
def ConfluenceURL = "https:/test-123.atlassian.net/wiki/rest/api/content"
post(ConfluenceURL)
.header('Content-Type', 'application/json')
.header('Authorization','Basic YWRtaW5AYW')
.body([

type: 'page',
title: 'IT Contract ',
space: [
key: "AL"
],
body: [
storage: [
value: "Issue Key : "+ issueKey,

representation: "storage"
]
]

]).asString().body

0 votes
Derek Fields _RightStar_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 11, 2019

You simply need to add the macro code into the template content.  The easiest way to get the content code is to insert a Jira macro into a test page and then look at the source format. You will see something like this:

<ac:structured-macro ac:macro-id="75aa2d99-580c-4f90-acf8-1af6548ef9d8" ac:name="jira" ac:schema-version="1">
<ac:parameter ac:name="server">localhost JIRA</ac:parameter>
<ac:parameter ac:name="columns">key,summary,type,created,updated,due,assignee,reporter,priority,status,resolution</ac:parameter>
<ac:parameter ac:name="serverId">24fa42dc-2a9d-3954-8878-59c90d00a3d0</ac:parameter>
<ac:parameter ac:name="key"><ISSUE KEY></ac:parameter>
</ac:structured-macro>

 put this text into your body content, replacing the <ISSUE KEY> with the actual issue key (e.g., $issue.key). This will cause the macro to be part of the new page.

Dachi_Peradze November 11, 2019

Hello Derek.
I Couldnt find a way to add macro in confluence page. there is no such option for my cloud confluence.
what can be the reason?

Derek Fields _RightStar_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 12, 2019

When you edit a Confluence page, you should be able to insert a Jira macro by clicking on the macro menu, which is the + sign on the upper right of the menu. You should see Jira Issue/Filter macro. Click on that and then insert you Jira issue there. Once you have done that, you need to view the source format to get the actual body content that you need to add to your ScriptRunner script.

Like Shubham Burghate likes this
Dachi Peradze November 12, 2019

I did it but on cloud it has different format

it is in Divs and spans, which has unique id s whit them, do you know how to work with that?

Andrey Permyakov October 8, 2020

Hi, @Derek Fields _RightStar_ 

This is work for storage, how to make it work in wiki

I tried to insert {jiraissues: ${issuekey}} into the page template, this created a macro on the confluence page but did not create a link in the ticket


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 groovy.xml.MarkupBuilder

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

// the issue provided to us in the binding
Issue Issue = issue

// if you don't want to create confluence pages based on some criterion like issue type, handle this, eg:


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

def authenticatedRequestFactory = confluenceLink.createImpersonatingAuthenticatedRequestFactory()

// write storage format using an XML builder
def writer = new StringWriter()
def xml = new MarkupBuilder(writer)
xml.'ac:structured-macro'('ac:name': "jira") {
'ac:parameter'('ac:name': "key", issue.key)
'ac:parameter'('ac:name': "server", "JIRA")
'ac:parameter'('ac:name': "serverId", "1234-1234-1234-1234")
}

// add more paragraphs etc
//xml.p("""${issue.description}""")

// print the storage that will be the content of the page
log.debug(writer.toString())

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

связано с 
{jiraissues:${issue.key}}
${issue.description}
"""

def params = [
type : "page",
title: pageTitle,
space: [
key: "ALM" 
body : [


storage: [
// value : writer.toString(),
// representation: "storage"
// ],
//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"]
}
}
})

 

Thanks.

 

Suggest an answer

Log in or Sign up to answer