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.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.issue.comments.CommentManager
import 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 key
IssueManager im = ComponentAccessor.getIssueManager()
MutableIssue issue = im.getIssueObject(toChangeIssue)
if(1 == 1) {
// Get Variables of last comment
CommentManager 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 Email
def message = lastCommentBody.replaceAll("[\r\n]+", " ").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 exists
UserManager userManager = ComponentAccessor.getUserManager()
ApplicationUser existance = userManager.getUserByName(emailAddr)
// Prepare for sending
def 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 exists
if(existance != null){
sendEmail (emailAddr, subject, body)
}
}
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.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.issue.comments.CommentManager
import 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 key
IssueManager im = ComponentAccessor.getIssueManager()
MutableIssue issue = im.getIssueObject(toChangeIssue)
if(1 == 1) {
// Get Variables of last comment
CommentManager 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 Email
def message = lastCommentBody.replaceAll("[\r\n]+", " ").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 exists
UserManager userManager = ComponentAccessor.getUserManager()
ApplicationUser existance = userManager.getUserByName(emailAddr)
// Send Mail only if user exists
if(existance != null){
sendEmail (emailAddr, subject, body)
}
}
// Prepare for sending
def 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! :D
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.