Skip to main content
Skip table of contents

Show work logged per user and issue for a structure


GROOVY
package examples.docs.structure

import com.atlassian.query.Query
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.almworks.jira.structure.api.permissions.PermissionLevel
import com.almworks.jira.structure.api.StructureComponents
import com.almworks.jira.structure.api.forest.ForestSpec
import com.almworks.jira.structure.api.row.StructureRow
import com.almworks.jira.structure.api.row.RowManager
import com.almworks.jira.structure.api.item.ItemIdentity
import com.almworks.jira.structure.api.item.CoreIdentities
import com.almworks.jira.structure.api.util.JiraComponents
import com.almworks.integers.LongArray
import com.almworks.integers.LongIterator
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.label.LabelManager
import com.atlassian.jira.issue.worklog.WorklogManager
import com.atlassian.jira.issue.IssueManager

@Grab(group = 'com.almworks.jira.structure', module = 'structure-api', version = '16.10.0')

@WithPlugin("com.almworks.jira.structure")

@PluginModule
StructureComponents structureComponents

def plugin = com.atlassian.jira.component.ComponentAccessor.pluginAccessor.getPlugin('com.almworks.jira.structure')

def structureManager = structureComponents.getStructureManager()
def forestService = structureComponents.getForestService()
def permission = PermissionLevel.valueOf("ADMIN")
def helper = plugin.getModuleDescriptor('helper').module

def struct = structureManager.getStructuresByName("test", permission)[0]
def structureName = struct.getName()
def forestSpec = ForestSpec.structure(struct.getId())
def forestSrc = forestService.getForestSource(forestSpec)
RowManager rowManager = structureComponents.getRowManager()
def forest = forestSrc.getLatest().getForest()

// this variable will store all the elements in our structure that are issues (vs folders or generators).
LongArray onlyIssues = new LongArray()

// here we are iterating over all the rows of our structure to get the issues.
for (LongIterator ri : forest.getRows()) {
    StructureRow row = rowManager.getRow(ri.value())
    ItemIdentity itemId = row.getItemId()
    if (CoreIdentities.isIssue(itemId)){
        onlyIssues.add(itemId.getLongId())
    }
}

def wm = ComponentAccessor.getWorklogManager()
def issueManager = ComponentAccessor.getComponent(IssueManager)
def user = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()
def userToIssue = [:]
def seen = []

onlyIssues.each{ is ->
    def iss = issueManager.getIssueObject(is.value())
    def wl = wm.getByIssue(iss)

    def it = wl.each{
        def issue = iss.getKey()
        def author = it.getAuthorKey()
        def time = it.getTimeSpent()
        if (!seen.contains(author)){
            // put author in seen
 seen.push(author)
            def tempHM = [:]
            tempHM[issue] = time
            userToIssue[author] = tempHM
        } else {
            if(!userToIssue[author].containsKey(issue)){
                // create a new issue to time hashmap to add
 def tempHM = [:]
                tempHM[issue] = time
                userToIssue[author] += tempHM
            }
            else{
                userToIssue[author][issue] = userToIssue[author][issue] + time
            }
        }
    }
}

'<table><tr><th>Structure</th><th>User </th><th>Issue Worklogged (hours)</th></tr>' +
        userToIssue.collect {'<tr><td>' + it.key + '</td><td><table>' +
                it.value.collect {'<tr><td>' + it.key + '</td> <td>' + (it.value*0.000277778).toBigInteger() + '</td></tr>' }.join('') +
                '</table></td></tr>'}.join('') +
        '</table>'

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.