Create Confluence Page with label

Jordan Evans June 2, 2016

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
Answer accepted
adammarkham
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 3, 2016

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

Bhupesh Nagda November 23, 2017

Hi @adammarkham

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

adammarkham
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 23, 2017

@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.

0 votes
Mark McCormack (Adaptavist)
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 2, 2016

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.

Jordan Evans June 2, 2016

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

Mark McCormack (Adaptavist)
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 2, 2016

Jordan,

So it would seem.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events