Creating and retrieving JIRA remote issue links

flo November 6, 2012

I am trying to write a transition post function for JIRA v5.x. using the groovyscriptrunner

It should check if there is already a Confluence page linked with the issue, and create and link the page if not. I am developing this using the a groovy script and the scriptrunner plugin which can access the JAVA API.

While it was fairly easy to create the confluence page, I am struggling with the remote issue links.

  • How can I create a new remote issue link?
  • How can I determine if a link already exists for an issue?

This is how I try to create the link:

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.bc.issue.link.RemoteIssueLinkService
import com.atlassian.jira.issue.link.RemoteIssueLinkBuilder

//I use a wrapper class for the moment so I can run via scriptrunner and debug it in IDEA
class myWrapper {

    def doStuff() {

        //get the issue, this would already be available in an post action
        def issueService = ComponentManager.getInstance().getIssueService();
        def authContext = ComponentManager.getInstance().getJiraAuthenticationContext()

        def issueResult = issueService.getIssue(authContext.getUser(), "DEV-1");
        def issue = issueResult.getIssue()

        //build link
        def linkBuilder = new RemoteIssueLinkBuilder()
        linkBuilder.issueId(issue.id)
        linkBuilder.applicationName("myconf")
        linkBuilder.applicationType("com.atlassian.confluence")
        linkBuilder.relationship("Wiki Page")
        linkBuilder.title("testpage")
        linkBuilder.url("http://localhost:8090/display/LIN/testpage")
        linkBuilder.build()


        def validationResult = RemoteIssueLinkService.validateCreate(authContext.getUser(), linkBuilder)



    }
}

(new myWrapper()).doStuff()

When I run the code I get the following exception:

javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: static com.atlassian.jira.bc.issue.link.RemoteIssueLinkService.validateCreate() is applicable for argument types: (com.atlassian.crowd.embedded.ofbiz.OfBizUser, com.atlassian.jira.issue.link.RemoteIssueLinkBuilder) values: [admin:1, com.atlassian.jira.issue.link.RemoteIssueLinkBuilder@180ca9]

For me it looks like I do not get the *RemoteIssueLinkService* correctly,but I do not know what I have to do to fix that.

7 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

1 vote
Answer accepted
JamieA
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 6, 2012

You have the answer in the error. It's not a static method, so you need to get an instance of the class first:

def remoteIssueLinkService = ComponentManager.getComponentInstanceOfType(RemoteIssueLinkService.class)

remoteIssueLinkService.validateCreate(...)

flo November 8, 2012

Thanks works like a charm...

I am fairly new to this groovy and java stuff, so this was not obvious for me

JamieA
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 8, 2012

No worries. The rest of the code looked fairly competent so I assumed you weren't new to it. Otherwise I may have been more touchy feely.

Sachin Dhamale September 15, 2014

Hi All; With this code i am able to create link between jira issue and confluence page it show link on jira issue but it does not show jira link on confluence page (count of jira issue which are connected to page) so is there any changes required for this?

1 vote
JamieA
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.
October 4, 2014
1 vote
Sachin Dhamale September 15, 2014

hi All;

PluginAccessor pluginAccessor = ComponentAccessor.getPluginAccessor();
  
            //get the issue, this would already be available in an post action
            IssueService issueService = ComponentAccessor.getIssueService();
            JiraAuthenticationContext authContext = ComponentAccessor.getJiraAuthenticationContext();

            IssueResult issueResult = issueService.getIssue(authContext.getLoggedInUser(), "TEST-1");
            Issue issue = issueResult.getIssue();

            //build link
            RemoteIssueLinkBuilder linkBuilder = new RemoteIssueLinkBuilder();
            
            linkBuilder.issueId(issue.getId());
            linkBuilder.applicationName("Confluence");
            linkBuilder.applicationType("com.atlassian.confluence");
            linkBuilder.relationship("Wiki Page");
            linkBuilder.title("Welcome to Confluence");
            linkBuilder.url("http://localhost:8099/confluence/display/ds/Welcome+to+Confluence");
            linkBuilder.build();
            
            RemoteIssueLink link = linkBuilder.build();
            RemoteIssueLinkService remoteIssueLinkService = ComponentAccessor.getComponent(RemoteIssueLinkService.class);
            CreateValidationResult createValidateResult = remoteIssueLinkService.validateCreate(authContext.getUser(),link); //.validateCreate(authContext.getLoggedInUser(), link);
            remoteIssueLinkService.create(authContext.getLoggedInUser(), createValidateResult);

 

With this code i am able to create link between jira issue and confluence page it show link on jira issue but it does not show jira link on confluence page (count of jira issue which are connected to page) so is there any changes required for this?

 

0 votes
Arianna Fabbri November 24, 2014

Hi, how did you create Confluence page from Jira script? Thank you

0 votes
Sachin Dhamale October 5, 2014

Thanks Jamie its working

0 votes
JamieA
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 24, 2014

You need to call the method on the service, ie:

remoteIssueLinkService.createRemoteIssueLink(...)

> by what the hash tag signifies in your comment

Nothing, it's just what goes in the clipboard when I do "copy reference" from my ide.

Bryan Karsh
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 24, 2014

Thanks Jaimie,

I think my main issue was due to IDE being configured incorrectly. Anyway, I got it to work this way:

import com.atlassian.jira.issue.link.RemoteIssueLinkBuilder
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.link.RemoteIssueLinkManager
import org.apache.log4j.Category

log.setLevel(org.apache.log4j.Level.DEBUG)
log.debug "debug statements"

def authContext = ComponentAccessor.getJiraAuthenticationContext()

def linkBuilder = new RemoteIssueLinkBuilder()


// Setting variables as a test -- these will eventuall represent fields gathered from DB query for bulk upload later.

// need actual jira appId / issueID in globalId if you want to make a remote link as opposed to a weblink
def globalId = 'appId=350eeaaa-0de6-36d0-bc1d-95761684b830&issueId=275829'    
def issueId = 323532        // issue id of source issue
def linkRel = 'is related to'
def linkTitle = 'FOOBAR-35'  //pkey of destination issue
def linkSummary = 'test'   // summary of destination issue
def linkUrl = 'https://jira.example.com/support/browse/FOOBAR-35' // destination url
def appType = 'com.atlassian.jira' // type of application you are linking to
def appName = 'otherJira'  // name of application you are linking to


// Let's populate builder

linkBuilder.globalId(globalId)
linkBuilder.issueId(issueId.toLong())
linkBuilder.relationship(linkRel)
linkBuilder.title(linkTitle)
linkBuilder.summary(linkSummary)
linkBuilder.url(linkUrl)
linkBuilder.applicationType(appType)
linkBuilder.applicationName(appName)


// let's create link object

def link = linkBuilder.build()

// let's add link via RemoteIssueLinkManager

ComponentAccessor.getComponent(RemoteIssueLinkManager).createRemoteIssueLink(link, authContext.getUser().directoryUser);

-- for whatever reason, I couldn't get remoteIssueLinkService to work. Almost certainly user error on my part. But thanks all the same for your advice -- it led me to a solution. :)

Bryan Karsh
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.
July 2, 2014

Hi Jamie -- silly question. How do I make the link reciprocal? I could have sworn I had it working earlier.. but on second thought, it may have been when I was testing rest api.

Right now, the above code create a good remote link, but it doesn't appear on the target issue.

JamieA
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.
July 3, 2014

I'm not really sure how that works. Do you get a reciprocal link when you do it in the UI?

Sachin Dhamale September 23, 2014

Hi Jamie; with above code i am able to create link but it give Failed To Load with created link so that it is not created link from confluence side can u please help me on this

0 votes
Phillip Ponzer [Cprime]
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.
April 1, 2013

FYI, this code doesn't work out-of-the-box for JIRA 5+ (from what I can tell).

Here's what I had to have just to get it to compile:

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.bc.issue.issuelink.RemoteIssueLinkService
import com.atlassian.jira.issue.link.RemoteIssueLinkBuilder

//I use a wrapper class for the moment so I can run via scriptrunner and debug it in IDEA
class myWrapper {
    def doStuff() {

        //get the issue, this would already be available in an post action
        def issueService = ComponentManager.getInstance().getIssueService();
        def authContext = ComponentManager.getInstance().getJiraAuthenticationContext()

        def issueResult = issueService.getIssue(authContext.getUser(), "MYPROJECT-5");
        def issue = issueResult.getIssue()

        //build link
        def linkBuilder = new RemoteIssueLinkBuilder()
        linkBuilder.issueId(issue.id)
        linkBuilder.relationship("Wiki Page")
        linkBuilder.title("testpage")
        linkBuilder.url("http://www.mypage.com")

 	def link = linkBuilder.build()
 	def remoteIssueLinkService = ComponentManager.getComponentInstanceOfType(RemoteIssueLinkService.class) 
        def validationResult = remoteIssueLinkService.validateCreate(authContext.getUser(), link)
    }
}

(new myWrapper()).doStuff()

But after refreshing the page for the issue "MYPROJECT-5" doesn't show that any links were created. Any suggestions?

JamieA
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.
April 1, 2013

Also check logs for errors.

JamieA
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.
April 1, 2013

Not sure what you mean by jira 5+, in the original question you said you were using 5.x. What are you upgrading from and to? I;m not aware of anything that you are using changing in the 5.x range.

Phillip Ponzer [Cprime]
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.
April 1, 2013

Specifically, this line:

import com.atlassian.jira.bc.issue.link.RemoteIssueLinkService

Causes the error:

unable to resolve class com.atlassian.jira.bc.issue.link.RemoteIssueLinkService

JamieA
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.
April 1, 2013

What version? That class hasn't gone anywhere. I'll check it out tomorrow.

jim blizzard November 1, 2013

did this ever get resolved? I 'm having he same issue

JamieA
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 1, 2013

Works for me. What jira version?

Bryan Karsh
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 24, 2014

Hi Jamie,

I am running Script Runner 3 beta on Jira 6.2.5 I can't get this to work:

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

log.setLevel(org.apache.log4j.Level.DEBUG)
log.debug "debug statements"

def authContext = ComponentAccessor.getJiraAuthenticationContext()

IssueManager issueManager = ComponentAccessor.getIssueManager()

String myissue = "TO-10922"  //for testing in console

Issue issue = issueManager.getIssueObject(myissue)

def linkBuilder = new RemoteIssueLinkBuilder()
linkBuilder.issueId(issue.id)
linkBuilder.relationship("Wiki Page")
linkBuilder.title("testpage")
linkBuilder.url("http://www.mypage.com")

def link = linkBuilder.build()

log.debug "Link dump: " + link.dump() + "\r\n"

def remoteIssueLinkService = ComponentManager.getComponentInstanceOfType(RemoteIssueLinkService.class)
log.debug "remoteIssueLinkService dump: " + remoteIssueLinkService.dump() + "\r\n"
//remoteIssueLinkService.validateCreate(authContext.getUser(), link).dump()
log.debug remoteIssueLinkService.validateCreate(authContext.getUser(), link).dump()

Bryan Karsh
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 24, 2014

having issues posting stuff.. hmm... anyway, Any tips The remoteIssueLinkService changed with jira 6.2 I think. Can't figure out how to get it to work.

Bryan Karsh
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 24, 2014
def remoteIssueLinkService = ComponentManager.getComponentInstanceOfType(RemoteIssueLinkService.class)

log.debug "remoteIssueLinkService dump: " + remoteIssueLinkService.dump() + "\r\n"

remoteIssueLinkService.validateCreate(authContext.getUser(), link).dump()

JamieA
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 24, 2014

you're already using the builder, so just call createRemoteIssueLink(link, user).

JamieA
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 24, 2014

There's no validateCreate... you need to use com.atlassian.jira.issue.link.RemoteIssueLinkBuilder to create a RemoteIssueLink to pass to com.atlassian.jira.issue.link.RemoteIssueLinkManager#createRemoteIssueLink(com.atlassian.jira.issue.link.RemoteIssueLink, com.atlassian.crowd.embedded.api.User)

Bryan Karsh
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 24, 2014

Also -- confused by what the hash tag signifies in your comment:

com.atlassian.jira.issue.link.RemoteIssueLinkManager#createRemoteIssueLink(com.atlassian.jira.issue.link.RemoteIssueLink, com.atlassian.crowd.embedded.api.User)

I can't figure out how to access the createRemoteIssueLink. How do access that method?

Bryan Karsh
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 24, 2014

Hi Jamie. Sorry -- but still having problems groking in:

def linkBuilder = new RemoteIssueLinkBuilder()
linkBuilder.issueId(issue.id)
linkBuilder.relationship("Wiki Page")
linkBuilder.title("testpage")
linkBuilder.url("http://www.mypage.com")
 
def link = linkBuilder.build()

createRemoteIssueLink(link)

Gives me this error:

No signature of method: org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.createRemoteIssueLink() is applicable for argument types: (com.atlassian.jira.issue.link.RemoteIssueLink) values: [com.atlassian.jira.issue.link.RemoteIssueLink@25029354]

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
AUG Leaders

Atlassian Community Events