Snippets

Adaptavist Script Console Create A Confluence Page with values off an issue

Created by Kristian Walker last modified
/*
 * "This script console script provides an example script for SR for Jira cloud of how to create a new page inside of Confluence from Jira Cloud 
 * All right, title and interest in this code snippet shall remain the exclusive intellectual property of Adaptavist Group Ltd and its affiliates. Customers with a valid ScriptRunner 
 * license shall be granted a  non-exclusive, non-transferable, freely revocable right to use this code snippet only within their own instance of Atlassian products. This licensing notice cannot be removed
 * or amended and must be included in any circumstances where the code snippet is shared by You or a third party." 
 */
 
 //required imports
import org.apache.commons.codec.binary.Base64;

// Specify all the required parameters

// Specify the id of the parent page that the new page will be created under
def parentPageId = "<PageIDHere>"

//Specify a title for the new page
def pageTitle = "<PageTitleHere>"

// Specify the space key of the spcae that the new page will be created in
def spaceKey = "<PageDemoHere>"

// Specify Credentials to access Confluence
String authString = "<UsernameHere>"+":"+"<PasswordHere>"
byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
String authStringEnc = new String(authEncBytes);

// Specify the base url for the confluence instance
def confluenceBaseURL = "<ConfluenceBaseURLHere>"

// Get values out of fields on the issue
def issueKey = '<IssueKeyHere>'

def issue = get('/rest/api/2/issue/' + issueKey)
        .header('Content-Type', 'application/json')
        .asObject(Map)
        .body

def summary = issue.fields.summary

def description = issue.fields.description

// Specify the field values obtained in the storage format for the page
def storageFormat = """
${summary}
<br/>
${description.replaceAll('\n','<br/>')}
"""

// Specify the body of the rest request
def body = [
        type: "page",
        title: pageTitle,
        space: [
                key: spaceKey
        ],
        ancestors: [[
                            id: parentPageId
                    ]],
        body:[
                storage:[
                        value: storageFormat,
                        representation: "storage"
                ]
        ]
]

//create confluence (cloud) page
def createPageResult = post("${confluenceBaseURL}/rest/api/content")
        .header("Content-Type", "application/json")
        .header("Accept", "application/json")
        .header("Authorization", "Basic ${authStringEnc}")
        .body(body)
        .asObject(Map)
 
// Uncomment the code block below if you wish to add labels to the newly created page. 
/*
def addLabels = post("${confluenceBaseURL}/rest/api/content/${createPageResult.body.id}/label")
                        .header("Content-Type", "application/json")
                        .header("Accept", "application/json")
                        .header("Authorization", "Basic ${authStringEnc}")
                    .body(
                        [
                            // Note you can specify a comma seperated lists of strings here if you wish to add multiple labels to a page. 
                            // An example of adding more than 1 label is ""name"  : "ALabel , label2",
                            "name"  : "<LabelNameHere>",
                            "prefix": "global"
                        ])
                        .asString()
*/                        

// Return the result of the created page
return createPageResult

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.