Scriptrunner: How to include custom scripts with "issue" object Edited

I`ve got a "library" with common functions, .../scripts/util/Helper.groovy:

 

package util

import .......

public class Helper {
    public String getCurrentProjectKey() {
       return issue.projectObject.key
    }
}

 

And a script .../scripts/myscript.groovy:

import util.Helper

def helper = Helper

String currentProjectKey = helper.getCurrentProjectKey()

 

I use it as a post-function. But everytime I get an error or while checking either when running. The main problem is that "issue" object is not seen in library. I use different combination of code to make the "issue" object be recognizable, but nothing helps me...

For example, error:

file:/var/atlassian/application-data/jira/scripts/util/Helper.groovy: 29: Apparent variable 'issue' was found in a static scope but doesn't refer to a local variable, static field or class. Possible causes:
You attempted to reference a variable in the binding or an instance variable from a static context.
You misspelled a classname or statically imported field. Please check the spelling.
You attempted to use a method 'issue' but left out brackets in a place not allowed by the grammar.

How can I use "issue" object in my imported common libraries?

Or how can I get the current issue object?

Thanks in advance.

 

1 answer

Hi Maksim,

I suppose one way is to pass the issue as a parameter. For example your Helper class would be something like 

import com.atlassian.jira.issue.MutableIssue

class Helper {

static String getCurrentProjectKey(MutableIssue issue ) {
issue.projectObject.key
}

}

and then your script something like 

import static myExampleScripts.util.Helper.*

def projectKey = getCurrentProjectKey()

log.debug "Project Key is $projectKey"

PS. I would be really interested to know what kind of helper methods you use ...

Kind regards,

Thanos

Hello Thanos!

Thank you for reply, I will try your advice,

 

I want to use common methods like getCurrentProjectRoles and others. I use different scripts, so I dont want to use same code in every script.

 

Is it possible to do something like:

import myExampleScripts.util.Helper

def helper = Helper(issue)

def currentIssueAssignee = helper.getCurrentIssueAssignee()

Is there any way an issue object to give to the class on its init and then use it in functions?

Hey Maksim, 

Sure, you can. So in that case the Helper class will be 

import com.atlassian.jira.issue.MutableIssue

class Helper {

private MutableIssue issue

Helper(MutableIssue issue) {
this.issue = issue
}

String getCurrentProjectKey() {
issue.projectObject.key
}
}

And then you script will look like 

import myExampleScripts.util.Helper

def helper = new Helper(issue)
def projectKey = helper.getCurrentProjectKey()

log.debug "Project Key is $projectKey"

I edited my first answer in order to make it more readable.

Regards, Thanos

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Sep 18, 2018 in Jira

What modern development practices are at the heart of how your team delivers software?

Hey Community mates! Claire here from the Software Product Marketing team. We all know software development changes rapidly, and it's often tough to keep up. But from our research, we've found the h...

27,210 views 2 7
Join discussion

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