refactor: 重构发送通知的后台

This commit is contained in:
Captain.B 2020-12-03 15:57:38 +08:00
parent 30e3432d6d
commit 3e00db1011
8 changed files with 44 additions and 6 deletions

View File

@ -216,6 +216,7 @@ public class APIBackendListenerClient extends AbstractBackendListenerClient impl
String successContext = "";
String failedContext = "";
String subject = "";
String event = "";
if (StringUtils.equals(NoticeConstants.Mode.API, report.getTriggerMode())) {
successContext = "接口测试 API任务通知:'" + report.getName() + "'执行成功" + "\n" + "请点击下面链接进入测试报告页面" + "\n" + url;
failedContext = "接口测试 API任务通知:'" + report.getName() + "'执行失败" + "\n" + "请点击下面链接进入测试报告页面" + "\n" + url;
@ -226,6 +227,12 @@ public class APIBackendListenerClient extends AbstractBackendListenerClient impl
failedContext = "接口测试定时任务通知:'" + report.getName() + "'执行失败" + "\n" + "请点击下面链接进入测试报告页面" + "\n" + url;
subject = Translator.get("task_notification");
}
if (StringUtils.equals("Success", report.getStatus())) {
event = NoticeConstants.Event.EXECUTE_SUCCESSFUL;
}
if (StringUtils.equals("Error", report.getStatus())) {
event = NoticeConstants.Event.EXECUTE_FAILED;
}
NoticeModel noticeModel = NoticeModel.builder()
.successContext(successContext)
@ -234,6 +241,7 @@ public class APIBackendListenerClient extends AbstractBackendListenerClient impl
.failedMailTemplate("ApiFailedNotification")
.testId(testResult.getTestId())
.status(report.getStatus())
.event(event)
.subject(subject)
.build();
noticeSendService.send(report.getTriggerMode(), noticeModel);

View File

@ -17,6 +17,10 @@ public class NoticeModel {
* 保存状态
*/
private String status;
/**
* Event
*/
private String event;
/**
* 消息主题
*/

View File

@ -1,5 +1,6 @@
package io.metersphere.notice.service;
import com.alibaba.nacos.client.utils.StringUtils;
import io.metersphere.commons.constants.NoticeConstants;
import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.notice.domain.MessageDetail;
@ -22,6 +23,10 @@ public class NoticeSendService {
@Resource
private DingNoticeSender dingNoticeSender;
private void event(String event) {
}
private NoticeSender getNoticeSender(MessageDetail messageDetail) {
NoticeSender noticeSender = null;
switch (messageDetail.getType()) {
@ -56,8 +61,10 @@ public class NoticeSendService {
messageDetails = noticeService.searchMessageByType(taskType);
break;
}
messageDetails.forEach(messageDetail ->
this.getNoticeSender(messageDetail).send(messageDetail, noticeModel)
);
messageDetails.forEach(messageDetail -> {
if (StringUtils.equals(messageDetail.getEvent(), noticeModel.getEvent())) {
this.getNoticeSender(messageDetail).send(messageDetail, noticeModel);
}
});
}
}

View File

@ -63,6 +63,7 @@ public class PerformanceNoticeTask {
String successContext = "";
String failedContext = "";
String subject = "";
String event = "";
if (StringUtils.equals(NoticeConstants.Mode.API, loadTestReport.getTriggerMode())) {
successContext = "性能测试 API任务通知:" + loadTestReport.getName() + "执行成功" + "\n" + "请点击下面链接进入测试报告页面" + "\n" + url;
failedContext = "性能测试 API任务通知:" + loadTestReport.getName() + "执行失败" + "\n" + "请点击下面链接进入测试报告页面" + "\n" + url;
@ -74,6 +75,13 @@ public class PerformanceNoticeTask {
subject = Translator.get("task_notification");
}
if (PerformanceTestStatus.Completed.name().equals(loadTestReport.getStatus())) {
event = NoticeConstants.Event.EXECUTE_SUCCESSFUL;
}
if (PerformanceTestStatus.Error.name().equals(loadTestReport.getStatus())) {
event = NoticeConstants.Event.EXECUTE_FAILED;
}
NoticeModel noticeModel = NoticeModel.builder()
.successContext(successContext)
.successMailTemplate("PerformanceApiSuccessNotification")
@ -82,6 +90,7 @@ public class PerformanceNoticeTask {
.testId(loadTestReport.getTestId())
.status(loadTestReport.getStatus())
.subject(subject)
.event(event)
.build();
noticeSendService.send(loadTestReport.getTriggerMode(), noticeModel);
}

View File

@ -100,6 +100,7 @@ public class IssuesService {
.subject(Translator.get("task_defect_notification"))
.mailTemplate("IssuesCreate")
.paramMap(paramMap)
.event(NoticeConstants.Event.CREATE)
.build();
noticeSendService.send(NoticeConstants.TaskType.DEFECT_TASK, noticeModel);
}

View File

@ -73,6 +73,7 @@ public class TestCaseCommentService {
.subject(Translator.get("test_review_task_notice"))
.mailTemplate("ReviewComments")
.paramMap(paramMap)
.event(NoticeConstants.Event.COMMENT)
.build();
noticeSendService.send(NoticeConstants.TaskType.REVIEW_TASK, noticeModel);
}

View File

@ -112,6 +112,7 @@ public class TestCaseReviewService {
.subject(Translator.get("test_review_task_notice"))
.mailTemplate("ReviewInitiate")
.paramMap(paramMap)
.event(NoticeConstants.Event.CREATE)
.build();
noticeSendService.send(NoticeConstants.TaskType.REVIEW_TASK, noticeModel);
}
@ -216,6 +217,7 @@ public class TestCaseReviewService {
.subject(Translator.get("test_review_task_notice"))
.mailTemplate("ReviewEnd")
.paramMap(paramMap)
.event(NoticeConstants.Event.UPDATE)
.build();
noticeSendService.send(NoticeConstants.TaskType.REVIEW_TASK, noticeModel);
}
@ -318,6 +320,7 @@ public class TestCaseReviewService {
.subject(Translator.get("test_review_task_notice"))
.mailTemplate("ReviewDelete")
.paramMap(paramMap)
.event(NoticeConstants.Event.DELETE)
.build();
noticeSendService.send(NoticeConstants.TaskType.REVIEW_TASK, noticeModel);
} catch (Exception e) {
@ -460,6 +463,7 @@ public class TestCaseReviewService {
.subject(Translator.get("test_review_task_notice"))
.mailTemplate("ReviewEnd")
.paramMap(paramMap)
.event(NoticeConstants.Event.UPDATE)
.build();
noticeSendService.send(NoticeConstants.TaskType.REVIEW_TASK, noticeModel);
} catch (Exception e) {

View File

@ -115,6 +115,7 @@ public class TestPlanService {
.subject(Translator.get("test_plan_notification"))
.mailTemplate("TestPlanStart")
.paramMap(paramMap)
.event(NoticeConstants.Event.CREATE)
.build();
noticeSendService.send(NoticeConstants.TaskType.TEST_PLAN_TASK, noticeModel);
}
@ -158,6 +159,7 @@ public class TestPlanService {
.subject(Translator.get("test_plan_notification"))
.mailTemplate("TestPlanEnd")
.paramMap(paramMap)
.event(NoticeConstants.Event.UPDATE)
.build();
noticeSendService.send(NoticeConstants.TaskType.TEST_PLAN_TASK, noticeModel);
}
@ -258,9 +260,9 @@ public class TestPlanService {
deleteTestCaseByPlanId(planId);
testPlanProjectService.deleteTestPlanProjectByPlanId(planId);
int num = testPlanMapper.deleteByPrimaryKey(planId);
List<String> userIds = new ArrayList<>();
List<String> relatedUsers = new ArrayList<>();
AddTestPlanRequest testPlans = new AddTestPlanRequest();
userIds.add(testPlan.getCreator());
relatedUsers.add(testPlan.getCreator());
try {
BeanUtils.copyBean(testPlans, testPlan);
String context = getTestPlanContext(testPlans, NoticeConstants.Event.DELETE);
@ -269,10 +271,11 @@ public class TestPlanService {
paramMap.put("creator", user.getName());
NoticeModel noticeModel = NoticeModel.builder()
.context(context)
.relatedUsers(userIds)
.relatedUsers(relatedUsers)
.subject(Translator.get("test_plan_notification"))
.mailTemplate("TestPlanDelete")
.paramMap(paramMap)
.event(NoticeConstants.Event.DELETE)
.build();
noticeSendService.send(NoticeConstants.TaskType.TEST_PLAN_TASK, noticeModel);
} catch (Exception e) {
@ -510,6 +513,7 @@ public class TestPlanService {
.subject(Translator.get("test_plan_notification"))
.mailTemplate("TestPlanEnd")
.paramMap(paramMap)
.event(NoticeConstants.Event.UPDATE)
.build();
noticeSendService.send(NoticeConstants.TaskType.TEST_PLAN_TASK, noticeModel);
} catch (Exception e) {