The Atlassian Community Forums are currently in read-only mode. We will be relaunching on a new platform on September 22 (read more here). We apologize for the extended downtime. For concerns or questions, please email communitymanagers@atlassian.com. See you on the other side, on the new Atlassian Community Forums! :)

×

Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to set comment author as specific user with Groovy Script

Julia Nunes Pedrosa
Contributor
July 30, 2021

Hello everyone, 

I've installed myGroovy plugin on my Jira Data Center instance and I've been able to create awesome scripts to auto-comment issues based on specific workflow transitions.

Some of my scripts are working great, but I'm having issues when trying to set the author of a comment to an specific user used in our automations.

In my previous scripts I had successfully set the author to the Current User (When closing an issue), but now I'd like to comment on the creation of the issue, so I could not use the "Logged In" user cause it would look confusing to the reporter to create the issue and see an automated comment written by himself.

That's what I've got so far.

This one works fine, but sets the comment to the Current User:

import com.atlassian.jira.component.ComponentAccessor

final boolean dispatchEvent = true
final String commentBody = """ Hi [~${issue.reporter.name}]Comment text here."""
def author = ComponentAccessor.jiraAuthenticationContext.loggedInUser

ComponentAccessor.commentManager.create(issue, author, commentBody, dispatchEvent)

 This one isn't working. The username of the user I'm trying to set as the author of the new comment is "jira_automation":

import com.atlassian.jira.component.ComponentAccessor

final boolean dispatchEvent = true
final String commentBody = """ Hi [~${issue.reporter.name}]Comment text here."""
def author = ComponentAccessor.userManager.getUserByName(jira_automation)

ComponentAccessor.commentManager.create(issue, author, commentBody, dispatchEvent)

 

1 answer

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

3 votes
Answer accepted
Payne
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 Champions.
July 30, 2021

Try surrounding the username in quotes in the call to getUserByName(); that method expects a String argument, so by not surrounding it in quotes, it is likely being treated as an undefined variable.

Julia Nunes Pedrosa
Contributor
July 30, 2021

OMG, it worked!!

Thank you so much, Payne!

Like Payne likes this
DEPLOYMENT TYPE
SERVER
TAGS
AUG Leaders

Atlassian Community Events