The visual rule editor, which we explored in Chapter 1, Key Concepts of Automation, makes the creation of complex automation rules really easy and requires no knowledge of the Jira API or any specialist scripting knowledge in order to get started.
While this approach will work for a large number of use cases, there will always be cases where you will need direct access to issue data and the ability to perform more complex functions against this data. This is where smart values come to the rescue.
This chapter will introduce you to smart values. You will learn how to find smart values, how smart functions work, and how you can introduce your own smart values into automation rules. You will also get an overview of how to manipulate dates, lists of data, text values, and how to perform numeric calculations using math expressions.
In this chapter, we'll cover the following topics:
By the end of this chapter, you will have learned how you can use smart values and functions to manipulate dates, text strings, and lists, as well as how to use math expressions and functions to perform calculations involving numerical values.
The requirements for this chapter are as follows:
In both instances, you will need to have at least project administrator access to a Service Management project and a Scrum Software project to be able to follow the examples in this chapter. For the examples in this chapter, we have used the IT Service Management project template to create the Service Management project, and the Scrum Software project template to create the Software project.
You can download the latest code samples for this chapter from this book's official GitHub repository at https://github.com/PacktPublishing/Automate-Everyday-Tasks-in-Jira. The Code in Action videos for this chapter are available at https://bit.ly/39Hk44d.
Smart values allow you to access and manipulate almost all issue data in the context of an automation rule. In addition to allowing you to access the issue data, smart values also provide advanced functionality in the form of functions that allow you to perform complex operations on the issue data.
In this section, we'll explore what smart values are available to you and how to recognize and use them in rules. We will also introduce you to smart value functions and explain how you can create your own smart values for use in your rules.
Let's begin by taking a look at where we can find smart values and how to use them.
Smart values are generally set in the rule context by the trigger that initiated execution of the rule. In addition, certain actions, such as the Send web request action, can also introduce smart values into the rule context.
Each of the smart values made available by these triggers are data structures that contain fields. These can either be simple field types, such as text or numbers, or more complex data structures that, in turn, contain other fields.
For example, a trigger such as Issue updated will set the smart value {{issue}} to the current Jira issue in the rule context. This smart value is a data structure that represents an issue in Jira and contains both simple and complex fields.
Examples of simple fields would be {{issue.summary}} and {{issue.key}}, which allow you to access the summary and key of the issue, respectively. An example of a complex field, a field that itself is a data object, would be {{issue.assignee}}, as it itself contains further fields, which allow you to access the assignee's display name, email address, and accountId fields in the case of Jira Cloud or key in Jira Server or Jira Data Center. These would be represented as follows: {{issue.assignee.displayName}}, {{issue.assignee.emailAddress}}, {{issue.assignee.accountId}}, and {{issue.assignee.key}}, respectively.
Tip
To access a custom field in a smart value, you can either use the custom field's name or its ID. For example, if you had a custom field named My Custom Field with an ID value of 10000, you could access it in a smart value as either {{issue.My Custom Field}}, {{issue.my custom field}}, or {{issue.customfield_10000}}. You can find the custom field ID by editing the custom field from the Custom Fields admin section and copying the ID from the resulting URL. However, you should only use this approach if, for some reason, you have multiple custom fields with the same name.
In the following table, we can see the smart values made available by each automation trigger that you can use in subsequent rule components:
The preceding table shows the main smart value for each trigger and, for the majority of cases, you can find the fields available for each of these smart values listed at https://support.atlassian.com/jira-software-cloud/docs/smart-values-general for Jira Cloud, or https://confluence.atlassian.com/display/AUTOMATION/Smart+values for Jira Server and Jira Data Center.
Smart values are based on the Mustache templating system (https://mustache.github.io). To use smart values (or tags, as they are referred to in Mustache), you surround keys using double curly braces (or mustaches). Using this format signifies to the rule engine that it needs to treat the given value as a smart value, and not just static text.
To access fields within smart value objects, you use dot notation. For example, to access the display name of the assignee of an issue, you would use dot notation as follows: {{issue.assignee.displayName}}.
Tip
When you reference a field or value that does not exist in a particular issue, the smart value will be empty. You can instead specify that a smart value has a default value by using the pipe (|) symbol. For example, the smart value {{issue.description|No description was supplied}} will return the text No description was supplied if the Description field is empty.
Smart values can be used in Advanced compare condition, JQL condition, and User condition, as well as in most rule actions.
Let's take a look at an example rule that uses smart values.
In this example rule, we are going to add a comment to the issue when the issue is first assigned, which will notify the reporter that we're working on their request:
We need to examine the changelog to ensure that the previous assignee was empty before continuing. The changelog contains all the fields that were changed in the issue during the update that caused the trigger to fire. For each changed field, the changelog will contain the from and to values, as well as the text representations of those values in the fromString and toString fields.
Set the condition fields as follows and click Save:
First value: {{changelog.assignee.fromString}}
Condition: equals
Second value: Leave blank
Add the following text to the Comment field and click Save:
Hi {{issue.reporter.displayName}}
We're now looking into your issue {{issue.summary}}.
We'll get back to you soon with an update.
{{issue.assignee.displayName}}.
The rule should look as follows:
In this section, we learned how we can use smart values in our rules to make them more flexible and, in the next section, we will introduce you to smart value functions.
In addition to the ability to access data fields with smart values, you can also manipulate these data values with the addition of functions.
Functions are generally appended to the end of smart values using dot notation. For example, to truncate the issue summary to the first 50 characters and add an ellipsis to the end of the truncated summary, we would use the following smart function: {{issue.summary.abbreviate(50)}}.
Important note
Smart value functions can only be applied to fields of the corresponding type. For example, date and time functions can only be applied to the created, updated, duedate, resolutiondate, and any custom fields with Date Picker or Date Time Picker types.
Smart functions can also be chained together, allowing us to perform multiple transformations in a single step. For example, the following smart function chain will convert the summary to lowercase, extract the fifth to tenth characters, and then append an exclamation to the end: {{issue.summary.toLowerCase().substring(5,10).concat("!")}}.
Let's look at an example of using smart value functions in a rule.
In this example, we are going to create a manually triggered rule that a user can invoke to create a customized comment on the issue requesting the reporter to upload attachments to help further diagnose an issue:
Add the following text to the Comment field and click on Save:
Hi {{issue.reporter.displayName.split(" ").first}}
We require a little more info in order to diagnose your issue further.
Please take a screenshot and attach it to this issue by no later than {{now.plusBusinessDays(2).fullDate}}.
Thank you :)
{{issue.assignee.displayName}}
The rule should look similar to the following screenshot:
Important note
In Jira Cloud, it is possible to use the Create variable action to create a custom smart value based on other smart value functions and expressions that you can then make use of in later conditions and actions.
In this section, we learned what smart values are and how we can use them to access data in automation rules. We also learned how to find the correct smart value for triggers and how to find the associated smart value fields. We also looked at smart value functions and saw how we can chain them together to apply multiple transformations in a single step.
In the next section, we will look at how we can use smart values to manipulate and format dates in automation rules.
As a project management and tracking tool, having the ability to manipulate dates in Jira is critical to the correct operation of the application, and smart values in automation rules give us a lot of flexibility in how we can use and manipulate dates and times in rules.
In this section, we will look at how we can use smart values to format dates and times for inclusion in other date and time fields, as well as how to format them for use in other fields such as text fields, the advanced field editor, or for use in notifications.
We will also explore all the available formats and functions applicable to dates and see how we can use these in automation rules.
Tip
You can access the current date and time using the smart value {{now}}. All formatting and date manipulation functions that can be used with date fields can also be used with the {{now}} smart value.
Let's take a look at how we can format dates using smart value functions.
The ability to format dates and times gives us flexibility in how we can display these to users, in either comments, text fields, or notifications. It also allows us to be able to specify the exact format to use when communicating with external systems that require date and time inputs in specific formats and in reformatting dates and times received from external systems.
Finally, being able to control the format of dates and times allows us to manipulate them using smart value functions and ensure that we can set a date or time field with the resulting calculation correctly.
In the following table, we can see how the date, Tuesday, December 31, 2019 10:25:30 PM CET will be formatted using each of the available date and time formats available to automation rules:
As you can see from the preceding list, dates and times can be formatted for display in a variety of ways, and it is also possible to use a custom pattern if none of the standard ones are suitable.
Tip
The pattern syntax used for the custom formatters format("pattern") and as("pattern") are defined by the underlying Java platform and the full syntax descriptions can be found at the following link, https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/format/DateTimeFormatter.html, under the heading Patterns for Formatting and Parsing.
In addition to the formats that we have just looked at, with smart values, it is also possible to format dates and times for specific locales and time zones, or even the locale or time zone of a specific user, such as the assignee or reporter of an issue.
The following formatting functions are provided to enable locale- and time zone-based formatting of dates and times:
The full list of supported locales can be found at https://www.oracle.com/java/technologies/javase/jdk11-suported-locales.html#modules. The list of time zones is defined by the tz database and the full list can be found at https://en.wikipedia.org/wiki/List_of_tz_database_time_zones.
Let's see how we can use date formats in an example rule.
In this rule, when a new employee onboarding request is created, we need to ensure that certain tasks, such as provisioning a laptop and desk phone, are completed before the new employee starts.
We want the sub-task summaries to include the start date so that it is immediately apparent to the user assigned to the request when the task is due:
Complete the fields as follows, and then click Save:
Field: Request Type
Condition: equals
Value: Onboard new employees
Add two sub-tasks with the following summaries:
Provision new laptop by {{issue.duedate.longDate}}
Setup new desk phone by {{issue.duedate.longDate}}
The rule should look similar to the following screenshot:
Now that we've seen how to format dates and time, let's take a look at how we can manipulate them using smart value functions.
In addition to providing the ability to format dates, a number of smart value functions are provided that enable us to perform operations on dates. These operations allow us to perform comparisons on dates and times, perform basic arithmetic on dates and times, and manipulate the various components of dates and times.
Let's take a look at the functions you can use to manipulate dates in your rules. The first set of functions relate to date comparisons and allow you to compare one date with another:
The next set of functions allows us to manipulate dates and times in various ways:
Now that we've taken a look at the various functions available to manipulate dates and times, let's take a look at a couple of examples of how to use them in rules.
For this example, we have a number of sub-tasks for each user story in the software project, and we would like to update the due date of the user story to 5 business days from the current date when the last sub-task transitions to In Progress:
Set the condition fields as follows and click Save:
Field: Issue Type
Condition: equals
Value: Sub-task
Set the condition fields as follows and then click Save:
Related issues: Sub-tasks
Condition: All match specified JQL
Matching JQL: status = "In Progress"
Your rule should now look similar to the following screenshot:
Let's now take a look at another example of using smart value functions to manipulate dates.
For this example, we want to set the due date of an issue to the 15th of the current month if the issue is created before the 15th of the month, and to the 15th of the following month if the issue is created after the 15th of the month:
First value: {{issue.created}}
Condition: greater than
Second value: {{now.startOfMonth.plusDays(15)}}
Your rule should now look as per the following screenshot:
Your rule should look similar to the following screenshot:
In this section, we have taken a look at the various smart value date and time functions available and learned how to use these to manipulate dates in automation rules. Next, we'll take a look at how we can use smart values to work with text.
In a similar fashion to working with dates and times, smart value functions that deal with text strings allow us to manipulate any text field type, including the sub-attributes of fields where they are also text-type fields.
In this section, we will look at how smart value functions for text allow us to manipulate text, encode it for use in HTML, JSON, XML, or URLs, and how we can use functions to test for specific characteristics within blocks of text.
Let's take a look at the functions available to manipulate text in automation rules. The first set of functions we'll look at deal with text comparisons and return either true or false depending on whether the check passes or fails:
The next set of functions allows us to convert text into other types, such as numbers, dates, or lists:
Tip
Use the isNumeric test on the text field prior to conversion to ensure you don't get errors by trying to convert text that cannot be represented as numbers.
Tip
A regular expression is a special text string for describing a search pattern. These patterns are usually used to perform find or find-and-replace operations on text strings or to validate input. In Jira automation, regular expressions are based on the underlying Java implementation, which can be found at https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html.
The following set of functions allows us to change the way text is displayed, extract portions of text, or even replace blocks of text:
The final set of functions allows us to encode text to comply with specific standards. This is especially useful when integrating with external systems or when sending email notifications from rules:
Now that we've looked at the various functions available that we can use to manipulate text, let's take a look at how we can leverage these in automation rules.
A common scenario faced by many teams is that after a ticket has been resolved, the reporter replies to say thank you. This will generally cause the ticket to re-open and someone will have to manually go and resolve the ticket again.
In this example, we'll use text smart value functions to examine the latest comment and auto-resolve the issue if we find the words "thank you":
Complete the condition fields as follows and click Save:
First value: {{issue.comments.last.body.trim.toLowercase}}
Condition: contains regular expression
Second value: thanks+you
The rule should now look as follows:
In this section, we have seen how we can use smart value functions to compare and manipulate text values. In the next section, we'll take a look at how we can use smart values and functions to work with lists of items.
There are a number of scenarios you'll come across when authoring automation rules, where you'll be presented with a list of items. This could be a list of versions or components attached to an issue, or a list of issues, such as all the sub-tasks of a particular issue.
Smart value functions for lists give us the ability to work with these lists of items by iterating over each item and being able to perform additional functions against attributes of the items. We also have access to functions that allow us to perform simple calculations against all the items in a list.
In this section, we'll take a look at the smart value functions available for accessing and formatting lists of items.
We'll cover examples of iterating over lists using smart values when we look at sending notifications via email and Slack in Chapter 4, Sending Automated Notifications.
Let's now take a look at the smart value functions available for working with lists:
Let's take a look at how we can incorporate what we have learned about lists into a practical example that uses a smart value list function to calculate the sum of story points in sub-tasks.
In this example, we are tracking story points against sub-tasks and would like to ensure that the parent story always reflects the correct total of story points whenever the story points in one of its sub-tasks' changes. You will need to enable the configuration context for the Story Points custom field to include sub-tasks in addition to Epics and Stories:
Set the condition fields as follows and click Save:
Field: Issue Type
Condition: equals
Value: Sub-task
Add the following smart value expression and click Save:
{{issue.subtasks.Story Points.sum}}
In this section, we have learned how we can use smart value list functions to iterate over items in a list and how to perform operations against values in a list.
Next we'll take a look at the mathematical operations available for use in automation rules.
So far, we have seen how to use smart values for working with dates, text, and lists and how they are able to help you create powerful and flexible automation rules.
In this section, we are going to look at math expressions and functions. Math expressions and functions will allow you to perform calculations directly in numeric fields as well as allow you to reference numeric fields in calculations for display in text fields. In Jira, all numeric fields are based on floating-point numbers.
To use any of the math expressions, they need to be surrounded by the following smart value block in order to be recognized as shown in the following example:
{{#=}} <math expression> {{/}}
The {{#=}} operator signifies to the automation engine that you are about to perform a calculation and is a custom addition to the underlying Mustache library.
Important note
Jira Cloud offers additional capabilities for working with numbers, including the ability to format numbers using locales or with custom patterns.
Let's take a look at the math expressions in common between Jira Cloud and Jira Server first.
Math expressions support the usual operations you'd expect to see, as can be seen in the following table:
In addition to the basic operations, math expressions support Boolean operators as well, and the supported operators can be seen in the following table:
Math expressions also support a number of functions that can be used, and we can see a full list of supported functions in the following table:
There are also a few constant values that are supported for use in math expressions, and these are listed here:
In addition to the preceding common functions and operations, Jira Cloud introduces some additional numerical operations. These operations do not need to be surrounded by the smart value block we learned about earlier, but rather can be applied directly to numerical smart values or fields.
Let's take a look at these additional numerical operations:
Jira Cloud also adds the ability to format numerical values using smart value functions, and these functions are listed here:
As we've seen, there is a comprehensive number of smart value functions that we can make use of to perform advanced mathematical operations in automation rules. Let's now take a look at using math expressions in an example.
In this example, we're going to update the original estimate of an epic any time tracking fields in the epic's underlying issues change:
Set the fields of the condition as follows and click Save:
Related issues: Epic
Condition: Exists
Add the following math expression to the Original Estimate (System) field and click Save:
{{#=}}{{fieldChange.to}}/60 – {{fieldChange.from}}/60 + {{issue.original estimate}}/60{{/}}
The rule should now look similar to the following screenshot:
In this section, we have seen the comprehensive list of smart value mathematical functions available for use in your automation rules and how we can use these to manipulate numerical values.
In this chapter, you have learned how to enhance the functionality of automation rules by using smart values and smart value functions.
We learned which smart values are set by the various triggers and how you can access their attributes or fields. In addition, you learned about smart value functions and how they can be used in isolation against a particular field or chained together to perform multiple operations on data in a single step.
Finally, we learned about the various smart value functions available for dates, text, lists, and numerical values and how these can be used to format and manipulate data to create extremely powerful and flexible rules.
Smart values and their associated functions can be used in nearly every field in automation rule conditions and actions, and we'll learn throughout the rest of this book how they will help us to create much more powerful and richer rules as a result of their use.
In the next chapter, we will be looking at how to send notifications to various channels from your automation rules.
18.219.22.169