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

Synchronize Labels Between Two Pages

Features
Listeners
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.event.events.label.LabelEvent
import com.atlassian.confluence.pages.PageManager
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.confluence.labels.LabelManager
import com.atlassian.confluence.pages.Page
import com.atlassian.confluence.event.events.label.LabelRemoveEvent
import com.atlassian.confluence.event.events.label.LabelAddEvent

def spaceSource = '<SPACE_SOURCE_KEY>'
def pageSource = '<PAGE_SOURCE_TITLE>'
def spaceDest = '<SPACE_DEST_KEY>'
def pageDest = '<PAGE_DEST_TITLE>'

def labelEvent = event as LabelEvent

//get page
def pageManager = ComponentLocator.getComponent(PageManager)
def labelManager = ComponentLocator.getComponent(LabelManager)

def sourcePage = pageManager.getPage(spaceSource, pageSource)
def destPage = pageManager.getPage(spaceDest, pageDest)
def currentPage = labelEvent.labelled as Page
def sourceLabel

// check if destination page exist, if not return.
if ( !destPage ) {
    log.warn "Destination page with title '${pageDest}' not available on the '${spaceDest} space'"
    return
}

// Check for specific Event to add or remove label in destination or source.
if ( currentPage.title != pageSource && currentPage.title != pageDest ) {
    log.warn "The currentpage does not match page source or destination"
    return
} else if ( currentPage.title == pageSource && labelEvent instanceof LabelRemoveEvent ) {
    sourceLabel = labelEvent.label
    labelManager.removeLabel(destPage, sourceLabel)
} else if ( currentPage.title == pageSource && labelEvent instanceof LabelAddEvent ) {
    sourceLabel = labelEvent.label
    labelManager.addLabel(destPage, sourceLabel)
} else if ( currentPage.title == pageDest && labelEvent instanceof LabelRemoveEvent ) {
    sourceLabel = labelEvent.label
    labelManager.removeLabel(sourcePage, sourceLabel)
} else if ( currentPage.title == pageDest && labelEvent instanceof LabelAddEvent ) {
    sourceLabel = labelEvent.label
    labelManager.addLabel(sourcePage, sourceLabel)
}
Having an issue with this script?
Report it here