User Tools

Site Tools


validators

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
validators [2018/04/20 10:02]
root added field validator
validators [2019/02/13 09:10] (current)
root added cascading drop down
Line 160: Line 160:
 </code> </code>
  
 +===  Validate if user is allowed to change the OD Environment field ===
 +
 +<code java>
 +import com.atlassian.jira.component.ComponentAccessor
 +import com.atlassian.jira.issue.Issue
 +import org.apache.log4j.Logger
 +import org.apache.log4j.Level
 +def log = Logger.getLogger("com.acme.CreateSubtask")
 +log.setLevel(Level.DEBUG)
 +
 +Issue issue = issue
 +
 +def customFieldManager = ComponentAccessor.getCustomFieldManager()
 +def issueManager = ComponentAccessor.getIssueManager()
 +def customField = customFieldManager.getCustomFieldObjectByName("OD Environment")
 +def originalIssue = issueManager.getIssueObject(issue.id)
 +
 +def originalValue = originalIssue.getCustomFieldValue(customField).toString()
 +def newValue = issue.getCustomFieldValue(customField).toString()
 +
 +log.debug("originalIssue ${originalValue}")
 +log.debug("newIssue ${newValue}")
 +
 +if ( newValue != originalValue ) {
 +    if (! originalValue.equals("null") ){
 +    log.debug("false -> Changing from not None")
 +        return false
 +
 +    }else{
 +        log.debug("true -> changing from none")
 +        return true
 +    }
 +}else{
 +    log.debug("true -> not changing")
 +    return true
 +}
 +</code>
 +
 +=== Allow only specific users in project ABC or users in a group "JIRAGROUP" to create issues validator ===
 +
 +<code java>
 +import com.atlassian.jira.component.ComponentAccessor
 +import com.atlassian.jira.issue.Issue
 +def groupManager = ComponentAccessor.getGroupManager()
 +
 +(issue.projectObject.key == 'ABC' && issue.issueType.name == 'Bug' && currentUser.name in ["XYZ", "TEST"] ) || groupManager.isUserInGroup(currentUser, "JIRAGROUP")
 +
 +
 +</code>
 +
 +=== Only allow users in group "users" to execute this action ===
 +
 +<code java>
 +import com.atlassian.jira.component.ComponentAccessor
 +import com.atlassian.jira.issue.Issue
 +def groupManager = ComponentAccessor.getGroupManager()
 +
 +groupManager.isUserInGroup(currentUser, "users")
 +</code>
 +
 +=== Check if user is in group "users" or project role "Developers" to execute this action ===
 +
 +<code java>
 +import com.atlassian.jira.component.ComponentAccessor
 +import com.atlassian.jira.issue.Issue
 +import com.atlassian.jira.security.roles.ProjectRoleManager
 +
 +def projectManager = ComponentAccessor.projectManager
 +def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager)
 +def role = projectRoleManager.getProjectRole("Developers")
 +
 +def groupManager = ComponentAccessor.getGroupManager()
 +
 +if (groupManager.isUserInGroup(currentUser, "users") || projectRoleManager.isUserInProjectRole(currentUser, role, issue.getProjectObject())){
 + return true
 +}
 +</code>
 +
 +=== Check if cascading child drop down is set to "None" ===
 +The following script was found on Atlassian community here: [[https://community.atlassian.com/t5/Jira-questions/How-to-check-the-second-value-of-a-cascading-select-list-in-a/qaq-p/759808|How to check the second value of a cascading select list in a custom field]]
 +Use the **Custom Validator** for this script-runner script.
 +<code java>
 +import com.atlassian.jira.component.ComponentAccessor
 +import com.atlassian.jira.issue.MutableIssue
 +import com.opensymphony.workflow.InvalidInputException
 +
 +def issue = issue as MutableIssue
 +def cf = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("CascadingSelect")
 +def cfValue = issue.getCustomFieldValue(cf) as HashMap
 +
 +def parentOption = cfValue.values().getAt(0)?.value
 +def childOption = cfValue.values().getAt(1)?.value
 +
 +if (!childOption) {
 +    throw new InvalidInputException("Child option is Null")
 +}
 +</code>
validators.1524232962.txt.gz · Last modified: 2018/04/20 10:02 by root