How should a custom listener that detect issue component changes? Edited

JIRA 7.4.2

Adaptavist Scriptrunner for JIRA 5.1.0

I am trying to create a custom listener that listens for component settings in issues and switches a custom field that contains the account the work on the component should be charged to.

What I'm wondering is how to detect component changes, since the component field isn't a field, but a relation that has special method in the API (Issue.getComponents())?

Right now my script looks like this, and according to the log things are called, but I'm not sure how correct this is...?

log.error "component field listener (1)"
def event = event as IssueEvent;
def changeHistoryManager = ComponentAccessor.getChangeHistoryManager();
def issue = event.getIssue() as MutableIssue;

def componentFieldChanges = changeHistoryManager.getChangeItemsForField(issue, "Component");
def componentFieldChange = componentFieldChanges.last();
log.error "component field listener (2)"
if (componentFieldChange != null) {
log.error "component field listener (3)"
}

 Thanks!

 

- Steinar

 

PS looking at this I see that this probably is wrong because I will always get componentFieldChanges on an issue that has had at least one.  

1 answer

1 accepted

This seems to work:

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.event.issue.IssueEvent;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.MutableIssue;
import org.ofbiz.core.entity.GenericValue;

log.error "component field listener (1)"
def event = event as IssueEvent;
def changeHistoryManager = ComponentAccessor.getChangeHistoryManager();
def issue = event.getIssue() as MutableIssue;

def changelog = event.getChangeLog();
log.error "component field listener (1.1) changelog: " + changelog
def changedFields = findFieldsMentionedInChangeLog(changelog)
log.error "component field listener (1.2) changedFields: " + changedFields


if (changedFields.contains("Component")) {
log.error "component field listener (3)"
}

def Set<String> findFieldsMentionedInChangeLog(GenericValue changelog) {
Set<String> fields = [];
def childEventChanges = changelog.getRelated("ChildChangeItem");
for (childEventChange in childEventChanges) {
fields.add(childEventChange.getString("field"))
}
fields;
}

It prints out "component field listener (3)" when the Component field is modifed and stops at the "(1.2)" step when I edit a different field such as e.g. issuetype.

But I have no idea how robust my GenericValue iteration is? What happens if there are no "ChildChangeItem" relations.

Also, I'm using a deprecated method to get the list (because I don'tknow what argumens the new method should get....)

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Julia Dillon
Posted Apr 17, 2018 in Jira

Tell us how your team runs on Jira!

Hey Atlassian Community! Today we are launching a bunch of customer stories about the amazing work teams, like Dropbox and Twilio, are doing with Jira. You can check out the stories here. The thi...

796 views 2 19
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you