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.
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....)
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.