Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.


Excerpt

This tutorial will guide you through writing Groovy scripts that test the value of a field and perform an action based on the outcome.

For this you need to access the field of an issue looking at the Issue interface and compare it against a value using the Groovy operators. 

...

Table of Contents
maxLevel1

2.1

...

Perform an action testing the value of a field against a String

This section of the tutorial guides you through writing a conditional statement that compares the value of a field against a String. Imagine a functional use case where you want to allow the progress on an issue only when the issue is assigned to the logged in userFor this you will need to write a Groovy script in the Scripted (Groovy) condition to compare the usernames of the Assignee and the logged in user. 

Note

Note that you need not create a new condition for this. The imported backup has everything in place.

Writing the script

Step 1 - Navigate to the condition

  1. Go to the Administration icon  and click on it.
  2. Go to Issues - > Workflows
  3. Locate the workflow GIJ: Process Management Workflow
  4. Edit it
  5. Click on transition Start Progress
  6. Go to the Conditions tab
  7. Edit the Scripted (Groovy) condition which is the first condition in the list.

Step 2 - Write the script in the condition

  1. Write the following script in Groovy script.

    Code Block
    languagegroovy
    linenumberstrue
    issue.get("assignee").name == currentUser.name

    (1) Accessing the assignee of an issue using the get() method and fetching the name of the ApplicationUser using .name


  2. Your configuration should look like this:

Step 3 - Test your script

  1. Click on Test Groovy Script.
  2. Input the issue key GIJ-5
  3. Click on Test
  4. The following result will be displayed.

Step 4 - Save the configuration

  1. Click on Update
  2. Publish the workflow (you dont need to save a backup copy).

Step 5 - Test it on an issue

  1. View the issue GIJ-5 in a new tab.
  2. Check that the transition Start Progress is not available for a trigger on the issue GIJ-5.
  3. Click on Assign to me, to assign it to the logged in user.
  4. Check that the transition Start Progress is now available for a trigger on the issue GIJ-5. 

Use cases

The output of the code is a Boolean. You can use this script in any other relevant context, such as allowing only the reporter of the issue to close the issue.

...

2.

...

2 Perform an action testing for two values of a field

This section of the tutorial will teach you writing a simple condition and testing it in the Groovy script tester on the JMWE administration pages. Let us write a simple condition to check that the priority of an issue is Critical or Highest

...

Step 2 - Write the script in the editor

  1. Write the following script in the editor.

    Code Block
    languagegroovy
    linenumberstrue
    if(issue.getAsString("priority") == "Critical" || issue.getAsString("priority") == "Highest"){
      return true
    }
    else{
      return false
    }


  2. Your script will look like this:

Step 3 - Test your script

  1. Click on Test Groovy Script.
  2. Input the issue key GIJ-3
  3. Click on Test
  4. The following result will be displayed. 
  5. Image Modified

Step 4 - Simplify your script

You can simplify the script removing the if-else and returning the result of the comparison which is a Groovy truthy or falsy

  1. Go to the editor
  2. Replace the script with the following code:

    Code Block
    languagegroovy
    linenumberstrue
    issue.getAsString("priority") == "Critical" || issue.getAsString("priority") == "Highest"


Step 5 - Retest your script

  1. Click on Test again.
  2. The following result will be displayed. 
  3. Image Modified

Use cases

The output of the code is a boolean value. You can use this script in any relevant context, such as, Assign the issue to the Project lead only if the priority of the issue is Critical or Highest.

...

2.

...

3 Perform actions testing for different values of the field

This section of the tutorial will guide you through writing multiple conditions using the switch control structure

...

  1. Go back to the previous tab.
  2. Edit the workflow in Diagram mode.
  3. Click on the Create transition
  4. Click on Post-functions
  5. Edit the second post-function in the list.

Step 2 - Write the script in the post-function

  1. Write the following script in Value.

    Code Block
    languagegroovy
    linenumberstrue
    def componentimpact = issue.getAsString("components"impact")
    switch (impact) def{
    priority =case "Company wide" switch (component) {: "Highest"; break
     case "FinanceMore than one project" : priority = "HighestHigh" ; break
     case "ProductionSingle Planningproject" || "Quality ManagementIndividual" : priority = "HighMedium" ; break
     default : priority = "Medium"; break
    }


  2. Your script should look like this:

Step 3 - Test your script

  1. Click on Test Groovy Script.
  2. Input the issue key GIJ-5
  3. Click on Test
  4. The following result will be displayed.

Use cases

The output of the code is a String representing the priority of an issue. You can use this script in any relevant context, such as, setting the priority of the issue to based on the Component/s.

...

2.4 Perform an action testing for a specific value of a multi-valued field

Assign the issue to the Project lead and change the priority to Highest when an issue has been flagged.

Stop user from transitioning to the next status if a particular task is not completed (Has the manager approved?)


Code Block
languagegroovy
linenumberstrue
issue.get("Flagged").any {
  it.getValue() == "Impediment"}

2.5 Perform an action testing for a specific value of a multi-valued field

Send an email only when Send email check box is selected