...
In this Jira Snapshot example, the development Development Issues table lists Jira tasks, or issues, with subtasks for each task listed in the second level.
...
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:
Code Block language groovy // 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 costs overview 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” 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:
Code Block 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”level.”
Enter a title for the 2nd level.
Enter a query in the “Search JQL” field, to retrieve all sub-tasks:
Code Block 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.
...