Create Confluence Page with label

Hi,

I'm using JIRA Script Runner to create a page in Confluence as part of a Workflow Step Post Function as detailed here:

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

All is working fine, but I need to add a label to the page, which I can't find out any examples of doing on the Script Runner documentation.

Does anyone know how to do this?

Thanks

2 answers

1 accepted

3 votes

Hi Jordan,

Unfortunately you will have to do another REST POST request to the Confluence labels endpoint after the page has been created.

Your script should now look something like:

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 com.atlassian.sal.api.net.ReturningResponseHandler
import groovy.json.JsonBuilder

/**
 * Retrieve the primary confluence application link
 * @return confluence app link
 */
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()

// set the page title - this should be unique in the space or page creation will fail
def pageTitle = issue.key + " Discussion"
def pageBody = """h3. ${issue.summary}

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

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

def params = [
    type: "page",
    title: pageTitle,
    space: [
        key: "TEST" // 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: "storage"
        ]
    ]
]

// Get create content response body as a map
def contentResponse = authenticatedRequestFactory
    .createRequest(Request.MethodType.POST, "rest/api/content")
    .addHeader("Content-Type", "application/json")
    .setRequestBody(new JsonBuilder(params).toString())
    .executeAndReturn(new ReturningResponseHandler<Response, Map>(){
    @Override
    Map handle(Response response) throws ResponseException {
        if (response.statusCode != HttpURLConnection.HTTP_OK) {
            throw new Exception(response.getResponseBodyAsString())
        }

        return response.getEntity(Map)
    }
})


// This grabs the current page id thats been created
def contentId = contentResponse.id

def labelsBody = [
    [
        prefix: "global",
        name  : "label-name1"
    ],
    [
        prefix: "global",
        name  : "label-name2"
    ],

]

authenticatedRequestFactory
    .createRequest(Request.MethodType.POST, "/rest/api/content/$contentId/label")
    .addHeader("Content-Type", "application/json")
    .setRequestBody(new JsonBuilder(labelsBody).toString())
    .execute(new ResponseHandler<Response>() {
    @Override
    void handle(Response response) throws ResponseException {
        if (response.statusCode != HttpURLConnection.HTTP_OK) {
            throw new Exception(response.getResponseBodyAsString())
        }
    }
})

Hope this helps.

Adam

Hi @Adam Markham [Adaptavist]

In case we have a template in confluence and want to create the page using the template then how should the template be passed in the API call?

Also how can I copy the URL of the page inside the JIRA ticket(maybe in description)? so that user can access the page directly from JIRA after it is created.

 

Thanks & Regards,

Bhupesh

@Bhupesh Nagda your better off creating a separate question for this so more people will see it.

After a question is accepted as answered people don't usually revisit it to add further comments.

Hi Jordan,

You might need to create the page first and then request to this endpoint to create the labels separately:

https://docs.atlassian.com/atlassian-confluence/REST/latest/#content/{id}/label-addLabels

I've asked for help on this one (from an SR developer) and he'll see if he can put something together for you later.

regards,

Mark.

Hi Mark,

Thanks for that, I'll take a look now!

I take it from this example that there isn't an easy way to add labels to a page as its created (e.g. as part of the creation parameters)?

def params = [
    type: "page",
    title: pageTitle,
    space: [
        key: "TEST" // set the space key - or calculate it from the project or something
    ],
    body: [
        storage: [
            value: pageBody,
            representation: "storage"
                 ]
    ]
]

Cheers

Jordan

Jordan,

So it would seem.

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published Thursday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

362 views 0 6
Read article

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