How to get page name and url of the confluence page which is linked to an JIRA issue in script runne
Asked by nallu:

Hi , I am trying to write script field which will get page name and page url of the confluence page which is linked to an issue (RemoteIssueLinks). Can you please help me on how to find it through script runner - groovy code.

I have tried tried remote link service. But, There are no methods to get the URL and page name. I am able to get only pageId.

Here is my code,

```
def hasAnyRemoteConfluenceLink(ApplicationUser user, Issue issue, Logger log = null) {
def applicationLinkService = ComponentAccessor.getComponent(ApplicationLinkService)
def remoteIssueLinkService = ComponentAccessor.getComponent(RemoteIssueLinkService)
def conf_link = applicationLinkService.getApplicationLinks(ConfluenceApplicationType)?.first()
if (!conf_link) {
log?.error "No confluence application link found."
return false
}
//return remoteIssueLinkService.getRemoteIssueLinksForIssue(user, issue)?.remoteIssueLinks?.any{it.applicationType == RemoteIssueLink.APPLICATION_TYPE_CONFLUENCE}
def x = []
if(remoteIssueLinkService.getRemoteIssueLinksForIssue(user, issue)?.remoteIssueLinks?.any{it.applicationType == RemoteIssueLink.APPLICATION_TYPE_CONFLUENCE}) {
 remoteIssueLinkService.getRemoteIssueLinksForIssue(user, issue)?.remoteIssueLinks?.any{
   x.push(it.getApplicationName())
   x.push(it.getApplicationType())
   x.push(it.getGlobalId())
   x.push(it.getIconTitle())
   x.push(it.getIconUrl())
   x.push(it.getId())
 } 
 }
}
```


Answers:
author: Marc Minten _EVS_
body: Indeed, the page title is not stored in Jira, it is retrieved when necessary by Jira (this way you are sure always seeing the current page title!).

You can get the page title by executing a REST call to Confluence (over the application link) : must be something like (error and exception handling to be added :-))

```
def authenticatedRequestFactory = conf_link.createAuthenticatedRequestFactory()
String requestUrl = "rest/api/content/${yourPageId}"
def request = authenticatedRequestFactory.createRequest(Request.MethodType.GET, requestUrl)
request.addHeader("Content-Type", "application/json")
String myResult = request.execute()
Object page = new JsonSlurper().parseText(myResult)
String pageTitle = page["title"]
```

To get the URL to the page, just construct it as

```

String pageURL = "${confl_link.getRpcUrl()}/pages/viewpage.action?pageId=${yourPageId}"
```

comments: author: nallu
body: Hi Marc, Thanks a lot for the help. I have tried as you suggested. But I am getting error.

Here is the error,
> 2019-05-14 09:59:55,491 ERROR \[runner.ScriptFieldPreviewRunner\]: \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* 2019-05-14 09:59:55,491 ERROR \[runner.ScriptFieldPreviewRunner\]: Script field preview failed for field that has not yet been created groovy.lang.MissingPropertyException: No such property: Request for class: Script1097 at Script1097$_hasAnyRemoteConfluenceLink_closure2.doCall(Script1097.groovy:35) at Script1097.hasAnyRemoteConfluenceLink(Script1097.groovy:29) at Script1097.hasAnyRemoteConfluenceLink(Script1097.groovy) at Script1097.run(Script1097.groovy:47)

Here is my code,
> import com.atlassian.applinks.api.ApplicationLinkService  
> import com.atlassian.applinks.api.application.confluence.ConfluenceApplicationType  
> 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.RemoteIssueLink  
> import com.atlassian.jira.user.ApplicationUser  
> import org.apache.log4j.Logger
>
> import com.atlassian.applinks.api.ApplicationLinkRequestFactory  
> import com.atlassian.applinks.spi.auth.AutoConfiguringAuthenticatorProviderPluginModule
>
> import groovy.json.JsonSlurper
>
> <br />
>
> //static boolean  
>
> def hasAnyRemoteConfluenceLink(ApplicationUser user, Issue issue, Logger log = null) {  
> def applicationLinkService = ComponentAccessor.getComponent(ApplicationLinkService)  
> def remoteIssueLinkService = ComponentAccessor.getComponent(RemoteIssueLinkService)  
> def conf_link = applicationLinkService.getApplicationLinks(ConfluenceApplicationType)?.first()  
> if (!conf_link) {  
> log?.error "No confluence application link found."  
> return false  
> }  
> //return remoteIssueLinkService.getRemoteIssueLinksForIssue(user, issue)?.remoteIssueLinks?.any{it.applicationType == RemoteIssueLink.APPLICATION_TYPE_CONFLUENCE}  
> def x = \['test'\]  
> if(remoteIssueLinkService.getRemoteIssueLinksForIssue(user, issue)?.remoteIssueLinks?.any{it.applicationType == RemoteIssueLink.APPLICATION_TYPE_CONFLUENCE}) {  
> remoteIssueLinkService.getRemoteIssueLinksForIssue(user, issue)?.remoteIssueLinks?.any{  
> def url = it.getUrl().toString();  
> def yourPageId = url.split('pageId=')\[1\]  
>
> def authenticatedRequestFactory = conf_link.createAuthenticatedRequestFactory()  
> String requestUrl = "rest/api/content/${yourPageId}"  
> def request = authenticatedRequestFactory.createRequest(Request.MethodType.GET, requestUrl)  
> request.addHeader("Content-Type", "application/json")  
> String myResult = request.execute()  
> Object page = new JsonSlurper().parseText(myResult)  
> String pageTitle = page\["title"\]  
>
> x.push(pageTitle);  
> }  
> }  
> return x  
> }
>
> hasAnyRemoteConfluenceLink(ComponentAccessor.jiraAuthenticationContext?.loggedInUser, issue)

I think, I am not importing correct API. Please help. I am also attaching screenshot to show static type checking error that i am getting.

![Screen Shot 2019-05-14 at 7.36.00 PM.png](https://community.atlassian.com/t5/image/serverpage/image-id/54508i3B5B17A4256E5102/image-size/large?v=v2&px=999 "Screen Shot 2019-05-14 at 7.36.00 PM.png")


author: Marc Minten _EVS_
body: You should add

```
import com.atlassian.sal.api.net.Request
```


author: nallu
body: Wow, This works like a charm. Thanks so much, Marc.

We have just started enhancing JIRA using script runner in our organization for internal use. Can you please recommend me a good web-sites where i can learn this kind of stuff.

I am currently referring <https://scriptrunner.adaptavist.com/latest/jira/quickstart.html>. But It doesn't help me for scenarios (For a instance - This scenario. ) I have googled and read many blogs and nothing helped me to find a solution to this.

Again, Thanks a lot for your help, Marc. I have been struggling for more than 2 days to find a solution to this problem. Finally, Its done because of your help.


author: Marc Minten _EVS_
body: Great!

I went through the doc (your link above), browsed the Jira Dev API, and... browsed a lot the internet for examples.

The doc (your link above) brought me the solution for the problem you mentioned (<https://scriptrunner.adaptavist.com/latest/jira/interacting-with-confluence-from-jira.html> ).... :-)


author: nallu
body: Oh thanks Marc.

One more doubt, Actually, Two remote links are linked to an issue. But my script returning only one (first one).

Any issue with the looping?

```
def confluencePageNames = [];

if(remoteIssueLinkService.getRemoteIssueLinksForIssue(user, issue)?.remoteIssueLinks?.any{it.applicationType == RemoteIssueLink.APPLICATION_TYPE_CONFLUENCE}) {
remoteIssueLinkService.getRemoteIssueLinksForIssue(user, issue)?.remoteIssueLinks?.any{
def url = it.getUrl().toString();
def yourPageId = url.split('pageId=')[1]

def authenticatedRequestFactory = conf_link.createAuthenticatedRequestFactory()
String requestUrl = "rest/api/content/${yourPageId}"
def request = authenticatedRequestFactory.createRequest(Request.MethodType.GET, requestUrl)
request.addHeader("Content-Type", "application/json")
String myResult = request.execute()
Object page = new JsonSlurper().parseText(myResult)
String pageTitle = page["title"]

confluencePageNames.push(pageTitle);
} 
}
return confluencePageNames
```


author: Marc Minten _EVS_
body: ? I don't see a loop ?

I guess you need to use ".each" in stead of ".any" for your loop. ".any" returns a Boolean if a condition is met for any item in a collection, it does not loop...)


author: nallu
body: Ah..My mistake. I am sorry. I didn't notice .any. thanks again.


author: nallu
body: Hi Marc,

I need to tweak the script such a way that it will only fetch the remote links which is linked in 'mentioned-in' section.

But my current script returns remote links which are in 'mentioned-in' section as well as in 'Wiki Pages' section. Please see the screenshot to understand it better.

![Screen Shot 2019-05-15 at 4.37.49 PM.png](https://community.atlassian.com/t5/image/serverpage/image-id/54624iF0FDC4416E3E7928/image-size/large?v=v2&px=999 "Screen Shot 2019-05-15 at 4.37.49 PM.png")

Do you have any idea on how to retrieve remote links which are available only in 'mentioned-in' sections?


author: Marc Minten _EVS_
body: I think it.relationship contains that info...


Tags: addon-com.onresolve.jira.groovy.groovyrunner

jira-server