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

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...'");
}
}
0 vote
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;
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

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 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 ...

3,254 views 14 20
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