Hi All,
I managed to produce a confluence page from Jira cloud with Scriptrunner .
The problem is that it does not always work. I've figured out that multi-text fields (description) have a problem when they have a line break.
My question to Scriptrunner Support
With help from Scriptrunner I managed to remove line breaks (JsonStringEncoder) but the Script still not working.
Now the description text is without breaks but with \r\n (replaceAll didn't work)
The code:
import com.fasterxml.jackson.core.io.JsonStringEncoder
def baseUrl = new URL("https://myurl.atlassian.net/wiki/rest/api/content")
def issueKey = issue.key
def result = get('/rest/api/2/issue/' + issueKey)
.header('Content-Type', 'application/json')
.asObject(Map)
String Description = result.body.fields["description"]
def USER_NAME_AND_PASS = "MyUser:pass"
JsonStringEncoder e = JsonStringEncoder.getInstance();
String encodedDescription = new String(e.quoteAsString(Description));
def body = '''
{
"type" : "page",
"status" : "current",
"title" : "NGS Kickoff Meeting Protocol (Project Number: '''+IssueKey+''') ['''+date+''']",
"space" : { "key" : "INCPM" },
"ancestors": [{
"id": 318963721
}],
"body" : {
"storage" : {
"value": "'''+encodedDescription+'''",
"representation" : "storage"
}
}
}'''
def connection = baseUrl.openConnection()
def authString = USER_NAME_AND_PASS.getBytes().encodeBase64().toString()
connection.setRequestProperty( "Authorization", "Basic ${authString}" )
connection.setRequestProperty( "Content-type", "application/json");
connection.setRequestProperty( "Accept", "application/json" );
connection.with {
doOutput = true
requestMethod = 'POST'
outputStream.withWriter { writer ->
writer << body
}
println content.text
}
Thanks,
Shira
Hi Shira,
Can you please post the contents of the description variable so we can see why the JSON encoder is not working for this variable with line breaks.
Regards,
Kristian
Hi Kristin,
The content of the description before the JSON encoder:
Created by Shira
hhh*&
test this
The content with the JSON encoder:
Created by Shira\r\nhhh*&\r\n\r\ntest this \r\n
Thanks,
Shira
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
I managed to remove the \r\n char
String jsonFormattedString = encodedDescription.replaceAll("\\\\r", " ").replaceAll("\\\\n", "")
and now my content is :
Created by Shira hhh*& test this
Receive this error:
java.io.IOException: Server returned HTTP response code: 400 for URL: at Script1$_run_closure1.doCall(Script1.groovy:61) at Script1.run(Script1.groovy:55) at Script1$run.call(Unknown Source) at Script1$run.call(Unknown Source) at com.adaptavist.sr.cloud.workflow.AbstractScript.evaluate(AbstractScript.groovy:32) at com.adaptavist.sr.cloud.workflow.AbstractScript$evaluate$2.callCurrent(Unknown Source) at com.adaptavist.sr.cloud.events.ScriptExecution.run(ScriptExecution.groovy:27) at ConsoleScriptExecution1_groovyProxy.run(Unknown Source)
And the page was not created...
You have an idea for a solution?
Thanks,
Shira
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Shira,
What is the response body from that POST request to /wiki/rest/api/content?
Normally with a 400 Bad Request response you'd get a response body containing information about what was wrong with the request.
Thanks,
Jon
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
The problem was the character &.
Now everything works great.
Thanks,
Shira
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Shira,
Thank you for confirming that this issue has been resolved.
Kristian
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.