Forums

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

Script Runner Behaviour Script - Find the project key of a Story's Epic Link

Daniel Tsirlin
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
January 18, 2018

Hi all,


I am trying to find the project key associated with the Epic Link that is linked to a Story using a Behaviour which is mapped to the Epic Link field on Story issue types and using a validation script.

 

My use case is that I want to restrict a Story from being linked to an Epic which is not in the same project as the Story.  I plan to use the project key related to the Story and Epic issue as our project keys are unique.

 

I've been able to get the project key related to the mapped Story with the following code:

 

import com.atlassian.jira.project.Project

Project project = getIssueContext().getProjectObject()
String projectCategory = project.getProjectCategory().getName() as String

String projectKey = project.getKey()

 

My aim is to do the following to get the Epic's related project key:

1. Get issue key for Epic Link field

2. Create issue object for Epic using above issue key

3. Get project issue from above issue

4. Get project key from above project

 

My current code attempt is:


import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueManager

def epicLinkField = getFieldByName("Epic Link")
def epicLinkIssueKey = epicLinkField.getValue().toString()

IssueManager issueManager = ComponentAccessor.getIssueManager()

String epicIssue = issueManager.getIssueObject(epicLinkIssueKey).getProjectObject().getKey()

 

My errors come after the .getIssueObject().

For debugging, I've set the error on the Epic link field (epicLinkField.setError()) to be random text.   If I chop the last line in the code above to end after getIssueObject, then the the error shows as expected.

 

As soon as I add any Issue class method after this, either on the same line or on a new line which casts on to the variable epicIssue, the Epic Link error no longer appears.

 

I assumes this is crashing the script, but I am not told anything in the JIRA admin area unlike when a workflow script validator fails.

The code above works in some workflow validator scripts I have (although a different way to get the key for the Epic Link field), however it is failing here.

 

I expect that if I can get the project object for the Epic issue, then I can do epicProjectKey = epicProjectObject.getKey() like I've done to get the project key of the mapped Story.

 

Any help on what I can do?  Thanks

 

 

 

 

 

 

 

2 answers

1 accepted

Suggest an answer

Log in or Sign up to answer
1 vote
Answer accepted
PD Sheehan
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 Champions.
April 4, 2020

Scriptrunner I think used to be called "groovy runner" ... so really it's only groovy/java.

But a groovy script can execute a command on the server just like you would from the command line.

So if you want to run a python script, you can do something like this:

def sout = new StringBuilder()
def serr = new StringBuilder()
def cmd = "/path/to/python/script.py"
def proc = cmd.execute()
proc.consumeProcessOutput(sout,serr)
proc.waitForOKill(30000)
if(serr){
log.error "Error exeucting python script with standard error output: \n $serr"
log.debug "Erroring Python script had standard output:\n $sout
} else {
log.debug "Python script executed succesfully with standard output:\n $sout"
}
DH
Contributor
April 6, 2020

Thank you @PD Sheehan , that makes total sense. I will try this, but haven't yet. Nonetheless, you answered my question and I will mark it as Answered.

0 votes
Jyoti Bisoi
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
August 20, 2020

How to catch error in python script? Above code does  run the script but not failing the post function even if the python script failed. Can you please suggest any way to fail post function with example please.

PD Sheehan
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 Champions.
August 20, 2020

There is no exit/failure path (that I'm aware of ) for any post-function script.

That's what validators are for.

The best you can do is consume the standard error output from the command execution and let the user know.

Maybe you need to split your python script in 2, a validation script and an execution script.

Executed the validation script from a custom script validator, if it fails, throw an exception and stop the transition. If it passes, run the execution script as a postfunction to perform whatever action you need it to.

TAGS
AUG Leaders

Atlassian Community Events