【问题标题】:How to create pages in confluence using script runner如何使用脚本运行器在 Confluence 中创建页面
【发布时间】:2018-09-18 19:35:20
【问题描述】:

我在 ScriptRunner 上找到的所有文档以及在 Confluence 中创建页面都假定您是从 Jira 执行的,因此它添加了不必要的验证步骤。我想在没有来自 Jira 或外部系统的输入的情况下在 Confluence 中运行脚本。

这是我到目前为止挂在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"]
        }
    }
})

以上示例是本文档的精简版:https://scriptrunner.adaptavist.com/latest/jira/interacting-with-confluence-from-jira.html 感谢有关如何创建页面的任何指导。

【问题讨论】:

    标签: groovy confluence confluence-rest-api


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多