Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

validator mygroovy

Arkadiusz Baka
Contributor
August 21, 2019

I want to write validator to  check date. 

When i throw Exception - script not add comment

When i dont throw Exception -script add comment but dont block transition 

Any help how this can be done?

 

Code:

import com.atlassian.jira.component.ComponentAccessor;

import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
import com.opensymphony.workflow.InvalidInputException;
import org.apache.log4j.Category;
import java.time.LocalDate;
import com.atlassian.jira.issue.comments.CommentManager
import groovy.xml.MarkupBuilder;
import com.atlassian.jira.config.properties.APKeys;
import com.atlassian.jira.user.ApplicationUser

// Get a pointer to the issue
Issue issueKey = issue;

// Get the current logged in user
def CurrentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser() as ApplicationUser

def issueManager = ComponentAccessor.getIssueManager();
def customFieldManager = ComponentAccessor.getCustomFieldManager();
def cField = customFieldManager.getCustomFieldObject("customfield_13200");
def cFieldValue = (String)issue.getCustomFieldValue(cField);

// Get access to the Jira comment and component manager
CommentManager commentManager = ComponentAccessor.getCommentManager();


LocalDate a = LocalDate.parse(cFieldValue.substring(0,10));
LocalDate b = LocalDate.now().plusMonths(3);

if (a>b){

commentManager = ComponentAccessor.getCommentManager()
commentManager.create(issueKey, CurrentUser, "Wrong date", true)
InvalidInputException error= new InvalidInputException("Wrong date");
throw error;

return false;

}else {
return true;}

 

1 answer

0 votes
Nic Brough -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.
August 21, 2019

A validator has a very simple response - true for pass, false (with an optional error message) for fail.

You don't need (or want) to throw any error, the "return false" does the job.

Arkadiusz Baka
Contributor
August 21, 2019

When i remove "throw error"  doesn't work. Script add comment and check custome field but doesn't block transition (issue change status)

Nic Brough -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.
August 21, 2019

You should never be amending an issue in a validator, and that includes commenting on it. 

Remove the code that adds a comment, that is almost certainly the problem, it's putting the issue into an unknown state in memory and failing on that.

Arkadiusz Baka
Contributor
August 21, 2019

i create new validator , still doesn't work (change status)

CODE:

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
import org.apache.log4j.Category;
import java.time.LocalDate;


def issueManager = ComponentAccessor.getIssueManager();
def customFieldManager = ComponentAccessor.getCustomFieldManager();
def cField = customFieldManager.getCustomFieldObject("customfield_13200");
def cFieldValue = (String)issue.getCustomFieldValue(cField);


LocalDate a = LocalDate.parse(cFieldValue.substring(0,10));
LocalDate b = LocalDate.now().plusMonths(3);

if (a>b){

return false;

}

Nic Brough -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.
August 21, 2019

It's not supposed to change status.  It should return true if the issue passes validation and false if it does not.  It is part of a status change.

I think you need to just add "return true" to the end of it, so it allows the transition when a=<b

Alexey Belostotskiy August 21, 2019

MyGroovy doesn't handle return value for validators.

You must throw InvalidInputException for validation error

Suggest an answer

Log in or Sign up to answer