Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Trying to create a confluence page from a jira workflow at issue creation time

Deleted user May 11, 2018

I'm trying to use jmwe to create a confluence page during issue creartion time and I'm not having any luck. I've found a few groovy snippets but none actually crate a page. no errors in the log, but no confleunce page. I was able to create one using command line curl, but i can't seem to do it within workflow. i've just used the jmwe scripting so far for simple things, and I'm not even sure if I'm using it correctly. Is this even possible before I burn any more time on it?

Thanks,

Jay

1 answer

0 votes
David Fischer
Community Champion
May 12, 2018

Hi Jay,

it should be possible to do it in JMWE using a Groovy script. Can you share pointers to the Groovy snippets you tried? You also need to have an application link between your Jira and Confluence instances, I assume you already have it?

Deleted user May 14, 2018

Hi David,

Thanks for the reply.  If I go to Application Links section of Jira it lists System Confluence, it is marked as Primary, so I guess that means I do have the applications link? I can see jira items in confluence and vice versa.

I have to admit I am just cut and pasting snippets that other have successfully used, so I have no great insight into this :) I was able to write a standalone script using the REST API and successfully create a confluence page, and the parameters look to be the same to me. One big difference is that I hardwired userid/password for my test, I didn't use the authentication used in this snippet. Here's what I used. Again, sorry if I'm completely in left field in what I'm doing, it's a learn-as-you-go moment for me :) :

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 groovy.json.JsonBuilder
import groovy.xml.MarkupBuilder

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()
// write storage format using an XML builder
def writer = new StringWriter()
def xml = new MarkupBuilder(writer)
xml.'ac:structured-macro' ('ac:name': "jira") {
    'ac:parameter' ('ac:name': "key", issue.key)
}
// add more paragraphs etc
xml.p ("Some additional info here.")

// print the storage that will be the content of the page
log.debug(writer.toString())
// set the page title - this should be unique in the space or page creation will fail
def pageTitle = issue.summary
def pageBody = ""
def params = [
    type: "page",
    title: pageTitle,
    space: [
        key: "TEST" // set the space key - or calculate it from the project or something
    ],
//        ancestors: [
//       [
//           type: "page",
//           id: "1048633",
//       ]
//     ],

    body: [
        storage: [ value: writer.toString(), representation: "storage" ]
   
    ]
]
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())
        }
    }
})

David Fischer
Community Champion
May 14, 2018

Have you tried to run this script on the "Groovy script tester" page? It will not only show you any error, it will also output any logs written by the script.

I just noticed, near the end, some syntax error, maybe due to some copy/paste:

.execute(new ResponseHandler<Response>() {

should be

.execute(new ResponseHandler<Response>() {
Deleted user May 14, 2018

groovy.PNGHi David,

I did run it through the verification tool and it checked out OK, even before above syntax change. I even introduced a real syntax issue and still gave me the green checkbox saying it tested OK, almost like it's treating it like a giant comment/no-op. I look in JMWE logs and it seems OK, no errors. Right now I'm putting this in a conditional validation section of a variable assignment Here part of the log after running workflow that executes conditional.

Jay

David Fischer
Community Champion
May 14, 2018

Hi Jay,

you put the script in the Conditional Execution template of a  "Copy field value to parent" post-function on Jira Cloud! Groovy scripting is only supported on Jira Server. You must have been looking at the documentation for JMWE for Jira Server.

Unfortunately, there is currently no way to achieve what you are trying to do with JMWE for Jira Cloud.

Deleted user May 14, 2018

Uggh, I didn't even think of cloud vs server as I was looking for solutions. I guess I'll have to have users manually create confluence page and link it to jira issue. Thanks for setting me straight on this.

Regards,

Jay

Like jason_arcala likes this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events