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

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

 

 

 

 

 

 

 

1 answer

Hello @Daniel Tsirlin,

I can't get the whole picture - this validator in postfucntion or Behavior script when you changing some fields (on creating/editing screen)?

as I understand,

epicIssue = issueManager.getIssueObject(epicLinkIssueKey)

will work?

try to add logging to be sure:

logger.debug(epicIssue.getClass())

and don't forget to define your logger.

 

Ivan

Hi Ivan,

Before your message I found out that my code works when I hardcode a string representing epicLinkIssueKey and that my issue is getting that value.

 

Using:

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

I get the string equalling to "key:<valueOfIssueKey>"

Using this value for getIssueObject() won't work since there is the "key:" at the front.

scriptrunner is telling me that the output of getValue() is an Object type, so i tried epicLinkIssueKey['key'] (before adding toString()) and it doesn't like it.  I tried making my own map and use the same notation for getting keys and it works, so i assume that the Object isn't what I'm expecting.

 

For now i've decided to make a substring of the output of getValue().toString() to start at index 4, thus it returns just the issue key, however this is quite ugly haha.

 

I don't exactly know how to use the logger you mentioned, but do you know how I could figure out what sort of object you get from getValue()?  The documentation for scriptrunner seems to be poor at best and doesn't help at all.

 

Regards,

Daniel 

Hi @Daniel Tsirlin,

interesting workaround :)

 

about logging this works for me:

import org.apache.log4j.Logger
import org.apache.log4j.Level

Logger logger = Logger.getLogger("")
logger.setLevel(Level.DEBUG)

logger.debug("Check me in catalina.out")

This should works.

and to get class of object in groovy use:

logger.debug(object.getClass())

 Btw, ScriptRunner has a great tool in admin area, called "Script Consle". You able to see result and logs via UI (but I prefer console with tail -f catalina.out)

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Thursday in Jira

5 ways you can make the most of Jira Software and Bitbucket Cloud

As part of the Bitbucket product team I'm always interested in better understanding what kind of impact the use of our tools have on the way you work. In a recent study we conducted of software devel...

80 views 0 5
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