How to create pages in confluence using script runner

2.9k Views Asked by At

All of the documentation I've found on ScriptRunner and creating pages in Confluence assumes you are doing it from Jira, so it adds unnecessary steps to authenticate. I want to run the script in Confluence with no input from Jira or outside system.

Here is the code I had so far that gets hung up on the authenticatedRequestFactory:

import com.atlassian.applinks.api.ApplicationLink
import com.atlassian.applinks.api.ApplicationLinkService
import com.atlassian.applinks.api.application.confluence.ConfluenceApplicationType
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



def confluenceLink = getPrimaryConfluenceLink()
assert confluenceLink // must have a working app link set up

def authenticatedRequestFactory = confluenceLink.createImpersonatingAuthenticatedRequestFactory()




// set the page title - this should be unique in the space or page creation will fail
def pageTitle = "Teset Discussion"
def pageBody = """h3. Test

{quote}This is a quote{quote}

Yada yada, use this page to discuss the above...
"""

def params = [
        type: "page",
        title: pageTitle,
        space: [
                key: "COM" // set the space key - or calculate it from the project or something
        ],
        /* // if you want to specify create the page under another, do it like this:
         ancestors: [
             [
                 type: "page",
                 id: "14123220",
             ]
         ],*/
        body: [
                storage: [
                        value: pageBody,
                        representation: "wiki"
                ],
        ],
]


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())
        }
        else {
            def webUrl = new JsonSlurper().parseText(response.responseBodyAsString)["_links"]["webui"]
        }
    }
})

The above sample is a trimmed down version from this documentation: https://scriptrunner.adaptavist.com/latest/jira/interacting-with-confluence-from-jira.html Appreciate any direction on how to just create a page.

1

There are 1 best solutions below

0
On

Example code was shared on the following Atlassian thread: https://community.developer.atlassian.com/t/how-to-create-a-confluence-page-with-scriptrunner-via-rest-api/23695 It reviews how to create a page in Confluence without having to be connected to Jira.