项目发送邮件

项目发送邮件

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) {
                // 强制匹配 zhangsan(zhangsan)
                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);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

从未、淡定

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值