Create Confluence page from JIRA addon

Erik Gotera January 2, 2018

Hello,

I'm developing an addon in JIRA and I'm looking the way to create a new Page in Confluence and attach a file to that new page.

 

The file is a new one type XML that I create in the same addon.

 

Is it possible?

I couldn't find any documentation about.

 

Thanks!

1 answer

1 accepted

0 votes
Answer accepted
Aron Gombas _Midori_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 3, 2018

You can create Confluence pages and attachments using the Confluence REST API. Your use case should be completely trivial.

Extra hint: if I were you, I'd also utilize the Application Links technology to securely authenticate into Confluence from JIRA. This will make your life easier.

Erik Gotera January 4, 2018

Thanks Aron,

Do you have any example doing both things?

Make the authentication via Application Links and after the creation of the Confluence page.

I found two examples doing those separate:

 

Application Link authentication: 

https://scriptrunner.adaptavist.com/latest/jira/interacting-with-confluence-from-jira.html

 

Creation Confluence page:

https://github.com/stirlingcrow/Confluence-AccessRestApiWithJava/blob/master/src/main/java/ConfluenceRestApi2CreateEntry.java

 

How I can mix both?

Thansk!

Aron Gombas _Midori_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 4, 2018

No, I don't have a working example, sorry.

Erik Gotera January 5, 2018

I found the solution to Create a new Page in Confluence.

This is the code:

 

ApplicationLink conflLink = applicationLinkService.getPrimaryApplicationLink(ConfluenceApplicationType.class);
ApplicationLinkRequestFactory authenticatedRequestFactory = conflLink.createImpersonatingAuthenticatedRequestFactory();

JSONObject newPage = defineConfluencePage(wikiPageTitle,wikiPageContent,wikiSpace, parentPageId);


authenticatedRequestFactory
.createRequest(Request.MethodType.POST, REQUEST_URL)
.addHeader("Content-Type", "application/json")
.setRequestBody(newPage.toString())
.execute(new ResponseHandler<Response>() {
@Override
public void handle(Response response) throws ResponseException {
if(response.getStatusCode() != HttpURLConnection.HTTP_OK) {

}
}
});


public JSONObject defineConfluencePage(String pageTitle, String wikiEntryText, String pageSpace, int parentPageId) throws JSONException {
JSONObject newPage = new JSONObject();

newPage.put("type","page");
newPage.put("title", pageTitle);

JSONObject spaceOb = new JSONObject();
spaceOb.put("key",pageSpace);
newPage.put("space", spaceOb);

JSONObject jsonObjects = new JSONObject();

jsonObjects.put("value", wikiEntryText);
jsonObjects.put("representation","storage");

JSONObject storageObject = new JSONObject();
storageObject.put("storage", jsonObjects);

newPage.put("body", storageObject);

return newPage;
}

 

Now, I'm trying to create an attachment to a page.

I have the following code:

authenticatedRequestFactory
.createRequest(Request.MethodType.POST, "rest/api/content/"+idPage[0]+"/child/attachment")
.addHeader("Content-Type", "multipart/form-data")
.addHeader("X-Atlassian-Token", "nocheck")
.setRequestBody(newAttachment.toString())
.execute(new ResponseHandler<Response>() {
@Override
public void handle(Response response) throws ResponseException {
if(response.getStatusCode() != HttpURLConnection.HTTP_OK) {
//TODO Control de errores
// throw new Exception(response.getResponseBodyAsString());
}
}
});

 

But I get the error:

{"statusCode":500,"message":"java.lang.RuntimeException: org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found"}

 

What I'm doing wrong?

Thanks!

Like Jose Simón likes this
Aron Gombas _Midori_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 5, 2018

This looks to me that this end-point expects you to send a multi-part POST where the attachment is a separate part.

Please check how you to build the request, but this looks wrong to me:

.setRequestBody(newAttachment.toString())
Erik Gotera January 9, 2018

Thanks Aron,

Yes, that was wrong.

The right way to do that is:

RequestFilePart requestFilePart = new RequestFilePart("text/xml", identificador, data, "file");
List<RequestFilePart> fileParts = new ArrayList<RequestFilePart>();
fileParts.add(requestFilePart);

authenticatedRequestFactory
.createRequest(Request.MethodType.POST, "rest/api/content/" + datosPage.get("id") + "/child/attachment")
.addHeader("X-Atlassian-Token", "nocheck")
.setFiles(fileParts)
.execute(new ResponseHandler<Response>() {
@Override
public void handle(Response response) throws ResponseException {
if (response.getStatusCode() != HttpURLConnection.HTTP_OK) {
try {
throw new Exception(response.getResponseBodyAsString());
} catch (Exception e) {
log.error(e.getMessage());
String commentBody = "ERROR adjuntando XML a la página:\n" + e.getMessage();
principalUtils.createComment(issueFinal, commentBody);
}
}
}
});

Is sending the file with .setFiles.

 

I have another question, I want to put some text in the body of the page, specific two macros:

  • Attachments Macro
  • JIRA Issue

How I could do that?

Thanks!

Like Jose Simón likes this

Suggest an answer

Log in or Sign up to answer