Error on calling CopyTree built-in script via scripted post-function
Asked by Andr? Sousa:

Hi all.

I'm making a scripted post-function that automatizes the Confluence space creation in a certain transition of my workflow. Since I'm doing this from Jira, I needed to write a script, and it is working fine, but when I call the built-in script "Copy Tree", from Scriptrunner, I get the following error:

```
Cannot invoke method getSpace() on null object, stack-trace=java.lang.NullPointerException: Cannot invoke method getSpace() on null object at com.onresolve.scriptrunner.canned.confluence.admin.CopyTree.doValidate(CopyTree.groovy:237)
```

The block of code where I call this:

```
import com.atlassian.sal.api.component.ComponentLocator
import groovyx.net.http.HTTPBuilder
import groovyx.net.http.ContentType
import groovy.json.JsonSlurper
import groovy.json.JsonBuilder
import groovyx.net.http.Method
import org.apache.http.HttpRequest
import org.apache.http.HttpRequestInterceptor
import org.apache.http.protocol.HttpContext
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.Field
import java.lang.Long

def customFieldManager = ComponentAccessor.getComponent(CustomFieldManager)
def fieldSpaceKey = customFieldManager.getCustomFieldObject("customfield_15629")

def getApplicationConnection(String url) {
    def http = new HTTPBuilder(url)
    http.client.addRequestInterceptor(new HttpRequestInterceptor() {
        void process(HttpRequest httpRequest, HttpContext httpContext) {
            httpRequest.addHeader('Authorization', 'Basic ' + '*****:*****'.bytes.encodeBase64().toString())
            httpRequest.addHeader('X-Atlassian-Token', 'no-check')
        }
    })

    return http
}

def copyAgileSpaceTemplate(sourcePage, targetPage, projectKey) {
    def copyPageTreeParams = [
        FIELD_SRC_PAGE_ID: "[\"" + sourcePage + "\"]",
        FIELD_TARGET_PAGE_ID: "[\"" + targetPage + "\"]",
        FIELD_TITLE_TRANSFORM: "import com.atlassian.confluence.pages.AbstractPage\r\n\r\ntitleTransform = {AbstractPage p -> p.title = '${projectKey} ' + p.title}"
    ]

    def confluence = getApplicationConnection("confluence_base_url")

    confluence.request(Method.POST, ContentType.JSON) { req ->
        uri.path = "rest/scriptrunner/latest/canned/com.onresolve.scriptrunner.canned.confluence.admin.CopyTree"
        send ContentType.URLENC, [scriptParams: new JsonBuilder(copyPageTreeParams)]
        response.success = { resp, json ->
            log.warn("CopyTree response status: ${resp.statusLine}")
            assert resp.statusLine.statusCode == 200
        }
        response.failure = {resp, json ->
            log.warn(json)
        }
    }
}

copyAgileSpaceTemplate(83460136, 93683725, "ISJC")
```

It looks like that some part of the source code of the built-in script can't understand the parameters given.

I'm using Jira Server 7.2.3 and Scriptrunner 4.3.13.

Thanks in advance!!


Answers:
author: Tiffany Wortham
body: Hi Andr?!  

After examining the source code, I've determined that this might be an issue which has to do with this bug which was fixed some time ago: <https://productsupport.adaptavist.com/browse/SRCONFSUP-5>  

You might want to try updating ScriptRunner to see if that fixes this problem for you. However, I'd encourage you to try using a feature such as Remote Control so that you don't have to keep credentials in your scripts.  

Here's some documentation you'll find helpful if you go that route: <https://scriptrunner.adaptavist.com/latest/confluence/interacting-with-other-apps.html>

comments: author: Andr? Sousa
body: Hi, [@Tiffany Wortham](/t5/user/viewprofilepage/user-id/1420833) !

Well, I stopped that development for a while and, although my ScriptRunner is a little bit outdated, recently I discovered that there's a way that I could get where I wanted with a simpler solution. So I just used the Bob Swift's Jira CLI plugin and achieved my objectives with just a few commands. It worked very well.

I appreciate your help and the informations, thank you very much :)


Tags: built-in-scripts

confluence-server

copy

page-tree

scriptrunner

server