Created
October 5, 2014 10:04
-
-
Save jechlin/fe1bbdb5ee5445af8346 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.issueTypeObject.name == "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.'ac:structured-macro' ('ac:name': "jira") { | |
'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 + " Discussion" | |
def params = [ | |
type: "page", | |
title: pageTitle, | |
space: [ | |
key: "TEST" // set the space key - or calculate it from the project or something | |
], | |
body: [ | |
storage: [ | |
value: writer.toString() | |
] | |
] | |
] | |
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()) | |
} | |
} | |
}) |
@madhushell - do you not have that available in the response?
if(response.statusCode != HttpURLConnection.HTTP_OK) {
looks like it would contain the response, which should have the information needed to get the URL.
I currently use a webhook from JIRA that ties into a custom .NET WebAPI app I created that does this same functionality (weighing the possibility of replacing it with this to get rid of the external dependency). My version posts the content to the API in the same manner, then gets a reference to the page from the API Response so that it can add labels to the page based on information from JIRA. There isn't a friendly URI in the response, but using the page ID returned to reference the page seems to work for my purposes.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I had the following error
{noformat}
"{"statusCode":500,"data":{"authorized":false,"valid":true,"errors":[]},"message":"com.atlassian.confluence.api.service.exceptions.ServiceException: java.lang.UnsupportedOperationException: Cannot convert from null to storage"}"
{noformat}
when I used the above code. So I added
storage: [ value: writer.toString(), representation: "storage" ]
I'm just sharing here, in case anyone encounter a similar issue.
BTW would be great if we can get the confluence page url returned so that I can have this copied to Release notes url field.
FYI , I'm using the above script to spin off release notes page on a release jira creation.