Skip to main content
Book your demo
Example scripts
To homepage
New
Confluence
Cloud icon
Cloud

Bulk convert migrated built-in macros

Created 1 month(s) ago, Updated 0 day(s) ago
App in script
ScriptRunner For Confluence
ScriptRunner For Confluence
by Adaptavist
Compatibility
compatibility bullet
Confluence
Language |
groovy
def spaceKeys = "ds,ds1" // Replace the spaceKeys to your own value
logger.info("Fetching spaces - ${spaceKeys}")
def spaces = Spaces.search("space.key in (${spaceKeys})")
spaces.each { space ->
    logger.info("Fetching all pages for space - ${space.key}")
    def pages = Spaces.getByKey(space.key).getAllPages(){
        setBodyFormat("storage")
    }
    pages.each { page ->
        logger.info("Extracting body contents for page '${page.title}'")
        def storage = page.body.storage.value
        def updatedMacro = replaceEmptyAcNameForParameter(replaceStructuredMacroAcName(storage))
        if (updatedMacro == storage) {
            logger.info("Not updating page '${page.title}'")
            return
        }
        logger.info("Updating page '${page.title}' with new body")
        page.update(){
            title = page.title
            status = "current"
            body = updatedMacro
        }
        logger.info("Page '${page.title}' has been updated.")
    }
}

static def replaceStructuredMacroAcName(String storage) {
    def SEARCH = ["ac:name=\"add-label\"", "ac:name=\"choose-label\"", "ac:name=\"page-info\""]
    def REPLACEMENT = ["ac:name=\"sr-add-labels-macro\"", "ac:name=\"sr-choose-label-macro\"",
                       "ac:name=\"sr-page-info-macro\""]
    def modifiedStorage = storage
    SEARCH.eachWithIndex { acName, index ->
        modifiedStorage = modifiedStorage.replace(acName, REPLACEMENT[index])
    }
    modifiedStorage
}

static def replaceEmptyAcNameForParameter(String storage) {
    def EMPTY_AC_FORMAT = "ac:name=\"\">"
    def SEARCH = ["current-version", "title", "created-by", "created-date", "diffs", "labels", "modified-by",
                  "modified-date", "modified-users", "page-id", "participants", "versions"]
    def REPLACED_AC_FORMAT = "ac:name=\"infoType\">"
    def REPLACEMENT = ["Current version", "Title", "Created by", "Created date", "Diffs", "Labels", "Modified by",
                       "Modified date", "Modified users", "Page id", "Participants", "Versions"]

    def modifiedStorage = storage
    SEARCH.eachWithIndex { acName, index ->
        def fullSearchString = EMPTY_AC_FORMAT + acName
        def fullReplacementString = REPLACED_AC_FORMAT + REPLACEMENT[index]
        modifiedStorage = modifiedStorage.replace(fullSearchString, fullReplacementString)
    }
    modifiedStorage
}
Having an issue with this script?
Report it here