If a user is mentionned in a comment, add it as watcher (data center)
Asked by Garn:

Hi,

My goal is to add as watcher the first person tagged in a comment (no iterative branch in this case). I'm in a data center instance.

![automation watcher.png](https://community.atlassian.com/t5/image/serverpage/image-id/342881i20F953DCE3B88384/image-size/large?v=v2&px=999 "automation watcher.png")

I followed the recommendation from Bill in this topic :

<https://community.atlassian.com/t5/Jira-questions/Automation-Help-Add-watcher-when-user-mentioned-in-comments/qaq-p/2121329>

I have done a test by creating a comment with my coworker tagged "Test Automation @mycoworker" and checked in the audit log : the @mycoworker is not recognized.

It looks like the smart value is not working for me:

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

![automation watcher 2.png](https://community.atlassian.com/t5/image/serverpage/image-id/342882iFB24FB05CAD7DFA0/image-size/large?v=v2&px=999 "automation watcher 2.png")

Any help on this please?

Thanks,

Regards


Answers:
author: Bill Sheboy
body: Hi [@Garn](/t5/user/viewprofilepage/user-id/5429711)

*First thing, I am using Jira Cloud and not Data Center, and so please test the solutions I offer with your site.*

The other question you linked to uses a solution approach for Jira Cloud, which includes a user's account id to implement a mention. However, I believe Jira Server and Data Center use a different mechanism with the user name.

Let's confirm that with your test rule: add a write to the audit log with this:

```
last comment body: {{issue.comments.last.body}}
```

Test your rule and review the log. I suspect the entry looks like this:

```
last comment body: here is my comment mentioning [~billsheboy] in the sentence
```

If that is the case, you may adjust the match() and remove() function parameters accordingly.

Kind regards,  
Bill

comments: author: Garn
body: Hi Bill,

Thank you for your help!

It displays:  

```
last comment body: Test Watcher [~201034280]   
```

With 201034280 being the identifier of the user.

Could you please share what it looks like in cloud so I can understand how to adjust match and remove functions please?

Thanks,


author: Bill Sheboy
body: Thanks for testing that!

For Jira Cloud, the mentions work like this, where 12345abcde would be the user's Atlassian account identifier:

```
[~accountid:12345abcde] 
```

And so a change for your version to get the user identifier could be this:

```
{{issue.comments.last.body.split(" ").match("(\[~.*\])").substringBetween("[~","]")}}
```

Please test this for your instance.


author: Garn
body: Yes, thanks, we're almost there!  

Now I have an error on the manage watchers action:

![automation watcher 4.png](https://community.atlassian.com/t5/image/serverpage/image-id/343394i1A9CEA78252E74D5/image-size/large?v=v2&px=999 "automation watcher 4.png")

In my action, I'm adding as watcher my variable {{addwatchers}}

![automation watcher 3.png](https://community.atlassian.com/t5/image/serverpage/image-id/343393i8238BA91611AC2C9/image-size/large?v=v2&px=999 "automation watcher 3.png")

Do you have any clue on the error right here?   

Thank you,


author: Bill Sheboy
body: I do not recognize that error message. Let's check on some things to narrow down that symptom...

First, check if the user mentioned can be added manually to the issue as a watcher, and then remove them.

Next, modify your *write to the audit log* for the variable to this and retest. This will identify if there are any stray characters leading / following the user identifier. If there are, they may be removed with the trim() function added to the end.

```
addwatchers: ZZ{{addwatchers}}ZZ
```

Finally, let's try to set the watch using the user display name. This will confirm if the watcher cannot be set with the user identifier in a rule action.

To try this, copy the Create Variable action and in the second one, later in the rule, set the value to the exact display name for the user.


author: Garn
body: Hi Bill,

I confirm that I can manually add the user on the ticket.

I have updated the audit log with : addwatchers: ZZ{{addwatchers}}ZZ

The updated audit log displays :

```
Log action

```

Log  
addwatchers: ZZ201034280ZZ

```


```

I have created two variables:

watcher : \~201034280

watchertwo : 201034280

watcher three : FirstName LastName(201034280)

And the audit log:

watcher : {{watcher}} -- {{watchertwo}} ----- {{watcherthree}}

Please find the results with the same error:

![automation watcher.png](https://community.atlassian.com/t5/image/serverpage/image-id/343755i33CD86098EE590C7/image-size/large?v=v2&px=999 "automation watcher.png")


author: Garn
body: [@Bill Sheboy](/t5/user/viewprofilepage/user-id/1685313) any idea?


author: Bill Sheboy
body: Hi [@Garn](/t5/user/viewprofilepage/user-id/5429711)

Sorry for the delay in responding as I have been offline for a few weeks.

Thanks for confirming what the variable contains, as the one with just the id should work in the action. (Unless those ids in Jira Data Center are numbers...in which case conversion is needed with asNumber.)

Would you please post an image of your current, complete rule and an image of the action where you Create Variable and where you use that with Manage Watchers?


author: Garn
body: Yes, let me give you here the different codes for variables and the audit log to ease the understanding:

![aut1.png](https://community.atlassian.com/t5/image/serverpage/image-id/346485i19120A4793C51407/image-size/large?v=v2&px=999 "aut1.png")

Variables:  
addwatchers : {{issue.comments.last.body.split(" ").match("(\\\[\~.\*\\\])").substringBetween("\[\~","\]")}}

asNumber : {{issue.comments.last.body.split(" ").match("(\\\[\~.\*\\\])").substringBetween("\[\~","\]").asNumber}}

![aut2.png](https://community.atlassian.com/t5/image/serverpage/image-id/346484i07590D6C430428B9/image-size/large?v=v2&px=999 "aut2.png")

audit log2: add watchers: {{addwatchers}} ------- addwatchers with zz: ZZ{{addwatchers}}ZZ ---------- asNumber: {{asNumber}}

watcher : \~201034280

watchertwo : 201034280

watcherthree : Thomas AXXXXXXXXXX(201034280)

audit log3: watcher : {{watcher}} -- {{watchertwo}} ----- {{watcherthree}}

![aut3.png](https://community.atlassian.com/t5/image/serverpage/image-id/346486iB682FB34878AEAF6/image-size/large?v=v2&px=999 "aut3.png")

---- and now the audit log:

![aut4.png](https://community.atlassian.com/t5/image/serverpage/image-id/346487i1D7418CFFD8B211B/image-size/large?v=v2&px=999 "aut4.png")

Looks like my asNumber is not working in the log action 2.


author: Bill Sheboy
body: asNumber is a function and so I advise not using that as a variable name to avoid confusing the rule.

Next, you are trying to add multiple things at one time as watchers and so that could be causing challenges seeing what works, or does not. Let's simplify to *one single test* and evaluate what happens:

* trigger: issue commented
* action: create variable:
  * name: varMentionedUsers
  * smart value:

```
{{issue.comments.last.body.split(" ").match("(\[~.*\])").substringBetween("[~","]").join(",")}}
```

* action: write the created variable to the audit log

```
varMentionedUsers contains: {{varMentionedUsers}}
```

* action: manage watchers, adding this:

```
{{varMentionedUsers.split(",")}}
```

Please note I explicitly added the join() and split() to avoid problems with the list being interpreted incorrectly.


author: Garn
body: Hi Bill,

Thank you a lot for your help and not giving up this topic ?  
Still the same error.

![auto2.png](https://community.atlassian.com/t5/image/serverpage/image-id/346987i145A060313E5646D/image-size/large?v=v2&px=999 "auto2.png")![auto.png](https://community.atlassian.com/t5/image/serverpage/image-id/346986i7584D7B68BAFF85F/image-size/large?v=v2&px=999 "auto.png")


author: Bill Sheboy
body: In the add watchers, please try using this instead:

```
{{varMentionedUsers.split(",").asNumber}}
```

That will confirm if a number is required (rather than text from the variable).


author: Garn
body: Hi Bill,

Still the same for me even with the change

![Auto.png](https://community.atlassian.com/t5/image/serverpage/image-id/347651i7BC7EB78A9085EC0/image-size/large?v=v2&px=999 "Auto.png")


author: Bill Sheboy
body: I am unclear why this is not working for your rule with a text (or a number) value for the watcher id. And I am certain setting the watcher from a variable works for Cloud and for other posts I have seen for Data Center.

I recommend working with your Jira Site Admin to submit a ticket to the Atlassian Support team. Perhaps they will see something we are missing. <https://support.atlassian.com/contact/#/>


Tags: automation

data-center

jira-data-center

watcher