ScriptRunner Scripted Fields in Jira Snapshots
See this use-case live on our sandbox environment here: https://radbee-sandbox.atlassian.net/wiki/spaces/DEVT/pages/157089959/Cost+Overview
In this Jira Snapshot example, the Development Issues table lists Jira tasks, or issues, with subtasks for each task listed in the second level.
The budgeting system in this case requires that, in order to calculate costs for a issue, each subtask must have an assigned cost. These subtask costs are actual, entered values, and the sum of subtasks costs will be calculated and assigned to the task.
Here are the Scripted Fields for this task, and the costs are calculated on the right. The $3,900 cost is the sum of the cost of the $2,400 and $1,500 subtasks.
The sum calculation itself is created in the “Calculated Cost” scripted field.
Prerequisites
This use case assumes that ScriptRunner for Jira is installed on the Jira instance (from which the Jira Snapshots are taken)
How to Do It:
In Jira, set up your ScriptRunner scripted field. Follow the instruction here: https://docs.adaptavist.com/sr4jc/latest/features/scripted-fields
This is the code snippet for the “Calculated cost” field:
// sum up the values of this custom field final customFieldName = 'Cost' final parentIssueKey = issue.key as String final customFieldId = getFieldIdByName(customFieldName) List subTasksKey = getSubTasksKeyByIssue(parentIssueKey) as List // if the issue doesn't have any sub-tasks or is a subtask itself then no need for action if (subTasksKey.empty) { return } def firstSubTask = getIssueByKey(subTasksKey[0]) as Map if (!existFieldInIssue(firstSubTask.key as String, customFieldId)) { def fields = firstSubTask.fields as Map def issueType = fields.issuetype as Map def project = fields.project as Map logger.info "Custom field with name $customFieldName is not configured for issue type ${issueType.name} and project ${project.key}" return } def sum = subTasksKey.sum { subTaskKey -> def subtask = getIssueByKey(subTaskKey as String) as Map if (subtask.fields[customFieldId]!=null) subtask.fields[customFieldId] else 0 } return sum assert result.status == 204 String getFieldIdByName(String fieldName) { def customFieldObject = get('/rest/api/2/field') .asObject(List) .body .find { (it as Map).name == fieldName } (customFieldObject as Map).id } List getSubTasksKeyByIssue(String parentIssueKey) { def parentIssue = getIssueByKey(parentIssueKey) as Map def fields = parentIssue.fields as Map def subtasks = fields.subtasks as List<Map> subtasks*.id } Map getIssueByKey(String issueKey) { def result = get("rest/api/2/issue/$issueKey") .header('Content-Type', 'application/json') .asObject(Map) assert result.status == 200: result.body result.body } Boolean existFieldInIssue(String issueKey, String fieldId) { def issue = getIssueByKey(issueKey) def issueFields = issue.fields as Map issueFields.containsKey(fieldId) }
Follow the instructions here: Integration: Including ScriptRunner Scripted Fields In Jira Snapshots to add this field to the available fields in Jira Snapshots.
To create the Cost Overview table in Confluence, navigate to the page where this table should appear, and enter EDIT mode.
In the top editor toolbar, click the “+” icon and type “jira s” in the search bar. Then select the “Jira Snapshots” macro.
In the “Edit Jira Snapshots Macro” overlay:
Enter a title in the “Level title” field to represent the first level or “list” of Jira issues.
Enter a query in the “Search JQL” field to limit the scope of issues, such as:
project = DEVT AND issuetype in standardIssueTypes() order by created DESC
In the “Add fields to display” field, select the desired columns, including the ScriptRunner scripted field.
Add the next level, so that the individual sub-tasks are also shown in the table. You’ll need to select “+ Add new level.”
Enter a title for the 2nd level.
Enter a query in the “Search JQL” field, to retrieve all sub-tasks:
parent=$key
In the “Add fields to display” field, select the desired columns.
Click the “Insert” button at the bottom right, to complete the macro’s configuration.
Click the “Publish” button at the top right of the page.
Finally, click the “Take New Snapshot” button to generate a static list of issues.