Set a custom field using data from a HTTP request
Introduction
As you may already know, JEMH can set static or dynamic values for custom fields using the Custom Field Default feature found in Project Mappings.
When configuring a dynamic value via a script, you can make a HTTP request and use the response data. This is particularly useful if for example, you wish to query the REST API of a Jira app.
JEMH exposes methods on its utility class known as "jemhUtils" that allow this to be done. These methods can then be used in a Velocity scripted Custom Field Default.
Relevant syntax
Method | Description | Parameters | Returns |
---|---|---|---|
| Execute a HTTP request using the GET method to the specified URL. |
| JSONObject for you to parse |
| Since 3.1.5. Execute a HTTP request using custom options. Full information on this method can be found on its page. |
Example using GET request
We will use the Jira REST API to get a JSON representation of an issue. From the JSON data returned, we will set a custom field with the number of watchers on the issue. The REST API states that the request URL (relative to your Jira base URL) will take the form of:
GET /rest/api/2/issue/{issueIdOrKey}
A response for this call will look similar to this:
Example JSON response body
{
"expand": "renderedFields,names,schema,operations,editmeta,changelog,versionedRepresentations",
"id": "10200",
"self": "http://localhost:2990/jira/rest/api/latest/issue/10200",
"key": "SOFDEFAULT-1",
"fields": {
"issuetype": {
"self": "http://localhost:2990/jira/rest/api/2/issuetype/10104",
"id": "10104",
"description": "A problem which impairs or prevents the functions of the product.",
"iconUrl": "http://localhost:2990/jira/secure/viewavatar?size=xsmall&avatarId=10303&avatarType=issuetype",
"name": "Bug",
"subtask": false,
"avatarId": 10303
},
"timespent": null,
"project": {
"self": "http://localhost:2990/jira/rest/api/2/project/10003",
"id": "10003",
"key": "SOFDEFAULT",
"name": "SOFTWARE DEFAULT",
"projectTypeKey": "software",
"avatarUrls": {
"48x48": "http://localhost:2990/jira/secure/projectavatar?avatarId=10324",
"24x24": "http://localhost:2990/jira/secure/projectavatar?size=small&avatarId=10324",
"16x16": "http://localhost:2990/jira/secure/projectavatar?size=xsmall&avatarId=10324",
"32x32": "http://localhost:2990/jira/secure/projectavatar?size=medium&avatarId=10324"
}
},
"fixVersions": [],
"aggregatetimespent": null,
"resolution": null,
"customfield_10302": null,
"resolutiondate": null,
"workratio": -1,
"lastViewed": "2019-08-20T15:31:00.994+0100",
"watches": {
"self": "http://localhost:2990/jira/rest/api/2/issue/SOFDEFAULT-1/watchers",
"watchCount": 1,
"isWatching": false
},
"created": "2019-08-20T15:30:45.498+0100",
"priority": {
"self": "http://localhost:2990/jira/rest/api/2/priority/4",
"iconUrl": "http://localhost:2990/jira/images/icons/priorities/low.svg",
"name": "Low",
"id": "4"
},
"customfield_10100": null,
"customfield_10101": null,
"customfield_10102": null,
"labels": [],
"timeestimate": null,
"aggregatetimeoriginalestimate": null,
"versions": [],
"issuelinks": [],
"assignee": null,
"updated": "2019-08-20T15:31:00.918+0100",
"status": {
"self": "http://localhost:2990/jira/rest/api/2/status/10000",
"description": "",
"iconUrl": "http://localhost:2990/jira/images/icons/status_generic.gif",
"name": "To Do",
"id": "10000",
"statusCategory": {
"self": "http://localhost:2990/jira/rest/api/2/statuscategory/2",
"id": 2,
"key": "new",
"colorName": "blue-gray",
"name": "To Do"
}
},
"components": [],
"timeoriginalestimate": null,
"description": "This is the description of the issue.",
"customfield_10010": null,
"customfield_10011": null,
"customfield_10012": null,
"timetracking": {},
"customfield_10005": "0|i0000v:",
"customfield_10007": null,
"customfield_10008": [],
"attachment": [],
"customfield_10009": null,
"aggregatetimeestimate": null,
"summary": "This is the summary",
"creator": {
"self": "http://localhost:2990/jira/rest/api/2/user?username=admin",
"name": "admin",
"key": "admin",
"emailAddress": "admin@admin.com",
"avatarUrls": {
"48x48": "https://www.gravatar.com/avatar/64e1b8d34f425d19e1ee2ea7236d3028?d=mm&s=48",
"24x24": "https://www.gravatar.com/avatar/64e1b8d34f425d19e1ee2ea7236d3028?d=mm&s=24",
"16x16": "https://www.gravatar.com/avatar/64e1b8d34f425d19e1ee2ea7236d3028?d=mm&s=16",
"32x32": "https://www.gravatar.com/avatar/64e1b8d34f425d19e1ee2ea7236d3028?d=mm&s=32"
},
"displayName": "admin",
"active": true,
"timeZone": "Europe/London"
},
"subtasks": [],
"reporter": {
"self": "http://localhost:2990/jira/rest/api/2/user?username=admin",
"name": "admin",
"key": "admin",
"emailAddress": "admin@admin.com",
"avatarUrls": {
"48x48": "https://www.gravatar.com/avatar/64e1b8d34f425d19e1ee2ea7236d3028?d=mm&s=48",
"24x24": "https://www.gravatar.com/avatar/64e1b8d34f425d19e1ee2ea7236d3028?d=mm&s=24",
"16x16": "https://www.gravatar.com/avatar/64e1b8d34f425d19e1ee2ea7236d3028?d=mm&s=16",
"32x32": "https://www.gravatar.com/avatar/64e1b8d34f425d19e1ee2ea7236d3028?d=mm&s=32"
},
"displayName": "admin",
"active": true,
"timeZone": "Europe/London"
},
"customfield_10000": null,
"aggregateprogress": {
"progress": 0,
"total": 0
},
"customfield_10004": null,
"environment": null,
"duedate": null,
"progress": {
"progress": 0,
"total": 0
},
"comment": {
"comments": [],
"maxResults": 0,
"total": 0,
"startAt": 0
},
"votes": {
"self": "http://localhost:2990/jira/rest/api/2/issue/SOFDEFAULT-1/votes",
"votes": 0,
"hasVoted": false
},
"worklog": {
"startAt": 0,
"maxResults": 20,
"total": 0,
"worklogs": []
}
}
}
Configuration
Navigate to Profile → Project Mapping → Custom Field Defaults.
Create New Custom Field Default and click Edit
Select the Custom Field
Set Field Value Type to Velocity Scripted Value
Enter the following script:
#set ( $data = $jemhUtils.invokeLocalRest("/rest/api/latest/issue/SOFDEFAULT-1")) #if ($data) $data.fields.watches.watchCount #end
The first line of the script will make a HTTP request and set the response into the reference called $data. The second line traverses the data structure (note that field names are case-sensitive). When the script is executed, the script should result in the number of watchers being rendered:
Result
Working with arrays in response data
Arrays in response data are are JSONArray objects. The standard way to iterate through an array will not work here as JSONArray is not an Iterable. Note the spaces separating "intValue()", "-" and "1" are important.