【发布时间】: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