Forums

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

Issue regarding post function error when creating new space with script runner code

Marian Gebauer July 25, 2022

Hello Atlassian Community

 

I have run into some problem with script used in post function. I am using script runner. I am using REST API to do this. In first call I create space using POST /rest/api/space. This operation is done successfully. Second call is to get Space ID  GET rest/api/space?spaceKey=" + NewProjectKey. After obtaining new space id i want to update this new space with templated space but this operation fails POST /rest/page-hierarchy/copy.

 

Below is error i am getting:

 


java.lang.Exception: {"statusCode":400,"data":{"authorized":true,"valid":false,"allowedInReadOnlyMode":true,"errors":[{"message":{"key":"copy.page.hierarchy.dialog.invalid.destination","args":[]},"fieldName":"destinationPageId"}],"successful":false},"message":"The supplied parameters are invalid.","reason":"Bad Request"}
at Script5101$3.handle(Script5101.groovy:123)
at com.atlassian.applinks.core.auth.ApplicationLinkAnalyticsRequest$AnalyticsResponseHandler.handle(ApplicationLinkAnalyticsRequest.java:264)
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.OAuthRequest.execute(OAuthRequest.java:71)
at com.atlassian.applinks.core.auth.ApplicationLinkAnalyticsRequest.execute(ApplicationLinkAnalyticsRequest.java:164)
at com.atlassian.sal.api.net.Request$execute$1.call(Unknown Source)
at Script5101.run(Script5101.groovy:119)

 

Here is code i am using:

 

import com.atlassian.applinks.api.ApplicationLink
import com.atlassian.applinks.api.ApplicationLinkService
import com.atlassian.applinks.api.application.confluence.ConfluenceApplicationType
import com.atlassian.jira.issue.Issue
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.sal.api.net.Request
import com.atlassian.sal.api.net.Response
import com.atlassian.sal.api.net.ResponseException
import com.atlassian.sal.api.net.ResponseHandler
import groovy.json.JsonBuilder
import groovy.json.JsonSlurper
import org.apache.log4j.Logger
import org.apache.log4j.Level

def mylog = Logger.getLogger(getClass())
mylog.setLevel(Level.DEBUG)

/**
* Retrieve the primary confluence application link
* @return confluence app link
*/
def ApplicationLink getPrimaryConfluenceLink() {
def applicationLinkService = ComponentLocator.getComponent(ApplicationLinkService)
final ApplicationLink conflLink = applicationLinkService.getPrimaryApplicationLink(ConfluenceApplicationType)
conflLink
}

// the issue provided to us in the binding
Issue issue = issue

// if you don't want to create confluence pages based on some criterion like issue type, handle this, eg:
//if (!issue.issueTypeObject.name == "Bug") {
// return
//}

def confluenceLink = getPrimaryConfluenceLink()
assert confluenceLink // must have a working app link set up

def authenticatedRequestFactory = confluenceLink.createImpersonatingAuthenticatedRequestFactory()
def NPDtemplateID = "76743490"
def NewProjectKey = "NPDPROJECT2"
def NewSpaceID
def createSpace = [

key: NewProjectKey,

name: NewProjectKey,
]

authenticatedRequestFactory
.createRequest(Request.MethodType.POST, "/rest/api/space")
.addHeader("Content-Type", "application/json")
.setRequestBody(new JsonBuilder(createSpace).toString())
.execute(new ResponseHandler<Response>() {
@Override
void handle(Response response) throws ResponseException {
if (response.statusCode != HttpURLConnection.HTTP_OK) {
throw new Exception(response.getResponseBodyAsString())
} else {
def webUrl = new JsonSlurper().parseText(response.responseBodyAsString)["_links"]["webui"]
}
}
})
authenticatedRequestFactory
.createRequest(Request.MethodType.GET, "/rest/api/space?spaceKey=" + NewProjectKey)
.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 {
def webUrl = new JsonSlurper().parseText(response.responseBodyAsString)["_links"]["webui"]
//return Response.ok(new JsonBuilder(userResponse).toString()).build()

NewSpaceID = new JsonSlurper().parseText(response.responseBodyAsString)["results"]["id"].join(", ")
NewSpaceID = NewSpaceID.toString()
mylog.debug(response)
mylog.debug(response.responseBodyAsString)

}
}
})//["results"]["id"]
mylog.debug(NewSpaceID)
//assert NewSpaceID

def toCopySpace = [

"copyAttachments": "true",
"copyPermissions": "true",
//copyProperties: "true",
"copyLabels": "true",
"titleOptions":[

"prefix": "",
"search": "Test NPD Template",
"replace": "NewProjectKey"

],
"originalPageId": "NPDtemplateID",
"destinationPageId": "NewSpaceID.toString()"
]
//mylog.debug("toCopySpace :", toCopySpace.toString())
//mylog.debug("destinationPageId :", destinationPageId.toString())
mylog.debug(NPDtemplateID.getClass())
mylog.debug(NewSpaceID.getClass())
mylog.debug(toCopySpace.getClass())

mylog.debug(toCopySpace)


authenticatedRequestFactory
.createRequest(Request.MethodType.POST, "/rest/page-hierarchy/copy")
.addHeader("Content-Type", "application/json")
.setRequestBody(new JsonBuilder(toCopySpace).toString())
.execute(new ResponseHandler<Response>() {
@Override
void handle(Response response) throws ResponseException {
if (response.statusCode != HttpURLConnection.HTTP_OK) {
throw new Exception(response.getResponseBodyAsString())

} else {
def webUrl = new JsonSlurper().parseText(response.responseBodyAsString)["_links"]["webui"]

}
}
})

 

What i am doing wrong here?

 

Thank you 

0 answers

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
8.22.2
TAGS
AUG Leaders

Atlassian Community Events