Forums

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

Unpublished Changes to page when trying to update it by Scriptrunner and Rest Api from Jira?

ABoerio
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 31, 2019

Hi all, 

I normally code some scripts in scriptrunner to get information that I then copy into pages in confluence, to obtain personalized reports, etc... Nothing strange.

In the last days, I had issues with a script in particular, when it seems that under some circumstances I'm not getting a new published pages, but only creating a draft.

The following is a modified version of the code I'm using, to explain what happens.

 

//Generic
import groovy.xml.MarkupBuilder

//Manage Link with Confluence
import com.atlassian.applinks.api.ApplicationLink
import com.atlassian.applinks.api.ApplicationLinkService
import com.atlassian.applinks.api.application.confluence.ConfluenceApplicationType
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

//My own library to get info on space pages (number of pages in spaces, existence, current version, etc...)
import com.omt.jira.SpaziOMT

//define first option for page content
def writer = new StringWriter()
def xml = new MarkupBuilder(writer)

//define second option for page content
def writer2 = new StringWriter()
def xml2 = new MarkupBuilder(writer2)


//Create just some text
xml.p{
h3("Test Update new")
h4("")
}

//Create a table
xml2.table(class: "aui") {
thead {
tr {
th("Project")
th("Year")
th("Month")
th("Work")
}
}
tbody{
tr{
td('testProject')
td('testYear')
td('testMonth')
td('testWork')
}
}
}


// Space where to search for the page
def spaceOutput="PMO"
// Title of the page to be updated
def pageTitle = "Monthly-Worklog-Date-Expl"

//Define an object to handle methods for getting info on pages
def spaziOMT=new SpaziOMT()
//Get info on number of pages included in the space
def quantePagine=spaziOMT.quantePagine(spaceOutput)
def numPagine=quantePagine.toInteger()
//Get the id of the page to be updated
def idp=spaziOMT.idPagina(spaceOutput, pageTitle, numPagine)
//Get the version of the page to be updated
def versione=spaziOMT.versionePagina(spaceOutput, pageTitle, numPagine)
def pageVersion=versione.toInteger()

/**
I had initially doubts on the use of an external class/method, as I'm new to this
but it actually works, I'm getting always the right page id and version,
so I know then how to build the next call by confluence api to update the page to a new version
**/

//New connection with Confluence
def ApplicationLink getPrimaryConfluenceLink() {
def applicationLinkService = ComponentLocator.getComponent(ApplicationLinkService.class)
final ApplicationLink conflLink = applicationLinkService.getPrimaryApplicationLink(ConfluenceApplicationType.class)
conflLink
}
def confluenceLink = getPrimaryConfluenceLink()
assert confluenceLink // must have a working app link set up
def authenticatedRequestFactory = confluenceLink.createImpersonatingAuthenticatedRequestFactory()

//Proceed just in case the page exists
if (pageVersion>=0){
//define the new version
def newPageVersion=pageVersion+1

log.warn('Versione iniziale pagina: '+ versione)
log.warn ('Versione finale pagina: '+newPageVersion)

//Params for the call
def params = [
type: "page",
version: [
number:newPageVersion
],
title: pageTitle,
body: [
storage: [
value: writer.toString(),
//value: writer2.toString(),
representation: "storage"
],
],
]

authenticatedRequestFactory
.createRequest(Request.MethodType.PUT, "rest/api/content/"+idp+"?status=current") // Here you need to edit to your URL
.addHeader("Content-Type", "application/json")
.setRequestBody(new JsonBuilder(params).toString())
.execute(new ResponseHandler<Response>() {
@Override
void handle(Response response) throws ResponseException {
}
})

} else {
//La pagina non esiste, non faccio nulla
log.warn("Page doesn't exists")
}

return writer

 

If I get to the console, and run it, I get something like

image.png

which is what I need. OK, it works.

If I click another time I get 93>94, another time and get 94>95, etc... OK Almost stupid would say... :-)

But here the issue, then.

If I modify the code and simply move from

 body: [
storage: [
value: writer.toString(),
//value: writer2.toString(),
representation: "storage"

to

 body: [
storage: [
//value: writer.toString(),
value: writer2.toString(),
representation: "storage"

which in the end it means that I try to insert a simple table instead that some text, I'll be able to update the page just once.

The logs will show moving from 95 to 96 version, and then no more changes, even if I continue running the code. No errors, but no more updates.

If I look at the page it shows

image.png

More strange, if I modify again the code and send again the first writer to the page, it start again updating.

I'm getting crazy.

2 answers

0 votes
Dat Tang
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
April 6, 2020

I face the exact same error as @ABoerio . May anyone check this issue?

0 votes
ABoerio
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 31, 2019

Update. I did it in the most stupid way for now, that means I run the update twice. The first one to send text content (it makes just a blank page), and the second one to send the table.

In this way I move 2 versions forward instead than just one, but it works.

I pray for suggestions on how to write a better code... :-)

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events