Skip to main content
Example scripts
arrow icon
To homepage
Confluence
Data centre icon
Data Center

Synchronize Two pages

Created 1 year ago, Updated 1 month(s) ago
App in script
ScriptRunner For Confluence
ScriptRunner For Confluence
by Adaptavist
Compatibility
compatibility bullet
Confluence (7.15 - 8.6)
compatibility bullet
ScriptRunner For Confluence (7.10.0)
Language |
groovy
import com.atlassian.confluence.spaces.Space
import com.atlassian.confluence.pages.AttachmentManager
import com.atlassian.confluence.pages.PageManager
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.confluence.labels.LabelManager
import com.onresolve.scriptrunner.parameters.annotation.SpacePicker
import com.onresolve.scriptrunner.parameters.annotation.ShortTextInput
import com.atlassian.sal.api.transaction.TransactionTemplate

@SpacePicker(label = 'Space Source', description = 'Select the space source', multiple = false)
Space spaceSource

@ShortTextInput(label = 'Page Source Title', description = 'Copy the page source title')
String pageSource

@SpacePicker(label = 'Space Destination', description = 'Select the space destination', multiple = false)
Space spaceDest

@ShortTextInput(label = 'Page Destination Title', description = 'Copy the page destination title')
String pageDest

def pageManager = ComponentLocator.getComponent(PageManager)
def labelManager = ComponentLocator.getComponent(LabelManager)
def attachmentManager = ComponentLocator.getComponent(AttachmentManager)
def transactionTemplate = ComponentLocator.getComponent(TransactionTemplate)

//get Space Key
String sourceKey = spaceSource?.key
String destKey = spaceDest?.key

//get page
def sourcePage = pageManager.getPage(sourceKey, pageSource)
def destPage = pageManager.getPage(destKey, pageDest)

//get page body in source page
def sourcePageBody = sourcePage.bodyContent

// get page label
def sourceLabel = sourcePage.labels
def destLabel = destPage.labels

// get attachment for destination page
def sourceAttach = attachmentManager.getLatestVersionsOfAttachments(sourcePage)
def destAttach = attachmentManager.getLatestVersionsOfAttachments(destPage)
def sourceAttachName = sourceAttach*.nameForComparison
def destAttachName = destAttach*.nameForComparison

// 'saveNewVersion' and 'copyAttachment' cannot be run concurrently since we will run into CONFSERVER-65136.
// We will need to run everything inside one 'TransactionTemplate'
transactionTemplate.execute {
// Add newly added attachment
    sourceAttachName.each { attach ->
        if ( !destAttachName.contains( attach )) {
            sourceAttach.each { attachment ->
                if ( attachment.nameForComparison == attach ) {
                    log.warn "Attachment $attach added"
                    attachmentManager.copyAttachment(attachment, destPage)
                }
            }
        }
    }

// Delete newly deleted attachment
    destAttachName.each { attach ->
        if ( !sourceAttachName.contains( attach )) {
            destAttach.each { attachment ->
                if ( attachment.nameForComparison == attach ) {
                    log.warn "Attachment $attach deleted"
                    attachmentManager.removeAttachmentFromServer(attachment)
                }
            }
        }
    }

// add newly added Label
    sourceLabel.each { label ->
        if (!destLabel.contains( label )) {
            labelManager.getLabel(label)
            labelManager.addLabel(destPage, label)
            log.warn "label ===> $label is added"
        }
    }

// delete newly delete label
    destLabel.each { label ->
        if (!sourceLabel.contains( label )) {
            labelManager.getLabel(label)
            labelManager.removeLabel(destPage, label)
            log.warn "label ===> $label is removed"
        }
    }
// set new body for destination page
    pageManager.saveNewVersion( destPage ) { pageObject ->
        pageObject.setBodyContent(sourcePageBody)
    }
}
Having an issue with this script?
Report it here