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 Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

2,934 views 12 18
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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot