How to auto-add watcher from the person at in the latest comment?
Asked by Craig Change:

I comment a jira as follows:

hi [@Lily](/t5/user/viewprofilepage/user-id/169046) @\[deleted\] it's a lovely day, isn't it?

I want to use jira automation to add lily and tom as the watchers, but don't know how to fulfill this. Could any guy help?

Thanks,

Craig


Answers:
author: Bill Sheboy
body: Hi [@Craig Change](/t5/user/viewprofilepage/user-id/2425530)

Until that suggestion is implemented, you could built this functionality yourself using Automation for Jira. For example:

* trigger: issue commented
* action: create a variable, and use text functions and regular expressions to extract the mentioned users account id values
* condition: advanced compare to check if there were any mentions
* advanced branch: to iterate over the created variable as a list
  * action: add watchers for each mentioned user

To learn more about automation rules, please see this documentation and examples:

* <https://www.atlassian.com/software/jira/guides/expand-jira/automation>
* <https://www.atlassian.com/software/jira/automation-template-library#/label/all/1453>
* <https://community.atlassian.com/t5/Automation-articles/Jira-Automation-all-the-resources-you-need-in-one-page/ba-p/1388465>

Kind regards,  
Bill

comments: author: Kit Friend
body: Hey [@Bill Sheboy](/t5/user/viewprofilepage/user-id/1685313) - I'm a newbie to variable writing - do you have an example of what the action would look like here? We're trying to achieve the same thing


author: Bill Sheboy
body: Hi [@Kit Friend](/t5/user/viewprofilepage/user-id/4628581)

Here are more details on what I described to extract the account id values from the comments...

* trigger: issue commented
* action: create variable...
  * name: varAddWatcher
  * smart value:

```
{{issue.comments.last.body.replace("[~accountid"," [~accountid").split(" ").match("(accountid:.*)]").remove("accountid:")}}
```

* advanced compare condition: check if {{varAddWatcher}} does not equal empty
* advanced branch...
  * smart value: {{varAddWatcher.split(",")}}
  * variable name: varWatcher
    * action: manage watchers
      * add these watchers: {{varWatcher}}

Kind regards,  
Bill

**UPDATED** 2023-Feb-06: I updated the expression for varAddWatcher to split on spaces before searching for the accountId values.

**UPDATED** 2023-Jun-15: In the unlikely case there is no leading space between mentions, I updated the smart value expression to add a space before mentions and then split on them.


author: Kit Friend
body: Thanks [@Bill Sheboy](/t5/user/viewprofilepage/user-id/1685313) really kind of you!


author: Kit Friend
body: [@Bill Sheboy](/t5/user/viewprofilepage/user-id/1685313) have you had this working successfully for multiple users being tagged? It's working fine for me on single users, but errors when there's more than one. I'm wondering if it's to do with the initial variable creation action only creating the first one as a smart value...


author: Bill Sheboy
body: Hi [@Kit Friend](/t5/user/viewprofilepage/user-id/4628581)

Here is the fix below. This rule which I believed worked in the past stopped working when there were *multiple mentions on the same text line*. The improvement below first splits the comment by spaces into separate lines, and then tests each entry to find the accountId.

{{issue.comments.last.body.split(" ").match("(accountid:.\*)\]").remove("accountid:")}}

I tested this successfully for a few scenarios; please let me know if you see any other problems with it.

Thanks,  
Bill


author: Kit Friend
body: [@Bill Sheboy](/t5/user/viewprofilepage/user-id/1685313) I've been getting a (low number of) persistent errors where if multiple mentions are on the same text line *without* spaces or any punctuation between them, the amended rule fails. Did this come up in your testing?

i.e. this works fine:   
![CleanShot 2023-06-14 at 17.03.49.png](https://community.atlassian.com/t5/image/serverpage/image-id/263061i66E813CAF67155E5/image-size/large?v=v2&px=999 "CleanShot 2023-06-14 at 17.03.49.png")

but this fails:

![CleanShot 2023-06-14 at 17.03.55.png](https://community.atlassian.com/t5/image/serverpage/image-id/263060i6CEA43F494D43D6B/image-size/large?v=v2&px=999 "CleanShot 2023-06-14 at 17.03.55.png")


author: Bill Sheboy
body: Hi [@Kit Friend](/t5/user/viewprofilepage/user-id/4628581)

No, I had not tried that scenario...and it failed for me also. I had to explicitly delete the space after the first mention to create this condition.

One work-around is to always ensure there is a space before the mention text and then split:

{{issue.comments.last.body.replace("\[\~accountid"," \[\~accountid").split(" ").match("(accountid:.\*)\]").remove("accountid:")}}

I will update my earlier answer to include this also.

Kind regards,  
Bill


author: Mark Lendrum
body: Thank you, very helpful


author: Mark Lendrum
body: [@Bill Sheboy](/t5/user/viewprofilepage/user-id/1685313) The auto-add doesn't seem to work when I mention a user without any preceding text or space before the mention. If there is a space or word ("Hi") then it does work. Is there a way to achieve this?


author: Bill Sheboy
body: Hi [@Mark Lendrum](/t5/user/viewprofilepage/user-id/5478106) -- Welcome to the Atlassian Community!

This scenario and solution approach has changed several times in community posts. Please try this one:

```
{{issue.comment.last.body.split(" ").match("\[~accountid:(.*)\]").distinct}}
```

That will produce a list of unique user account id values that were mentioned.

Kind regards,  
Bill


author: Craig Change
body: <https://jira.atlassian.com/browse/JSWCLOUD-21006>


Tags: cloud