How to create a Groovy expression for commeting as a post function?

For a transition I want to automatically create a comment to the issue as a post function and use information from the issue itself within the generated comment.

The following expression represents pseudo-code:

{User_how_causes_the_transition} has approved to grant {Custom_Field_ID_1234} the privilege {Custom_Field_ID_5678}.
{Assignee}: Please issue the privilege now.

 I see myself infront of the Jira "Add Parameters To Function" dialog with "Comment type" set to "Groovy Expression".

What do I have to enter into the "Value"?

I could not find a list containing what options do I have with Groovy within Jira. Ok, there is an "You can use the following variables in your Groovy script" line but how do I know what properties of the objects are available?

2 answers

0 votes

Hi Soren,

Assuming {User_how_causes_the_transition} is always the name of the current logged in user and {Custom_Field_ID_1234} and {Custom_Field_ID_5678} are values of respective custom fields, the code should look something like this:

import com.atlassian.jira.component.ComponentAccessor

def cfMgr = ComponentAccessor.getCustomFieldManager()

def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser().getDisplayName()
def cf1 = issue.getCustomFieldValue(cfMgr.getCustomFieldObject(1234))
def cf2 = issue.getCustomFieldValue(cfMgr.getCustomFieldObject(5678))

return "${currentUser} has approved to grant ${cf1} the privilege ${cf2}. ${issue.getAssignee().getDisplayName()}: Please issue the privilege now." as String

Thank you! I will try your snippet in a minute or so.

Do you have a reference list for "all the properties" of e.g. "ComponentAccessor" or "issue"?

JIRA Platform Java APIs

DistributionDocumentation

JIRA Server

Latest production version

All versions (including EAPs)

JIRA CloudLatest version

JIRA Software Java API

DistributionDocumentation

JIRA Software ServerAll versions

JIRA Service Desk Java API

DistributionDocumentation

JIRA Service Desk ServerLatest production version
JIRA Service Desk ServerAll versions

I got the following error while creating an issue:

Property 'currentUser' not found

 PS: Jira version is 6.3.5

Ivan Tovbin Community Champion Dec 14, 2017

Incidentally, which addon are you using to create this post-function?

Ivan Tovbin Community Champion Dec 14, 2017

For 6.3.5 try changing 'getLoggedInUser()' to 'getUser()' in the 'currentUser' variable definition.

I am assuming Groovy post-function comments is provided by Adaptavist ScriptRunner for JIRA (4.1.3.14), but I am not sure (just taken over the administration).

And changing it to "getUser" won't help.

The error says it could not find the property (the variable we defined). I tried to change it from "def" to "String" but this also not worked.

Ivan Tovbin Community Champion Dec 16, 2017

That's odd. Could you describe your use case more thoroughly?

Thank you for your help and sorry for the delay.

 

The workflow describes a permission granting process in which different transitions have different conditions on which group of users may able to use the transitions.

  1. Any user can create an issue. The reporter cannot do anything else than viewing and commenting on the issue.
  2. The issue will be automatically assigned to the responsible user (aka supervisor).
  3. The supervisor can use transitions like "approve request" or "reject request" which also requires the supervisor to enter a comment in the transition screen.
  4. No matter what the supervisor acutuall have commented and as part of the approve/reject transition, the system will add a new automatically generated comment with a "standard text" template to help further issue workers identify so called "processing comments" more easier.

Hopefully I could explain the use case your way so you can follow my thoughts ;)

0 votes
Ivan Tovbin Community Champion Jan 12, 2018

Ok, let's try this again.

Create a scripted post-function on the transition which you want to generate a comment using the code below. I'm going to assume that this comment should be posted on behalf the user who triggered this transition (e.g. the supervisor?)

import com.atlassian.jira.component.ComponentAccessor

def commentMgr = ComponentAccessor.getCommentManager()
def cfMgr = ComponentAccessor.getCustomFieldManager()
def cf1Value = issue.getCustomFieldValue(cfMgr.getCustomFieldObject(1234 as Long))
def cf2Value = issue.getCustomFieldValue(cfMgr.getCustomFieldObject(5678 as Long))
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getUser()

commentMgr.create(issue, currentUser, "${currentUser.getDisplayName()} has approved to grant ${cf1Value} the privilege ${cf2Value}. ${issue.getAssignee().getDisplayName()}: Please issue the privilege now." as String, true)


Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Thursday in Jira

Continuous visibility with the new Jira Software Cloud and Bitbucket Cloud integration

Hey Atlassian community, I am a PM on the Bitbucket Cloud team and I am excited to share that we just released an improvement to the Jira Software and Bitbucket Cloud integration. With the newly im...

75 views 0 3
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