Hello i have some problems with the groovy syntax and dont get my mistake. Would be nice if somebody can help me.If i add the following mail script into an "if" condition it doesnt work any more; if there is no if it runs... the if i mean is exampled by if(1 == 1) {}; after deleting the if(1 == 1) {} the script runs perfect.
import com.atlassian.jira.issue.IssueManagerimport com.atlassian.jira.issue.MutableIssueimport com.atlassian.jira.user.ApplicationUserimport com.atlassian.jira.user.util.UserManagerimport com.atlassian.jira.issue.comments.CommentManagerimport com.atlassian.mail.Email;import com.atlassian.mail.server.MailServerManager;import com.atlassian.mail.server.SMTPMailServer;import com.atlassian.jira.component.ComponentAccessor;def toChangeIssue = "TEST-1234"// search Issue by keyIssueManager im = ComponentAccessor.getIssueManager()MutableIssue issue = im.getIssueObject(toChangeIssue)if(1 == 1) {// Get Variables of last commentCommentManager commentMgr = ComponentAccessor.getCommentManager()def lastCommentAuthorFullName = commentMgr.getComments(issue).last().getAuthorFullName()def lastCommentId = commentMgr.getComments(issue).last().getId()def lastCommentBody = commentMgr.getComments(issue).last().getBody()// Variablen for Emaildef message = lastCommentBody.replaceAll("[]+", " ").trim().strip().replaceAll(" ", " ")def emailAddr = message.replaceAll( /(.+)(\[\~(.*?)\])(.+)/, "\$3") //.replaceAll("(\[\~(.*?)\])","$2").replaceAll("(\[(.*?)\|(.*?)\])","$3").def subject = toChangeIssue + " | Info: " + lastCommentAuthorFullName + " | " + issue.updated.format("dd.MM.yyyy - HH:mm:ss")def body = message.replaceAll( /(.+)(\[\~(.*?)\])(.+)/, "\$4").substring(1)// Check if user existsUserManager userManager = ComponentAccessor.getUserManager()ApplicationUser existance = userManager.getUserByName(emailAddr)// Prepare for sendingdef sendEmail(String emailAddr, String subject, String body) {SMTPMailServer mailServer = ComponentAccessor.getMailServerManager().getDefaultSMTPMailServer()if (mailServer) {Email email = new Email(emailAddr);email.setSubject(subject);email.setBody(body);mailServer.send(email);} else {// Problem getting the mail server from JIRA configuration, log this error}}// Send Mail only if user existsif(existance != null){sendEmail (emailAddr, subject, body)}}
Hi @Markus W_ BENES
It looks like the issue is that you are defining a function inside of an IF statement. Your sendEmail function is defined within the IF. Could you try the following (where I have moved the definition outside of the IF) and see if this works?
import com.atlassian.jira.issue.IssueManagerimport com.atlassian.jira.issue.MutableIssueimport com.atlassian.jira.user.ApplicationUserimport com.atlassian.jira.user.util.UserManagerimport com.atlassian.jira.issue.comments.CommentManagerimport com.atlassian.mail.Email;import com.atlassian.mail.server.MailServerManager;import com.atlassian.mail.server.SMTPMailServer;import com.atlassian.jira.component.ComponentAccessor;def toChangeIssue = "TEST-1234"// search Issue by keyIssueManager im = ComponentAccessor.getIssueManager()MutableIssue issue = im.getIssueObject(toChangeIssue)if(1 == 1) {// Get Variables of last commentCommentManager commentMgr = ComponentAccessor.getCommentManager()def lastCommentAuthorFullName = commentMgr.getComments(issue).last().getAuthorFullName()def lastCommentId = commentMgr.getComments(issue).last().getId()def lastCommentBody = commentMgr.getComments(issue).last().getBody()// Variablen for Emaildef message = lastCommentBody.replaceAll("[]+", " ").trim().strip().replaceAll(" ", " ")def emailAddr = message.replaceAll( /(.+)(\[\~(.*?)\])(.+)/, "\$3") //.replaceAll("(\[\~(.*?)\])","$2").replaceAll("(\[(.*?)\|(.*?)\])","$3").def subject = toChangeIssue + " | Info: " + lastCommentAuthorFullName + " | " + issue.updated.format("dd.MM.yyyy - HH:mm:ss")def body = message.replaceAll( /(.+)(\[\~(.*?)\])(.+)/, "\$4").substring(1)// Check if user existsUserManager userManager = ComponentAccessor.getUserManager()ApplicationUser existance = userManager.getUserByName(emailAddr)// Send Mail only if user existsif(existance != null){ sendEmail (emailAddr, subject, body) }}// Prepare for sendingdef sendEmail(String emailAddr, String subject, String body) {SMTPMailServer mailServer = ComponentAccessor.getMailServerManager().getDefaultSMTPMailServer()if (mailServer) {Email email = new Email(emailAddr); email.setSubject(subject); email.setBody(body); mailServer.send(email); } else {// Problem getting the mail server from JIRA configuration, log this error }}
too simple to find it on my own; Thank you so much; made my day!
It looks like you're new here. Sign in or register to get started.