Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

calling CopyTree built in script in own scripts for script runner 5.5.7

Hello. We need regularly copy our Q&A pages from agent internal Q&A knowledge base in confluence to several spaces for clients. Because of lack documentation about using canned CopyTree built-in script in own scripts we write a script based on the internet found examples.

https://community.atlassian.com/t5/Confluence-questions/Script-runnenr-Calling-canned-CopyTree-script/qaq-p/265483

https://community.atlassian.com/t5/Answers-Developer-Questions/Set-page-author-after-ScriptRunner-CopyTree/qaq-p/504667

But after passing params to doValidate we get an error. It seems that something changed since those posts. Here is the script source code:

import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import com.onresolve.scriptrunner.canned.confluence.admin.CopyTree
import com.onresolve.scriptrunner.canned.confluence.admin.ChangeContentAuthor
import com.onresolve.scriptrunner.canned.util.SimpleBuiltinScriptErrors
import com.atlassian.confluence.pages.AbstractPage
import com.atlassian.confluence.pages.PageManager
import com.atlassian.sal.api.component.ComponentLocator 
import com.atlassian.confluence.user.AuthenticatedUserThreadLocal
import groovy.json.JsonBuilder
import groovy.json.JsonSlurper
import groovy.json.JsonParserType
import groovy.transform.BaseScript
import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.MediaType
import javax.ws.rs.core.Response
import org.apache.log4j.Level
log.setLevel(Level.INFO)
@BaseScript CustomEndpointDelegate delegate
copyPageTree(
    httpMethod: "POST"
    , groups: ["confluence-users"]
	) 
	{ MultivaluedMap queryParams, String body ->
      log.debug("HttpBody= ${body}")
      JsonSlurper slurper = new JsonSlurper()
      if(!body){
         return Response.serverError().entity([error: "No parameters specified"]).build()
      } else {
         //Read and validated the post parameters         
         def bodyObject = slurper.parseText(body)
         assert bodyObject instanceof Map
         if(!bodyObject?.pageTreeSource || !bodyObject?.targetParentPage || !bodyObject?.replaceToken || !bodyObject?.replaceValue) {
             return Response.serverError().entity([error: "All parameters are required: pageTreeSource, targetParentPage, replaceToken, replaceValue"]).build()
         }

         String output;
         def titleTransform = ""
         output += "pageTreeSource=${bodyObject.pageTreeSource}, targetParentPage=${bodyObject.targetParentPage}, replaceToken=${bodyObject.replaceToken}, replaceValue=${bodyObject.replaceValue}\n"
         //Define the CopyTree canned script input parameters
         def inputs =[             
             (CopyTree.FIELD_SRC_PAGE_ID): "[\"${bodyObject.pageTreeSource}\"]",
             (CopyTree.FIELD_TARGET_PAGE_ID): "[\"${bodyObject.targetParentPage}\"]",             
         ] as Map<String, Object>
         		 
         output += "CopyTree input: $inputs"         
         CopyTree copyTree = new CopyTree()         
         def errorCollection = (SimpleBuiltinScriptErrors) copyTree.doValidate(inputs as Map, false)
         if(errorCollection.hasAnyErrors()) {
             log.warn("Could not copy page tree: $errorCollection")
             return Response.serverError().entity([error: errorCollection.getErrorMessages()]).build();
         }
         return Response.ok().entity(output).build(); 
      }
   }

 

Here is the error message:

"message":"assert nodes.size() <= 1\n | | |\n | 12 false\n [[, \", 5, 1, 6, 0, 9, 6, 1, 0, \", ]]","stack-trace":"Assertion failed: \n\nassert nodes.size() <= 1\n | | |\n | 12 false\n [[, \", 5, 1, 6, 0, 9, 6, 1, 0, \", ]]\n\n\tat com.onresolve.scriptrunner.canned.confluence.admin.CopyTree.doValidate(CopyTree.groovy:245)\n\tat Script8$_run_closure1.doCall(Script8.groovy:47)\n\tat com.onresolve.scriptrunner.runner.rest.common.UserCustomScriptEndpoint.doEndpoint(UserCustomScriptEndpoint.groovy:371)\n\tat com.onresolve.scriptrunner.runner.rest.common.UserCustomScriptEndpoint.postUserEndpoint(UserCustomScriptEndpoint.groovy:271)\n","status-code":"INTERNAL_SERVER_ERROR"}

Can anyone help? 

1 answer

0 votes

For anyone needing a solution to this:

 

You do not need to pass the brackets ( [ and ] ) as strings in the arguments. These brackets are there to put the argument value in a list. Therefore, pass your arguments like this:


inputs = [
   FIELD_SRC_PAGE_ID : ["65593"],
   FIELD_TARGET_PAGE_ID: ["someOtherValue"]
]

I encountered the same problem with same error message. This solution worked for me

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Marketplace Apps & Integrations

5 mobile apps for Jira Cloud to boost productivity

  It’s very important to have access to the workflow process from anywhere. Especially if you manage the work of others. There is no difference whether you’re out of office, or drive a ca...

216 views 2 5
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you