How to create pages in confluence using script runner

时间:2018-09-18 19:35:20

标签: groovy confluence confluence-rest-api

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 个答案:

答案 0 :(得分:0)

示例代码在以下Atlassian线程上共享:https://community.developer.atlassian.com/t/how-to-create-a-confluence-page-with-scriptrunner-via-rest-api/23695它回顾了如何在Confluence中创建页面而无需连接到Jira。