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())
}
}
}
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}"
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.Loggerimport com.atlassian.applinks.api.ApplicationLinkRequestFactory
import com.atlassian.applinks.spi.auth.AutoConfiguringAuthenticatorProviderPluginModuleimport groovy.json.JsonSlurper
//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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You should add
import com.atlassian.sal.api.net.Request
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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 )…. :-)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
? 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...)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
Do you have any idea on how to retrieve remote links which are available only in 'mentioned-in' sections?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I think it.relationship contains that info...
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.