JIRA Use a Groovy script on post function with arguments


Is it possible to create a groovy script and passed arguments to it into a post function ?

For example, I would like make a script to initialize a date custom field. The script will be "InitializeDate.groovy"

And I define the name of the custom field in the post function that call the script, something like :

\var\initializeDate.groovy "Start Date"

i thought the string "Start Date" could be retrieved with a "$1" argument in the script, it doesn't run.

4 answers

1 accepted

2 votes
Accepted answer
Henning Tietgens Community Champion Feb 25, 2014

You could use the attached (SimplePostfunction.groovy.txt) "simple scripted post function" script and use this to call methods from your other scripts. Remove the .txt at the end and put it into the directory according to the package.

Jamie, is there a reason why there is not builtin simple scripted post function? I created one some time ago, but I thought there maybe a reason why you don't included something similar.

No reason, next major release will have the ability to point to a file or use an inline script. Basically everything will be a built-in script, but one of them will be a CustomRunWhateverYouWant type thing.

The simple validator has slightly more logic, in that it handles the validation error.

Henning Tietgens Community Champion Feb 25, 2014

Ok, sounds nice. For now I attached the simple postfunction script to my answer.

Put that logic into the script. Rather than adding the field name to the workflow, in your script you can say "if workflow or issue type is X, then initialise date Y".

Alternatively you can read status or transition properties from your script.

It isn't exactly that I want.

In fact, I'm dependent of other teams to put groovy files on Jira server. I would like avoid to call them each time I need use same functionnality on other fields.

In this case, I would like create a script to initalize a Date and Time field with the current date, and call a script with 2 parameters: the name of the concerned field and a parameter to set the comportment if field is already filled.

And I would like be able to call this script in workflow transition each time I need customize a new workflow.

But I understand from your answer that it isn't possible, is it?

@Jamie Echlin [Adaptavist] You mention: "Alternatively you can read status or transition properties from your script." I am trying to figure out how to read a value from a transition property, yet I cannot seem to locate an example or API reference to get me there. Do you happen to have an example of how to read a transition property (key, value pair) within a .groovy file (to be run by Script Runner)?

ComponentAccessor.getWorkflowManager().getWorkflow(issue).getDescriptor().getAction(transientVars['actionId']).getMetaAttributes() That will give you the extra properties you define in the workflow, if that's what you mean. That's just sample code, I wouldn;t actually write it like that.

Thank you @Jamie Echlin [Adaptavist] ! This is exactly what I needed. Example code for retrieving Workflow Transition Properties: import com.atlassian.jira.ComponentManager ComponentManager componentManager = ComponentManager.getInstance() def myTransitionProperty = componentManager.getWorkflowManager().getWorkflow(issue).getDescriptor().getAction(transientVars['actionId']).getMetaAttributes().MyTransitionProperty

Thank you so much. It runs.

Any idea why -X PUT is now working with the Script Runner?

def curl = "/usr/bin/curl -u FECRU_CREDENTIALS -H Content-Type:application/json -H Accept:application/json -X PUT -d FECRU_DATA FECRU_REST_API"

-X GET is working fine

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Nov 29, 2018 in Jira

How to set up an incident workflow from the VP of Engineering at Sentry

Hey Atlassian community, I help lead engineering at Sentry, an open-source error-tracking and monitoring tool that integrates with Jira. We started using Jira Software Cloud internally last year, a...

1,107 views 0 8
Read article

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