Creating a Confluence page from JIRA Post-Function, link back doesn't create

Matt June 19, 2017

I've got a script that creates a page in confluence and links to the JIRA issue, but the link in JIRA doesn't create. Is there something I'm missing in my script? The application link works fine and I can create the link manually, but it would be preferable for the link to auto-create.

Any thoughts?

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

// 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.key + " " + issue.summary
//def pageBody = ${issue.description}
def params = [
    type: "page",
    //id: "11599873",
    title: pageTitle,
    space: [
        key: "PPM" // set the space key - or calculate it from the project or something
    ],
        ancestors: [
        [
           type: "page",
           id: "10977287",
       ]
     ],
    body: [
        storage: [ value: writer.toString(), representation: "storage" ]
		//storage: [ value: pageBody, 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())
        }
    }
})

I'm able to manually create the link in JIRA to the created page in Confluence, but I really need the links to be created on both sides when the page is created. What am I doing wrong?

I've even removed and reestablished the application link, and I have the remote API enabled in Confluence, made sure the servers are on the same time...I'm at a loss as to why the link isn't being created in JIRA.

 

4 answers

1 vote
Alvin Chan [Ease Solutions] March 23, 2018

Hi All,

Not sure if this thread is still alive, but while troubleshooting this topic with my colleague recently, we found that the JIRA instance name and server id has to be included, mentioned here:

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

 

My colleague managed to get this script working after including that part. Hope this helps :)

Roulliz July 4, 2019

Hi i have the exact same issue. However, i am not quite sure what do i put for:

 

ac:parameter' ('ac:name': "server", "System JIRA")

'ac:parameter' ('ac:name': "serverId", "YOUR-SERVER-ID")

0 votes
Thanos Batagiannis _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.
September 15, 2017

Hey guys, 

Are there any special characters in the title that you create for the confluence page ? 

I just found this Cannot link a JIRA issue to a Confluence page (over SSL) when the Page Title contains Comma, Colon, and/or other Special Characters, so try to create a page with a title witout any specia characters ...

regards, Thanos

Matt September 15, 2017

I will take a look, but I really tried to keep the script as simple as possible. In my code above, it's only adding a [space] between the issue key and issue summary. Perhaps with there being a "-" in the issue key, that is causing the problem (ex: PPM-215).
I've got to build this out in a test environment before I can do any more changes. I'll post back once I've found anything.

Matt September 15, 2017

Actually, looking further at the issue linked above, I don't know that it applies. I can manually link issues without a problem, it's just the script that's giving me issues.

Thanos Batagiannis _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.
September 15, 2017

Hey Matt,

So what is the title of the page when you manually link issues ?

I mean in the script the tile of the page is problably something like 

TEST-1 A Test Issue

So maybe the "-" cause the issue ....

Matt September 15, 2017

It's possible. Like I said, I need to build it out in a different env and I'll let you know.

Thanos Batagiannis _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.
September 15, 2017

cool, I will wait :)

Alejandro Suárez - TecnoFor
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
September 15, 2017

Hi Thanos, I just tried what you suggested and it dind't work.

Here is the code i tried, as you can see I use only plain text in the title of the page.

 

package examples.docs

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
//import com.atlassian.jira.component.ComponentAccessor
//import com.atlassian.jira.issue.CustomFieldManager
//import com.atlassian.jira.issue.fields.CustomField
//import com.atlassian.jira.ComponentManager
//import com.atlassian.jira.issue.IssueManager


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)
//'ac:parameter' ('ac:name': "title", issue.key)
//'ac:parameter' ('ac:name': "columns", "summary,created,updated,assignee,reporter,priority,status,resolution,quiero,para que...")
//'ac:parameter' ('ac:name': "jqlQuery", "issuekey ="+ issue.key)

}

// add more paragraphs etc

xml.p ("Página para la discusión de la Historia de usuario mencionada")

 

// 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 = "Test" //There is not  any other page in confluence whit this name.

def params = [
type: "page",
title: pageTitle,
space: [
key: issue.projectObject.key // set the space key - or calculate it from the project or something
],
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())
}
}
})

0 votes
Alejandro Suárez - TecnoFor
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
September 12, 2017

I have the same exact problem plus I'm not capable of put the issue key in the Title of the page. Any thoughts?

Matt September 15, 2017

You can define the Title like so:

def pageTitle = issue.key + " " + issue.summary

However, there might be an issue with the "-" in the issue key. There are a few varibles you can use. 

0 votes
Alvin Chan [Ease Solutions] July 20, 2017

Hi Matt,

I got the exact same issue as you did with a similar script, my JIRA version is 7.3.8 and Confluence version is 6.2.3. Did you figure anything out yet?

I checked that my JIRA logs did not have any errors, but on Confluence, I saw lines like the following in the logs:

===============================================

Caused by: java.lang.NullPointerException
at com.atlassian.confluence.plugins.jira.links.JiraRemoteIssueLinkManager$JiraIssueLinkMacro.hashCode(JiraRemoteIssueLinkManager.java:205)
at java.util.HashMap.hash(HashMap.java:338)
at java.util.HashMap.put(HashMap.java:611)
at java.util.HashSet.add(HashSet.java:219)
at com.atlassian.confluence.plugins.jira.links.JiraRemoteIssueLinkManager.getRemoteLinkMacros(JiraRemoteIssueLinkManager.java:88)
at com.atlassian.confluence.plugins.jira.links.JiraRemoteIssueLinkManager.createIssueLinksForEmbeddedMacros(JiraRemoteIssueLinkManager.java:62)
at com.atlassian.confluence.plugins.jira.ConfluenceEventListener.createJiraRemoteLinksForNewPage(ConfluenceEventListener.java:162)
===============================================

I would really be interested to know the solution here as well, if any :)

 

 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events