Scriptrunner listener: How to distinguish between modification to issue and creation of subtask? Edited

Hello,

 

I have a listener which performs some behavior when a change is made to the due date of an issue. The problem is, this behavior is also triggered when a subtask is created for the issue, because technically this is a modification of the parent issue, and apparently to the due date, as well.

How can I differentiate between a true modification to the parent issue's due date and the creation of a subtask, which is interpreted as a modification to the parent issue's due date?

Relevant section of script below:

 

//Takes an IssueEvent and, should that IssueEvent be a change of Due Date, adds the previous Due Date as a label to the affected Issue
    public void dueDateChanged (IssueEvent issueEvent) {

        Issue issue = issueEvent.getIssue();
        List<GenericValue> changeItems = null;
        ApplicationUser user = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser();

        
    
        try {
            GenericValue changeLog = issueEvent.getChangeLog();         //grab all changes on this issue
            HashMap<String, Object> fields = new HashMap<String, Object>();
            fields.put("group", changeLog.get("id"));       //make map out of them (unsure what "group" and "id" are for)
            GenericDelegator delegator = changeLog.getDelegator();
            changeItems = delegator.findByAnd("ChangeItem", fields);        //place them in a list
        } catch (GenericEntityException e) {
            log.error(e.getMessage());
        }

        //iterate through list of changes and find if the change was to the duedate
        for(GenericValue changeVal : changeItems) {
            
            String field = changeVal.getString("field");
         
            if(field.equals("Due Date & Time")) {
                
                //check if the issue is brand new, to prevent generation of DUE DATE CHANGE comment for change from null to something
                Object checkPrevDueDate = changeVal.getString("oldstring"); //see if such a string even exists in changeVal!
                String issueTypeId = issue.getIssueTypeId();        //see if we're dealing with a subtask here

                if(!checkPrevDueDate.equals(null) && !issueTypeId.equals("5")) {     //if such a string does exist, i.e. the due date has actually been set at least once

                    String newDueDate = changeVal.getString("newstring");
                    String prevDueDate = changeVal.getString("oldstring"); 

                    addComment(issue, user, "{color:#d04437}*DUE DATE CHANGE*{color}\n\nOld: " + prevDueDate + "\nNew: " + newDueDate + "\nReason: ");
                    prevDueDate = prevDueDate.split(" ")[0];
                    addLabelToIssue(issue, user, prevDueDate);
                }
            }
        }
    }
}

    DueDateChanger dueDateChanger = new DueDateChanger();
    dueDateChanger.dueDateChanged(event);   //event is automatically given to us by CustomListener environment(?) in JIRA

 

As you can see, I tried excluding the case where the issue ID is 5 (subtask in our JIRA), but this had no effect. It also isn't exactly what I want, because someone might change the due date of a subtask, which should trigger the script because it's a legitimate change.

1 answer

Hi Samantha, 

So in a custom listener that will listen for issue Updated events you can check which field (in your case the duedate field) trigered the event using the following script 

def change = event?.getChangeLog()?.getRelated("ChildChangeItem")?.find {it.field == "duedate"}

if (change) {
    log.debug "The due date changed"
    def oldValue = change.oldstring
    def newValue = change.newstring
}

regards, Thanos

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Oct 16, 2018 in Jira

Looking for anyone who made the switch to Data Center

The Jira Marketing team is putting together an ebook on migrating to Data Center. We're looking for pro tips on how you staffed your project team and organized your Proof of Concept. Share yo...

1,385 views 17 10
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