How should a custom listener that detect issue component changes?

Steinar Bang
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 7, 2017

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

2 votes
Answer accepted
Steinar Bang
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 7, 2017

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