Find drafts in decsendent pages with given title

As I found that there is a bug in confluence that let you fail moving a page with its descendants to another space if there are drafts :roll_eyes: below the selected page with the same name as pages in the new space, I started to write a script to find these drafts as there are no standard functions for that.

You can follow the progress of the bug at https://jira.atlassian.com/browse/CONFSERVER-60238

I tested this with our confluence data center instance.

import com.atlassian.confluence.api.model.pagination.LimitedRequestImpl
import com.atlassian.confluence.content.service.SpaceService
import com.atlassian.confluence.pages.Page
import com.atlassian.confluence.pages.PageManager
import com.atlassian.confluence.spaces.Space
import com.atlassian.sal.api.component.ComponentLocator
import org.apache.log4j.Logger

import com.onresolve.scriptrunner.parameters.annotation.ShortTextInput
/**
 * This script is intended to find all drafts with a given title below a page with given title in a space with given key
 *
 * @author buchholz
 */

@ShortTextInput(label = 'page name', description = '')
String pageName
if (pageName == null) {
    return 'No page name given. Stop execution'
}

@ShortTextInput(label = 'space key', description = '')
String spaceKey
if (spaceKey == null) {
    return 'space key must not be null'
}

@ShortTextInput(label = 'draft name', description = '')
String draftName
if (draftName == null) {
    return 'draft name must not be null'
}

return MyDraftPageService.resolvePagesChildDrafts(pageName, spaceKey, draftName)

/**
 * Simple service to help finding drafts.
 */
class MyDraftPageService {
    final private static Logger LOGGER = Logger.getLogger('com.example.page.draft')
    final private static SpaceService spaceService = ComponentLocator.getComponent(SpaceService)
    final private static PageManager pageManager = ComponentLocator.getComponent(PageManager)

    /**
     * Look up space service and given space.
     * Note that many methods of SpaceManager are deprecated and new methods are internal or deliver wrong Space class.
     * See https://community.atlassian.com/t5/Confluence-articles/Get-spaces-by-keys-with-the-Confluence-API/ba-p/1939901
     *
     * @param aSpaceKey to resolve the space
     */
    static Space resolveSpaceByKey(final String aSpaceKey) {
        return spaceService.getKeySpaceLocator(aSpaceKey).getSpace()
    }

    static List<Page> resolvePagesChildDrafts(final String aPageTitle, final String aSpaceKey, final String aDraftTitle) {
        final Space space = resolveSpaceByKey(aSpaceKey)
        final Page parentPage = pageManager.getPagesStartingWith(space, aPageTitle)?.first()
        final List<Page> draftsWithTitle = resolveDraftsWithTitle(parentPage, aDraftTitle)
        final List<Page> childrenDraftsWithTitle = parentPage.getDescendants().collect {
            resolveDraftsWithTitle(it, aDraftTitle)
        }.flatten() as List<Page>
        draftsWithTitle.addAll(childrenDraftsWithTitle)
        return draftsWithTitle
    }

    static List<Page> resolveDraftsWithTitle(final Page aPage, final String aDraftTitle) {
        final List<Page> draftsUnderPage = pageManager.getDraftChildren(aPage, LimitedRequestImpl.create(1000), null).results
        LOGGER.debug("found drafts: $draftsUnderPage")
        final List<Page> draftsWithTitle = draftsUnderPage.findAll { it.nameForComparison.contains("Draft: $aDraftTitle by ") }
        draftsWithTitle.each {
            LOGGER.info("found page '${it.nameForComparison}', child of '${it.parent.nameForComparison}', with id '${it.contentId}'")
            // use with care e.g. if creator is not available
            // it.remove(pageManager)
        }
        return draftsWithTitle
    }
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.