Create Confluence Page on Postfuncion with attachment from issue

Hi All,
Recently I found groovy script what can create a confluence page on transition, and it works great. Question is can it add also attachment from JIRA issue to this page ??  I google XML builder and did not found any class what can handle that operation if you had some idea please share it.

some info:

JIRA version : 6.3.15

Conflence version: 5.8.6

SR version:


Thank in advice for all help

code of create confluence page :

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

def ApplicationLink getPrimaryConfluenceLink() {
    def applicationLinkService = ComponentLocator.getComponent(ApplicationLinkService.class)
    final ApplicationLink conflLink = applicationLinkService.getPrimaryApplicationLink(ConfluenceApplicationType.class);
// 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:
//if (! == "Bug") {
//    return
def confluenceLink = getPrimaryConfluenceLink()
assert confluenceLink // must have a working app link set up
def authenticatedRequestFactory = confluenceLink.createAuthenticatedRequestFactory()
// 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)
// add more paragraphs etc
xml.p ("Some additional info here.")

// print the storage that will be the content of the page
// set the page title - this should be unique in the space or page creation will fail
def pageTitle = issue.summary
def pageBody = ""
def params = [
    type: "page",
    title: pageTitle,
    space: [
        key: "HRC" // set the space key - or calculate it from the project or something
        ancestors: [
           type: "page",
           id: "1048633",
    body: [
        storage: [ value: writer.toString(), representation: "storage" ]
    .createRequest(Request.MethodType.POST, "rest/api/content")
    .addHeader("Content-Type", "application/json")
    .setRequestBody(new JsonBuilder(params).toString())
    .execute(new ResponseHandler<Response>() {
    void handle(Response response) throws ResponseException {
        if(response.statusCode != HttpURLConnection.HTTP_OK) {
            throw new Exception(response.getResponseBodyAsString())

1 answer

You will need to make an addition request to add attachments... you can see what data you need to send here:{id}/child/attachment-createAttachments


Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Oct 09, 2018 in Jira Core

How to manage many similar workflows?

I have multiple projects that use variations of the same base workflow. The variations depend on the requirements of the project or issue type. The variations mostly come in the form of new statuses ...

222 views 6 0
Join discussion

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you