Conditional execution/validation using a Groovy expression

The Post functions and Validators provided by the JMWE add-on have a Conditional execution/Conditional Validation section to control the execution of the post-function/validator respectively. You need to provide a Groovy script in the editor based on whose result, the post-function/validator will either be executed or skipped.

On this page:

Conditional execution of a post-function

To conditionally execute a post-function, select Only if condition is true option in the Conditional execution section of the post-function and add a Groovy expression in the Condition. The post-function is executed only if the written condition evaluates to true or a Groovy truthy. In post-functions that operate on related Issues (like Transition related issues, Set field value of related issues, etc.) the conditional execution script runs once for each related issue, and the related issue is available through the relatedIssue variable and the deprecated linkedIssue variable in your Groovy script.

Conditional validation

To conditionally execute a validator, select Conditional validation option in the Validator scope section of the validator and add a Groovy expression in the Condition. The validator is executed only if the written condition evaluates to true or a Groovy truthy. In validators that operate on related Issues (like Related Issues Validator etc.), the conditional execution script runs once for each related issue, and the related issue is available through the relatedIssue variable and the deprecated linkedIssue variable in your Groovy script.

Expected Value for a condition

The value should be either a boolean value (true or false), or more generally any value that will be interpreted as either "truthy" or "falsy". Click on the Expected Value tab of the Groovy editor to know the expected value. You can also quickly test your written script against any issue using the script tester tool. For example:

Groovy expressions returning a "truthy" value: 

  • 1 == 1 or any boolean expression that is true
  • issue.get("assignee") if the issue is assigned
  • issue.getAsString("assignee") == "jdoe" if the issue is assigned to user "jdoe"
  • issue.get("description") if the issue's description is not empty

Groovy expressions returning a "falsy" value: 

Any value will be interpreted as "falsy" (the equivalent of false) except: true, a non-empty string, a non-empty Collection, a non-empty Map, a non-empty array, a number different from zero (0)

For example, false, 0, "", []

Below are a few use cases that detail the usage of these functionalities.

Use cases

Conditional execution

(lightbulb)  Automatically escalate an issue if it is being raised with a "Blocker" priority.

 Steps
  • Add the Transition issue post-function to the Create transition of the issue's workflow.
  • Input the transition name or ID of the transition that escalates the issue.
  • Select the Only if condition is true option in Conditional execution and write the following code in the editor.

    return (issue.get("priority").getName() == "Blocker")
  • Place the transition at the end of the list of post-functions.

(lightbulb) Auto-assign a created issue to the Product owner, only if the issue is a Bug

 Steps
  • Create a Product Owner project role, with the product owner as the only member.
  • Add the Assign to role member post-function to the Create transition of the issue workflow.
  • Select Product Owner as the project role.
  • Select the Only if condition is true option in Conditional execution and write the following code in the editor.

    return (issue.get("issuetype").getName() == "Bug")

(lightbulb) Add a comment on the Epic when its user stories of a client-side project are resolved.

 Steps
  • Add the Comment linked issues post-function to the Resolved transition of the Story workflow.
  • Choose the issue link type has Epic and write the following content in the Comment section.

    issue.getKey() + " " + "has been resolved"
  • Select the Only if condition is true option in Conditional execution and write the following code in the editor.

    return (issue.get("project").getName() == "Client Project")

Conditional validation

(lightbulb) Allow the members of the Managers project role to omit the comment

 Steps
  • Add the Comment Required Validator to the transition of the issue's workflow.
  • Select the Conditional Validation option in Validator scope and write the following code in the editor.

    !(currentUser.isInProjectRole("Managers", issue.get("project")))

(lightbulb) Force the user to provide a Fix Version/s on resolving a bug, only when the resolution is "Fixed"

 Steps
  • Add the Field is required Validator to the Resolve transition of the issue's workflow.
  • Select the Conditional Validation option in Validator scope and write the following code in the editor.

    issue.getAsString("resolution") == "Fixed"