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;}
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.
When i remove "throw error" doesn't work. Script add comment and check custome field but doesn't block transition (issue change status)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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;
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
MyGroovy doesn't handle return value for validators.
You must throw InvalidInputException for validation error
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.