i have jira plugin needs listen issueevent
s , respond various status transitions (as in "to -> in progress", "in progress -> done", etc.). in cases, doing requires knowing both current , previous issue status. there's no obvious api getting latter bit of information issueevent
.
my setup simple:
@named("myeventlistener") public class jirachangelistener implements initializingbean, disposablebean { @componentimport private final eventpublisher eventpublisher; @componentimport private final customfieldmanager fieldmanager; @componentimport private final issuemanager issuemanager; @inject public jirachangelistener(eventpublisher publisher, customfieldmanager fieldmanager, issuemanager issuemanager) { this.eventpublisher = publisher; this.fieldmanager = fieldmanager; this.issuemanager = issuemanager; } @eventlistener public void onissueevent(issueevent event) { issue issue = event.getissue(); long eventtype = event.geteventtypeid(); string issuestatus = issue.getstatusid(); //the *current* issue status //[read various internal plugin settings] //now handle event if (eventtype == eventtype.issue_created_id) { //triggered upon creation; don't need old status here if (apiavailable && issuestatus.equals(triggerstatus)) { //we can stuff (apiavailable) , should stuff (issuestatus == triggerstatus) dostuff(issue); } } else if (eventtype == eventtype.issue_genericevent_id) { //triggered upon status transition (and possibly other actions); //need see if status has been modified string oldstatus = [???]; //fixme: need previous status here boolean statuschanged = ! issuestatus.equals(oldstatus); if (apiavailable && statuschanged && issuestatus.equals(triggerstatus)) { //we can stuff (apiavailable && statuschanged) , should stuff (issuestatus == triggerstatus) dostuff(issue); } } } }
so catch line goes:
string oldstatus = [???];
what's correct way programmatically issue's previous status, or otherwise identify event has been received because issue's status has changed?
note i'm working jira 7.x. support previous jira versions isn't concern.
found helpful information here, , put following together:
private string getpreviousstatusid(issue issue, issueevent event) { try { genericvalue changelog = event.getchangelog(); hashmap<string, object> fields = new hashmap<string,object>(); fields.put("group", changelog.get("id")); list<genericvalue> changes = changelog.internaldelegator.findbyand("changeitem", fields); for(genericvalue changeval : changes){ string field = changeval.getstring("field"); if ("status".equals(field)) { return changeval.getstring("oldvalue"); } } } catch(exception e) { e.printstacktrace(); } //if no changes made 'status' field (or errored out), return issue's current status return issue.getstatusid(); }
works charm.
Comments
Post a Comment