Work with Issue and Entity Properties
Add key/value entity properties to issues, projects, users, and comments using HAPI for use in script automations.
Migrating to Jira Cloud? This feature is available in Cloud. |
Entity properties allow you to add key/value stores to issues, projects, users, and comments. Your scripts can use these properties for automations, for example, to store a user's department name on the user object.
There are two different mechanisms you can use to create these data stores. Check out the table below to compare the two mechanisms:
| Entity properties (more modern) | Property sets (can be applied to any Jira entity) |
|---|---|
| Permissions are respected, for example, you can only write properties on an issue if you can edit that issue (HAPI provides you with a way to override security). | Have no permissions model. |
| Are accessible through a REST API—if the user can view the issue, they can access all the properties through this REST API. | Are not accessible through the out-of-the-box REST API. |
| Are limited to 32k. | Effectively unlimited size for strings. |
| As well as simple data types, you can serialize your own classes and store them on entities. | - |
| Issue properties can be indexed and searched with JQL. | - |
| - | HAPI only enables property sets for ApplicationUser objects currently. |
Entity properties
Setting and getting issue properties
The following is a simple example of setting and getting an issue property:
def issue = Issues.getByKey('ABC-1')
// setting the property
issue.entityProperties.setString('my first property', 'Hello World!')
// retrieving the property value
issue.entityProperties.getString('my first property') // returns 'Hello World!'You can also set and get several other property types:
import groovy.json.JsonOutput
import java.time.LocalDate
def entityProperties = Issues.getByKey('ABC-1').entityProperties
entityProperties.setString('foo', 'bar')
entityProperties.setBoolean('onboarded', true)
entityProperties.setInteger('meaning of life', 42)
entityProperties.setLong('birth year', 1972L)
entityProperties.setLocalDate('my date', LocalDate.now())In addition to the property types listed above, you can store maps, lists, or your own classes. For example, to store a map you could use the following:
entityProperties.setAsActualType('tasks', [
content: 'complete design work',
estimateInDays: 2,
])
// retrieve the properties as a Map
entityProperties.getAsActualType('tasks', Map)You can store instances of your own classes. Check out the Searchable entity properties section to find out how.
Overwriting security restrictions
To read and write properties overriding the security restrictions, use getEntityPropertiesOverrideSecurity() :
def entityProperties = Projects.getByKey('ABC').entityPropertiesOverrideSecurity
entityProperties.setString('foo', 'bar')Searchable entity properties
You can store your own instances of your own class and have it as a searchable entity property.
setJson and getJson:entityProperties.setJson('my json', JsonOutput.toJson([foo: 'bar', qux: 3]))Creating a searchable entity property
Property sets
Reading and writing user properties
As discussed in the table above, property sets are an older form of storing key/value pairs. Use the following to read and write user properties:
def user = Users.loggedInUser
def propertySet = user.propertySet
propertySet.setString('foo', 'bar')
propertySet.getString('foo')