how to delete specific issue components using automation for Jira

Michele D'Adamo November 16, 2020

Hi, 

I'm currently exploring the capabilities of automation for Jira to have a hierarchy alignemnt on Components fileds of a group Epic-Story-subtask.

I'm already able to copy components from a parent. Also to add new components when the parent changes (or to replace them).

 

My current problem is about how to delete a specific component value from the list of compontents of an issue.

 

The practical example is the following one. But, currently, I'm still not able to delete a specified component.

step1) sub-tasks and stories already inherited Epic components (maybe appending to any already existing)

step2) one component is deleted from a parent issue (let's say the epic)

step3) the same component SHOULD be deleted from the childrens (let's say at lest from the stories under that epic)

 

I was trying to use smart values/advanced rules/variables to solve step3) but nothing seems to fit my needs.

Could anyone help me?

 

thank you in advance for the help.

 

5 answers

1 accepted

4 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.
November 17, 2020

Hi @Michele D'Adamo 

You can remove a component using automation with custom fields and the list, string, advanced JSON edit, and create variable functions.  It is a bit messy, so if you want the details of how to try this, please let me know.

Summary of how this works: concatenate the component values into Json, remove the target value, and use the remaining Json to update the component list.

Best regards,

Bill

Michele D'Adamo November 22, 2020

Hi Bill,

 

yes it is exactly what i wanted to try, but something is not totally clear to me. 

 

I should to use "additional fields" box into a "edit issue fields". But I never worked with Jsons. I mean, never programming them.

 

It will be really helpfull to read how to proceed. Could you kindly try to explain me? actually I'm trying to understand recursive concatenation with smart values.

I'm already able to extract the list of the components, separated by comma, before change and after.

 

Thanks!

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, 2020

Hi @Michele D'Adamo 

Here is the information for how I removed a component.  Please consider adding logging to see what is happening during the processing.

  • Ask your site admin to create a custom text field for issues, such as WorkingComponent, to hold the item to be deleted.  (Side note: automation Create Variable values cannot be used as inputs to functions, which is why this custom field is needed.)
  • Create an automation rule with your desired trigger
  • Action: Edit Issue for your custom field, WorkingComponent, with the target value you want to remove from the component lists, wrapping it in single quotation marks.  You can get this target value from another field or hard-code it.  In my example, I am removing the third component value from the 0-based array
'{{issue.components.name.split(",").get(2)}}'
  • Action: Re-fetch so the issue and custom field are re-loaded
  • Action: Create Variable (for example, named initialJson) to represent the JSON used to update the component list, initially containing all values.  The syntax substitutes single quotes initially.
{{#issue.components}}{ 'name': '{{name}}'} , {{/}}
  • Action: Create Variable (for example, named finalJson) to represent the JSON used actually update the components.
    • Replace WorkingComponent with your custom field's name
    • The first remove() deletes the target value component
    • The second remove() deletes any leftover JSON text after the first remove
    • The substringBeforeLast() removes any trailing commas
    • And the final replace() substitutes double-quotation marks for the single ones
{{firstJson.remove(issue.WorkingComponent).remove("{ 'name': } , ").substringBeforeLast(",").replace("'","\"")}}
  • Action: Edit Issue fields, advanced using your JSON
    {

    "fields": { "components" : [ {{finalJson}} ] }

    }


Here is the documentation on advanced field editing with JSON, the string functions, and the list functions I used:

https://support.atlassian.com/jira-software-cloud/docs/advanced-field-editing-json/

https://support.atlassian.com/jira-software-cloud/docs/smart-values-text-fields/

https://support.atlassian.com/jira-software-cloud/docs/smart-values-lists/

 

Best regards,

Bill

Like # people like this
Michele D'Adamo December 12, 2020

Ciao Bill, 

 

Many thanks for your help. I have to say that it is working!

I'm able to move modified epic components to the children now.

 

The next step will be to manage any additional component at children level (not present into fathers) But i should have enough info to update also in this direction. Just need the time to think about :)

 

Best Regards,

Michele

Like Bill Sheboy likes this
Alok Juneja April 18, 2023

@Bill Sheboy I am wondering if there is some substitute to "create variable" in Jira data center. The question here is that we are struggling to delete one component/s from Jira issue and we got stuck and not moving forward. Is there any other approach that we can apply to achieve on data center. 

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 19, 2023

Hi @Alok Juneja 

First thing, I am using Jira Cloud and so do not have Server/Data Center to test this...

I can think of two ways:

  1. This should work, although more slowly: Rather than using Create Variable, use either an Entity Property on the issue or a custom field.  When the value is set, use Re-fetch Issue to reload, and then proceed with the rule logic.
  2. I do not know if this will work/is possible: Try to implement all of the logic in-line, in one single Edit Issue action.  There are limits to what can/cannot be used as smart value, function parameters due to scoping, and so this may not work.

Kind regards,
Bill

Like Alok Juneja likes this
Alok Juneja April 24, 2023

@Bill Sheboy Thanks for tips. The option 1 worked on Jira data center. I managed to create custom field and once work is done I deleted their contents. 

I get interested into "Entity Property", since I have never worked with it before. I am wondering if you have some tips about it in case I want to proceed with "Entity property" then how can I move forward. This is mostly from learning perspective.

 

Regarding option 2, I am experimenting on it. In case I find some breakthrough, I post it here for readers who land on this page in future.

Once again thanks @Bill Sheboy for the tip.

Like Bill Sheboy likes 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.
April 25, 2023

I am glad to learn that helped.

You may set entity properties at various levels, although the automation rules do not support set/read of them all directly, as described here:

https://confluence.atlassian.com/automation0801/jira-automation-actions-1223822666.html

For ones at other scopes (e.g., project) you would need to call the REST API with a web request.  For your scenario, I do not believe you would need to do that.

Like Alok Juneja likes this
1 vote
Benoit October 10, 2023

In JSON format in a modify issue use this syntax:
{
"update": {
"components": [{
"remove": {"name": "Component_Name_To_Remove"}
}]
}
}

0 votes
Benoit October 10, 2023

.

0 votes
David Leal
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.
September 3, 2023

Great solution from @Bill Sheboy, but I think it is possible to remove components without creating a custom field. Interesting question @Michele D'Adamo actually I had to face a similar problem.

 Here is a more elaborated rule, based on my real scenario, which serves to add and remove components.

Here is the scenario:

  1. Create a manual rule with input values for adding components, removing components, and a set of issue keys.
  2. The rule will be running under a given epic and the mission is to adjust the components adding and/or removing them for indicated keys, if the input keys are not provided, then it takes all the issues that have as epic link the trigger issue.
  3. Inputs are processed to clean them, for example, extra spaces, duplicated commas, replacing semi-colon with colon, removing string delimiters, etc.
  4. The issue keys could have the project prefix or just the numbers. In the case of just numbers, it finds the project key and adds to them as a prefix, i.e. "XYZ-". Since most of the actions are project-specific, no need to enter a project prefix every time.

 

Here is the definition of the manual rules with the corresponding entries. We use some default values for testing purposes:

 manualRuleSpecification.png

Here is the entire automation rule:

jiraAutomationRule.png 

 

Let's see some specific settings:

{{replaceRegEx}}. This is what we use to add the project prefix to the input issues keys:

 {{project.key}}-$1

Clean and standardize userInputs.keys and set the variable {{key}}s:

{{userInputs.keys.trim().replaceAll("\\s*['\"]+\\s*","").replaceAll("\\s*[,;]+\\s*$","").replaceAll("\\s+", " ").replaceAll("\\s?[,;]+\\s?", ",").replaceAll("\\s?[\-_]+\\s?", "-").replaceAll("(?:[A-Z]+-)*([0-9]+)",replaceRegEx)}}

 

The last replaceAll is used for extracting the numbers adding the project key as a prefix and adding the dash. It works in both cases, then the issue numbers are provided (1,2,3, etc.), or the issue keys (XYZ-1,XYZ-2,XYZ-3, etc.). Since as per the documentation of replaceAll function only $1 can be used, the first group is not captured (?:).

 

Similarly, we clean and process the user inputs: addCom, and remCom, as follows defining the follow smart values:

 

{{addCom}}:

{{userInputs.addComp.trim().replaceAll("\\s*['\"]+\\s*","").replaceAll("\\s*[,;]+\\s*$","").replaceAll("\\s+", " ").replaceAll("\\s?[,;]+\\s?", ",")}}

 

{{remCom}}:

 {{userInputs.remComp.trim().replaceAll("\\s*['\"]+\\s*","").replaceAll("\\s*[,;]+\\s*$","").replaceAll("\\s+", " ").replaceAll("\\s?[,;]+\\s?", ",")}}

 

Look up issues. In the case case issue keys are not provided via input, then we use this {{lookupissue}} smart variable, as issue keys, for all issues that belong to the epic:

"Epic Link" = {{issue.key}}

{{epicIssues}}, is the string representation of all issues within the epic delimited by a comma:

{{#lookupIssues}}{{.}}{{^last}},{{/}}{{/}}

We define again the smart value {{keys}} (yes it is possible, you can define a variable with the same name more than once). Since smart values don't support if-else, it is constructed as follows:

{{#if(not(keys.isEmpty()))}}{{keys}}{{/}}{{#if(keys.isEmpty())}}{{epicIssues}}{{/}}

Idea taken from here: answer to this question Ternary operator (or if/else) with smart values from @Chad Waldman

Now we have all the input information to edit related issues.

If all match condition checks one of the addCom or remCom is provided. We check the length of each string. Since The if condition can only allow all matches or any match, to mix and or condition, we use the length of both inputs is greater than zero. The condition that {{keys}} contains the project key is just an additional check for the input keys, not going further if the issue keys are not specific to the current project or any failure in processing information.

The smart variable {{qq}} just has a single quote ("), we use it for appending the string delimiter.

We use Branch rule/related issues and the type of related issue is specified through a JQL:

 key in ({{keys}})

 

The variable {{issueComps}}, has all the components of the issue triggered in the branch delimited by a comma:

 {{#issue.components.name}}{{.}},{{/}}

The variable {{issueCompsPlusAddCom}} has the existing components plus the components to add:

{{issueComps.concat(addComp)}}

{{issueComps}} has a trailing comma, so we don't need to add it building  {{issueCompsPlusAddCom}}.

Notice, when we set the components Jira doesn't take into account repeated component names, so we don't need to exclude duplicates.

To remove the list of remCom from the previous variable, we use a regular expression {{orCondRegex}}:

({{remComp.replace(",","|")}}),?

 Replacing the "," with "|" we would get an or regex condition. The last portion is to remove the trailing comma in case it is present.

The variable {{comps}}:

{{issueCompsPlusAddComp.replaceAll(orCondRegex,"").replaceAll("(,+)", ",")}}

Removes all the remComp values from issueCompsPlusAddComp and removes any duplicated comma as a result of the deletion.

 The variable {{compsCsv}} converts {{comps}} variable with CSV delimited by string quotation (").

Now we have all the information we need to build the jsonComps variable:

{{#compsCsv.split(",")}}{ "name" : {{.}}{{^last}}} , {{/}}{{/}}}

 

Spit is used to generate a list, then we iterate over all elements adding the prefix "name : " and adding as a suffix "}," except for the last one.

Finally, using Advanced Issue Edit, we build the final JSON file as follows:

{
  "fields" : {
    "components" : [ {{jsonComps}} ]
  }
}

We included some log instructions to trace the information. If you don't want to execute the last command and just debug the entire process without modifying the issue, just add a false condition before to visualize the information in the log without making the changes.

Even for components the Jira REST API JSON file allows add/remove actions, such actions can be applied for single values only, not for modifying an array of components to add or remove.

 Here is the log output:

jiraog.png

The branch runs for two issues, to the log trace concatenates the information for each issue.

I hope it helps,

David

0 votes
Petter Gonçalves
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
November 17, 2020

Hello @Michele D'Adamo

Thank you for reaching out.

Indeed, Jira automation does not have a function to remove a component of a child issue (Stories, Sub-task, etc) when it gets removed in the parent issue (Epic or Parent task). We have the following feature request to get this function implemented on the rule triggers, so you would be able to use the Smart values to properly reference it in the rule actions:

Trigger rule when value is added/removed from multi-select type field 

You can keep an eye on that feature request to check any updates.

If the child issues should have the exact same components as the parent issues, a possible solution would be to completely override the components of the child issues with the ones selected/removed in the Parent issue:

Screen Shot 2020-11-17 at 16.24.32.png

For more details, you can check the section "Add values to multi-select fields" of the documentation below:

Edit issue Fields 

Let us know if you have any questions.

Suggest an answer

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

Atlassian Community Events