Auto-Comment with Script Runner Grovvy Script

hello everybody,

i want to generate a comment over script runner and a groovy script when executing a transition. 

that is the script. i want to read the value in the reporter-field

import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.user.*;
import com.atlassian.jira.issue.fields.DefaultFieldManager;

ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().getUser();
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

reporter = ""+issue.getFieldValue( ComponentAccessor.getFieldManager().getFieldObjectByName("Reporter")).displayName
wertreporter = issue.getFieldValue(reporter)


reporter = "Hallo *" + wertreporter + "*, \
\n\ndein Notebook wird vorbereitet.\
\n\nGrüsse\
\nTechnical Services"

CommentManager commentMgr = ComponentAccessor.getCommentManager()
commentMgr = (CommentManager) ComponentAccessor.getComponentOfType(CommentManager.class)

//Der wirkliche Kommentar
commentMgr.create(issue, currentUser, reporter, true)

this is the error:

Time (on server): Wed May 24 2017 13:13:53 GMT+0200 (W. Europe Daylight Time)
The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.
2017-05-24 13:13:53,894 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2017-05-24 13:13:53,894 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: ITSD-161, actionId: 61, file: /data/jira/home/groovy/ts-jsd-notebook-change-deployment.groovy
groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.issue.fields.DefaultFieldManager.getFieldObjectByName() is applicable for argument types: (java.lang.String) values: [Reporter]
	at ts-jsd-notebook-change-deployment.run(ts-jsd-notebook-change-deployment.groovy:14)

can somebody help?

toni

1 answer

1 accepted

Hey Toni,

I've taken your script and cleaned it up a little bit for you. I tested it myself and didn't get any kind of error:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.user.ApplicationUser
import com.onresolve.jira.groovy.user.FieldBehaviours

ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().loggedInUser

def wertreporter = issue.reporter.name

def reporter = "Hallo *" + wertreporter + "*, \n dein Notebook wird vorbereitet.\n Grüsse \n Technical Services"

CommentManager commentMgr = ComponentAccessor.getCommentManager()

//Der wirkliche Kommentar
commentMgr.create(issue, currentUser, reporter, true)

I think this should do what you are wanting. Here are some detailed steps over how to use this script in a post function:

Steps to use in transition:

  • Go to the workflow of the project that you would like this functionality to be present in.
  • Edit the workflow in Diagram view.
  • Click on the transition that you would like to add it to.
  • Choose Post-Function in the pop-up that appears to the right of the screen.
  • Choose to add a new Post-Function and then select "Script Post-Function" and hit "Add."
  • Then choose a "Custom Script Post-Function."
  • You then add the code, or something similar, in the Inline Script box (add a relivent note).
  • After you add the Post-Function, ensure that it is the very last script ran by pushing it down to the very bottom of the diolog box that reads "The following will be processed after the transition occurs."

You may then publish the workflow and you're good to go.

Hello Aidan,

i know how to edit and publish a workflow :->

My Problem was that i want to print the displayname of the reporter over the grrovy script.

 

Anyway thank you it works fine.

 

Best regrads

Roni

Hello Aidan,

how can i print the displayname and not the username?

 

Hallo test,

dein Notebook wird vorbereitet.

Grüss
Technical Services

 

solved

def wertreporter = issue.reporter.displayName

Can you please help me how to print a custom message with approver or rejector name. For ex: if the issue is in waiting for approval, once it is approved i would like to display a comment as "approved by name of the approver "

Any help will be greatly appreciated as it is very urgent.

Can anybody helpme?

are you working with jira-service-desk?

Yes, working with jira-service-desk

@Srinivas Adepu It depends on how your approval process works, but assuming that the issue will be approved when the approver transitions the issue, you can set up a Script Post-Function that contains code to create a comment like this:

import com.atlassian.jira.component.ComponentAccessor

def commentManager = ComponentAccessor.commentManager
commentManager.create(issue, currentUser, "This is a new comment by ${currentUser.name}", false)

Both the variables "issue" and currentUser will be available to you when creating a Custom Script Post-Function. 

Let me know if that'll work for you! :D

Aidan

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Sep 18, 2018 in Jira

What modern development practices are at the heart of how your team delivers software?

Hey Community mates! Claire here from the Software Product Marketing team. We all know software development changes rapidly, and it's often tough to keep up. But from our research, we've found the h...

21,678 views 2 7
Join discussion

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you