Confluence Server REST API Search with ScriptRunner

Paweł Jakimiak April 9, 2020

Hi,

I have a problem with searching pages in Confluence. I executing my request from Jira by scriptrunner:

private checkIfPageExist(String pageName, confluenceLink)
{
def status = false;
log.error "checkIfPageExist pageName: "+pageName
try{

if(pageName.contains(" "))
{
pageName = pageName.replace(" ","%20")
}

def authenticatedRequestFactory = confluenceLink.createImpersonatingAuthenticatedRequestFactory()
authenticatedRequestFactory
.createRequest(Request.MethodType.GET, "/rest/api/content/search?cql=title=%22${pageName}%22")
.addHeader("Content-Type", "application/json")
.execute(new ResponseHandler<Response>() {
@Override
void handle(Response response) throws ResponseException {
if(response.statusCode != HttpURLConnection.HTTP_OK) {
throw new Exception(response.getResponseBodyAsString())
}
else {
log.error "response: "+response.getResponseBodyAsString()
def resp = new JsonSlurper().parseText(response.responseBodyAsString)
if(resp.results.title[0].toString().toLowerCase() == pageName.toLowerCase())
{
status = true;
}
}
}
})
}
catch(Exception ex)
{
log.error "Error: ${ex}"
}
finally
{
return status
}
}

 

I have created page in Confluence with name "Test". In my problematic case this function trying to find page with name "test". Function returns false because search engine does not recognize the size of characters.
But another function in my code is resposible for creating page and creating fail because Confluence returns information that page with the same name exist.

How can I provide recognizing size of characters in searching via API in Confluence?

2 answers

Suggest an answer

Log in or Sign up to answer
0 votes
Greg Bailey May 10, 2021

I to am attempt to connect to Confluence Server from our Jira Server using this approach.

However, I am getting a 'nonce used' error when attempt to execute my confluence search.

The error stack is below. 

Per https://confluence.atlassian.com/jirakb/error-was-nonce_used-with-parameters-oauth_problem-nonce_used-755139671.html it appears we can no longer use this approach to execute calls against Confluence from Jira.  Is this known?  Would anyone happen to know a workaround if it even exists?  Obviously this approach is preferred versus having to make REST calls and hard code credentials or even an auth token into the script. And to head off the likely question I've tried with my application link set to both 'oauth' and 'oauth with impersonation', same results...

java.lang.Exception: oauth_problem=nonce_used at Script1150$1.handle(Script1150.groovy:42) at com.atlassian.applinks.oauth.auth.OAuthResponseHandler.handle(OAuthResponseHandler.java:48) at com.atlassian.plugins.rest.module.jersey.JerseyRequest$1.handle(JerseyRequest.java:115) at com.atlassian.plugins.rest.module.jersey.JerseyRequest$1.handle(JerseyRequest.java:113) at com.atlassian.plugins.rest.module.jersey.JerseyRequest$2.handle(JerseyRequest.java:134) at com.atlassian.sal.core.net.HttpClientRequest.executeAndReturn(HttpClientRequest.java:102) at com.atlassian.plugins.rest.module.jersey.JerseyRequest.executeAndReturn(JerseyRequest.java:131) at com.atlassian.plugins.rest.module.jersey.JerseyRequest.execute(JerseyRequest.java:113) at com.atlassian.applinks.core.auth.ApplicationLinkRequestAdaptor.execute(ApplicationLinkRequestAdaptor.java:50) at com.atlassian.applinks.oauth.auth.OAuthResponseHandler.handle(OAuthResponseHandler.java:46) at com.atlassian.plugins.rest.module.jersey.JerseyRequest$1.handle(JerseyRequest.java:115) at com.atlassian.plugins.rest.module.jersey.JerseyRequest$1.handle(JerseyRequest.java:113) at com.atlassian.plugins.rest.module.jersey.JerseyRequest$2.handle(JerseyRequest.java:134) at com.atlassian.sal.core.net.HttpClientRequest.executeAndReturn(HttpClientRequest.java:102) at com.atlassian.plugins.rest.module.jersey.JerseyRequest.executeAndReturn(JerseyRequest.java:131) at com.atlassian.plugins.rest.module.jersey.JerseyRequest.execute(JerseyRequest.java:113) at com.atlassian.applinks.core.auth.ApplicationLinkRequestAdaptor.execute(ApplicationLinkRequestAdaptor.java:50) at com.atlassian.applinks.oauth.auth.OAuthRequest.execute(OAuthRequest.java:71) at com.atlassian.sal.api.net.Request$execute$0.call(Unknown Source) at Script1150.findConfluencePages(Script1150.groovy:38) at Script1150.run(Script1150.groovy:14)

0 votes
Jonny Carter
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.
December 10, 2020

Hey, @Paweł Jakimiak ! So, I think the problem is caused because your are searching for titles using the EQUALS operator, which is case sensitive. If you want to do a case insensitive search, you should use the CONTAINS operator.

So, instead of

"/rest/api/content/search?cql=title=%22${pageName}%22"

use a URL like

"/rest/api/content/search?cql=title~%22${pageName}%22"

That will find pages regardless of case.

That said, your check on this line:

if(resp.results.title[0].toString().toLowerCase() == pageName.toLowerCase())

may need a little work if you use the CONTAINS (~) operator. That will only check that the first result in your search results matches your pageName variable. That may not be the case if you use the CONTAINS operator, since that will find more results. You might want something more like:

if (resp.results.any { it.toString().toLowerCase() == pageName.toLowerCase() })

to see if any of the pages returned have the same name, regardless of case, as using the CONTAINS operator is likely to yield many more results than EQUALS.

For more information, check out the Operators reference in the CQL docs.

TAGS
AUG Leaders

Atlassian Community Events