How to access Confluence page content using script runner?

I would like to setup a script job that scans a page for web inks and marks in red those that are not valid any more.

The reason why I do not want to use a macro for this is that I want watchers to be notified of this change.

I have installed Confluence script runner, I had a look at the confluence API especially regarding ContentEntityObject but I could not figure out a way to read the page content and update it.

1 answer

1 accepted

2 votes
Accepted answer

Hi Roberto,

My name is Rafael Franco and I'm the Tech Lead of the Product Team here at Adaptavist. Thanks for using ScriptRunner for Confluence.

May I ask how do you define that a link is not valid any more? 

Regards,

Rafael

Hi Rafael,

I would just use HttpUrlConnection to send a "HEAD" command and then verify whether its return code is 200

Hi Roberto,

Ok, clear now. What you need essentially is understand that each page is composed of a sort of meta language called storage format https://confluence.atlassian.com/doc/confluence-storage-format-790796544.html

In essence what you need to do is look for all pages, iterate over them, get the page content and update it, so something like:

ContentEntityObject contentEntityObject = page.getContentEntityObject()
 
String body = contentEntityObject.getBodyAsString()
 
//Do a regex to replace the "a" links and add a style="background-color:red" to it
 
after the replace do 
 
contentEntityObject.setBodyAsString() with the updated body
 
and then finally:
 
this.pageManager.saveContentEntity(contentEntityObject, SILENT_SAVE_CONTEXT);

Roberto, link checking could add quite a load to the Confluence server, so I'd recommend you have some way to restrict how often connections are made

Matt, I guess this is a one time operation, so should be fine

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Nov 29, 2018 in Marketplace Apps

How to set up an incident workflow from the VP of Engineering at Sentry

Hey Atlassian community, I help lead engineering at Sentry, an open-source error-tracking and monitoring tool that integrates with Jira. We started using Jira Software Cloud internally last year, a...

1,440 views 0 8
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you