Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

concatenate an item in to issue.labels.asJsonStringArray ?

Dane Kantner February 18, 2022

I'm trying to copy the array of labels from the original issue (when creating a new issue) _and_ add a new label at the same time... it seems to allow copy of old but not add at same time. I can get it to "add" two smart variables and have a variable for each of these, but the one is an array... and it gives formatting errors because it's not proper json when I add them next to each other. Is there a way to concatenate an item in to issue.labels.asJsonStringArray so it outputs one proper list?

3 answers

1 accepted

1 vote
Answer accepted
Mark Segall
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 18, 2022

Hi @Dane Kantner - If you're looking to add new labels, you could do so in a second action.  For the first action, create the issue and copy the labels.  Then create a branch on the newly created issue and edit the issue with the new fields you wish to add.  Something like this:

2022-02-18_15-04-09.png

Dane Kantner February 19, 2022

Thank you -- I didn't realize I could do a branch off of created issues specifically and was assuming I had to come up w/ the JQL to find it (and there is no valid JQL to find what I had just created, the label I'm trying to add is actually a unique identifier that I could search on but it doesn't exist yet)

0 votes
Stefan Salzl
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 19, 2022

I was facing same behaviour yesterday and was reading through documentation:
https://support.atlassian.com/cloud-automation/docs/advanced-field-editing-using-json/#Adding-labels

 

Documentation:
The documentation says:

Labels
A system field that is an array of text values.

"labels" : ["examplelabelnumber1", "examplelabelnumber2"] 

 

and further:
For fields that take arrays of text:

{
"fields": {
"labels": {{issue.parent.labels.asJsonStringArray}}
}
}

 

Description of the behaviour:
I want to add more than 1 labels from one issue to another one (eg. from parent to child) whereas the let´s say the child already got 2 labels and the parent has 2 different labels.

I tried to update a linked issue´s label field as follows:

{
"update": {
"labels": [
{
"add": {{destinationIssue.labels.asJsonStringArray}}
}
]
}
}

 

but audit log output says looks like:

image.png

 

@Darryl Lee , @Mark Segall :
Did I get anything wrong regarding the field or stringArray? Or could that be a bug?

 

Best
Stefan

Dane Kantner February 19, 2022

I am trying to take triggerIssue.labels.asJsonStringArray and add a value to it so that the original triggerIssue.labels are kept BUT ALSO adding another string value to that list.

Darryl Lee
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 19, 2022

@Dane Kantner Adding a label to the newly created issue should not affect the triggerIssue. 

As to @Stefan Salzlssue, it looks like the "add" function can only take a string, not an array. I think there's a way to expand out the array but I need to test if it works with "add"... standby. 

Like Stefan Salzl likes this
Dane Kantner February 20, 2022

I'm not trying to modify the trigger issue, I'm just trying to copy it labels array and add one more item to it to it in the newly created issue. The labels.asJsonStringArray does return the labels from the original issue properly as expected, and I can use that to add to the created issue, but the documentation says you can't set or add the fields twice, so it really needs to be added in to that string array in order to properly solve this issue, hence the question on how to concatenate to that array.  (The ability to add it after the fact as a branch edit is an OK work-around but it's not perfect b/c it does delay processing an extra second or two; a second edit is much more expensive than a local variable edit to the labels array and adding it in to the field from the origination of the new issue create; I wonder since the add to array functionality doesn't exist if I could use regex to transform the returned JsonStringArray to alter it to inject a new string?)

Darryl Lee
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 21, 2022

I really don't think that adding a label afterwards will slow it down that much, and it's a much more readable rule.

But since I'm a glutton for punishment...

Figured out how to make this work via Advanced editing using JSON using a few replaces, along with a split, and then another conversion back to an array:

{
     "fields": {
          "labels": {{issue.labels.asJsonStringArray.replace("[","").replace("]",",\"new-label\"").split(",").asJsonArray}}
          }
     }
}

Note that you can't set this using the normal field setting, because in that interface, the Labels field can only take strings that are separated out in the UI. I could not figure out a way to pass it a list/array directly, and giving it a comma-delimited string just created one really long label.

Like Dane Kantner likes this
Dane Kantner February 21, 2022

Thanks I will give that replace gig a whirl -- it's ugly to h ave to read, but I've seen worse. :P I will potentially need to test if the jsonarray is empty to begin with too for robustness sakes.

 

The issue of why a second edit is "expensive" compared to doing it all in one create or edit is I may be creating 20 issues sequentially rather fast, and each of those creates fires a trigger rule where it's then trying to group/classify the finding in to a "resource" issue type and creates a "resource" issue OR groups it in to that existing resource issue that may have just been created. In the subsequent triggers, a 1s delay means I have to essentially delay the actual finding creations themselves by the equivalent amount of time to guarantee it has been processed. I had to add a 1s delay in initial creations already after making the change as a branch edit. If I can take it from 1s extra processing to 10ms, that's a lot more throughput at EOD.

Like Stefan Salzl likes this
Darryl Lee
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 21, 2022

Ah, ok makes sense that you'd want to minimize extra branches with multiple issue creation.

Speaking of checking for an empty array, I didn't think about that until I tried to solve @Stefan Salzl's problem using a similar approach.

Please see my updated code below, that in fact checks to make sure the triggering issue's Labels array isn't empty.

Like # people like this
Philippe Lassalle August 23, 2023

@Stefan Salzl I have the exact same issue as you mentioned. I couldn't find a way to make update attribute with add operation works with an array as it works for fields attribute. Did you manage to find a way to make this work? 

@Darryl Lee thanks for the workaround you provided, the fact that it relies on the fields attribute (shortcut to update attribute with Set operation) doesn't help when we want just to use update with add operation. Any reason why update/add doesn't work as expected? 

Darryl Lee
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 23, 2023

Hi @Philippe Lassalle - can you post what you've tried, and what errors you may have gotten?

Are you getting the same error as @Stefan Salzl?

Operation value must be a string (labels)

The problem was Stefan was trying to basically do:

"add" : "Label1, Label2"

From the documentation, when you use add for labels (notably if you have multiple labels) they have to be in this format:

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

]
}
}

So use update labels is not a simple array which would work for fields. It is an array of add objects, and you have to format it accordingly.

Like Stefan Salzl likes this
Philippe Lassalle August 24, 2023

@Darryl Lee thanks that clarifies, I'll head into that direction, meaning formatting the issues.labels array accordingly. 

0 votes
Darryl Lee
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 18, 2022

The other approach that is fiddlier, but potentially more powerful, is to use Advanced Edit as part of Create New Issue:

Luckily there's an example in the documentation!

I think this would work:

{
    "update": 
        "labels": [{
                "add": "extra-label"
        }]
    }
}

 

Darryl Lee
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 21, 2022

Oops, my idea above isn't quite right, because I ended up with this error when trying to both Copy Labels from the Current Issue *and* add the extra one in the More options -> Additional fields JSON section:

Error creating issue
    Field 'labels' cannot appear in both 'fields' and 'update'

So then, as mentioned in the other thread, here's what ultimately did work, in case you wanted to also mark this answer as correct, since I think it's a little closer to what you were looking for.

Again, this would go in the More options -> Additional fields section when you Create a new issue:

{
     "fields": {
          "labels": {{issue.labels.asJsonStringArray.replace("[","").replace("]",",\"new-label\"").split(",").asJsonArray}}
          }
     }
}

Like Stefan Salzl likes this
Darryl Lee
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 21, 2022

Ooof, I realize this "fix" has the same problem as what I thought was going to fix @Stefan Salzl's issue:

I didn't account for the possibility that the original issue may not have *any* labels. Augh. So in that case I end up with an extra leading comma, which is not valid JSON:

"labels": [,"new-label"]

Ok, the fix is fixed. Ugh, sure would be nice if Automation had an ELSE function.

{
     "fields": {
         "labels": [
               {{#if(not(issue.labels.isEmpty))}}
                    {{#labels}}"{{.}}"{{^last}},{{/}}{{/}},"new-label"
               {{/}}
               {{#if(issue.labels.isEmpty)}}
                    "new-label"
               {{/}}
         ]
     }
}

This is *much* cleaner than all of that JsonArray hackery though. Whew.

Marc Isikoff July 18, 2023

Automation does have an "Else" function within branching for If statements it can create at the bottom of the automation paragraph an else that you can code. But if you mean "else" in the context of Additional Fields, then no.

Thomas Marshall - Network January 11, 2024

@Darryl Lee Thank you!

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events