Skip to main content
Example scripts
arrow icon
To homepage
Jira
Cloud icon
Cloud

Create a Confluence Page for Each Subtask of an Issue in Cloud

Created 3 month(s) ago, Updated 17 day(s) ago
App in script
ScriptRunner For Jira
ScriptRunner For Jira
by Adaptavist
Compatibility
compatibility bullet
Jira
Language |
groovy
def spaceKey = 'LTSE'
def issue = Issues.getByKey('TEST-1')
def spaceSource = get("/wiki/api/v2/spaces?keys=${spaceKey}")
    .header('Content-Type', 'application/json')
    .asObject(Map)
if (spaceSource.status != 200) {
    return "Error fetching  ${spaceSource}"
}

def spaceId = spaceSource.body.results[0].id as String
def subtasks = issue.getSubtasks()
def title = issue.getSummary()
def rootContent = "<h2>Description</h2><p>${issue.getDescription()}</p>"
def parent = createConfluencePage(title, spaceId, rootContent, null)

subtasks.forEach {subtask ->
    def subtitle = "${subtask.getKey()} - ${subtask.getSummary()}"
    def pageContent = "<h2>Description</h2><p>${subtask.getDescription()}</p>"
    createConfluencePage(subtitle, spaceId, pageContent, parent)
}

String createConfluencePage(String pageTitle, String spaceId, String pageContent, String parentPage) {
    def params = [
        type : "page",
        title: pageTitle,
        spaceId: spaceId,
        body : [
            storage: [
                value         : pageContent.toString(),
                representation: "storage"
            ]
        ]
    ] as Map
    if (parentPage != null) {
        params["parentId"] = [parentPage].collect { [id: parentPage.toString()] }
    }

    def pageResult = post('/wiki/api/v2/pages')
        .header('Content-Type', 'application/json')
        .body(params)
        .asObject(Map).body

    if (pageResult.statusCode) {
        logger.error("Failed to create a new page. Confluence responded with error code: {}", pageResult.statusCode)
    } else {
        logger.info("Successfully created a new space with id: {}", pageResult)
    }
    pageResult.id
}

parent
Having an issue with this script?
Report it here