Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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.

...

  1. In Jira, set up your ScriptRunner scripted field. Follow the instruction here: https://docs.adaptavist.com/sr4jc/latest/features/scripted-fields

    1. This is the code snippet for the “Calculated cost” field:

      Code Block
      languagegroovy
      // 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)
      }
      
  2. Follow the instructions here: Integration: Including ScriptRunner Scripted Fields In Jira Snapshots to add this field to the available fields in Jira Snapshots.

  3. To create the costs overview Cost Overview table in Confluence, navigate to the page where this table should appear, and enter EDIT mode.

  4. In the top editor toolbar, click the “+” icon and type “jira s” in the search bar.  Then select the “Jira snapshots” Snapshots” macro.

  5. In the “Edit Jira Snapshots Macro” overlay:

    1. Enter a title in the “Level title” field to represent the first level or “list” of Jira issues.

    2. Enter a query in the “Search JQL” field to limit the scope of issues, such as:

      1. Code Block
        project = DEVT AND issuetype in standardIssueTypes() order by created DESC
      2. In the “Add fields to display” field, select the desired columns, including the ScriptRunner scripted field.

    3. 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.”

    4. Enter a title for the 2nd level.

    5. Enter a query in the “Search JQL” field, to retrieve all sub-tasks:

      1. Code Block
        parent=$key
    6. In the “Add fields to display” field, select the desired columns.

    7. Click the “Insert” button at the bottom right, to complete the macro’s configuration.

  6. Click the “Publish” button at the top right of the page.

  7. Finally, click the “Take New Snapshot” button to generate a static list of issues.



...