Receive only first instance of match

Nadege
Contributor
February 3, 2025

I am trying to create an automation that stores an issue key found within a comment. Currently I have the following:

 

ABC-{{comment.body.match("ABC-(\\d{1,6})")}}

 

Not sure why, but it sends the code twice e.g ABC-1234, 1234

It seems like it is writing down the same match twice (it is from a link so not sure if that affects).


Is there a way to prevent this so that I have ABC-1234 only? Thanks

2 answers

0 votes
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 3, 2025

Hi @Nadege 

You appear to be extracting issue keys from a comment body.  There are several things to handle for that scenario:

First, the match() function can have difficulty with comment formatting, such as newlines.  Thus it makes sense to parse those out first, perhaps by using the replace function to convert them to spaces.

Next, your comment likely contains a smart link, and so a specific issue key could be found multiple times.  That could be reduced using the distinct function: https://support.atlassian.com/cloud-automation/docs/jira-smart-values-lists/#list.distinct

Finally, are you certain your comment contains one-and-only-one issue key?  Perhaps it could contain several.

Putting those together, this expression would return the distinct keys found in the comment.  

{{issue.comment.last.body.replace("\n", " ").match("([A-Z]+-\\d+)").distinct}}

 

Kind regards,
Bill

Vishal Biyani
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 3, 2025

This works

0 votes
Vishal Biyani
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 3, 2025

@Nadege 

Comments loop over in an array. you should add a condition to break out of the loop when you get first match

refer this article and share the screen shot of your automation if you face challenges

https://community.atlassian.com/t5/Automation-articles/Filtering-smart-value-lists/ba-p/1827588

Nadege
Contributor
February 3, 2025

Thanks for the response.

Is this also the case when there is only one comment? (the comment it is reading from is the first comment).

Vishal Biyani
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 3, 2025

if there is only one comment and there is only item matching the pattern, then one value should be returned.

Can you share the screenshot of the rule that you have written so far?

Nadege
Contributor
February 3, 2025

Currently is something like the following: automation.png

It edits the field with the value I return above, but no matter how many tickets there are (be it one or multiple) with the same issue key, it always returns twice. 

I believe it has something to do with the link that is reading from, as when I type in manually the automation works fine,

 

However, from the link (although within the actual link itself it only occurs once) it returns the value twice

Vishal Biyani
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 3, 2025

one of the possible reasons that I can think of is your ABC-123 is getting converted into a hyperlink automatically and hence you are getting 2 values.

Can you check that?

Also, if your use case requires to check on latest comment then you can use this smart value {{issue.comments.last.body}}

Nadege
Contributor
February 3, 2025

Most likely that is what is happening, is there a way to filter it to get only one value? For example, the first occurrence?

Suggest an answer

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

Atlassian Community Events