ScriptRunner listener to add a new label when the page is removing the specific existing label

Rauzan Fikri August 28, 2023

Hi All,

 

Any suggestion if I want to add a new label via Script Runner Listener when the page is removing the specific label in existing page.

 

Thanks

1 answer

1 accepted

0 votes
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 28, 2023

Hi @Rauzan Fikri

Could you provide a little more information, i.e. what label do you want to include when the previous label is removed?

Are you expecting this Listener to work only when a specific label is removed or if any label is removed?

I am looking forward to your feedback and clarification.

Thank you and Kind regards,

Ram

Rauzan Fikri August 28, 2023

e.g., if label in the page "label_a" has removed from the page then I want to add "label_b" to the same page where the label_a has removed.

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 29, 2023

Hi @Rauzan Fikri

For your requirement, it would be best to use ScriptRunner's Custom Listener and the ChangeHistoryManager object.

For your Listener, you need to use the IssueUpdated event.

Below is a sample working code for your reference:-

import com.atlassian.jira.component.ComponentAccessor

def issue = event.issue
def changeHistoryManager = ComponentAccessor.changeHistoryManager
def changeHistories = changeHistoryManager.getChangeHistories(issue)
def field = changeHistories.changeItems['field'].last() as List

if (field[0].toString() == 'labels') {
def lastLabelList = changeHistories.changeItems['oldstring'].last() as List
def lastLabel = lastLabelList[0].toString()

if (lastLabel.length() > 0) {
def number = lastLabel.replaceAll('[^0-9]', '') as Integer
def label = lastLabel.replaceAll('[0-9]', '') as String
issue.update {
setLabels("${label}${number+1}")
}
}
}

Please note that the sample working code above is not 100% exact to your environment. Hence, you will need to make the required modifications.

Also, please ensure you have updated your ScriptRunner plugin to the latest release, 8.10.0 or at least version 7.11.0, so you can use the ScriptRunner HAPI feature to simplify your code. The example above is done using the HAPI feature.

Below is  a screenshot of the Listener configuration:-

listener_config.png

Below are a couple of test screenshots for your reference:-

1. When the issue is created, a label Test_1 is added, as shown in the screenshot below:-

test1.png

2. Next, the Label is deleted and saved as shown in the screenshot below:-

test2.png

3. As expected, a new Label is automatically added once the Label is removed and saved. This time it's Test_2, as shown in the screenshot below:-test3.png

I hope this helps to answer your question. :-)

Thank you and Kind regards,

Ram

Like Rauzan Fikri likes this
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 29, 2023

Hi @Rauzan Fikri

If your question has been answered, kindly accept the answer provided.

Thank you and Kind regards,

Ram

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events