How to add simultaneously multiples labels with scriptrunner to a page ?

Florent R_ March 11, 2021

Hello community,

 

I created a script allowing to inherit the labels of a page to all its descendants. But I find the process complicated (nested loops) and would like to simplify it. Is it possible to use another method than labelManager.addLabel to simultaneously add multiple labels to the same page?

 

Thank you in advance for your help !

1 answer

0 votes
Dominic Lagger
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 12, 2021

Hi @Florent R_ 

ScriptRunner has a built-in script, where you can add, delete or rename labels in bulk. 

If you want to do this in a script, then you need to hack a little bit. Let me know if you need help. 

Regards, Dominic

Florent R_ March 13, 2021

Thanks @Dominic Lagger , can you help me start to access the content of a built-in script ?

Dominic Lagger
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 16, 2021

I will try it on my instance, but because it's a bit of a hack, I need some free time :-) 

Perhaps I can look at it this week, but not sure.

Dominic Lagger
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 17, 2021

Hi @Florent R_ 

I did some testing, but this built-in script is too hard for me. I couldn't manage it. 

I stuck with the CQLSearchUtils...

But you can try, here is what I got.

import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.confluence.core.ContentEntityManager
import com.atlassian.confluence.api.service.search.CQLSearchService
import com.onresolve.scriptrunner.canned.confluence.utils.CQLSearchUtils
import com.onresolve.scriptrunner.canned.util.SimpleBuiltinScriptErrors

import com.onresolve.scriptrunner.canned.confluence.admin.model.ManageLabelsCommand
import com.atlassian.confluence.labels.LabelManager
import com.atlassian.user.UserManager
import com.atlassian.confluence.pages.PageManager
import com.atlassian.confluence.pages.Page
import com.atlassian.confluence.user.ConfluenceUser
import com.onresolve.scriptrunner.canned.confluence.admin.Actions
import com.onresolve.scriptrunner.canned.confluence.admin.Locations
import com.onresolve.scriptrunner.runner.ScriptRunnerImpl
import org.apache.log4j.Level
import org.apache.log4j.Logger
import java.util.LinkedHashMap
import com.onresolve.scriptrunner.canned.confluence.admin.ManageLabels

def log = Logger.getLogger("com.onresolve.scriptrunner.runner.ScriptRunnerImpl")
log.setLevel(Level.INFO)

def pageManager = ComponentLocator.getComponent(PageManager)
def userManager = ComponentLocator.getComponent(UserManager)
def labelManager = ComponentLocator.getComponent(LabelManager)

def cqlSearchService = ScriptRunnerImpl.getOsgiService(CQLSearchService)
ContentEntityManager contentEntityManager = ComponentLocator.getComponent(ContentEntityManager)
CQLSearchUtils cqlSearchUtils = new CQLSearchUtils(cqlSearchService, contentEntityManager, pageManager)

List<Long> targetPageIds = [115082958]

ManageLabels manager = new ManageLabels(labelManager, pageManager, cqlSearchUtils, userManager)
ManageLabelsCommand command = new ManageLabelsCommand()

command.setAction(Actions.ADD)
command.setLocations(Locations.CUSTOM)
command.setTargetPageIds(targetPageIds)
command.setLabels("test")
command.setNewLabelName("newlabel")

SimpleBuiltinScriptErrors errorCollection = (SimpleBuiltinScriptErrors) manager.validate(command, false)
if(errorCollection.hasAnyErrors()) {
log.warn("Error while changing Author: $errorCollection")
result = errorCollection.getErrors()
} else {
result = "OK"
}

log.debug(result)

 Perhaps, you find the problem :)

Let me know if you could manage it.

Regards, Dominic

Florent R_ March 17, 2021

Thanks Dominic, but where did you found the source code of the build-in script ?

I will try anyway.

Have a nice day :-)

Dominic Lagger
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 17, 2021

I downloaded the JAR of the scriptrunner and decoded the class files... 

Florent R_ March 23, 2021

Thanks @Dominic Lagger , I tried what you said but i'm too newbie in coding. I will postpone that and come back at it later.

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
AUG Leaders

Atlassian Community Events