Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How include attachments file for confluence page Scriptrunner Edited

Hello
Guys, how to include file attachments from a task when creating a page in this script?

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.jira.component.ComponentAccessor
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 com.atlassian.jira.issue.AttachmentManager
import groovy.json.JsonBuilder
import groovy.json.JsonSlurper

 

AttachmentManager attachmentManager = ComponentAccessor.getAttachmentManager()

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

 

def issue = ComponentAccessor.issueManager.getIssueByCurrentKey("TESTCASE-17")
//Issue issue = issue

 

//
//if (!issue.issueTypeObject.name == "Bug") {
//return
//


def confluenceLink = getPrimaryConfluenceLink()
assert confluenceLink
//

 

def authenticatedRequestFactory = confluenceLink.createImpersonatingAuthenticatedRequestFactory()

//


def pageTitle = issue.key + " Discussion"
def pageBody = """h3. ${issue.summary + issue.attachement}

 

{quote}${issue.description}{quote}

 

Yada yada, use this page to discuss the above...
"""

def params = [
type : "page",
title: pageTitle,
space: [
key: "TESTSR" // 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: "14123220",
]
],*/
body : [
storage: [
value : pageBody,
representation: "wiki"
],
],
]

 

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())
} else {
def webUrl = new JsonSlurper().parseText(response.responseBodyAsString)["_links"]["webui"]
}
}
})

1 answer

Heya, @roman! So, you won't be able to simply inline the attachments into the Confluence page body from the issue.attachments API.

You will probably need to do a separate request to create the attachments after the page is created. Atlassian have some documentation on using the Confluence REST API to create attachments at https://confluence.atlassian.com/confkb/using-the-confluence-rest-api-to-upload-an-attachment-to-one-or-more-pages-1014274390.html. Their examples use curl, but you should be able to make HTTP requests using the same authenticatedRequestFactory in your above script.

Key ideas:

  1. You'll want to get the target page's ID from your first request to create the page.
  2. The notes on the REST API for creating attachments and creating multi-part requests are relevant to your interests here.

Suggest an answer

Log in or Sign up to answer
TAGS

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you