Question about script runner

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

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")
}

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

ComponentManager.getCustomFieldManager();  should be:

ComponentManager.getInstance().getCustomFieldManager()

or better:

ComponentAccessor.getCustomFieldManager()

 

 

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

 

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

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.

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

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

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

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

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.

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
Community showcase
Published Nov 29, 2018 in Marketplace Apps

How to set up an incident workflow from the VP of Engineering at Sentry

Hey Atlassian community, I help lead engineering at Sentry, an open-source error-tracking and monitoring tool that integrates with Jira. We started using Jira Software Cloud internally last year, a...

1,372 views 0 8
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