Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Automation rule to sync labels between a JSM ticket and its associated JS ticket

Drew Nedderman
Contributor
June 24, 2021

We have a JSM project where we collect issues from clients. When an agent can't resolve an issue, it is passed to a developer to take a look. If the developer finds a bug, they create a JS ticket and link it back to the JSM ticket.

I'm attempting to copy labels from the JSM ticket to the JS ticket when the link is made, then if the JSM ticket's labels are updated, have the JS ticket's labels be updated as well. I'm doing this with Automation for Jira.

I can do the first portion of that; copy labels when links are made.

The second portion is tricky. I've found where I can get a list of labels from the linked issues via smart values, and I can return them using the audit log action

2021-06-24 08_14_48-Automation rules - JIRApng.png

In order to extract the labels from the JSM ticket so they can be added to labels on the JS ticket, I need to do this {{#issue.labels}}"{{.}}"{{^last}},{{/}}{{/}} to get the labels surrounded by double quotes and comma-separated. Otherwise it gets the list of labels as a single label and throws an error about spaces not being allowed in labels.:

2021-06-24 08_37_37-Automation rules - JIRA.png

The part that appears to be tripping me up is when I'm editing labels using the More options section of the Edit issue action.

 

2021-06-24 08_17_13-Automation rules - JIRA.png

I'm attempting to pass this into the More options field:

{
"update": {
"labels": [
{
"add": ""{{#issue.labels.issuelinks.inwardissue.labels.asJsonStringArray}}"{{.}}"{{^last}},{{/}}{{/}}""
},
{
"add": ""{{#issue.labels.issuelinks.outwardissue.labels.asJsonStringArray}}"{{.}}"{{^last}},{{/}}{{/}}""
}
]
}
}

 I keep getting this in it the audit logs:

2021-06-24 08_24_42-Automation rules - JIRA.png

I've tried using single quotes to surround the entire value and I've tried only using one set of double quotes to surround the entire value with no success.

Any help here with properly formatting the JSON would be great!

 

1 answer

0 votes
Hyrum Steffensen _Appfire_
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.
June 24, 2021

Hello Drew,

If a quote is intended as a value in a JSON object, use an escaped quote: \"

For example, using the JSON from above:

{
    "update": {
        "labels": [
            {
                "add": "\"{{#issue.labels.issuelinks.inwardissue.labels.asJsonStringArray}}\"{{.}}\"{{^last}},{{/}}{{/}}\""
            },
            {
                "add": "\"{{#issue.labels.issuelinks.outwardissue.labels.asJsonStringArray}}\"{{.}}\"{{^last}},{{/}}{{/}}\""
            }
       ]
    }
}

When a quote is not escaped, the interpreter sees the quote as the beginning or ending of a string. With the escaped character, the interpreter sees the quote as a string literal.

Please let us know how it goes!

Regards,

Hyrum

Drew Nedderman
Contributor
June 24, 2021

Hi Hyrum, thank you for the explanation! I tried the JSON you provided and there are no longer errors with advanced editing as far as the automation is concerned.

However, I'm running into an issue where the linked issue is not getting the correct labels. Here's a screenshot of what the labels look like after running the rule:2021-06-24 17_17_49-[CT-761] Test for Label Copying - JIRA.png

That was after adding two labels at the same time to the trigger issue. I'm not sure what to make of this. Any thoughts? Thank you!

Hyrum Steffensen _Appfire_
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.
June 28, 2021

Hello Drew,

My apologies for the delay. I have been trying to wrap my head around using the advanced fields for linked issues. I found that I could only set labels from linked issues, not add them.

I found a quick and dirty way to accomplish this would be to use the UI like this:

Screen Shot 2021-06-28 at 1.46.10 PM.png

 

If I understand your scenario correctly, the script would run after you linked the JSM issue and would copy the labels from the JSM issue to the linked issues.

If you are still interested in using the advanced fields configuration, take a look at this gem (a youtube clip) by @Ravi Sagar _Sparxsys_. The idea here is that you can log the output of those smart values.

Screen Shot 2021-06-28 at 1.55.45 PM.png

Now that I have the ability to see the value that is being outputted, it will be much easier to see find a viable solution. I will revisit this tonight once I have more time.

Regards,

Hyrum

Like Ravi Sagar _Sparxsys_ likes this
Ravi Sagar _Sparxsys_
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.
June 28, 2021

Thanks for sharing @Hyrum Steffensen _Appfire_ 

Hyrum Steffensen _Appfire_
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.
June 28, 2021

Absolutely, Ravi! I only wish I had found your youtube clip earlier! 

Regards,

Hyrum

Drew Nedderman
Contributor
June 29, 2021

Hi, @Hyrum Steffensen _Appfire_ Thank you for continuing to look at this! I believe I've seen the video by @Ravi Sagar _Sparxsys_ and I've used that advice in other rules where I needed to use the audit log to make sure I was extracting the correct information from smart values.

Hyrum, you are correct that I need to add labels instead of set labels. This means that I need to use the advanced options.

I'm now trying to use this in the advanced field:

{
"update": {
"labels": [
{
"add": "{{#issue.labels}}\"{{.}}",{{/}} \"{{#issue.labels.issuelinks.inwardissue.labels.asJsonStringArray}}"\"
},
{
"add": "{{#issue.labels}}\"{{.}}\",{{/}} \"{{#issue.labels.issuelinks.outwardissue.labels.asJsonStringArray}}"\"
}
]
}
}

When I use a JSON formatted plugin in Notepad++, everything looks good with the above, but when I add it to he advanced field I can't enable the rule because I get this error:

Error while parsing additional fields - Failed to close 'issue.labels.issuelinks.outwardissue.labels.asJsonStringArray' tag

Any thoughts there? Thank you!

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.
June 29, 2021

Hi @Drew Nedderman 

I recommend that you extract the labels into a Created Variable action, and log that to the audit log.  That will help you diagnose what is happening.

When it is working, use the created variable in your JSON update.

Best regards,

Bill

Drew Nedderman
Contributor
June 29, 2021

Hi @Bill Sheboy , Thanks for the help here. I've added {{#issue.labels}}"{{.}}"{{^last}},{{/}}{{/}} to a created variable and set an action to log it. So far, I get empty results. I'm not sure what I'm doing wrong here. I haven't used the Create Variable before so I may be doing something incorrectly. What could I be doing wrong here?

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.
June 29, 2021

Drew, I put your exact smart value string in the audit log, and it worked for me:

{{#issue.labels}}"{{.}}"{{^last}},{{/}}{{/}}

Then, I tried creating a variable, which I gave the name of varLabels, and then in another action sent to the audit log as {{varLabels}} and it worked again.

Would you please post an image of what your rule looks like with these sections?  That may reveal where there is a problem.  Thanks! 

** And...regarding the attempt to get at the linked issues labels, I do not believe that is possible from the context of the trigger issue.  The links are available, but not the details of each of the linked issue.  Instead you would need to visit each linked issue, perhaps with branches.

Drew Nedderman
Contributor
July 1, 2021

Hi @Bill Sheboy , here's the rule I have set up. The Create Variable action has the smart value string from your last comment.

I'm able to get the labels from a linked issue, they can be seen in the 3rd audit log action, but they are enclosed in brackets. I'm not sure what that's about.

2021-07-01 10_55_32-Window.png

Hyrum Steffensen _Appfire_
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.
July 1, 2021

Hello Drew,

I am able to copy labels from a parent, but I was not able to copy labels from linked issues. I will show you what I tried, what worked, and what did not.

[imagining what success looks like]

Whenever dealing with code, I like to imagine what the goal is. Here is what our basic JSON should look like if we were creating labels without using any smart values.

{
    "update": {
        "labels": [
            {
                 "add": "label1"

            },
            {
                "add": "label2"
            }
        ]
    }
}

Unfortunately, the JSON we have been using seems to use "add" once with all the labels lumped together.

Here is the code we are using:

{"add":{{#issue.labels}}"{{.}}"{{^last}},{{/}}{{/}}}

Unfortunately this does not give us the desired result. Here is what the JSON looks when printed to the audit log:

{"add":"label1","test"}

Instead, we need to parse an "add" element for each label. For example:

{"update":{"labels":[{{#issue.labels}}{"add":"{{.}}"{{^last}}},{{/}}{{/}}}]}}

This produces the desired result as printed in the audit log:

{
    "update": {
        "labels": [
            {
                "add": "label1"
            },
           {
               "add": "label2"
            }
        ]
    }
}

However, while I was able to produce the desired JSON in the audit logs, I was not sure it would work for real. 

[copying labels from a parent]

The reason I decided to try copying labels from the parent is because it is explicitly stated in the documentation that these values are accessible. I swapped out "#issue.labels"in the above JSON for "#issue.parent.labels".

{"update":{"labels":[{{#issue.parent.labels}}{"add":"{{.}}"{{^last}}},{{/}}{{/}}}]}}

When I passed the JSON into the "advanced" field, it worked as expected.

Screen Shot 2021-07-01 at 10.17.46 PM.png

When I transitioned a Sub-task to "In Progress", it successfully copied the labels from the parent.

[getting labels from linked issues]

Looking at this Atlassian Community post, the smart values for inward and outward links are:

{{issue.issuelinks.inwardIssue}}
{{issue.issuelinks.outwardIssue}}

This prints out the linked issues just fine in the audit log. However, when I try printing out any values of these links, I unfortunately do not see any output.

{{issue.issuelinks.inwardIssue.summary}}

Just to make sure, I tried out the smart value using the formula we know works. Unfortunately, it did not work.

{{#issue.issuelinks.inwardIssue.labels}}

Screen Shot 2021-07-01 at 10.44.56 PM.png

[things to try next]

Perhaps there is a way to get values from linked issues in a way I have not thought of. However, take a look at this Atlassian Automation blog post. I think what you can do instead is to use the {{destinationIssue}} or {{issue}} smart value that is available when an issue is linked.

I seemed to have much better success using this method. Try:

{"update":{"labels":[{{#issue.labels}}{"add":"{{.}}"{{^last}}},{{/}}{{/}}}]}}

or 

{"update":{"labels":[{{#destinationIssue.labels}}{"add":"{{.}}"{{^last}}},{{/}}{{/}}}]}}

 

Screen Shot 2021-07-01 at 11.58.40 PM.png

See this Atlassian documentation on triggers for ideas.

[using a third-party plugin]

Another possibility is using a third-party plugin as found on the Atlassian Marketplace. For example, a SIL script using Power Scripts would look something like the following:

string [] linkedIssues = linkedIssues(key);
for (string linkedIssue in linkedIssues) {
    labels = labels + linkedIssue.labels;
}

The idea here is that you would still use Jira Automation as a listener, but you would let Power Scripts perform the action. This will give you the flexibility you are looking for, but would be much easier to implement.

See this screenshot on selecting a SIL script as an action:

Screen Shot 2021-07-01 at 11.09.29 PM.png

[summary]

This is an entirely interesting and challenging issue, Drew! Thanks for sharing it. I will still keep thinking of ideas and try different things out. 

Regards,

Hyrum

Please note that I am a product engineer for Power Scripts and that I work for Anova Apps, an Appfire company.

Hyrum Steffensen _Appfire_
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.
July 8, 2021

Hello Drew! How's everything going?

Suggest an answer

Log in or Sign up to answer