Auto-Comment with Script Runner Grovvy Script

Antonio D'Errico May 24, 2017

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

6 votes
Answer accepted
Aidan Derossett _Adaptavist_
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 Leaders.
May 26, 2017

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.

Antonio D'Errico May 28, 2017

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

Antonio D'Errico May 29, 2017

Hello Aidan,

how can i print the displayname and not the username?

 

Hallo test,

dein Notebook wird vorbereitet.

Grüss
Technical Services

 

Antonio D'Errico May 29, 2017

solved

def wertreporter = issue.reporter.displayName

srinivas September 28, 2017

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.

Srinivas Adepu October 10, 2017

Can anybody helpme?

Antonio D'Errico October 16, 2017

are you working with jira-service-desk?

Srinivas Adepu November 29, 2017

Yes, working with jira-service-desk

Aidan Derossett _Adaptavist_
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 Leaders.
December 6, 2017

@[deleted] 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