JIRA Use a Groovy script on post function with arguments

Céline M February 24, 2014

Hi,

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
Answer accepted
Henning Tietgens
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.
February 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.

JamieA
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.
February 25, 2014

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
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.
February 25, 2014

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

0 votes
GabrielleJ
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.
July 4, 2014

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

0 votes
Céline M February 25, 2014

Thank you so much. It runs.

0 votes
JamieA
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.
February 24, 2014

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.

Céline M February 25, 2014

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?

Jacob Rothenberger November 7, 2014

@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)?

JamieA
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.
November 8, 2014

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.

Jacob Rothenberger November 10, 2014

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

Suggest an answer

Log in or Sign up to answer