Skip to content

Instantly share code, notes, and snippets.

@f1ynng8
Forked from jechlin/CreateConfluencePage.groovy
Last active February 18, 2020 16:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save f1ynng8/b765df99cb81de0759c2673d511e5b34 to your computer and use it in GitHub Desktop.
Save f1ynng8/b765df99cb81de0759c2673d511e5b34 to your computer and use it in GitHub Desktop.
Fix internal server error
package examples.docs
import com.atlassian.applinks.api.ApplicationLink
import com.atlassian.applinks.api.ApplicationLinkService
import com.atlassian.applinks.api.application.confluence.ConfluenceApplicationType
import com.atlassian.jira.issue.Issue
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.xml.MarkupBuilder
def ApplicationLink getPrimaryConfluenceLink() {
def applicationLinkService = ComponentLocator.getComponent(ApplicationLinkService.class)
final ApplicationLink conflLink = applicationLinkService.getPrimaryApplicationLink(ConfluenceApplicationType.class);
conflLink
}
// the issue provided to us in the binding
Issue issue = issue
// if you don't want to create confluence pages based on some criterion like issue type, handle this, eg:
if (!issue.getIssueType() == "Bug") {
return
}
def confluenceLink = getPrimaryConfluenceLink()
assert confluenceLink // must have a working app link set up
def authenticatedRequestFactory = confluenceLink.createAuthenticatedRequestFactory()
// write storage format using an XML builder
def writer = new StringWriter()
def xml = new MarkupBuilder(writer)
xml.p("some thing here")
xml.'ac:structured-macro' ('ac:name': "jira") {
'ac:parameter' ('ac:name': "server", "127.0.0.1 JIRA")
'ac:parameter' ('ac:name': "serverId", "12345678-1234-1234-1234-1234567890")
'ac:parameter' ('ac:name': "key", issue.key)
}
// add more paragraphs etc
xml.p ("Some additional info here.")
// print the storage that will be the content of the page
log.debug(writer.toString())
// set the page title - this should be unique in the space or page creation will fail
def pageTitle = issue.key + issue.summary
def params = [
type: "page",
title: pageTitle,
space: [
key: "NGP" // 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: "1015846",
]
],
body: [
storage: [
value: writer.toString(),
representation: "storage"
]
]
]
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())
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment