How to add new components to an issue using advanced json

manas Sahoo January 20, 2021

Hello,

 

I want to add a new component to an issue on an automation event and it says how below advanced json can be used to set components, but in case my case I know the id of the component and I want to add to list of components and not remove them. 

 

"components" : [ { "name": "Active Directory"} , { "name": "Network Switch" } ]

 I tried

{
"update" : {
"components" : [{"add" : [{"name" : "Rollout"}]}]
}
}

but it gives json error on runtime

1 answer

1 accepted

3 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.
January 20, 2021

Hi @manas Sahoo  -- Welcome to the Atlassian Community!

The trick here is to gather the existing components first, and then append your new value.  Then use advanced edit with the JSON as you note.  Remember that the component must already exist for the project.

For an example rule:

  • Trigger: manual (or whatever you need)
  • Action: create variable with the JSON for your current components.  Let's call it firstJson.
{{#issue.components}}{ "name": "{{name}}"} {{^last}}, {{/}}{{/}}
  • Action: create a second variable, called secondJson, doing whatever you need to add your new component, adding it to the string.  In this example, I'll arbitrarily assume it is in the Description.  For your case, it appears to be hard-coded to "Rollout"
{{firstJson}} , { "name": "{{issue.description}}"}
  • Action: finally used advanced edit to replace the components
{
"fields": {
"components" : [ {{secondJson}} ]
}
}

You may combine the second and third steps (building the JSON) if you have a simple way to determine the component to add.

 

Best regards,

Bill

manas Sahoo January 21, 2021

Thank you Bill, I tried to copy paste as you have said and I am still getting an error

that Error while parsing additional fields. Not valid JSON.

 

Is there a way to know which one is causing the issues out of the below 3 steps

 

{{#issue.components}}{ "name": "{{name}}"} {{^last}}, {{/}}{{/}}

 

{{firstJson}} , { "name": "{{ "Rollout_Portugal" }}"}

 

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

manas Sahoo January 21, 2021

Ok  I found the issue in 2nd statement and changed it to 

{{firstJson}} , { "name": "Rollout_Portugal" }

 

But it fails if the jira has blank components, it works if the jira had components already. What is the best way to make it work for both scenarios ?

manas Sahoo January 21, 2021

I applied 2 branches and used a condition to use the above logic if components is already blank for the issue and that has worked well

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.
January 21, 2021

Hi!  It is this one: 

{{firstJson}} , { "name": "{{ "Rollout_Portugal" }}"}

I gave as an example using a field from the issue.  When you are using a hard-coded value you can remove the extra brackets.  Please try this one:

{{firstJson}} , { "name": "Rollout_Portugal"}

My apologies that my example led you astray.

__Bill

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.
January 21, 2021

Well done diagnosing the issues!  The action Log to write to the audit log can help solve such problems also.  For example, write pieces of JSON or field values to the log as you work to see where there are problems.

If this approach helped solved your question, please mark it as solved.  That will help others in the community find answeres to similar ideas faster in the future.  Thanks!

__Bill

Like manas Sahoo likes this
manas Sahoo January 21, 2021

Now I have created a rule to assign the component to all child objects when an epic has a certain label. Now I also want to make sure when a story is assigned to an epic with a specific label, we add the component to that story. I could not find the epic link as an field on field change event.  Do you know any way of catching the event of when a story of task is assigned to an epic ?

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.
January 21, 2021

Nope, and there are several defects/suggestions to add triggering on Epic Link changes:

https://codebarrel.atlassian.net/issues/?jql=project%20%3D%20AUT%20AND%20resolution%20%3D%20Unresolved%20AND%20description%20%20~%20%22epic%20link%22%20AND%20(description%20~%20%22trigger%22%20OR%20description%20~%20%22change%22)%20ORDER%20BY%20Key%20DESC

Your options may be to run a scheduled trigger rule, or to change an additional field that can be caught by the issue changed trigger.

manas Sahoo January 22, 2021

Thank you Bill. Is it possible to create a rule which says

 

On Issue create ,

Check the linked Epic's components and if it contains component "Rollout" 

Then on this new issue, add component Rollout

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.
January 22, 2021

Yes, I believe that is possible.

  • Trigger: issue created
  • Condition: trigger issue's component does not contain "Rollout" already
  • Branch: on the parent epic
    • Condition: epic's component contains "Rollout"
    • Actions... use the techniques we already noted to add "Rollout" to the trigger issue

Please try it and see what you get.

Also, you may want to consider creating a test project for experimenting on rules.  Then you can do what you wish and not worry about bending/breaking any existing project's issues.  When you have a rule working, you can re-create it in the target project, or ask your site-admin to copy it for you.

__Bill

Timo Vertanen August 11, 2022

Hi @manas Sahoo and @Bill Sheboy  and thanks for advices!

 

I'm trying the same as in this case but I got stuck in this:
"But it fails if the jira has blank components, it works if the jira had components already. What is the best way to make it work for both scenarios ?


I applied 2 branches and used a condition to use the above logic if components is already blank for the issue and that has worked well"

This is working for me too when there is already some component in the issue, but not when there isn't any components. Error is the same: 

Error while parsing additional fields. Not valid JSON.
 

So, can you give me an example for this: "I applied 2 branches and used a condition to use the above logic if components is already blank for the issue and that has worked well" 

I do have 2 branches and 2 more variables which are made almost like the 2 first variables but with different names and reference to name thirdJson:

{{#issue.components}}{ "name": "{{name}}"} {{^last}}, {{/}}{{/}}


{{thirdJson}} , { "name": "{{ "Rollout_Portugal" }}"}
 

 what are the differences in those 2 branch rules? What I'm doing wrong?

Best regards,

Timo

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.
August 11, 2022

Hi @Timo Vertanen 

For some context, would you please post an image of your complete rule?  That may help with suggestions on how to improve it.  Thanks!

Kind regards,
Bill

Timo Vertanen August 11, 2022

Hi @Bill Sheboy 

Hope these images helps:

First.pngSecond.png

third and fourth.pngthirdJson.png

 

fourthJson.png

components.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.
August 12, 2022

Hi @Timo Vertanen 

First a suggestion: please consider using one of the free browser addons (or a screen-capture tool) to capture a scrolling browser window.  That will help to capture your entire rule in one image, making it easier to see what is happening.

Next, your use case is different from the original one in this post.  You are using branches on multiple issues, and as a result the processing occurs in parallel and asynchronously...often leading to unpredictable results for edits like you are trying.

Before we proceed, would you please explain the problem you are trying to solve.  Knowing that will help determine how/if the problem can be solved with an automation rule.

Thanks,
Bill

Timo Vertanen August 14, 2022

Hi @Bill Sheboy 

In my case there are project A and project B. Issue is created in project A and in some cases it creates a linked issue to project B.
When user adds (editing or resolving issue) in project B, the value "Ohjelmistovirhe" to a custom field "juurisyy" (Select List (Multiple choices)), then it should automatically add component "Bugi" to linked issue in project A.


I assumed that this automation rule would work for me, but only works when there is already some else component in the issue in project A.

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.
August 15, 2022

Thanks for that information.  For your use case, there are a couple of approaches:

  • Conditionally process the components: use an if/else condition in order to handle the cases of "none" or "some" initial component values.  The different paths will lead to different actions for the additions later.
  • Add a "placeholder" component: early in the rule, always add a placeholder component of a known value.  Continue to process as you existing rule does.  At the end of the rule, remove the placeholder value.  Please note, this approach will not work if you are adding components within a branch.
Like Timo Vertanen likes this
Timo Vertanen August 16, 2022

@Bill Sheboy thank you for your help!

If my existing rule is not enough I'll try this "placeholder" solution.

Thanks,

Timo

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

@Bill Sheboy@Timo Vertanen  smart solution, I am just wondering why we cannot use add/remove, I haven't tested it, but according to Advanced field editing using JSON. In the section Supported Fields and Operations, it stays:

If you’re creating or editing issues, you can query a project’s createmeta or an issue’s editmeta information to view supported fields and operations.

if you put in your browser:

https://<YOURSITE>.atlassian.net/rest/api/latest/issue/<issueKey>/editmeta

I got add and remove add are available operations:

"components": {
"required": false,
"schema": {
"type": "array",
"items": "component",
"system": "components"
},
"name": "Components",
"key": "components",
"operations": [
"add",
"set",
"remove"
]

If it is possible, then the solution can be simplified.

Updated

Now I know the reason, add and remove only support single operation (i.e. one element to add or remove at a time), but not a list of elements, as per the following link: Could not find valid 'id' or 'name' in object when using the ADD operation for REST API.  You need to use the set operations, which allow more than one element, but this will set the field wit the values, it doesn't add the components to the existing component values.

Regards,

David

Like Bill Sheboy likes this

Suggest an answer

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

Atlassian Community Events