Create Confluence Page on Postfuncion with attachment from issue

Marcin Beczyński June 26, 2016

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: 4.1.3.14

 

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

def 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:
//if (! issue.issueTypeObject.name == "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
log.debug(writer.toString())
// 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" ]
    
    ]
]
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())
        }
    }
})

1 answer

1 vote
JamieA
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.
June 26, 2016

You will need to make an addition request to add attachments... you can see what data you need to send here: https://docs.atlassian.com/atlassian-confluence/REST/latest-server/#content/{id}/child/attachment-createAttachments

 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events