Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to use Confluence REST API GET request using Jira script runner ApplicationLinkRequestFactory

hojunhwang March 17, 2023

Hi I am trying to call GET request using Jira Scriptruner's ApplicationLinkRequestFactory.

But I have no idea how to get confluence page template id.

Is there a certain way?

 

def request = applicationLinkRequestFactory.createRequest(Request.MethodType.GET, "/rest/api/content/").addHeader("Content-Type", "application/json")
request.execute()
What should I used instead of "/rest/api/content/"

1 answer

1 accepted

1 vote
Answer accepted
Oday Rafeh
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.
March 17, 2023

Hi @hojunhwang 

To get the Confluence page template ID using ScriptRunner's ApplicationLinkRequestFactory, you need to make a GET request to the Confluence REST API's

/rest/api/template
You can use this endpoint to search for templates by space key or title.

Here's a script that demonstrates how to get a Confluence page template ID using ApplicationLinkRequestFactory:

import com.atlassian.applinks.api.ApplicationLink
import com.atlassian.applinks.api.ApplicationLinkService
import com.atlassian.applinks.api.application.confluence.ConfluenceApplicationType
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.sal.api.net.Request
import com.atlassian.sal.api.net.Response

def applicationLinkService = ComponentLocator.getComponent(ApplicationLinkService)
ApplicationLink confluenceAppLink = applicationLinkService.getPrimaryApplicationLink(ConfluenceApplicationType)

def templateTitle = "Your Template Title"
def spaceKey = "SPACE_KEY"

if (confluenceAppLink) {
def requestPath = "/rest/api/template?spaceKey=${spaceKey}&title=${templateTitle}"
def request = confluenceAppLink.createAuthenticatedRequestFactory().createRequest(Request.MethodType.GET, requestPath)
request.setHeader("Content-Type", "application/json")

def response = request.executeAndReturn(new Response.Handler<String>() {
@Override
String handle(Response response) throws IOException {
response.getEntity(String)
}
})

if (response) {
def jsonResponse = new groovy.json.JsonSlurper().parseText(response)
if (jsonResponse.results) {
def templateId = jsonResponse.results[0].id
log.debug("Template ID: ${templateId}")
return templateId
} else {
log.warn("No template found with the given title and space key.")
}
} else {
log.warn("Error: Unable to fetch Confluence templates.")
}
} else {
log.warn("Error: No Confluence application link found.")
}
 Make sure to replace
"Your Template Title" 

with the title of the Confluence template you're looking for and

"SPACE_KEY"

with the space key containing the template.

This script will search for a template with the specified title and space key and return the template ID if found. If it doesn't find the template, it will log a warning message.

Note that the user account used for the application link needs to have the appropriate permissions in Confluence to access the templates.

hojunhwang March 17, 2023

Hi @Oday Rafeh 

Thanks for the great answer.

If I want default template's id, how should I do? because in this case there is no space id that I can provide. I want to get id of software project space

Picture1.png

Like Oday Rafeh likes this
Oday Rafeh
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.
March 17, 2023

If you want to get the ID of a default template in a software project space, you can use the following script:

import com.atlassian.applinks.api.ApplicationLink
import com.atlassian.applinks.api.ApplicationLinkService
import com.atlassian.applinks.api.application.confluence.ConfluenceApplicationType
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.sal.api.net.Request
import com.atlassian.sal.api.net.Response

def applicationLinkService = ComponentLocator.getComponent(ApplicationLinkService)
ApplicationLink confluenceAppLink = applicationLinkService.getPrimaryApplicationLink(ConfluenceApplicationType)

def templateTitle = "Your Template Title"

if (confluenceAppLink) {
def requestPath = "/rest/api/template/search?title=${templateTitle}"
def request = confluenceAppLink.createAuthenticatedRequestFactory().createRequest(Request.MethodType.GET, requestPath)
request.setHeader("Content-Type", "application/json")

def response = request.executeAndReturn(new Response.Handler<String>() {
@Override
String handle(Response response) throws IOException {
response.getEntity(String)
}
})

if (response) {
def jsonResponse = new groovy.json.JsonSlurper().parseText(response)
if (jsonResponse.results) {
def templateId = jsonResponse.results[0].id
log.debug("Template ID: ${templateId}")
return templateId
} else {
log.warn("No template found with the given title.")
}
} else {
log.warn("Error: Unable to fetch Confluence templates.")
}
} else {
log.warn("Error: No Confluence application link found.")
}

 Make sure to replace "Your Template Title" with the title of the Confluence template you're looking for.

In this script, I removed the spaceKey parameter and used the

 /rest/api/template/search 

endpoint instead of

 /rest/api/template. 

This endpoint allows you to search for templates without specifying a space key. It will search for global templates as well as space-specific ones.

Keep in mind that this script searches for all available templates with the specified title. If multiple templates share the same title, it will return the ID of the first one in the results. Ensure that your search term is specific enough to return the desired template.

Alfira Merinova February 12, 2024

Hi @Oday Rafeh ,

Thank you for sharing this ScriptRunner script. I tried to use it without any changes (except indentations), but see the following error:

"unable to resolve class Response.Handler<java.lang.String>"

ScriptRunner Response Handler error.PNG

Could you please help me to fix it?

 

Jira Server v9.4.14
ScriptRunner v.8.1.0
Like Oday Rafeh likes this
Oday Rafeh
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.
February 14, 2024

To fix the error in your ScriptRunner script, you can adjust the way you handle the response from the Confluence API. Instead of using   Response.Handler, you can directly get the response as a string. check this simplified version of the script:

 

import com.atlassian.applinks.api.ApplicationLink
import com.atlassian.applinks.api.ApplicationLinkService
import com.atlassian.applinks.api.application.confluence.ConfluenceApplicationType
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.sal.api.net.Request
import groovy.json.JsonSlurper

def applicationLinkService = ComponentLocator.getComponent(ApplicationLinkService)
ApplicationLink confluenceAppLink = applicationLinkService.getPrimaryApplicationLink(ConfluenceApplicationType)

def templateTitle = "Your Template Title" // Change this to your actual template title

if (confluenceAppLink) {
def requestPath = "/rest/api/template/search?title=${URLEncoder.encode(templateTitle, 'UTF-8')}"
def request = confluenceAppLink.createAuthenticatedRequestFactory().createRequest(Request.MethodType.GET, requestPath)
request.setHeader("Content-Type", "application/json")

try {
def response = request.execute()
def jsonResponse = new JsonSlurper().parseText(response)

if (jsonResponse.results) {
def templateId = jsonResponse.results[0].id
println("Template ID: ${templateId}")
return templateId
} else {
println("No template found with the given title.")
}
} catch (Exception e) {
println("Error: Unable to fetch Confluence templates. ${e.message}")
}
} else {
println("Error: No Confluence application link found.")
}

Make sure to replace "Your Template Title" it with the actual title of the template you're looking for and let me know please if this helped 

Like Alfira Merinova likes this
Alfira Merinova February 14, 2024

Hi @Oday Rafeh ,

Thank you for the solution allowing to parse response directly without handling its headers.

It works.

Like Oday Rafeh likes this
Oday Rafeh
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.
February 14, 2024

I'm glad to hear that @Alfira Merinova and happy to help if anything is needed let me know. 

Like Alfira Merinova likes this

Suggest an answer

Log in or Sign up to answer