首页 > 解决方案 > 找不到匹配方法 - JIRA/Groovy

问题描述

我正在尝试计算问题在状态中花费的时间。但是遇到一些错误。下面的脚本进入脚本字段。下面是我的脚本:

import com.atlassian.jira.component.ComponentAccessor

def changeHistoryManager = ComponentAccessor.changeHistoryManager
def currentStatusName = issue?.status?.name

def rt = [0L]
changeHistoryManager.getChangeItemsForField (issue, "status").reverse().each {item ->

    def timeDiff = System.currentTimeMillis() - item.created.getTime()
    if (item.fromString == currentStatusName) {
        rt = -timeDiff
    }
    if (item.toString == currentStatusName){
        rt = timeDiff
    }
}
return (Math.round(rt.sum() / 3600000)) as Double

错误在脚本的最后一行(return 语句)。我不确定我做错了什么。

我得到的错误是:

静态类型检查 - 找不到匹配的 java.lang.Object#sum() 和找不到匹配的方法 java.lang.Match#round(java.lang.Object)

标签: groovyjira

解决方案


您在两个块中分配rt一个 Long 。if(只是一个 long,而不是 long 数组。)因此没有.sum()可用的方法。

你可以使用

rt << -timeDiff
// or
rt << timeDiff

将您的 timeDiffs 添加到数组中,而不是重新定义它。

您也可以将 rt 初始化为 0,然后使用rt += timeDifforrt -= timeDiff如果您愿意。看起来您根本不需要它作为数组存在。

可能对您有用的示例:

import com.atlassian.jira.component.ComponentAccessor

def changeHistoryManager = ComponentAccessor.changeHistoryManager
def currentStatusName = issue?.status?.name

def rt = 0L
changeHistoryManager.getChangeItemsForField (issue, "status").reverse().each {item ->

    def timeDiff = System.currentTimeMillis() - item.created.getTime()
    if (item.fromString == currentStatusName) {
        rt -= timeDiff
    }
    if (item.toString == currentStatusName){
        rt += timeDiff
    }
}
return rt / 3600000
// this could still be Math.round(rt/3600000) as Double if you need that; not sure what you're trying to do with the actual result

推荐阅读