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

This widget could not be displayed.

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
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted Wednesday in New to Jira

Are you planning to trial, or are currently trialling Jira Software? - We want to talk to you!

Hello! I'm Rayen, a product manager at Atlassian. My team and I are working hard to improve the trial experience for Jira Software Cloud. We are interested in   talking to 20 people planning t...

139 views 2 0
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