Question about script runner

Marc Mast January 28, 2015

I have a question about the script runner plugin.

 

Is it possible to check for a specific value in Security Level and if that value is present, make some other field required? I want to do this all in the create transition.

 

Thanks in advance.

3 answers

1 accepted

5 votes
Answer accepted
Alejo Villarrubia [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.
January 29, 2015

Hi Marc,

You could do something similar to this:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.security.IssueSecurityLevelManager
import com.opensymphony.workflow.InvalidInputException

def Issue issue = issue
def IssueSecurityLevelManager issueSecurityLevelManager = ComponentAccessor.getIssueSecurityLevelManager()

if (! issue.securityLevelId) return

def securityLevel = issueSecurityLevelManager.getSecurityLevel(issue.securityLevelId)
if (securityLevel.name == "level two" && ! issue.environment) {
    throw new InvalidInputException("environment", "You must enter an environment if you level two is your security level")
}
Marc Mast February 3, 2015

Thanks all for the answers, i'm almost there, but now I can't get the customfield part working (Script runner noob here). To clearify: this is gonna be used in the validator part of the workflow.

So far I got this:

import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.security.IssueSecurityLevelManager;
import com.opensymphony.workflow.InvalidInputException;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;


IssueManager issueManager = ComponentManager.getInstance().getIssueManager();
def Issue issue = issueManager.getIssueObject( "TBS-14" ); 


//def Issue issue = issue
CustomFieldManager customFieldManager = ComponentManager.getCustomFieldManager();
CustomField cf = customFieldManager.getCustomFieldObjectByName('Client')


def IssueSecurityLevelManager issueSecurityLevelManager = ComponentAccessor.getIssueSecurityLevelManager();

if (! issue.securityLevelId) return


def securityLevel = issueSecurityLevelManager.getSecurityLevel(issue.securityLevelId);
if (securityLevel.name == "Intern" && ! issue.getCustomFieldValue(cf)) {
throw InvalidInputException("Product", "Enter a product.")
}

The error I'm getting is this (Script Console):

groovy.lang.MissingMethodException: No signature of method: static com.atlassian.jira.ComponentManager.getCustomFieldManager() is applicable for argument types: () values: []
Possible solutions: getCustomFieldManager()
	at Script62.run(Script62.groovy:18)

I guess the declaration of the getCustomFieldManager is not correct... Any ideas?

When i comment out the CustomFieldManager  and the ComponentManager declaration, I get no error at the console (but it doesn't work at the workflow postfunction).

Marc

JamieA
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.
February 3, 2015

ComponentManager.getCustomFieldManager();  should be:

ComponentManager.getInstance().getCustomFieldManager()

or better:

ComponentAccessor.getCustomFieldManager()

 

 

2 votes
Adam Marszałek
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.
January 28, 2015

Hey Marc,

 

If I understand you correctly you should create new Script Validator on desired transition, then retrieve from issue object its security level id via getSecurityLevelId and if it's level you are looking for and other field value is missing, then result in failed validation. You can look for inspiration in Jamies example: Require Fix Version if resolution is Fixed.

Regards,
Adam

 

0 votes
Marc Mast January 28, 2015

Hi Adam,

Thanks for the answer. I've been struggling with it and I got this so far, but it's not validating when I choose security level Intern and product None:

import com.atlassian.jira.issue.Issue
import com.opensymphony.workflow.InvalidInputException
def Category log = Category.getInstance("com.onresolve.jira.groovy.PostFunction")
Issue myIssue = issue
if (myIssue.getSecurityLevelId() == "Intern" && myIssue.getCustomFieldValue("customfield_16100") == "None") {
    invalidInputException = new InvalidInputException("Kies een valide product.")
}

Am I missing something?

 

Regards,

Marc

Adam Marszałek
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.
January 28, 2015

Oh, that's due to getSecurityLevelId returning Long, not string, so you should find out what is id of your security level and put it in if statement.

Adam Marszałek
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.
January 28, 2015

And to be sure - you use "log = Category.getInstance("com.onresolve.jira.groovy.PostFunction")", but it's part of Validation, not Post Function, right?

Marc Mast January 28, 2015

It is part of validation, not postfunction. Do you suggest to remove that part?

Adam Marszałek
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.
January 28, 2015

Nope, it does not affect how validation work. Did you try to replace "Intern" with proper id?

Marc Mast January 28, 2015

Yup, I did. So far I replaced "Intern" with the ID (10903 in this case) but no luck. Got this now: if (myIssue.getSecurityLevelId() == 10903 etc. He stills accepts the security level (Intern; ID 10903) with the option None for product

Adam Marszałek
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.
January 28, 2015

Did you try to find out what exceptions are logged in JIRA log? It seems, that getCustomFieldValue accepts instance of CustomField type, not string. This should help as long as your customfield_16100's value returns string, which is quite probable.

JamieA
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.
January 29, 2015

See Alejo's answer for how to look up the security level if you want to compare it by name. For your problem with custom fields get the custom field by using CustomFieldManager... lots of examples for this around

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events