项目发送邮件
package ru.mail.jira.scripts.go;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.mail.Email;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.mail.MailException;
import com.atlassian.mail.server.SMTPMailServer;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.priority.Priority;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.*;
public class HtmlEmail{
private static final Logger logger = LoggerFactory.getLogger(HtmlEmail.class);
private Set getUserEmail(Issue issue){
Set userEmails = new HashSet();
ApplicationUser applicationUser = issue.getAssigneeUser();
if(applicationUser != null){
String userEmailAddress = applicationUser.getEmailAddress();
userEmails.add(userEmailAddress);
}
ApplicationUser creatorUser = issue.getCreator();
if(creatorUser != null){
String creatorUserEmailAddress = creatorUser.getEmailAddress();
userEmails.add(creatorUserEmailAddress);
}
ApplicationUser reporterUser = issue.getReporterUser();
if(reporterUser != null){
String reporterUserEmailAddress = reporterUser.getEmailAddress();
userEmails.add(reporterUserEmailAddress);
}
return userEmails;
}
private Set getTaskUsers(Issue issue){
Set userEmails = new HashSet<String>();
Collection<Issue> issues = issue.getSubTaskObjects();
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
CustomField moduleField = customFieldManager.getCustomFieldObject("customfield_10601");
List<String> moduleFieldValue = (List<String>)issue.getCustomFieldValue(moduleField);
logger.info("moduleFieldValue: {}", moduleFieldValue);
if(moduleFieldValue != null){
for( String item : moduleFieldValue) {
String userName = item.replaceAll('^(\\w+)\\(\\1\\)$','$1');
ApplicationUser user = ComponentAccessor.getUserManager().getUserByName(userName);
if(user != null){
String userEmail = user.getEmailAddress();
logger.info("ApplicationUser: {}, userEmail:{}", user, userEmail);
userEmails.add(userEmail);
}
}
}
userEmails.addAll(getUserEmail(issue));
for (Issue subIssue : issues){
userEmails.addAll(getUserEmail(subIssue));
}
return userEmails;
}
private void sendEmail(Issue issue, String subject, String body) {
Set userEmails = getTaskUsers(issue);
String emailAddr = String.join(", ", userEmails);
logger.error("userEmails: {}", emailAddr);
SMTPMailServer mailServer = ComponentAccessor.getMailServerManager().getDefaultSMTPMailServer();
Email email = new Email(emailAddr);
email.setSubject(subject);
email.setMimeType("text/html");
email.setBody(body);
try {
mailServer.send(email);
}catch (MailException e){
System.out.println(e);
}
}
private String tableCss(){
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("<style type=\"text/css\">");
stringBuffer.append("table,table tr th, table tr td { border:1px solid #000; }");
stringBuffer.append("td, th{text-align: left; padding: 0.2em 0.2em 0.2em 0.8em;}");
stringBuffer.append("td span img{ width: 16px;}");
stringBuffer.append("thead tr{background-color: beige;font-size: 18px;font-weight: bold;line-height: 35px;}");
stringBuffer.append("</style>");
return stringBuffer.toString();
}
private String subtaskContent(Collection<Issue> issues){
StringBuffer info = new StringBuffer();
Integer index = 1;
for (Issue issue: issues){
if("10206".equals( issue.getIssueTypeId())) {
continue;
}
String summary = issue.getSummary();
ApplicationUser assigneeUser = issue.getAssigneeUser();
ApplicationUser reporterUser = issue.getReporterUser();
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
CustomField moduleField = customFieldManager.getCustomFieldObject("customfield_10804");
String moduleFieldValue = (String)issue.getCustomFieldValue(moduleField);
String description = issue.getDescription();
List<String> module = new ArrayList<>();
module.add("项目模块:" + moduleFieldValue);
if(assigneeUser != null){
String assEmail = assigneeUser.getEmailAddress();
module.add("负责人:" + assEmail);
}
if(reporterUser != null){
String repEmail = reporterUser.getEmailAddress();
module.add("报告人:" + repEmail);
}
module.add("项目描述:"+ description);
info.append("<tr>");
info.append("<th>提测模块("+ (index++) +"):"+ summary +"</th><td>" + String.join("<br/>", module)+ "</td>");
info.append("</tr>");
}
return info.toString();
}
private String checkEmailContent(String subject, Issue issue){
logger.error("checkEmailContent: {}", issue);
Collection<Issue> issues = issue.getSubTaskObjects();
String description = issue.getDescription();
ApplicationUser assigneeUser = issue.getAssigneeUser();
ApplicationUser reporterUser = issue.getReporterUser();
List<String> emails= new ArrayList<>();
if(reporterUser != null){
emails.add(reporterUser.getEmailAddress());
}
if(assigneeUser != null){
emails.add(assigneeUser.getEmailAddress());
}
String content =
"<table style=\"width: 800px; min-height: 25px; line-height: 25px; text-align: left; border-collapse: collapse;\">"+
"<thead>"+
"<tr>"+
"<th colspan=2>提测邮件:项目" + subject + " </th>"+
"</tr>"+
"<tr>"+
"<th width=\"100px\">类目</th><th>内容</th>"+
"</tr>"+
"</thead>"+
"<tbody>"+
"<tr>"+
"<th>项目相关人员</th><td> "+ String.join(";", emails )+" </td>"+
"</tr>"+
"<tr>"+
"<th>项目描述</th><td> " + description + "</td>"+
"</tr>"+
subtaskContent(issues) +
"<tr>"+
"<th>注意事项</th><td></td>"+
"</tr>"+
"</tbody>"+
"</table>";
return content;
}
public void projectCheck(Issue issue){
String summary = issue.getSummary();
String subject = "送测邮件";
String body = "<h2>"+summary+"</h2>" + checkEmailContent(subject, issue) + tableCss();
sendEmail( issue , summary + subject + issue.getKey(), body);
}
private String subtaskBugContent(Collection<Issue> issues){
StringBuffer info = new StringBuffer();
Integer index = 1;
String baseUrl = ComponentAccessor.getApplicationProperties().getString("jira.baseurl");
for (Issue issue: issues){
if(!"10206".equals( issue.getIssueTypeId())) {
continue;
}
if( index > 15){
continue;
}
String summary = issue.getSummary();
String status = issue.getStatus().getSimpleStatus().getName();
Priority priority = issue.getPriority();
String color = priority.getStatusColor();
String url = priority.getSvgIconUrl();
ApplicationUser assigneeUser = issue.getAssigneeUser();
ApplicationUser reporterUser = issue.getReporterUser();
String description = issue.getDescription();
List<String> module = new ArrayList<>();
module.add("BUG名:" + summary);
module.add("BUG状态:" + status);
module.add("<span style=\"color: "+ color +"\">Priority: <img width=16 height=16 src=\"" + baseUrl + url + "\"/></span>");
if(assigneeUser != null){
String assEmail = assigneeUser.getEmailAddress();
module.add("负责人:" + assEmail);
}
if(reporterUser != null){
String repEmail = reporterUser.getEmailAddress();
module.add("报告人:" + repEmail);
}
module.add("项目描述:"+ description);
info.append("<tr>");
info.append("<th>BUG("+ (index++) +")</th><td>" + String.join("<br/>", module)+ "</td>");
info.append("</tr>");
}
return info.toString();
}
private String reportEmailContent(String subject, Issue issue){
logger.error("reportEmailContent: {}", issue);
Collection<Issue> issues = issue.getSubTaskObjects();
String description = issue.getDescription();
ApplicationUser assigneeUser = issue.getAssigneeUser();
ApplicationUser reporterUser = issue.getReporterUser();
List<String> emails= new ArrayList<>();
if(reporterUser != null){
emails.add(reporterUser.getEmailAddress());
}
if(assigneeUser != null){
emails.add(assigneeUser.getEmailAddress());
}
String content =
"<table style=\"width: 800px; min-height: 25px; line-height: 25px; text-align: left; border-collapse: collapse;\">"+
"<thead>"+
"<tr>"+
"<th colspan=2>报告邮件:项目" + subject + " </th>"+
"</tr>"+
"<tr>"+
"<th width=\"150px\">类目</th><th>内容</th>"+
"</tr>"+
"</thead>"+
"<tbody>"+
"<tr>"+
"<th>项目相关人员</th><td> "+ String.join(";", emails )+" </td>"+
"</tr>"+
"<tr>"+
"<th>项目描述</th><td> " + description + "</td>"+
"</tr>"+
subtaskBugContent(issues) +
"<tr>"+
"<th>注意事项</th><td></td>"+
"</tr>"+
"</tbody>"+
"</table>";
return content;
}
public void checkReport(Issue issue){
String summary = issue.getSummary();
String subject = "测试报告";
String body = "<h2>"+summary+"</h2>" + reportEmailContent(subject, issue) + tableCss();
sendEmail( issue , summary + subject + issue.getKey(), body);
}
}