JIRA Plugins SDK: How to find out changed data?

2019-08-14 04:28发布

问题:

I am using the JIRA plugins sdk to work on changed Issues.

I´ve implemented an IssueListener and I have access to the Issue itself and the IssueEvent.

How do I find out which property (summary, description, estimation ...) of my Issue has been changed ?

回答1:

The changelog is likely to contain what's been changed and there is a method on the IssueEvent object to get this (getChangeLog) and it returns a GenericValue object.

This post on the Atlassian Answers site gives some code related to an Atlassian tutorial on how to write JIRA Event Listeners.

The relevant code snippet is shown below:

if (eventTypeId.equals(EventType.ISSUE_UPDATED_ID)) {
    List<GenericValue> changeItems = null;

    try {
        GenericValue changeLog = issueEvent.getChangeLog();
        changeItems = changeLog.internalDelegator.findByAnd("ChangeItem", EasyMap.build("group",changeLog.get("id")));
    } catch (GenericEntityException e){
        System.out.println(e.getMessage());
    }

    log.info("number of changes: {}",changeItems.size());
    for (Iterator<GenericValue> iterator = changeItems.iterator(); iterator.hasNext();){
        GenericValue changetemp = (GenericValue) iterator.next();
            String field = changetemp.getString("field");
            String oldstring = changetemp.getString("oldstring");
            String newstring = changetemp.getString("newstring");
            StringBuilder fullstring = new StringBuilder();
            fullstring.append("Issue ");
            fullstring.append(issue.getKey());
            fullstring.append(" field ");
            fullstring.append(field);
            fullstring.append(" has been updated from ");
            fullstring.append(oldstring);
            fullstring.append(" to ");
            fullstring.append(newstring);
            log.info("changes {}", fullstring.toString());

            /* Do something here if a particular field you are
               looking for has being changed.
            */
            if(field == "Component") changeAssignee(changetemp, issue, user);
    }
}