Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jira: How to obtain the previous value for a custom field in a custom IssueEventListener

So how does one obtain the previous value of a custom field in a Jira IssueEventListener? I am writing a custom handler for the issueUpdated(IssueEvent) event and I would like to alter the handler's behavior if a certain custom field has changed. To detect the type of change I would like to compare the previous and current values.

(I'm am not asking about how to obtain its current value - I know how to get that from the related Issue)

I am developing against Jira 4.0.2 on Windows.

Is the best way to scan the change history for the last known value?

List changes = changeHistoryManager.getChangeHistoriesForUser(issue, user);
like image 573
munger Avatar asked Feb 27 '23 09:02

munger


1 Answers

I'm assuming the original poster is writing a JIRA plugin with Java. I cannot be certain of how to accomplish this task in JIRA v4.0.2, but here is how I managed to do so with JIRA v5.0.2 (the solutions may very well be the same):

public void workflowEvent( IssueEvent event )
{
  Long eventTypeId = event.getEventTypeId();
  if( eventTypeId.equals( EventType.ISSUE_UPDATED_ID ) )
  {
    List<GenericValue> changeItemList = null;
    try
    {
      changeItemList = event.getChangeLog().getRelated( "ChildChangeItem" );
    }
    catch( GenericEntityException e )
    {
      // Error or do what you need to do here.
      e.printStackTrace();
    }

    if( changeItemList == null )
    {
      // Same deal here.
      return;
    }

    Iterator<GenericValue> changeItemListIterator = changeItemList.iterator();
    while( changeItemListIterator.hasNext() )
    {
      GenericValue changeItem = ( GenericValue )changeItemListIterator.next();
      String fieldName = changeItem.get( "field" ).toString();
      if( fieldName.equals( customFieldName ) ) // Name of custom field.
      {
        Object oldValue = changeItem.get( "oldvalue" );
        Object newValue = changeItem.get( "newvalue" );
      }
    }
  }
}

Some possible key values for changeItem are:

  • newvalue
  • oldstring
  • field
  • id
  • fieldtype
  • newstring
  • oldvalue
  • group

For many of the custom field types Object oldValue is probably just a String. But I don't think that's true for every case.

like image 183
puzzud Avatar answered Apr 25 '23 17:04

puzzud