Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Change labels with Groovy script

Hi,

I want to run a groovy script when a button is clicked which changes the label.

We have three labels for page classification.

- Internal

- Confidential

- Public

 

I don't want that users need to change them manually. With Scriptrunner for Confluence I was easily able to create buttons in the action menu.

But I can't find an example script for Confluence.

The script should:

- Remove the existing classification label

- Add the new label

- Not change other existing labels

 

I found something similiar for Jira.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.label.LabelManager

def user = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()

LabelManager labelManager = ComponentAccessor.getComponent(LabelManager)
def labels = labelManager.getLabels(issue.id).collect{it.getLabel()}
labels -= 'labelToBeRemoved'
labelManager.setLabels(user,issue.id,labels.toSet(),false,false)

By adding labels +='label' I believe I could add the new label

 

Can someone help? :)

1 answer

Hey Magnus,

we needed to add Labels to an existing page. And used this code:

import com.atlassian.confluence.pages.Page
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.confluence.labels.Label
import com.atlassian.confluence.labels.LabelManager

static
Page addLabelsToPage(List<Label> labels, Page page) {
labels.each { Label label ->
ComponentLocator.getComponent(LabelManager).addLabel(page,label)
}
page 
}

Looks like there is a LabelManager in Confluence too ;)
Confluence Docs - LabelManager  

 

Hope that will help you with your Problem.

Greetings From Germany 

Florian

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Marketplace Apps & Integrations

5 mobile apps for Jira Cloud to boost productivity

  It’s very important to have access to the workflow process from anywhere. Especially if you manage the work of others. There is no difference whether you’re out of office, or drive a ca...

224 views 2 5
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you