Skip to main content
Example scripts
arrow icon
To homepage
Jira
Data centre icon
Data Center

Create a Confluence Page for Each Subtask of an Issue

Created 1 year ago, Updated 1 month(s) ago
Apps in script
ScriptRunner For Jira
ScriptRunner For Jira
by Adaptavist
ScriptRunner For Confluence
ScriptRunner For Confluence
by Adaptavist
Compatibility
compatibility bullet
Jira (7.7 - 8.6)
compatibility bullet
ScriptRunner For Jira (5.6.14)
Language |
groovy
import com.atlassian.applinks.api.ApplicationLink
import com.atlassian.applinks.api.ApplicationLinkService
import com.atlassian.applinks.api.application.confluence.ConfluenceApplicationType
import com.atlassian.jira.component.ComponentAccessor
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

def key = 'TEST'
def issueKey = 'CWT-31'
def issueManager = ComponentAccessor.issueManager
def issue = issueManager.getIssueObject(issueKey)

def title = """${issue.key} - ${issue.summary}"""
def writer = new StringWriter()
def xml = new MarkupBuilder(writer)
// add more paragraphs etc
xml.h2("Example page")
xml.p("${issue.description}")
def parent = createConfluencePage(title, key, null, writer)

issue.subTaskObjects.find { subtask ->
    // write storage format using an XML builder
    def subtitle = """${subtask.key} - ${subtask.summary}"""
    def subwriter = new StringWriter()
    def subxml = new MarkupBuilder(subwriter)
    // add more paragraphs etc
    subxml.h2("Description")
    subxml.p("""${subtask.description}""")
    createConfluencePage(subtitle, key, parent, subwriter)
}

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

int createConfluencePage(pageTitle, spaceKey, parentPage, pageContent) {
    def confluenceLink = createPrimaryConfluenceLink()
    assert confluenceLink // must have a working app link set up
    def authenticatedRequestFactory = confluenceLink.createImpersonatingAuthenticatedRequestFactory()

    def params = [
        type : "page",
        title: pageTitle,
        space: [
            key: spaceKey // set the space key - or calculate it from the project or something
        ],
        body : [
            storage: [
                value         : pageContent.toString(),
                representation: "storage"
            ]
        ]
    ]
    if (parentPage != null) {
        params["ancestors"] = [parentPage].collect { [id: parentPage.toString()] }
    }
    log.warn(params.toString())
    def responseBody
    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.responseBodyAsString)
            } else {
                responseBody = new JsonSlurper().parseText(response.responseBodyAsString)
            }
        }
    })

    responseBody["id"] as Integer
}

Having an issue with this script?
Report it here