Skip to main content
Example scripts
arrow icon
To homepage
Confluence
Cloud icon
Cloud

Add Label to Outdated Pages Job

Created 1 year ago, Updated 1 month(s) ago
App in script
ScriptRunner For Confluence
ScriptRunner For Confluence
by Adaptavist
Compatibility
compatibility bullet
Confluence
Language |
groovy
import com.atlassian.confluence.rest.clientv1.model.ContentArray
import com.atlassian.confluence.rest.clientv1.model.LabelArray

// NOTE: Make sure to setup SPACE_ID before running this script
final def TAG_PREFIX = "global" // The prefix for the label. global, my team, etc. (global is a default value)
final def TAG_NAME = "outdated" // The name of the label, which will be shown in the UI.
final def LAST_MODIFIED = "6m" // How old the pages which need a label. d/m/w/y represents day/month/week/year
final def SPACE_ID = "<YOUR_SPACE_ID>"

def contents = get("/wiki/rest/api/content/search")
        .queryString("cql", "type=page and space=${SPACE_ID} and lastmodified < now('-${LAST_MODIFIED}')")
        .queryString("limit", 200)
        .asObject(ContentArray).body

String nextUrl = contents._links.next
def outdatedPages = contents.results

while (nextUrl) {
    def nextContents = get("/wiki${nextUrl}").asObject(ContentArray).body
    nextUrl = nextContents._links.next
    outdatedPages += nextContents.results
}

outdatedPages.each { content ->
    // NOTE: You'll only need the script from lines 1-6 and 26-40 for CQL Script Jobs
    def response = post("/wiki/rest/api/content/${content.id}/label")
            .header("Content-Type", "application/json")
            .body(
                    [
                            [
                                    prefix: "${TAG_PREFIX}",
                                    name: "${TAG_NAME}",
                            ]
                    ])
            .asObject(LabelArray)
    if (response.status != 200) {
        logger.error("Error Status: {} - Failed to create tag for page with id {}. Reason: {}", response.status, content.id, response.statusText)
    } else {
        logger.info("Successfully tagged page: {}", content.title)
    }
}
Having an issue with this script?
Report it here