Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Get the Details of the Issue that Triggered the Script Post-Function

Azeus System Team October 5, 2018

Here is the scenario I want to implement:

I have a script in the Jira server that I need to execute during a transition. I used the Script Post-Function by ScriptRunner and I can confirm that the script is triggered. However, some commands in the script needs the details of the issue that underwent the transition as their input parameters. 

I've been searching for similar questions for a while. Most of what I found were using groovy scripts to GET via REST API but the requests still need a JQL requiring issue details that I need in the first place.

Is there a way for the Script Post-Function to send or write the details of the issue that triggered it to the script? Or write the issue JSON to a file so I can just grep the details I need and put it in the script?

Thank you.

1 answer

1 accepted

3 votes
Answer accepted
Nic Brough -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.
October 5, 2018

When you're writing a post-function script, you have direct access to the issue without having to do anything.

The object issue is passed into the script for you to work with directly, so you can do things like issue.getSummary() without importing anything.

Azeus System Team October 5, 2018

Thanks for answering Nic. Is there a way to print them locally in the server so they can be used by the script (I forgot to mention that the script is a bash script)?

Nic Brough -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.
October 5, 2018

If you're calling a bash script, then the call to it is some form of "exec" statement, in which you can include parameters. 

Let's say you had a bash script that simply said

echo $1

Then the code calling it in your post-function can say something like

exec ("/data/scripts/myjirascript.sh " + issue.getSummary() )

and you'll get the issue summary dumped out to the log.

Azeus System Team October 7, 2018

Hello Nic,

To test, I created a script called myscript.sh with the following contents:

echo $1 > /path/to/myscript.log

I created this post-function:

def sout = new StringBuffer(), serr = new StringBuffer()
def proc = '/path/to/myscript.sh + issue.getKey()'.execute()

proc.consumeProcessOutput(sout, serr)
proc.waitForOrKill(1000)
println sout

During the desired transition, the log file is created but there's nothing written to it. I also tried this one:

def sout = new StringBuffer(), serr = new StringBuffer()
def command = 'sh /path/to/myscript.sh' + issue.getKey()
def proc = command.execute()

proc.consumeProcessOutput(sout, serr)
proc.waitForOrKill(1000)
println sout

But it's worse as it doesn't create the log file at all.

I checked the Jira logs and all the issue details and fields are there instead of just the issue key.

Any hints where I went wrong? Sorry for the questions as I have no prior experience with groovy scripts. Thank you.

Nic Brough -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.
October 8, 2018

Well, no, it won't.  Look at the command you are executing - let's say you have an issue ABC-123, your command will say

sh ./path/to/myscript.shABC-123

Azeus System Team October 8, 2018

What a stupid mistake :( It's working now. Thanks for the help Nic!

Suggest an answer

Log in or Sign up to answer