Automatically Update the labels of sub tasks only if the Story's label is SEO_EMEA or MA_EMEA

sai_kiran January 27, 2020

Hello all,

 

I would like to request a macro where on the labels are supposed to get updated automatically depending on the labels present on the Story task.

I want the labels to be updated only if the Story has SEO_EMEA or MA_EMEA as one of its label.

1 answer

0 votes
SITTER_Adrien
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.
January 27, 2020

Hello @sai_kiran

Do you have Script Runner plugin installed in your Jira instance ?

sai_kiran January 27, 2020

Yes it is

SITTER_Adrien
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.
January 28, 2020

Hello @sai_kiran 

Here is a script you should add as a Listener of type Custom listener with events of type "Issue Updated":

// Listener to synchronise certains to label of certains
// issues type to it's subtasks
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.label.LabelManager

def labelMgr = ComponentAccessor.getComponent(LabelManager)
def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser

// List of label to synchronise
def labelsToCopy = ["SEO_EMEA", "MA_EMEA"]

// List of issue type that we apply synchronisation
def issueType = ["Bug", "Story"]

//Filter issue type
if (issueType.any{it == event?.issue.issueType.name})
{
// Get changes of labels
def labelsChange = event?.getChangeLog()?.getRelated("ChildChangeItem")?.findAll {it.field == "labels"}


if (labelsChange)
{
//get issue
def issue = event.issue

// Get previous labels
def previousLabels = Arrays.asList(((String)labelsChange.oldstring[0]).split())

// Get new labels
def newLabels = Arrays.asList(((String)labelsChange.newstring[0]).split())

// Filter previous labels
def previousLabelsToVerify = labelsToCopy.intersect(previousLabels)

// Filter new labels
def newLabelsToVerify = labelsToCopy.intersect(newLabels)

// Compute labels to delete
def labelsToDelete = previousLabelsToVerify - newLabelsToVerify

// Compute labels to add
def labelsToAdd = newLabelsToVerify - previousLabelsToVerify

//get subtasks
def subtasks = issue.subTaskObjects

subtasks.each { subtask ->
// get current label of the subtask
def subtaskLabels = labelMgr.getLabels(subtask.id).collect{it.label}
// Delete labels
subtaskLabels -= labelsToDelete
// Add labels
subtaskLabels += labelsToAdd
// Change labels of subtask
labelMgr.setLabels(currentUser, subtask.id, subtaskLabels.toSet(), false, true)
}
}
}

Just configure the two first array:

  • labelsToCopy is is an array of all Labels you want to synchronyse with subtasks
  • issueType is an array of the different issueType that need to be synchronise

If you need all issueType this can be easyly modified (delete first if)

If you need a filter on subtasks issueType create a filter on the subtasks collection.

Regards,

Adrien

Bárdóczky Zsófia September 2, 2020

Thank you, perfect solution!

Suggest an answer

Log in or Sign up to answer