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

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

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

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... :-)

0 votes
Dat_Tang I'm New Here Apr 06, 2020

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

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Posted in Confluence

What project did you transition or start on Confluence with the shift to remote work?

It’s been great to hear from fellow users over the last few weeks about the best tips and fun moments you’ve had working on Confluence since the transition to working remote. I’d love to keep the c...

32 views 2 4
Join discussion

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