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

Maksim Melnikov October 24, 2017

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

1 vote
Thanos Batagiannis _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.
November 16, 2017

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

Maksim Melnikov November 21, 2017

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?

Thanos Batagiannis _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.
November 21, 2017

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

Like # people like this

Suggest an answer

Log in or Sign up to answer