How to validate component depending on issue type (simple script validator)?

In my project AB i have two issuetypes

  1. issuetypeA
  2. issuetypeB

 

also i have losts of components

  1. "component name for A part 1"
  2. "component name for A part 2"
  3. "component name for A part 3"
  4. "component name for A part 4"
  5. "company B part 1"
  6. "company B part 2"
  7. "company B part 3"
  8. "company B part 4"

So the problem is to write correct validator for such rule:
with issuetypeA can be used only components which starts from or containce component name ("component name for A part 1" for example)

import com.atlassian.jira.component.ComponentAccessor;

if(issue.projectObject.name == 'AB' && issue.issueType.name == 'issuetypeA')
def components = issue.getComponentObjects();
if (components != null && "component".equals(components[0].getName()) {
return true;
}
return false;

But its fail

 Jira v7.5.0 and Adaptavist ScriptRunner for JIRA 5.2.2

4 answers

1 accepted

This widget could not be displayed.
import com.opensymphony.workflow.InvalidInputException;
import com.atlassian.jira.component.ComponentAccessor;

if(issue.issueType.name == 'yourIssueType' && issue.projectObject.key == 'projectKey'){
if(issue.components*.name =~ /component starts from/){
return true;
}
else {
throw new InvalidInputException("components", "Use component 'component starts from...'");
}
}
This widget could not be displayed.
Alexey Matveev Community Champion Dec 18, 2017

Try you code like this

import com.atlassian.jira.component.ComponentAccessor;


if(issue.projectObject.name == 'AB' && issue.issueType.name == 'issuetypeA') {
def components = issue.getComponentObjects();
if (components != null && "component".equals(components[0].getName())) {
return true;
}
}
return false;
This widget could not be displayed.
import com.atlassian.jira.component.ComponentAccessor;

if(issue.projectObject.name == 'AB' && issue.issueType.name == 'issuetypeA') {
def components = issue.getComponents();
if (components != null && "component".equals(components[0].getName())) {
return true;
}
}
return false;

Nope, its not works

This widget could not be displayed.

I think you just test whether the name of the first component in the issue equals "component". But as none of the names of your components is equal to "component", the test will always fail.

In addition, you test only the first component of the issue, I guess you should validate them all ??

And finally your validation will fail for all projects different from 'AB' and all issue types different from "issueTypeA"; i guess this is not wanted neither ???

Code (you can write this in a much more condensed form in Groovy, but I think it is easier to understand this way):

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.bc.project.component.ProjectComponent

if(issue.projectObject.name == 'AB' && issue.issueType.name == 'issuetypeA') {
for (ProjectComponent component in issue.getComponents()) {
if (!component.name.startsWith("component name for A")) {
return false;
}
}
}
return true;

I have logic like this

I have specific issue type and components

With this issue type i need to use only specific components.

So I need validation for this case

? Don't understand ? The answer is in my previous reply ???

What is still not working ???

It's OK. Now works

Suggest an answer

Log in or Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted 14 hours ago in New to Jira

Are you planning to trial, or are currently trialling Jira Software? - We want to talk to you!

Hello! I'm Rayen, a product manager at Atlassian. My team and I are working hard to improve the trial experience for Jira Software Cloud. We are interested in   talking to 20 people planning t...

41 views 1 0
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