Rule for Copy and Remove Labels, to child issues

Rodrigo Baccaro Kazlauskas October 20, 2022

Hey everyone, im trying to create a rule for copy and remove labels to child issues.

Then any changes to the labels must be copied to child issues.

Would anyone have an example?

3 answers

1 accepted

0 votes
Answer accepted
Bill Sheboy
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.
October 20, 2022

Hi @Rodrigo Baccaro Kazlauskas 

Would you please clarify your use case?  Do you want to

  1. keep the parents and child issues in synch (same exact labels), or
  2. child issues can have more labels than the parent and include all parent labels?

Kind regards,
Bill

Rodrigo Baccaro Kazlauskas October 21, 2022

Hi @Bill Sheboy 

Thakns for your reply.

 

I didnt think about the option 2, but this option will be better. Could you help me how to setup this 2nd option?

 

King regards

Bill Sheboy
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.
October 21, 2022

This is the more complicated option, as you first must remove the previous labels the parent used, and then add the current values.  Both of these use the {{changelog}} smart value.

For example, this rule would be triggered on a parent epic's labels changing:

  • trigger: Value changes for Labels
  • condition: issue type is Epic
  • branch: for stories (or other issues) in the Epic
    • action: edit issue Labels (to remove old values)
      • Values to add: leave this empty
      • Values to remove: {{#changelog.labels}}{{fromString.split(" ")}}{{/}}
    • action: re-fetch issue to reload it after the changes
    • action: edit issue Labels again (to add)
      • Values to add: {{#changelog.labels}}{{toString.split(" ")}}{{/}}
      • Values to remove: leave this empty

This one grabs the prior values (fromString) for the triggering epic, and splits them up so they can be removed from the child issue.  Similarly the new values (toString) are then added.  I believe this must be done in separate steps, with the Re-fetch in the middle, to avoid a collision.

I recommend trying this in either a test project or with test issues first.  If it does not work as you want, you can switch to Case 1 (simple replacement), as that is much simpler.

Rodrigo Baccaro Kazlauskas October 24, 2022

Hey @BIL 

I've tried this setup, but is not working. There is something wrong how i built it?

Jira automation label.jpg

Bill Sheboy
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.
October 24, 2022

Nope...my example rule that I wrote last year has stopped working since then.  Let me experiment using a JSON edit instead.

Bill Sheboy
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.
October 25, 2022

Okay, I rebuilt my example rule for this  :^)

I recommend testing to confirm it works as you want. 

This uses the change log, created variables and JSON, dynamically built, to first remove the old labels and then to add the new ones.  To learn about this method, please take a look at the linked documentation above.

  • trigger: value changes for Labels
  • condition: Initiator is not "Automation for Jira" user.  This is a precaution in case this rule runs slower for some reason, to prevent it from calling itself.
  • condition: issue type is Epic
  • action: create variable
    • name: varRemoveLabels
    • smart value: {{#changelog.labels}}{{#fromString.split(" ")}}{ "remove": "{{.}}" } {{^last}}, {{/}}{{/}}{{/}}
  • action: create variable
    • name: varAddLabels
    • smart value: {{#changelog.labels}}{{#toString.split(" ")}}{ "add": "{{.}}" } {{^last}}, {{/}}{{/}}{{/}}
  • branch: for stories (or other issues) in Epic
    • action: edit issue to change the labels using the dynamic JSON
{
"update": {
"labels": [
{{#if(exists(varRemoveLabels))}}{{varRemoveLabels}},{{/}}
{{varAddLabels}}
]
}
}

 

How it works...

To pass down the epic changes to labels and preserve and different values in the child issues (e.g. stories) we need to remove the old (before) epic labels and then add the new (after) epic labels.  Ideally, we want to do this in one step to avoid repeated changes to the same issue.

When an issue changes, information is captured in the change log, including the before and after values.  For labels, the changes are stored as a list of values with spaces between each label change.  So we can capture the before values with {{#changelog.labels}}{{fromString}}{{/}} and then split them with the embedded spaces.  We can do the same thing to get the after values with {{toString}}

When using the JSON edits, we need to use the specific format which allows both adding and removing changes to do this in one step.  For labels this looks like this:

{ 
"update": {
"labels": [
{
"remove": "Label1"
},
{
"add": "Label2"
}
]
}
}

The wrinkle for synching labels is we do not know how many will be added or removed, and so we dynamically build the bolded sections above for the {{varAddLabels}} and {{varRemoveLabels}}

The final piece is the "remove" section could be empty, and so that conditional logic test helps prevent a stray comma from breaking things.

Give it a try and please let me know how it helps.

Like # people like this
Akanksha Saha November 23, 2022

Hi @Bill Sheboy 

Thanks for your solution, I've tried to implement it however I keep getting the following error code when I'm adding the following section

  • action: edit issue to change the labels using the dynamic JSON

Screenshot 2022-11-23 133948.png

 

I should mention I am using a company-managed project on Jira Cloud. Do you have any idea why this error is appearing and what JSON commands I could/should use to avoid this?

Bill Sheboy
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.
November 23, 2022

Hi @Akanksha Saha  -- Welcome to the Atlassian Community!

That messages seems to indicate what it says: a permissions problem. 

What is the rule actor for your rule (under details at the top of the rule edit)?  And if it is the default of the "Automation for Jira" user, did someone change the permissions for that user and the project?

Kind regards,
Bill

Adam Ziecik April 28, 2023

Hello @Bill Sheboy

I am trying to use what you provided in this post to create an automation that will add a single select custom field value to the Labels of the same issue, but remove the old value. For example, if the custom field has value 1 that was originally copied over to labels and a user selects value 2, the automation would then remove value 1 and add value 2 in the labels instead. I am on a Data Center and the create variable is not available in DC as yet.

Would you be able to help out this a suggestion for such a situation?

Bill Sheboy
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.
April 28, 2023

Hi @Adam Ziecik 

First thing: I am using Jira Cloud and so I cannot test my suggestions on this one...

Next, it appears Atlassian has started work on adding the Create Variable action to the Server/Data Center version of automation rules.  You can follow this suggestion for progress updates: https://jira.atlassian.com/browse/JIRAAUTOSERVER-384

Back to your question...I wonder if you could do this in two steps:

  • use the change log to identify the labels to remove, and remove them
  • re-fetch the issue
  • use the change log to identify the labels to add, and add them

The part I do not know for Server/Data Center: does the content of the changelog remain stable after the re-fetch action?  If it does, this will probably work.

Kind regards,
Bill

Adam Ziecik May 2, 2023

@Bill Sheboy - thank you for getting back to me :)

 

I was thinking along these lines, but please could you support with how to get the proposed by you:

  • use the change log to identify the labels to remove, and remove them
  • use the change log to identify the labels to add, and add them

 

Capture.PNG

Bill Sheboy
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.
May 2, 2023

Please take a look earlier in this thread where the variables are created to do this: varRemoveLabels and varAddLabels.  Instead of creating variables, you would use the expressions directly in two different issue updates with JSON, with the issue re-fetch between them.

Adam Ziecik May 3, 2023

@Bill Sheboy - I did have a look and tried to follow it, but I got the following error. Probably because the JSONs I tired are wrong. Please, could you help with the JSONs for both remove and add labels ?

"com.codebarrel.automation.rule.error.action.additional.field.update.operations.invalid.execution
labels"
Bill Sheboy
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.
May 3, 2023

Would you please post images of your complete rule, including the details of the issue edits with JSON, and of the audit log details showing the rule execution?  Those may provide context for what you are seeing. 

The JSON I noted earlier worked for the original question and for Jira Cloud, so something may be different for your scenario.

Adam Ziecik May 4, 2023

@Bill Sheboy - thank you again for getting back to me.

 

I have no experience writing JSON. I tried to do the following:

 

For removing the label:

{
"update": {
"labels": [
{{varRemoveLabels}}
]
}
}

 

For adding the label:

{
"update": {
"labels": [
{{varAddLabels}}
]
}
}

 

 

I get the following error in the audit.

Capture10.PNG

Bill Sheboy
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.
May 4, 2023

Thanks for that information, @Adam Ziecik 

As you noted you are using the Server/Data Center version, there are no created variables and so that syntax cannot work.  Specifically, you cannot use {{varRemoveLabels}} and {{varAddLabels}}

Instead you need to replace the created variables in the JSON expression with what was used to create them.  Please try that, and then post an image of your complete rule so we can confirm what you are trying.  Thanks!

Adam Ziecik May 5, 2023

@Bill Sheboy 

 

I have one for removing the label copied from the custom field and another one for adding it, with the re-fetch in the middle like in the print screen I posted before. However, I guess, it would need to know what was the old value of the custom field to be removed rather than removing and adding the current custom field value as otherwise it does not make any sense.

Do you think you could guide me how to writhe such JSON?

 

{
"update": {
"labels": [
{
"remove": "{{issue.customfield_10874.value}}"
}]
}
}

 

Bill Sheboy
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.
May 5, 2023

You would substitute in the expression I noted earlier.  For example, the one for the remove would be this:

{{#changelog.labels}}{{#fromString.split(" ")}}{ "remove": "{{.}}" } {{^last}}, {{/}}{{/}}{{/}}

And then inserted into the JSON update would be:

{
"update": {
"labels": [
{{#changelog.labels}}{{#fromString.split(" ")}}{ "remove": "{{.}}" } {{^last}}, {{/}}{{/}}{{/}}
]
}
}

 

Adam Ziecik May 11, 2023

@Bill Sheboy - thank you very much for this.

 

I have used it and then replaced the "remove" with "add", but I received the following error message.

 

Capture1.PNG

Bill Sheboy
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.
May 11, 2023

There appears to be something different with your rule, or with Jira Data Center, in this automation scenario.  Let's try this step by step...

Please replace your edit with a write to the audit log for just the remove expression value:

REMOVE: {{#changelog.labels}}{{#fromString.split(" ")}}{ "remove": "{{.}}" } {{^last}}, {{/}}{{/}}{{/}}

Then add another write to the log with the full list of labels:

ALL: {{issue.labels}}

Then run your rule and post the audit log to show what happened.

Adam Ziecik May 17, 2023

@Bill Sheboy - I am sorry, but where do I have to add the write to the log ?

Bill Sheboy
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.
May 17, 2023

Please use the Log action to write things to the audit log.  This helps with diagnosing problems in rules and to confirm rule progress.

https://support.atlassian.com/cloud-automation/docs/jira-automation-actions/#Log-action

0 votes
Adam Ziecik April 28, 2023

*deleted*

0 votes
Grace Shaw February 7, 2023

***Assuming the child issues will always have the same labels as the parent:

 

1. Create the Trigger: "Field value changed" (Labels)

2. Create a "Branch rule / related issues":

  • Set "Type of related issues" to "JQL"
  • Use the following JQL filter:
issuekey in portfolioChildIssuesOf{{issue.key}}

3. Create an "Edit issues" action

  • Select "Labels" from the "Choose fields to set..." dropdown
  • In the Labels field, select "Copy from parent"

 

If the child issues may have different labels than the parent, then you will have to parse the changelog. 

Bill Sheboy
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.
February 8, 2023

Hi @Grace Shaw -- Welcome to the Atlassian Community!

My understanding is the function portfolioChildIssueOf() is only available with Advanced Roadmaps, provided with a Premium Jira license.  And the person who originally asked this question is not on that license level.

Kind regards,
Bill

Like Grace Shaw likes this
Grace Shaw February 8, 2023

@Bill Sheboy — I did not realize these details (that portfolioChildIssueOf is an Advanced Roadmaps-only feature, and the user's license level). Your answer is more robust anyway. Thanks for the feedback!

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
PERMISSIONS LEVEL
Site Admin
TAGS
AUG Leaders

Atlassian Community Events