Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Custom Script Listener Email Spam Upon Issue Update

david_lebrun
August 8, 2018

I'm running into a sporadic, not-testably-reproducible issue regarding a Custom Script Listener that updates an issue's due-date upon Priority change.  

For some issues, upon changing the priority, there seems to be an surge of Issue Update events that mirror the originally-triggering event and causes a huge amount of email spam (looking at the logs, the Custom Listener is being triggered every 5ms when this happens).  From the debug text in my script, when this occurs, the event where the priority was first change seems to continually get registered as a new event (they all show the same ChangeItems list referring to the initial change).

This doesn't always happen—in most cases, the custom listener updates the issue's due date without causing spam or triggering more events that trip off the same custom listener.  But when it does happen, it cause a huge CPU spike and floods our mail queue.

Since the custom listener is configured to trigger off of "Issue Update" events via the ScriptListener config, I'm assuming the following code (in particular, the last one where issueManager.updateissue is used) that updates the issueEvent with the updated dueDate somehow triggers an edge case that causes this bug to surface:


void updateIssueWithNewDueDate(Timestamp dueDate, IssueEventevent, String fieldName, StringBuffer debugText){
def issue = event.getIssue();
MutableIssue mutableIssue = (MutableIssue) issue;
if(!dueDate){
return
}else{
IssueManager issueManager = ComponentAccessor.getComponent(IssueManager.class)
mutableIssue.setDueDate(dueDate)
issueManager.updateIssue(event.getUser(), mutableIssue, EventDispatchOption.ISSUE_UPDATED, false)
}
}

 
Question: Should I be using a different method other than issueManager.updateissue(...) for persisting the due-date change to the issue?  Or is there some other bug underfoot here?(see: https://docs.atlassian.com/software/jira/docs/api/7.1.0-m01/com/atlassian/jira/issue/IssueManager.html#updateIssue-com.atlassian.jira.user.ApplicationUser-com.atlassian.jira.issue.MutableIssue-com.atlassian.jira.event.type.EventDispatchOption-boolean-) 

Here is the entire custom scriptRunner for more details.  A run down of the code:
The CustomListener is triggered against "Issue Updated" events on a project called SEC.  The script should update the due-date of the issiue when the priority of the issue changes (based off an irrelevant SLA calculation), but only for issues of type "Vulnerability":

import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.model.ChangeItem
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.event.type.EventDispatchOption

import java.sql.Timestamp
import java.util.HashMap
import java.util.Map

def String FIELD_PRIORITY = "priority"
def String FIELD_DUE_DATE = "Due Date"
def String ISSUE_TYPE = "Vulnerability"


def String PRIORITY_CRITICAL = "Critical";
def String PRIORITY_MAJOR = "Major";
def String PRIORITY_MEDIUM = "Medium";
def String PRIORITY_MINOR = "Minor";
def String PRIORITY_TRIVIAL = "Trivial";

def int SLA_DAYS_CRITICAL = 1;
def int SLA_DAYS_MAJOR = 7;
def int SLA_DAYS_MEDIUM = 30;
def int SLA_DAYS_MINOR = 120;
def int SLA_DAYS_TRIVIAL = 120;

boolean wasFieldChanged(IssueEvent event, String fieldName, String issueType, StringBuffer debugText) {
Issue issue = event.issue
String issueTypeName = issue.getIssueType().getName()
boolean isVulnerability = issueType.equals(issueTypeName)
addFieldToDebugText(debugText, "isVulnerability", String.valueOf(isVulnerability))
def changeItems = event?.getChangeLog()?.getRelated("ChildChangeItem")
addFieldToDebugText(debugText, "changeItems", changeItems.toString())
return isVulnerability && event?.getChangeLog()?.getRelated("ChildChangeItem")?.find{it.field == fieldName}
}

String getPriorityChangeValue(IssueEvent event){
event.issue.getPriority().name
}

Timestamp getCreateDateFromIssue(IssueEvent event){
Timestamp createDate = event.issue.getCreated();
return createDate;
}

Timestamp getDueDateFromPriority(String priority, Timestamp createDate, Map<String, Integer> priority2SLAMap){
Integer slaDays = priority2SLAMap.get(priority);
if(!slaDays){
return null
}
Calendar cal = Calendar.getInstance();
cal.setTime(createDate);
cal.add(Calendar.DAY_OF_WEEK, slaDays);
Timestamp dueDate = new Timestamp(cal.getTime().getTime());
return dueDate;
}

void updateIssueWithNewDueDate(Timestamp dueDate, IssueEventevent, String fieldName, StringBuffer debugText){
def issue = event.getIssue();
MutableIssue mutableIssue = (MutableIssue) issue;
if(!dueDate){
return
}else{
IssueManager issueManager = ComponentAccessor.getComponent(IssueManager.class)
mutableIssue.setDueDate(dueDate)
issueManager.updateIssue(event.getUser(), mutableIssue, EventDispatchOption.ISSUE_UPDATED, false)
}
}

void addFieldToDebugText(StringBuffer debugText, String fieldName, String fieldValue){
debugText <<= " " + fieldName + ": " + (fieldValue ?: "null") + " "
}

def Map<String, Integer> priority2SLAMap = new HashMap<String, Integer>();
priority2SLAMap.put(PRIORITY_CRITICAL, SLA_DAYS_CRITICAL);
priority2SLAMap.put(PRIORITY_MAJOR, SLA_DAYS_MAJOR);
priority2SLAMap.put(PRIORITY_MEDIUM, SLA_DAYS_MEDIUM);
priority2SLAMap.put(PRIORITY_MINOR, SLA_DAYS_MINOR);
priority2SLAMap.put(PRIORITY_TRIVIAL, SLA_DAYS_TRIVIAL);

def StringBuffer debugText = 'DEBUG TEXT: ' << " "
def wasPriorityChanged = wasFieldChanged(event, FIELD_PRIORITY, ISSUE_TYPE, debugText)
addFieldToDebugText(debugText, "Was priorty Changed", String.valueOf(wasPriorityChanged))
if (wasPriorityChanged) {
String priority = getPriorityChangeValue(event);
addFieldToDebugText(debugText, "priority", priority)
Timestamp createDate = getCreateDateFromIssue(event)
addFieldToDebugText(debugText, "Create Date", createDate.toString())
Timestamp dueDate = getDueDateFromPriority(priority, createDate, priority2SLAMap)
addFieldToDebugText(debugText, FIELD_DUE_DATE, dueDate.toString())
updateIssueWithNewDueDate(dueDate, event, FIELD_DUE_DATE, debugText);
}
log.warn(debugText) 

 

1 answer

1 accepted

Suggest an answer

Log in or Sign up to answer
1 vote
Answer accepted
Danyal Iqbal
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 Champions.
March 1, 2021

No it is not a glitch. In simple words MS Edge is infact just a skin/Theme for Chrome. Normally the user-agent for edge says Edg or Edge but older websites cannot detect the new edge browser so they detect chrome due to compatibility.

which product version are you using? an upgrade might help here.

acc1211
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 Champions.
March 1, 2021

I am currently using V 6.18 for the script runner and the edge browser is 88.0.705

 

if you see in the attach it shows that edge value is detected but browser comes upto be as chrome. Capture2.PNG

i thought that edge and chrome are from two different companies microsoft and google never  knew that it was the theme or the skin for Chrome . Let me check on that  

Nic Brough -Adaptavist-
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 Champions.
March 1, 2021

Edge and Chrome are indeed from MS and Google respectively.  MS dumped their browser technology and move to Chrome when they started building Edge.  Officially it was it makes no commercial sense to build your own tech, but from what I've heard and read, it was more to do with the MS technology being broadly rubbish and needing too much work to fix it.

acc1211
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 Champions.
March 2, 2021

Thank you @Danyal Iqbal @Nic Brough -Adaptavist-  for your response . i have made my research about it and i am satisfied

TAGS
AUG Leaders

Atlassian Community Events