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
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Sunday in Agility

You asked for it, so we delivered: images on issues have arrived

A picture tells a thousand words. And agility boards have just released their latest feature: cover images on issues – so now your board can tell a story at first glance. Upload attachmen...

171 views 1 10
Read article

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