环境
CentOS Linux release 7.9.2009 (Core)
Jira 8.16.1
描述
在父任务中有许多子任务,创建自定义字段“单行文本”-“子任务的完成百分比”在父任务界面显示。
当子任务完成时,自动更新该字段。
实现
使用 ScriptRunner 中的 Listeners 来实现该功能。
Events:Generic Event
脚本如下:
Github:https://github.com/Shankschn/Jira-Script/blob/main/scriptRunner/auto_calc_update_subtask_pct_field_on_parent
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.component.ComponentAccessor
def issue = event.issue as Issue
log.warn(issue.key)
log.warn(issue.issueType.name)
if (issue.issueType.name == "子任务") { // 子任务 issue.issueType.name
def issue2 = issue.getParentObject()
log.warn(issue2.key)
log.warn(issue2.issueType.name)
if (issue2.issueType.name == "任务") { // 父任务 issue2.issueType.name
issue = issue2
log.warn(issue2.key)
def subTaskSum = 0
issue.getSubTaskObjects()?.each { subtask ->
subTaskSum += 1
}
// customfield_12210 自定义字段
def tgtField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_12210")
def changeHolder = new DefaultIssueChangeHolder()
if (subTaskSum != 0) {
def subTaskOKSum = subTaskSum
issue.getSubTaskObjects()?.each { subtask ->
subtask.getResolution() != null ? subTaskOKSum -= 1 : subTaskOKSum
}
String jg = (int) (((subTaskSum-subTaskOKSum) * 100) / subTaskSum)
jg = jg + "%"
tgtField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(tgtField), jg), changeHolder)
}
}
}
参考
stackoverflow.com/questions/39483956/update-custom-field-value-using-a-scriptrunner-for-jira-custom-listener
community.atlassian.com/t5/Jira-questions/Sub-task-transition-when-Parent-task-changes-state/qaq-p/1135359