如何在IssueUpdated事件上使用ScriptRunner获取已修改字段的列表?

时间:2016-04-16 18:36:58

标签: groovy jira

在我的Jira实例中,我使用ScriptRunner插件为IssueUpdated事件创建了一个脚本侦听器,并且我试图获取已更改字段的列表。由于某种原因,getModifiedFields()方法是空的,有人可以帮我解决这个问题吗?

import com.atlassian.jira.issue.managers.DefaultIssueManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.ModifiedValue

log.setLevel(org.apache.log4j.Level.DEBUG)

Issue mainIssue = event.issue

MutableIssue mutableIssue = (MutableIssue)mainIssue
def modFields = mutableIssue.getModifiedFields()
log.debug("Modified fields count: "+modFields.count) // null
log.debug("Modified fields: "+modFields.toString()) //  [:]
log.debug("Original Ticket: "+mainIssue.key) // EPS-39

2 个答案:

答案 0 :(得分:0)

谷歌颠倒过后,我找到了问题的答案。更多背景信息:我的最终目标是使用主故障单中更改的内容更新目标Jira故障单(在另一个项目中)。我有一个包含原始票证密钥的自定义字段,因此我可以跟踪它(例如:票证“PRJ-1” - >“原始票证”:“TRG-1”)。要做到这一点,我要去ScriptRunner - >脚本听众 - >自定义侦听器并添加“Issue Updated”侦听器。

我仍然停留在我有自定义标签字段的部分,您可以通过代码末尾的注释看到,但至少我可以设法获取主要票证中已更改字段的列表

import com.atlassian.jira.issue.managers.DefaultIssueManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.ModifiedValue
import org.ofbiz.core.entity.GenericValue
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.bc.issue.DefaultIssueService

log.setLevel(org.apache.log4j.Level.DEBUG)

def customFieldManager = ComponentAccessor.getCustomFieldManager()
Issue mainIssue = event.issue
// Get Custom Field Object "Original Ticket"
def originalTicket = customFieldManager.getCustomFieldObjectByName("Original Ticket")
// Get Value of the Custom Field Object "Original Ticket"
String targetTicketKey = mainIssue.getCustomFieldValue(originalTicket).toString()

// Get Original Ticket Object based on the custom field value
def targetIssue = ComponentAccessor.getIssueManager().getIssueObject(targetTicketKey)

// Get list of modified values in the original ticket to update target ticket
List<GenericValue> changeItemsList = event.getChangeLog().getRelated("ChildChangeItem")
Iterator<GenericValue> changeItemListIterator = changeItemsList.iterator()
Object oldValue
Object newValue
def userManager = ComponentAccessor.getUserManager()
def auser = userManager.getUserByKey("Sync User")
def issueManager = ComponentAccessor.getIssueManager()
CustomField custom
// Loop for all the changed fields
while (changeItemListIterator.hasNext()) {
    GenericValue changeItem = (GenericValue)changeItemListIterator.next()
    String currentFieldName = changeItem.get("field").toString()
    log.debug("Current field: "+currentFieldName)
    oldValue = changeItem.get("oldstring")
    newValue = changeItem.get("newstring")
    if (oldValue != null && newValue != null){
        log.debug("Field changed from: "+oldValue+" to "+newValue)
        switch (currentFieldName){
            case "summary": 
              log.debug("Found switch: Summary")
              targetIssue.setSummary(newValue.toString())
            break
            case "description": 
              log.debug("Found switch: Description")
              targetIssue.setDescription(newValue.toString())
            break
            case "Affected Version(s)": 
              log.debug("Found switch: Affected Version(s)")
              // This is a label field. I'm stuck here and I don't know how to manipulate the value.
              // Labels are a set type, studing more about it.
              custom = customFieldManager.getCustomFieldObjectByName("Affected Version(s)")
              targetIssue.setCustomFieldValue(custom,newValue)
            break
            default: log.debug("Not found: "+currentFieldName)
        }
    }
    // Update my target Issue (in another project) that I'm trying to synchronize with the main issue.
    issueManager.updateIssue(auser, targetIssue,  com.atlassian.jira.event.type.EventDispatchOption.DO_NOT_DISPATCH, false)    
}

答案 1 :(得分:0)

mutableIssue.modifiedFields仅在脚本验证程序中使用。

在脚本化的Listener中迭代修改的文件:

List<HashMap<String, Object>> fieldsModified = event.getChangeLog()?.getRelated('ChildChangeItem') as List<HashMap<String, Object>>

for (HashMap<String, Object> field : fieldsModified) 
    log.debug("Field: ${field["field"]}, old value: ${field["oldstring"]}, new value: ${field["newstring"]}.")

受影响的版本是“摘要和说明”等系统字段。使用issue.affectedVersions进行更新。