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

View File

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

View File

@ -1,5 +1,6 @@
package io.metersphere.notice.service; package io.metersphere.notice.service;
import com.alibaba.nacos.client.utils.StringUtils;
import io.metersphere.commons.constants.NoticeConstants; import io.metersphere.commons.constants.NoticeConstants;
import io.metersphere.commons.utils.CommonBeanFactory; import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.notice.domain.MessageDetail; import io.metersphere.notice.domain.MessageDetail;
@ -22,6 +23,10 @@ public class NoticeSendService {
@Resource @Resource
private DingNoticeSender dingNoticeSender; private DingNoticeSender dingNoticeSender;
private void event(String event) {
}
private NoticeSender getNoticeSender(MessageDetail messageDetail) { private NoticeSender getNoticeSender(MessageDetail messageDetail) {
NoticeSender noticeSender = null; NoticeSender noticeSender = null;
switch (messageDetail.getType()) { switch (messageDetail.getType()) {
@ -56,8 +61,10 @@ public class NoticeSendService {
messageDetails = noticeService.searchMessageByType(taskType); messageDetails = noticeService.searchMessageByType(taskType);
break; break;
} }
messageDetails.forEach(messageDetail -> messageDetails.forEach(messageDetail -> {
this.getNoticeSender(messageDetail).send(messageDetail, noticeModel) 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 successContext = "";
String failedContext = ""; String failedContext = "";
String subject = ""; String subject = "";
String event = "";
if (StringUtils.equals(NoticeConstants.Mode.API, loadTestReport.getTriggerMode())) { if (StringUtils.equals(NoticeConstants.Mode.API, loadTestReport.getTriggerMode())) {
successContext = "性能测试 API任务通知:" + loadTestReport.getName() + "执行成功" + "\n" + "请点击下面链接进入测试报告页面" + "\n" + url; successContext = "性能测试 API任务通知:" + loadTestReport.getName() + "执行成功" + "\n" + "请点击下面链接进入测试报告页面" + "\n" + url;
failedContext = "性能测试 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"); 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() NoticeModel noticeModel = NoticeModel.builder()
.successContext(successContext) .successContext(successContext)
.successMailTemplate("PerformanceApiSuccessNotification") .successMailTemplate("PerformanceApiSuccessNotification")
@ -82,6 +90,7 @@ public class PerformanceNoticeTask {
.testId(loadTestReport.getTestId()) .testId(loadTestReport.getTestId())
.status(loadTestReport.getStatus()) .status(loadTestReport.getStatus())
.subject(subject) .subject(subject)
.event(event)
.build(); .build();
noticeSendService.send(loadTestReport.getTriggerMode(), noticeModel); noticeSendService.send(loadTestReport.getTriggerMode(), noticeModel);
} }

View File

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

View File

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

View File

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

View File

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