Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,293,534
Community Members
 
Community Events
165
Community Groups

Copy username from comment to User picker field

Hello Everyone,

I would like to create Project automation where we can copy the username when a comment is added to a user picker field.

Any idea how this can be done?

BR,

2 answers

1 accepted

Hi @Sal ,

I copied my reply in this separated answer; so, if it's ok for you, you can accept this. Thank you.

 

let be "Users in last comment" the user picker (multi-user) that we want fill with users in last comment.
Then add a new custom Script Listener that starts on "Issue Commented" event and set in Script this code:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.user.ApplicationUser

def issue = event.issue

def cfUsersInLastComment = ComponentAccessor.customFieldManager.getCustomFieldObjectsByName("Users in last comment").first()

def comments = ComponentAccessor.commentManager.getComments(issue)

String strComment = comments.last().body

List<String> userNames = strComment.findAll("\\[~.*?\\]")

def users = new ArrayList<ApplicationUser>()

userNames.each {users.add(ComponentAccessor.userManager.getUserByName(it.substring(2, it.length()-1)))}

cfUsersInLastComment.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cfUsersInLastComment), users), new DefaultIssueChangeHolder())

So, when a new comment is added to the issue, users in comment will be added to the user picker field.

Note: Each User in comments must be written as: @username (by selecting the one you want)

Hello @Andrea Pannitti,

Sorry, I was away for a few days. This is working as you said. But my scenario is a little different.

It is like I have a single select field and it should only populate when there is some specific word in a comment such as "additional" then "username". It will pick the username from the comment to the single select field and delete the comment.

I am new to groovy and not sure about the changes that can be done here.

Thanks in advance!

Hi @Sal ,

I'll answer you by points:

  1. To identify the users entered in the comment, the users must necessarily be entered as I said in my previous answer, otherwise there could be problems in finding them.
  2. Checking if the comment contains a certain word is not a problem but for what has been said in point 1 it should not be necessary
  3. Deleting the comment is also not a problem

Thus, it seems that the proposed solution lacks only the deletion of the comment.
If not, what is wrong for you?
I would ask you to be as precise as possible in order to clarify what you need.

Thanks

Hello @Andrea Pannitti,

When using a multi-user picker field it's working fine but I am not able to use this script for a single user picker field.

BR,

Hi @Sal ,

I think your problem depends on the fact that from the comment are extracts all users and therefore you have a list of users.
If so, try changing the last line of the previous code as follows:

cfUsersInLastComment.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cfUsersInLastComment), users.first()), new DefaultIssueChangeHolder())

Hello @Andrea Pannitti

This is working as expected. Thank you very much for your help!

BR,

0 votes
Brant Schroeder Community Leader May 10, 2022

@Sal Are you coping the username from the individual who created the comment?  If so you would need to get the comment author using the following smart value {{comment.author.displayName}}   This could then be added to a custom field.  I am not sure if you will be able to set a user picker field with it.  You would have to try.

@Brant Schroeder Sorry, I was not clear. When a user comments on a ticket with something like "add username" then the username should be added to a user picker custom field and delete the comment.

Hi @Sal,

this issue can be solved by ScriptRunner. Have you ScriptRunner?

@Andrea Pannitti I have script runner and automation for jira  as well.

Ok @Sal ,
let be "Users in last comment" the user picker (multi-user) that we want fill with users in last comment.
Then add a new custom Script Listener that starts on "Issue Commented" event and set in Script this code:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.user.ApplicationUser

def issue = event.issue

def cfUsersInLastComment = ComponentAccessor.customFieldManager.getCustomFieldObjectsByName("Users in last comment").first()

def comments = ComponentAccessor.commentManager.getComments(issue)

String strComment = comments.last().body

List<String> userNames = strComment.findAll("\\[~.*?\\]")

def users = new ArrayList<ApplicationUser>()

userNames.each {users.add(ComponentAccessor.userManager.getUserByName(it.substring(2, it.length()-1)))}

cfUsersInLastComment.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cfUsersInLastComment), users), new DefaultIssueChangeHolder())

So, when a new comment is added to the issue, users in comment will be added to the user picker field.

Note: Each User in comments must be written as: @username (by selecting the one you want)

Thank you @Andrea Pannitti !

I will test this on my instance.

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Confluence

Confluence: Where work and wellness meet

Feeling overwhelmed by the demands of work and life? With a 25% increase in the prevalence of anxiety and depression worldwide during the pandemic, for most of us, it’s a resounding yes . 🙋‍♀️ ...

749 views 5 21
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you