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

Replace multiple values in a String

Paulo Amaral January 5, 2024

I have an automation that sends the comments made to an issue to a Slack channel, replacing the user tag in Jira side with the appropriate user tag on Slack side (so people get notified).

 

What works so far:

On Jira, the mention appears as `[~accountid:7120...ff4600c98]`. I implemented a replace action (actually many of them in one rule) to change each account to Slack's  corresponding string `<@U5D3UAZHC0J>`. To perform replace after replace, I save the comments filed to a variable {{message}}, and using "Create Variable" multiple times, I replace each entry and update {{message}} (as in the screenshot). The field "Smart Value" of these replacement is hard-coded for all.

image.png
This works fine, but the maintenance is painful. As the list grows, it gets more difficult to find things and make changes (because of so much repetition, the fields are short and don't allow me to see the values well, etc.). Is there a smarter way to do this? 
What I have tried
I tried a couple of things involving the advanced branching, to later realise it runs in parallel and whatever is done in the branch is lost. The replacements made in the loop didn't affect the variable {{message}}. I see the reason for performance purposes, but it doesn't make sense to me not having a plain and normal synchronous loop.
I tried having all values in a Lookup Table component, and managed to iterate over them. I also tried to use slip() to break a string that contains all data colon-and-semicolon-separated: "user1-jira-account,user1-slack-id;user2-jira-account,user2-slack-id;" (and so on). Both ways worked for me, but I can't find how to update the message without repeating the call to .replace a multiple times.
Has anyone tried something similar? Ideas to share?
Thanks and regards
Paulo

1 answer

1 accepted

1 vote
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 7, 2024

Hi @Paulo Amaral 

Short answer: there is no synchronous processing, loop structure for automation rules at this time.  And even if there was, smart value, list iterator scoping may prevent accessing the data you need to preform the search / replace operations.

 

Longer answer:

Context is important for automation rules.  Would you please describe what problem are you trying to solve?  That is, "why do this?"  Knowing that and seeing your complete automation rule may reveal other solution approaches.

In the meantime, the method you described chaining replacements may reduce potential errors and improve rule performance.  For example:

{{issue.comment.last.body.replace(firstAccountIdStringfirstReplacement).replace(secondAccountIdStringsecondReplacement).replace(thirdAccountIdStringthirdReplacement)}}

 

Kind regards,
Bill

Paulo Amaral November 5, 2024

Hi Bill, thanks for answering. I tried your suggestion and it worked for me - using .replace(...).replace(...).replace(...).replace(...) . Not ideal but gets the job done.

Answering your question, what I tried to solve is: send comments to Slack using the "Send web request" action, but showing real user names instead of the accountid code.

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.
November 5, 2024 edited

Hi @Paulo Amaral 

Since you posted your original question, it appears the text function now converts account id values to the display name for users, eliminating the need for the chained replace() functions.  

{{issue.comments.last.body.text}}

One side-effect is this will also remove other formatting in the comment body.

Kind regards,
Bill

Paulo Amaral December 24, 2024

Hi Bill, thanks for following up. In my case, I don't want just to display names, but to display a special tag. The same user has a accountid on atlassian, and an ID on Slack (messaging app). I want to replace the accound it with the Slack tag. But this is useful to know, thanks.

Like Bill Sheboy likes this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Upcoming Atlassian Automation Events