Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Transition Via Scriptrunner Doesn't Start SLAs

Anthony Delgado December 18, 2018

I have a listener that catches a comment then checks if the comment was made by the reporter.  If so, and if the issue is currently in "Pending Customer" status, I'll transition it either to "Tier 2" or "In Progress", depending on the value of another custom field.  My Time to Resolution SLA is paused when in "Pending Customer", and should start back up after transitioning to "Tier 2" or "In Progress".  I recently added the code to fire an event, thinking that might knock the SLA loose.  

Any ideas on how to get the SLA to un pause?

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.bc.issue.IssueService;
import com.atlassian.jira.issue.IssueInputParametersImpl;
import com.onresolve.jira.groovy.user.FieldBehaviours;
import com.onresolve.jira.groovy.user.FormField;
import com.atlassian.mail.Email;
import com.atlassian.mail.server.MailServerManager;
import com.atlassian.mail.server.SMTPMailServer;
import com.atlassian.jira.event.issue.IssueEventBundle;
import com.atlassian.jira.event.issue.IssueEventManager;
import com.atlassian.jira.event.issue.IssueEventBundleFactory;
import com.atlassian.jira.event.issue.IssueEvent;

def status = issue.getStatusObject().getName()
def commentManager = ComponentAccessor.getCommentManager()
def comments = commentManager.getComments(issue)

//log.warn("defined userManager")
//log.warn(issue.getId())
//log.warn(issue.issueTypeObject.name)

if (["Pending Customer","Closed"].contains(status) && ["Incident","Service Request"].contains(issue.issueTypeObject.name)) {//(issue.issueTypeObject.name == "Incident" || issue.issueTypeObject.name == "Service Request")) {
//log.warn("Inside Status IF")

if (comments) {
//log.warn("yes there were comments found")
def author=comments.last().authorApplicationUser.getName()
//log.warn("author is: " + author)
def reporter=issue.getReporter().getName()
//log.warn("reporter is: " + reporter)

if (author==reporter) {
//log.warn("author=reporter")

// if this is closed, then we send an email to the customer and return true
if (["Closed"].contains(status)) {
log.warn("status is Closed and comment was made by reporter, send bounceback email")
//FormField supervisor = getFieldById(fieldChanged)
ComponentManager componentManager = ComponentManager.getInstance()
SMTPMailServer mailServer = ComponentAccessor.getMailServerManager().getDefaultSMTPMailServer();

if (mailServer) {
//log.warn("mailServer constructed successfully")
def reporterFullName = issue.getReporter().getDisplayName()
def emailAddress = comments.last().authorApplicationUser.getEmailAddress().toString()
//log.warn(emailAddress)
Email email = new Email(emailAddress)// Set the TO address, optionally CC and BCC
//log.warn(email)
email.setSubject("${issue.getKey()} ${issue.summary}") // todo: check the subject value
def project = issue.projectObject.name
String contentEnglish = "${reporterFullName},\r\n\r\nThank you for contacting";
String contentEnglishFrench = "${reporterFullName},\r\n\r\nThank you for contacting";

if (["Information Technology - Canada","People Business Services (PBS) - Canada","Business Services Center Canada"].contains(project)) {
email.setBody(contentEnglishFrench)
}
else {
email.setBody(contentEnglish)
}
//log.warn(email)
mailServer.send(email)

return true
}
else {
log.error("No SMTP mail server defined")
}
}
// Let's transition depending on value of reporting tier
def customFieldManager = ComponentAccessor.customFieldManager
def customField = customFieldManager.getCustomFieldObjectByName("Reporting Tier")
//log.warn(issue.getCustomFieldValue(customField))

if (issue.getCustomFieldValue(customField).toString().trim()=="Tier 2") {
// Transition to tier 2
log.warn("comment made by reporter, status is pending customer, let's transition to tier 2")

def currentUser = ComponentAccessor.getUserManager().getUserByName("automation-system-user") //ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def issueManager = ComponentAccessor.issueManager
def issue = issueManager.getIssueObject(event.issue.key)
IssueService issueService = ComponentAccessor.getIssueService()
def actionId = 161 // this is the "resume tier 2" transition in ITCAN
def transitionValidationResult
def transitionResult
//log.warn("The issue type is: " + issue.getIssueType().name)
transitionValidationResult = issueService.validateTransition(currentUser, issue.id, actionId, new IssueInputParametersImpl())

if (transitionValidationResult.isValid()) {
transitionResult = issueService.transition(currentUser, transitionValidationResult)

if (transitionResult.isValid()) {
log.warn("Transitioned issue $issue through action $actionId")

// fire issue updated event
Long EVENT_ID = new Long("13") // 2=issue updated event

IssueEventManager issueEventM = ComponentAccessor.getIssueEventManager()
IssueEventBundleFactory issueEventFactory = (IssueEventBundleFactory) ComponentAccessor.getComponent(IssueEventBundleFactory.class)

IssueEventBundle eventBundle = issueEventFactory.wrapInBundle(new IssueEvent (issue, null, currentUser, EVENT_ID, true))
issueEventM.dispatchEvent(eventBundle)
log.warn("fired issue updated event")
}
else {
log.warn("Transition result is not valid") }
}
else {
log.warn("The transitionValidation is not valid")
}
}
else {
// Transition to in progress
log.warn("comment made by reporter, status is pending customer, let's transition to in progress")

def currentUser = ComponentAccessor.getUserManager().getUserByName("automation-system-user") //ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def issueManager = ComponentAccessor.issueManager
def issue = issueManager.getIssueObject(event.issue.key)
IssueService issueService = ComponentAccessor.getIssueService()
def actionId = 151 // this is the "resume in progress" transition in ITCAN
def transitionValidationResult
def transitionResult
//log.warn("The issue type is: " + issue.getIssueType().name)
//log.warn(currentUser)
transitionValidationResult = issueService.validateTransition(currentUser, issue.id, actionId, new IssueInputParametersImpl())

if (transitionValidationResult.isValid()) {
transitionResult = issueService.transition(currentUser, transitionValidationResult)
if (transitionResult.isValid()) {
log.warn("Transitioned issue $issue through action $actionId")

// fire issue updated event
Long EVENT_ID = new Long("13") // 2=issue updated event

IssueEventManager issueEventM = ComponentAccessor.getIssueEventManager()
IssueEventBundleFactory issueEventFactory = (IssueEventBundleFactory) ComponentAccessor.getComponent(IssueEventBundleFactory.class)

IssueEventBundle eventBundle = issueEventFactory.wrapInBundle(new IssueEvent (issue, null, currentUser, EVENT_ID, true))
issueEventM.dispatchEvent(eventBundle)
log.warn("fired issue updated event")
}
else {
log.warn("Transition result is not valid")
}
}
else {
log.warn("The transitionValidation is not valid")
}
}
}
else {
//log.warn("author!=reporter")
//do nothing
}
}
else {
//log.warn("No Comment Made")
//do nothing
}

 

1 answer

Suggest an answer

Log in or Sign up to answer
0 votes
Simonas Jutkevičius May 16, 2019

Hi, did you find the solution? We have the same logic (customer -> tier 1/2) and the same issue.

TAGS
AUG Leaders

Atlassian Community Events