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

Automatically link Jira issue to Confluence Page using ScriptRunner

Dar Kronenblum
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 28, 2016

Hi

I'm trying to write script to be used as post function to link the issue to a specific confluence page.

I tried this script- it didn't return any error but didn't do much either.

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.bc.issue.issuelink.RemoteIssueLinkService
import com.atlassian.jira.bc.issue.link.RemoteIssueLinkService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.link.RemoteIssueLinkBuilder
 
class myWrapper {
    def doStuff() {
    def authContext = ComponentAccessor.getJiraAuthenticationContext()
  	Issue issue = ComponentAccessor.getIssueManager().getIssueObject("TEST-111")
    def linkBuilder = new RemoteIssueLinkBuilder()
    linkBuilder.issueId(issue.getId())
    linkBuilder.applicationName("Confluence- Test")
    linkBuilder.applicationType("com.atlassian.confluence")
    linkBuilder.relationship("Wiki Page")
    linkBuilder.title("test1")
    linkBuilder.url("http://conf:8090/display/TS/test1")
    def link = linkBuilder.build()
    def remoteIssueLinkService = ComponentManager.getComponentInstanceOfType(RemoteIssueLinkService.class)
    def validationResult = remoteIssueLinkService.validateCreate(authContext.getLoggedInUser(), link) 
	RemoteIssueLinkService.RemoteIssueLinkListResult links = remoteIssueLinkService.getRemoteIssueLinksForIssue(authContext.getLoggedInUser(), issue)
    
    }
}
(new myWrapper()).doStuff()

 

Versions:

Jira - 7.1.0

confluence -5.8.14

 

Thanks!!

 

Dar

 

3 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

6 votes
Answer accepted
Petar Petrov (Appfire)
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 28, 2016

You need to actually call remoteIssueLinkService.create() with the result from validateCreate, like this:

def remoteIssueLinkService = ComponentManager.getComponentInstanceOfType(RemoteIssueLinkService.class)
    def validationResult = remoteIssueLinkService.validateCreate(authContext.getLoggedInUser(), link)
	//new code start
	def createResult = remoteIssueLinkService.create(authContext.getLoggedInUser(), validationResult)
	//new code end
    RemoteIssueLinkService.RemoteIssueLinkListResult links = remoteIssueLinkService.getRemoteIssueLinksForIssue(authContext.getLoggedInUser(), issue)
Dar Kronenblum
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 28, 2016

Thank a lot Peter!!

works great!!

Petar Petrov (Appfire)
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 29, 2016

Cool, I'm glad I was able to help! Please accept the answer so the question is closed.

Tukaram Bhukya November 2, 2016

please some one can help me how to retrieve current issue id in above code instead of " Issue issue = ComponentAccessor.getIssueManager().getIssueObject("TEST-111")" .

I mean don't want to hard code value like "TEST-111"

Matt June 22, 2017

You can use something like "ComponentAccessor.getIssueManager().getIssueObject(issue.key)"

0 votes
Darin Hafer April 24, 2019

See my post above where I added the call to set the "globalId". This solved the issue for me.

0 votes
Diego Rodriguez October 31, 2018

Hi, I'm trying a little different version of this code in order to create a link to a Confluence page when a subtask is created. This code is located as a post-function in the Create transition of the subtask.

import com.atlassian.jira.bc.issue.issuelink.RemoteIssueLinkService
import com.atlassian.jira.bc.issue.link.RemoteIssueLinkService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.link.RemoteIssueLinkBuilder
import org.apache.log4j.Category

def Category log = Category.getInstance("com.onresolve.jira.groovy.*")
log.setLevel(org.apache.log4j.Level.INFO);

def urlPage = issue.getParentObject().getKey() + ": " + issue.getParentObject().getSummary()
urlPage = urlPage.replaceAll(":", "%3A")
urlPage = urlPage.replaceAll(" ", "+")

def authContext = ComponentAccessor.getJiraAuthenticationContext()
def linkBuilder = new RemoteIssueLinkBuilder()
linkBuilder.issueId(issue.getId())
linkBuilder.applicationName("Confluence")
linkBuilder.applicationType("com.atlassian.confluence")
linkBuilder.relationship("Wiki Page")
linkBuilder.title(issue.getParentObject().getKey() + ": " + issue.getParentObject().getSummary())
linkBuilder.url("http://host:8090/display/GDLD/" + urlPage)
def link = linkBuilder.build()

log.info("ID issue: " + issue.getId())
log.info("Texto URL: " + "http://host:8090/display/GDLD/" + urlPage)

def remoteIssueLinkService = ComponentAccessor.getComponent(RemoteIssueLinkService.class)
def validationResult = remoteIssueLinkService.validateCreate(ComponentAccessor.getUserManager().getUserByName("admin"), link)
if(validationResult.isValid()){
log.info("VALIDO")
def createResult = remoteIssueLinkService.create(ComponentAccessor.getUserManager().getUserByName("admin"), validationResult)
RemoteIssueLinkService.RemoteIssueLinkListResult links = remoteIssueLinkService.getRemoteIssueLinksForIssue(ComponentAccessor.getUserManager().getUserByName("admin"), issue)
}else{
log.info("NO VALIDO")
log.info(validationResult.getErrorCollection())
}

As a result I get the link created in Jira, but it shows the Couldn't load message. In Confluence Page I can't see the link back to jira issue.

LinkCouldn'tLoad.png

Any help??

Thanks!!

Abinash Satapathy April 1, 2019

Hello @Diego Rodriguez ,

I am facing similar issue like you.

Did you find out any Solutions for this issue.

 

Thanks

Abi

Darin Hafer April 24, 2019

I also have this issue, while there is an error, the link still works:

Screen Shot 2019-04-24 at 9.46.36 AM.jpg

 

Haven't solved it yet.

Darin Hafer April 24, 2019

linkbuilder-error.png

I do see this error in the log file, but not sure how to solve it. Maybe this is related to the 'Failed to load' message.

Darin Hafer April 24, 2019

I solved this in my instance of 7.9.2.

 

All I did was add a call to set the globalId of the Confluence page:

 

linkBuilder.globalId("appId=61cf4472-a82a-3304-a1fc-ede103860d55&pageId=121608199")

 

I get the Confluence AppId from calling this API:

 https://jira.americas.nwea.pvt/rest/applinks/1.0/applicationlink

 

I get the pageId from the page in Confluence.

Like # people like this
TAGS
AUG Leaders

Atlassian Community Events