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 Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

2,859 views 12 18
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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot