How to correct the scriptrunner 3.0.6 error

Aret Boduryan November 19, 2014

pdev/atlassian/application-data/jira-scripts/SendPreDeliveryMail.groovy

groovy.lang.GroovyRuntimeException: Failed to parse template script (your template may contain an error or be trying to use expressions not currently supported): startup failed

GStringTemplateScript14.groovy: 40: unable to resolve class com.atlassian.crowd.model.user.User

[202484]  @ line 40, column 31. [202485]               

if (it instanceof com.atlassian.crowd.model.user.User)

 

 

####### The full content of the scriptrunner file

 

 

 

/** /export/home/tkwrl/jira/SendPreDeliveryMail.groovy tkwrl, 30.05.2013

Assumed Preconditions: import com.atlassian.jira.component.ComponentAccessor

// must be member of pass-releasemanager group def groupManager = ComponentAccessor.getGroupManager() if (!groupManager.isUserInGroup(currentUser?.name, 'pass-releasemanager')) {   return false; }

def fieldChangeType = customFieldManager.getCustomFieldObjectByName('Change Type') // ChangeType must not be none if (!issue.getCustomFieldValue(fieldChangeType)) {   return false }

issue.status.name in ['Open','Resolved','Test OK']

or

issue.status.name in ['In Production']   // different transition and label of button; Announce PD Installation  $Id: SendPreDeliveryMail.groovy 500254 2013-11-11 08:20:26Z tkb16 $  $URL: http://passent3:8080/svn/libraries/trunk/scripts/scripts-jira/SendPreDeliveryMail.groovy $ */

import com.atlassian.crowd.embedded.api.User //import com.atlassian.jira.ComponentManager import com.atlassian.jira.component.ComponentAccessor //import com.atlassian.jira.ManagerFactory import com.atlassian.jira.config.properties.APKeys import com.atlassian.jira.config.properties.ApplicationProperties import com.atlassian.jira.issue.CustomFieldManager import com.atlassian.jira.issue.Issue import com.atlassian.jira.issue.IssueManager import com.atlassian.jira.issue.MutableIssue import com.atlassian.jira.issue.fields.CustomField import com.atlassian.jira.issue.watchers.WatcherManager import com.atlassian.jira.security.groups.GroupManager import com.atlassian.jira.security.roles.ProjectRole import com.atlassian.jira.security.roles.ProjectRoleManager import com.atlassian.jira.user.util.UserUtil import com.atlassian.jira.user.UserUtils import com.atlassian.mail.Email import com.atlassian.mail.MailException import com.atlassian.mail.MailFactory import com.atlassian.mail.queue.SingleMailQueueItem import com.atlassian.mail.server.MailServerManager import com.atlassian.mail.server.SMTPMailServer //import com.onresolve.jira.groovy.canned.utils.ConditionUtils import com.onresolve.scriptrunner.canned.jira.utils.ConditionUtils import com.opensymphony.workflow.WorkflowContext import groovy.text.GStringTemplateEngine import org.apache.log4j.Category import org.ofbiz.core.entity.GenericValue

import java.util.regex.Matcher import javax.activation.DataHandler import javax.activation.FileDataSource import javax.mail.BodyPart import javax.mail.Multipart import javax.mail.internet.AddressException import javax.mail.internet.InternetAddress import javax.mail.internet.MimeBodyPart import javax.mail.internet.MimeMultipart

class PreDeliveryMail {      public static String FIELD_PREVIEW_ISSUE          = "FIELD_PREVIEW_ISSUE"      public static String FIELD_EMAIL_TEMPLATE         = "FIELD_EMAIL_TEMPLATE"      public static String FIELD_EMAIL_FORMAT           = "FIELD_EMAIL_FORMAT"      public static String FIELD_TO_ADDRESSES           = "FIELD_TO_ADDRESSES"      public static String FIELD_TO_USER_FIELDS         = "FIELD_TO_USER_FIELDS"      public static String FIELD_EMAIL_SUBJECT_TEMPLATE = "FIELD_EMAIL_SUBJECT_TEMPLATE"      public static String FIELD_INCLUDE_ATTACHMENTS    = "FIELD_INCLUDE_ATTACHMENTS"      public static String FIELD_FROM                   = "FIELD_FROM"           public static String CC_ADDRESSES                 = "pass-releasemanager@six-group.com"   // will be extended by currentUser           //public static String TO_2ND_LEVEL               = "pass-releasemanager@six-group.com"  // for testing, will be 6772      //public static String TO_PASSPROD                = "pass-releasemanager@six-group.com"  // for testing, will be passprod@six-group.com      public static String TO_2ND_LEVEL                 = "pass_2nd_level@six-group.com"      public static String TO_PASSPROD                  = "passprod@six-group.com"           public static String SUBJECT_ADD_TEMPLATE         = "(A) Bitte Pre-Delivery \$issue.key zu bestehendem ITSM Change hinzufuegen"      public static String SUBJECT_NEW_TEMPLATE         = "(A) Bitte neuen ITSM Change zu folgendem Pre-Delivery erfassen: \$issue.key "      public static String SUBJECT_INST_TEMPLATE        = "(I) Pre-Delivery \$issue.key wurde auf PASS/iPASS Produktion installiert"      public static String BODY_TEMPLATE = """ <html> <head>  <style type="text/css">

.tableBorder, .grid {     background-color: #fff;     width: 100%;     border-collapse: collapse; }

.tableBorder td, .grid td {     vertical-align: top;     padding: 2px;     border: 1px solid #ccc; }

.strong {     font-weight: bold; }

</style>

</head> <body>  <%

def addRow = { fieldName, fieldValue ->    out.println "<tr><td class='strong'>" + fieldName + ":</td><td>" + fieldValue + "</td></tr>"; }

def formatAndAddRow = { fieldName, fieldValue ->      if (fieldValue != null && fieldValue instanceof java.lang.Iterable) {         addRow(fieldName, fieldValue.collect() {             if (it instanceof com.atlassian.crowd.model.user.User) {                 it.firstName +" "+ it.lastName +" ("+it.name+")"             } else if (it instanceof com.atlassian.jira.project.version.Version ) {                 it.name             } else  if (it instanceof org.ofbiz.core.entity.GenericValue) {                 it.name             } else {                 it             }         }.join(", "));             }     else {        addRow(fieldName, fieldValue);     } }

def addCustomFieldRow = { fieldName ->        //def field = componentManager.getCustomFieldManager().getCustomFieldObjectByName(fieldName);     def field = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName(fieldName);     def val = "";     if (field != null) {         val = issue.getCustomFieldValue(field);            if (val == null || val instanceof org.codehaus.groovy.runtime.NullObject) {             val = ""         }     }     formatAndAddRow(fieldName, val); }

def url = com.atlassian.jira.component.ComponentAccessor.getApplicationProperties().getString("jira.baseurl"); def issueLink = { i ->    (i.resolution != null ? "<strike>" : "") +'<a href="'+url+"/browse/" + i.key + '" title="'+i.summary+'">'+  i.key +"</a>" + (i.resolution != null ? "</strike>" : ""); }

def addPatchedIssues = {    long patchesLinkType = 10400;    links = issueLinkManager.getOutwardLinks(issue.id);    for ( l in links ) {        if (l.linkTypeId == patchesLinkType) {         patchedIssue = l.getDestinationObject();         addRow("is a patch for... ", issueLink(patchedIssue) + ": " + patchedIssue.getSummary());        }    } }

if (issue.status.name == "In Production") {   // --- this is commented out for two reasons:   //     1. In ITSM8 Tasks have to be closed by implementors, Change will be resolved automatically and closed by Change Manager   //     2. There are Pre-Deliveries which span multiple dates in the scope of a single Change:   //        they must not advise closing the Change upon completion of a task which isn't the last one!   //def fieldChangeNr = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Change Nr");   //out.println  "<p><strong>@Primas: Bitte Change " + issue.getCustomFieldValue(fieldChangeNr) + " schliessen.</strong></p>" }

out.println '<table class="tableBorder" cellspacing="0" cellpadding="0" border="0">'

addCustomFieldRow("Change Nr"); addCustomFieldRow("Change Type"); addCustomFieldRow("Change Start"); addCustomFieldRow("Change End"); addCustomFieldRow("Downtime"); formatAndAddRow("Pre-Delivery", issueLink(issue)); addCustomFieldRow("PD Type"); formatAndAddRow("Summary", issue.summary); formatAndAddRow("Status", issue.status.name); formatAndAddRow("Description", issue.description); addPatchedIssues(); //obsolete for PD: formatAndAddRow("Fix Version/s", issue.fixVersions); addCustomFieldRow("Developer"); addCustomFieldRow("Tester"); addCustomFieldRow("Implementor"); addCustomFieldRow("Affected Institution/s"); formatAndAddRow("Component/s", issue.components); addCustomFieldRow("Risk"); addCustomFieldRow("Fallback possible"); addCustomFieldRow("Fallback Description");

out.println  "</table>" %> </body></html>    """

    //def componentManager = ComponentManager.getInstance()

      def issueManager = ComponentAccessor.getIssueManager()     def watcherManager = ComponentAccessor.getWatcherManager()     def customFieldManager = ComponentAccessor.getCustomFieldManager()     def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager.class)     def groupManager = ComponentAccessor.getComponent(GroupManager.class)     def userUtil = ComponentAccessor.getUserUtil()     //def mailServerManager = ComponentAccessor.getMailServerManager()  //SMTPMailServer mailServer = mailServerManager.getDefaultSMTPMailServer()     //def mailServer = mailServerManager.getDefaultSMTPMailServer()  SMTPMailServer  mailServer = MailFactory.getServerManager().getDefaultSMTPMailServer();         //dynamically assign Subject and To Address depending on Issue Status and ChangeNr     def subject           = "No Subject"     def toAddress         = "nobody@nowhere.com"

    Category log = Category.getInstance(PreDeliveryMail.class)

    Map doScript(Map params) {         println "DEBUG params = $params"         println "DEBUG params['FIELD_PREVIEW_ISSUE'] =" + params['FIELD_PREVIEW_ISSUE'] as String         MutableIssue issue = params['issue'] as MutableIssue         def issueManager = ComponentAccessor.getIssueManager()         def transientVars = params['transientVars']

        // preview mode         if (!issue) {             println "DEBUG no issue - preview mode"             issue = issueManager.getIssueObject(params[FIELD_PREVIEW_ISSUE] as String)             println "issue set to FIELD_PREVIEW_ISSUE ( $issue )"         }

        Boolean doIt = ConditionUtils.processCondition(params[ConditionUtils.FIELD_CONDITION] as String, issue, false, params)         if (! doIt) {             println "DEBUG won't do it - unmatched conditions"             return [:]         }        

        // do validation again         String emailFormat = params[FIELD_EMAIL_FORMAT]         log.debug("emailFormat: $emailFormat")

        if (mailServer && ! MailFactory.isSendingDisabled()) {             //addTemplateParams(params)             //Writable template = BODY_TEMPLATE  // mergeEmailTemplateBody(params)             chooseSubjectAndToAddress(issue)             String body = mergeEmailTemplate(params, BODY_TEMPLATE) // template.toString()             String currentUser = ((WorkflowContext) transientVars.get("context")).getCaller()             println "DEBUG currentUser = $currentUser"             String fromAddress= UserUtils.getUser(currentUser).getEmailAddress()             Email email = new Email(toAddress)             email.setSubject(mergeEmailTemplate(params, subject).toString())             email.setFrom('"' + currentUser + '" <' + fromAddress + '>' )             email.setCc(CC_ADDRESSES + "," + fromAddress )                       email.setMimeType(emailFormat == "HTML" ? "text/html" : "text/plain")             email.setBody(body)

   try {               log.debug ("Sending mail to ${email.getTo()}")               log.debug ("with body ${email.getBody()}")               SingleMailQueueItem item = new SingleMailQueueItem(email);               ComponentAccessor.getMailQueue().addItem(item);             } catch (MailException e) {               log.warn ("Error sending email", e)             }             }  else {             println   "No mail server or sending disabled."             log.warn ("No mail server or sending disabled.")         }

        params.remove("event")         return params     }

    /*       dynamically assign Subject and To-Address depending on Issue Status and ChangeNr       */     private chooseSubjectAndToAddress(Issue issue) {       def customFieldManager = ComponentAccessor.getCustomFieldManager()       def fieldChangeNr = customFieldManager.getCustomFieldObjectByName('Change Nr')       def changeNr  = issue.getCustomFieldValue(fieldChangeNr)       println "DEBUG chooseSubjectAndToAddress(): issue.status = $issue.status"       println "DEBUG chooseSubjectAndToAddress(): changeNr = $changeNr"       if (issue.status.name == "In Production") {         subject        = SUBJECT_INST_TEMPLATE         toAddress      = TO_PASSPROD       } else if (issue.getCustomFieldValue(fieldChangeNr)) {         subject        = SUBJECT_ADD_TEMPLATE         toAddress      = TO_2ND_LEVEL       } else {         subject        = SUBJECT_NEW_TEMPLATE         toAddress      = TO_2ND_LEVEL       }       println "DEBUG chooseSubjectAndToAddress(): subject set to   = $subject"       println "DEBUG chooseSubjectAndToAddress(): toAddress set to = $toAddress"     }       private Writable mergeEmailTemplate(Map params, String template, Boolean isPreview = false) {         GStringTemplateEngine engine = new GStringTemplateEngine()         Map binding = [:]         binding.putAll(params)         MutableIssue issue         if (params["issue"]) {             issue = params["issue"] as MutableIssue         }         else {             issue = issueManager.getIssueObject(params[FIELD_PREVIEW_ISSUE] as             String)         }         println "DEBUG issue is $issue"         ApplicationProperties applicationProperties = ComponentAccessor.getApplicationProperties()         binding.put("baseUrl", applicationProperties.getString(APKeys.JIRA_BASEURL))         binding.put("ComponentAccessor", ComponentAccessor)         binding.put("issue", issue)         binding.putAll(ConditionUtils.setupBinding(issue, binding))

        engine.createTemplate(template).make(binding)     }

    public Boolean isFinalParamsPage(Map params) {         true     }         }

    static main(arguments){       println "DEBUG invocation as standalone script"             def params = new HashMap()       params.put("transientVars", transientVars)       params.put("issue", transientVars.issue)       params.put("FIELD_PREVIEW_ISSUE", "PASS-926")       params.put("FIELD_EMAIL_FORMAT", "HTML")

      def spdm = new PreDeliveryMail()       spdm.doScript(params)     }

 

1 answer

0 votes
JamieA
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 Leaders.
November 19, 2014

That is hopeless... take a look at the question as it's rendered and ask yourself if you would spent time trying to decipher that. Please cut the relevant snippets and use the {code} macro, if you need to post the whole thing use gist.github.com.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events