Показать работу, зарегистрированную для каждого пользователя, и задачу для структуры


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()

//эта переменная будет хранить все элементы нашей структуры, которые являются задачами (вместо папок или генераторов).

LongArray onlyIssues = new LongArray()

// здесь мы перебираем все строки нашей структуры, чтобы получить задачи.

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)){

// помещаем автора в поле зрения

 seen.push(author)

def tempHM = [:]

tempHM[issue] = time

userToIssue[author] = tempHM

} else {

if(!userToIssue[author].containsKey(issue)){

// создаем новую задачу, чтобы добавить хэш-карту времени

 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>'

По материалам Atlassian JIRA Structure: Show work logged per user and issue for a structure