Snippets

Adaptavist Adaptavist ScriptRunner for Jira Cloud Example Workflow Validators

Created by Kristian Walker last modified
/*
 * This script provides some example expressions which can be added as 'ScriptRunner Script Validators* inside of ScriptRunner for Jira Cloud'.
 * The conditions must written as a Jira Expression using the expression framework which Atlassian document at: https://developer.atlassian.com/cloud/jira/platform/jira-expressions/
 * All right, title and interest in this code snippet shall remain the exclusive intellectual property of Adaptavist Group Ltd and its affiliates. Customers with a valid ScriptRunner 
 * license shall be granted a  non-exclusive, non-transferable, freely revocable right to use this code snippet only within their own instance of Atlassian products. This licensing notice cannot be removed
 * or amended and must be included in any circumstances where the code snippet is shared by You or a third party." 
*/ 

// Speicify that at least one linked issue must be entered when the issue is created 
issue.links.length >0 // You may also want to set the Error message to have text similar to 'You must specify at least one linked issue in order to be able to create this issue.'

// Specify that at least one Fix Version is added to the issue when it is created
issue.fixVersions.length >0 // You may also want to set the Error message to have text similar to 'You must specify at least one Fix Version in order to be able to create this issue.'

// Specify that at least one Component is added to the issue when it is created
issue.components.length >0 // You may also want to set the Error message to have text similar to 'You must specify at least one Component in order to be able to create this issue.'

// Specify that both select list drop downs in a custom cascading select field must be populated in order to create an issue. 
// Note: You should replace 'customfield_10093' in the example below with the ID of the cascading select list field inside of your instance. 
issue.customfield_10093.child.value.length >0 // You may also want to set the Error message to have text similar to 'You must specify a value in both of the Cascading Select List field drop downs in order to be able to create this issue.'

// Specify that All Sub Tasks must be in the 'In Progress' status before the parent issue can be transitioned
issue.subtasks.filter(subtask => subtask.status.name == 'In Progress').length == issue.subtasks.length // You may also want to set the Error message to have text similar to 'All subtasks must be in the In Progress status before this parent issue can be transitioned.'

// Specify that the description field must contain more than 30 characters
// Note the plainText paramater matches the comment as plain text but you can match it as Rich Text if needed by changing the paramater to RichText
issue.description.plainText.length >30 // You may also want to set the Error message to have text similar to 'The description field must contain more than 30 characters.'

// Specify that the issue must be in an active sprint
issue.sprint.state == 'active' // You may also want to set the Error message to have text similar to 'This issue must be in a valid Sprint.'

// Validate that a set number of new attachments must be uploaded during the transition.  
// The example below mandates that 3 new attachments must be uploaded on the transition screen.  
// This validator will also work on the Create transition as well.
issue.attachments.filter(attachment => attachment.id == null).length ==3 // You may also want to set the Error message to have text similar to 'You must upload 3 attachments.'

// Validate that all issues below an Epic must be in the Done status before the Epic issue itself can be closed.
// The isEpic paramater returns true if the issue is an Epic and false if it is any other issue type
// The stories paramater returns all the issues below the Epic issue to allow there status to be checked. 
issue.isEpic && issue.stories.filter(story => story.status.name == 'Done').length == issue.stories.length //You may also want to set the Error message to have text similar to 'You must ensure all issues inside the Epic are in the Done status before the Epic issue can be closed'

// Validate that only users in a specific project role can create an issue of a certain type
// The first expression mandates the issue type must be a specific issue type.
// The second expression joined by the && operator mandates that the user must be in a specific project role to create the issue type from expression1
// The third expression specifies that all other issue types used by the project can be created. add new issue types by adding them in and seperating them with a | character
issue.issueType.name.match('^(<IssueTypeHere>)$') != null && user.getProjectRoles(project).map(p => p.name).includes("<ProjectRoleHere>") || issue.issueType.name.match('^(<IssueTypeHere>|<IssueTypeHere>)$') != null //You may also want to set the Error message to have text similar to 'You must be in the X Project Role to create the X Issue Type'


// Validate that one of two label fields must have a value 
// Note: You should replace 'customfield_10099' and 'customfield_10100' in the example below with the ID's of the label fields inside of your instance. 
issue.customfield_10099 !=null || issue.customfield_10100 !=null //You may also want to set the Error message to have text similar to 'You must specify a value in one of the label fields'



Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.