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

Matt December 18, 2017

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

1 vote
Answer accepted
Alex Gubar December 26, 2017
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...'");
}
}
1 vote
Alexey Matveev
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.
December 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;
0 votes
Marc Minten _EVS_
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.
December 20, 2017

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;
Matt December 21, 2017

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

Marc Minten _EVS_
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.
December 22, 2017

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

What is still not working ???

Matt December 28, 2017

It's OK. Now works

0 votes
Matt December 18, 2017
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

Suggest an answer

Log in or Sign up to answer