refactor: 接口自动化执行发送通知修改

This commit is contained in:
Captain.B 2021-09-08 14:38:04 +08:00 committed by 刘瑞斌
parent 83b4264a1d
commit 45636c5186
1 changed files with 19 additions and 11 deletions

View File

@ -26,11 +26,13 @@ import io.metersphere.base.mapper.ApiScenarioReportMapper;
import io.metersphere.base.mapper.TestPlanApiScenarioMapper; import io.metersphere.base.mapper.TestPlanApiScenarioMapper;
import io.metersphere.base.mapper.ext.ExtApiScenarioReportDetailMapper; import io.metersphere.base.mapper.ext.ExtApiScenarioReportDetailMapper;
import io.metersphere.base.mapper.ext.ExtApiScenarioReportMapper; import io.metersphere.base.mapper.ext.ExtApiScenarioReportMapper;
import io.metersphere.base.mapper.ext.ExtProjectMapper;
import io.metersphere.commons.constants.*; import io.metersphere.commons.constants.*;
import io.metersphere.commons.exception.MSException; import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.*; import io.metersphere.commons.utils.*;
import io.metersphere.dto.ApiReportCountDTO; import io.metersphere.dto.ApiReportCountDTO;
import io.metersphere.dto.NodeDTO; import io.metersphere.dto.NodeDTO;
import io.metersphere.dto.UserDTO;
import io.metersphere.i18n.Translator; import io.metersphere.i18n.Translator;
import io.metersphere.log.utils.ReflexObjectUtil; import io.metersphere.log.utils.ReflexObjectUtil;
import io.metersphere.log.vo.DetailColumn; import io.metersphere.log.vo.DetailColumn;
@ -38,6 +40,7 @@ import io.metersphere.log.vo.OperatingLogDetails;
import io.metersphere.log.vo.api.ModuleReference; import io.metersphere.log.vo.api.ModuleReference;
import io.metersphere.notice.sender.NoticeModel; import io.metersphere.notice.sender.NoticeModel;
import io.metersphere.notice.service.NoticeSendService; import io.metersphere.notice.service.NoticeSendService;
import io.metersphere.service.UserService;
import io.metersphere.track.service.TestPlanReportService; import io.metersphere.track.service.TestPlanReportService;
import org.apache.commons.beanutils.BeanMap; import org.apache.commons.beanutils.BeanMap;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
@ -79,6 +82,10 @@ public class ApiScenarioReportService {
SqlSessionFactory sqlSessionFactory; SqlSessionFactory sqlSessionFactory;
@Resource @Resource
private NoticeSendService noticeSendService; private NoticeSendService noticeSendService;
@Resource
private UserService userService;
@Resource
private ExtProjectMapper extProjectMapper;
public ApiScenarioReport complete(TestResult result, String runMode) { public ApiScenarioReport complete(TestResult result, String runMode) {
// 更新场景 // 更新场景
@ -596,7 +603,7 @@ public class ApiScenarioReportService {
apiScenarioMapper.updateByPrimaryKey(scenario); apiScenarioMapper.updateByPrimaryKey(scenario);
// 发送通知 // 发送通知
sendNotice(scenario); sendNotice(scenario, report);
} }
lastReport = report; lastReport = report;
if (report.getExecuteType().equals(ExecuteType.Marge.name())) { if (report.getExecuteType().equals(ExecuteType.Marge.name())) {
@ -615,28 +622,29 @@ public class ApiScenarioReportService {
} }
private void sendNotice(ApiScenario result) { private void sendNotice(ApiScenario scenario, ApiScenarioReport result) {
BeanMap beanMap = new BeanMap(result); BeanMap beanMap = new BeanMap(scenario);
String event; String event;
String status; String status;
if (StringUtils.equals(result.getLastResult(), "Success")) { if (StringUtils.equals(scenario.getLastResult(), "Success")) {
event = NoticeConstants.Event.EXECUTE_SUCCESSFUL; event = NoticeConstants.Event.EXECUTE_SUCCESSFUL;
status = "成功"; status = "成功";
} else { } else {
event = NoticeConstants.Event.EXECUTE_FAILED; event = NoticeConstants.Event.EXECUTE_FAILED;
status = "失败"; status = "失败";
} }
String userId = result.getCreateUser();
UserDTO userDTO = userService.getUserDTO(userId);
Map paramMap = new HashMap<>(beanMap); Map paramMap = new HashMap<>(beanMap);
if (SessionUtils.getUser() != null) { paramMap.put("operator", userDTO.getName());
paramMap.put("operator", SessionUtils.getUser().getName()); paramMap.put("status", scenario.getLastResult());
}
paramMap.put("status", result.getLastResult());
String context = "${operator}执行接口自动化" + status + ": ${name}"; String context = "${operator}执行接口自动化" + status + ": ${name}";
NoticeModel noticeModel = NoticeModel.builder() NoticeModel noticeModel = NoticeModel.builder()
.operator(SessionUtils.getUserId()) .operator(userId)
.context(context) .context(context)
.subject("接口自动化通知") .subject("接口自动化通知")
.successMailTemplate("api/ScenarioResult") .successMailTemplate("api/ScenarioResult")
@ -645,8 +653,8 @@ public class ApiScenarioReportService {
.event(event) .event(event)
.build(); .build();
String taskType = NoticeConstants.TaskType.API_AUTOMATION_TASK; Organization organization = extProjectMapper.getOrganizationByProjectId(scenario.getProjectId());
noticeSendService.send(taskType, noticeModel); noticeSendService.send(organization, NoticeConstants.TaskType.API_AUTOMATION_TASK, noticeModel);
} }
public String update(APIScenarioReportResult test) { public String update(APIScenarioReportResult test) {