Create Confluence page from JIRA addon Edited

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
Accepted answer

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.

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!

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

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!

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())

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!

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Oct 16, 2018 in Jira

Looking for anyone who made the switch to Data Center

The Jira Marketing team is putting together an ebook on migrating to Data Center. We're looking for pro tips on how you staffed your project team and organized your Proof of Concept. Share yo...

1,287 views 17 10
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you