From e95cbfd496e3c79c9ef2c55b74431c392d44f68c Mon Sep 17 00:00:00 2001 From: "Captain.B" Date: Fri, 27 Aug 2021 16:13:01 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20jenkins=20=E9=80=9A=E7=9F=A5?= =?UTF-8?q?=E5=8F=91=E9=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ApiAutomationController.java | 2 +- .../ApiDefinitionExecResultService.java | 17 +++++--- .../notice/sender/AbstractNoticeSender.java | 10 +++-- .../notice/sender/NoticeModel.java | 4 ++ .../notice/sender/SendNoticeAspect.java | 1 + .../notice/service/NoticeSendService.java | 39 ++++++++++++++++++- .../notice/service/NoticeService.java | 6 +-- .../notice/PerformanceNoticeEvent.java | 27 ++++++------- 8 files changed, 75 insertions(+), 31 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/controller/ApiAutomationController.java b/backend/src/main/java/io/metersphere/api/controller/ApiAutomationController.java index 5b0c4e3311..f2765655e9 100644 --- a/backend/src/main/java/io/metersphere/api/controller/ApiAutomationController.java +++ b/backend/src/main/java/io/metersphere/api/controller/ApiAutomationController.java @@ -201,7 +201,7 @@ public class ApiAutomationController { public String runByJenkins(@RequestBody RunScenarioRequest request) { request.setExecuteType(ExecuteType.Saved.name()); request.setTriggerMode(TriggerMode.API.name()); - request.setRunMode(ApiRunMode.SCENARIO.name()); + request.setRunMode(ApiRunMode.JENKINS.name()); return apiAutomationService.run(request); } diff --git a/backend/src/main/java/io/metersphere/api/service/ApiDefinitionExecResultService.java b/backend/src/main/java/io/metersphere/api/service/ApiDefinitionExecResultService.java index 88cb3d32c5..a6090ce902 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiDefinitionExecResultService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiDefinitionExecResultService.java @@ -160,10 +160,15 @@ public class ApiDefinitionExecResultService { .failedMailTemplate("api/CaseResult") .paramMap(paramMap) .event(event) + .excludeSelf(true) .build(); String taskType = NoticeConstants.TaskType.API_DEFINITION_TASK; - noticeSendService.send(taskType, noticeModel); + if (StringUtils.equals(ReportTriggerMode.API.name(), result.getTriggerMode())) { + noticeSendService.send(ReportTriggerMode.API.name(), taskType, noticeModel); + } else { + noticeSendService.send(taskType, noticeModel); + } } private String getName(String type, String id, String status, Long time, String resourceId) { @@ -225,7 +230,7 @@ public class ApiDefinitionExecResultService { String finalSaveResultType = saveResultType; Map apiIdResultMap = new HashMap<>(); - Map caseReportMap = new HashMap<>(); + Map caseReportMap = new HashMap<>(); if (CollectionUtils.isNotEmpty(result.getScenarios())) { result.getScenarios().forEach(scenarioResult -> { @@ -235,7 +240,7 @@ public class ApiDefinitionExecResultService { ApiDefinitionExecResult saveResult = new ApiDefinitionExecResult(); saveResult.setId(UUID.randomUUID().toString()); saveResult.setCreateTime(System.currentTimeMillis()); - saveResult.setName(getName(type, item.getName(), status, saveResult.getCreateTime(),saveResult.getId())); + saveResult.setName(getName(type, item.getName(), status, saveResult.getCreateTime(), saveResult.getId())); ApiDefinitionWithBLOBs apiDefinitionWithBLOBs = apiDefinitionMapper.selectByPrimaryKey(item.getName()); String caseId = null; if (apiDefinitionWithBLOBs != null) { @@ -254,7 +259,7 @@ public class ApiDefinitionExecResultService { } } } - if(StringUtils.isNotEmpty(caseId)){ + if (StringUtils.isNotEmpty(caseId)) { apiIdResultMap.put(caseId, item.isSuccess() ? TestPlanApiExecuteStatus.SUCCESS.name() : TestPlanApiExecuteStatus.FAILD.name()); } @@ -301,14 +306,14 @@ public class ApiDefinitionExecResultService { apiDefinitionExecResultMapper.updateByPrimaryKeyWithBLOBs(prevResult); } apiDefinitionExecResultMapper.insert(saveResult); - caseReportMap.put(caseId,saveResult); + caseReportMap.put(caseId, saveResult); }); } }); } testPlanLog.info("TestPlanReportId[" + testPlanReportId + "] APICASE OVER. API CASE STATUS:" + JSONObject.toJSONString(apiIdResultMap)); TestPlanReportExecuteCatch.updateApiTestPlanExecuteInfo(testPlanReportId, apiIdResultMap, null, null); - TestPlanReportExecuteCatch.updateTestPlanExecuteResultInfo(testPlanReportId,caseReportMap,null,null); + TestPlanReportExecuteCatch.updateTestPlanExecuteResultInfo(testPlanReportId, caseReportMap, null, null); } public void deleteByResourceId(String resourceId) { diff --git a/backend/src/main/java/io/metersphere/notice/sender/AbstractNoticeSender.java b/backend/src/main/java/io/metersphere/notice/sender/AbstractNoticeSender.java index e3710a7928..28bdf5ebdb 100644 --- a/backend/src/main/java/io/metersphere/notice/sender/AbstractNoticeSender.java +++ b/backend/src/main/java/io/metersphere/notice/sender/AbstractNoticeSender.java @@ -21,7 +21,6 @@ import java.io.IOException; import java.net.URL; import java.nio.charset.StandardCharsets; import java.util.ArrayList; -import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.stream.Collectors; @@ -181,9 +180,12 @@ public abstract class AbstractNoticeSender implements NoticeSender { } } // 排除自己 - toUsers.removeIf(u -> StringUtils.equals(u.getUserId(), noticeModel.getOperator())); + if (noticeModel.isExcludeSelf()) { + toUsers.removeIf(u -> StringUtils.equals(u.getUserId(), noticeModel.getOperator())); + } // 去重复 - HashSet receivers = new HashSet<>(toUsers); - return new ArrayList<>(receivers); + return toUsers.stream() + .distinct() + .collect(Collectors.toList()); } } diff --git a/backend/src/main/java/io/metersphere/notice/sender/NoticeModel.java b/backend/src/main/java/io/metersphere/notice/sender/NoticeModel.java index 41fd090b1b..c5620e6741 100644 --- a/backend/src/main/java/io/metersphere/notice/sender/NoticeModel.java +++ b/backend/src/main/java/io/metersphere/notice/sender/NoticeModel.java @@ -57,4 +57,8 @@ public class NoticeModel { * 接收人 */ private List receivers; + /** + * + */ + private boolean excludeSelf; } diff --git a/backend/src/main/java/io/metersphere/notice/sender/SendNoticeAspect.java b/backend/src/main/java/io/metersphere/notice/sender/SendNoticeAspect.java index 42c125111e..c634e20a80 100644 --- a/backend/src/main/java/io/metersphere/notice/sender/SendNoticeAspect.java +++ b/backend/src/main/java/io/metersphere/notice/sender/SendNoticeAspect.java @@ -142,6 +142,7 @@ public class SendNoticeAspect { .paramMap(paramMap) .event(sendNotice.event()) .status((String) paramMap.get("status")) + .excludeSelf(true) .build(); noticeSendService.send(sendNotice.taskType(), noticeModel); } diff --git a/backend/src/main/java/io/metersphere/notice/service/NoticeSendService.java b/backend/src/main/java/io/metersphere/notice/service/NoticeSendService.java index c4953f25fb..04cf730631 100644 --- a/backend/src/main/java/io/metersphere/notice/service/NoticeSendService.java +++ b/backend/src/main/java/io/metersphere/notice/service/NoticeSendService.java @@ -11,6 +11,7 @@ import io.metersphere.notice.sender.impl.*; import org.springframework.stereotype.Component; import javax.annotation.Resource; +import java.util.ArrayList; import java.util.List; @Component @@ -59,8 +60,8 @@ public class NoticeSendService { List messageDetails; switch (taskType) { case NoticeConstants.Mode.API: - String loadReportId = (String) noticeModel.getParamMap().get("id"); - messageDetails = noticeService.searchMessageByTypeBySend(NoticeConstants.TaskType.JENKINS_TASK, loadReportId); + String projectId = (String) noticeModel.getParamMap().get("projectId"); + messageDetails = noticeService.searchMessageByTypeBySend(NoticeConstants.TaskType.JENKINS_TASK, projectId); break; case NoticeConstants.Mode.SCHEDULE: messageDetails = noticeService.searchMessageByTestId(noticeModel.getTestId()); @@ -82,6 +83,40 @@ public class NoticeSendService { } } + public void send(String triggerMode, String taskType, NoticeModel noticeModel) { + // api和定时任务调用排除自己 + noticeModel.setExcludeSelf(false); + try { + List messageDetails = new ArrayList<>(); + + if (StringUtils.equals(triggerMode, NoticeConstants.Mode.SCHEDULE)) { + switch (taskType) { + case NoticeConstants.TaskType.API_AUTOMATION_TASK: + case NoticeConstants.TaskType.PERFORMANCE_TEST_TASK: + messageDetails = noticeService.searchMessageByTestId(noticeModel.getTestId()); + break; + default: + break; + } + } + + if (StringUtils.equals(triggerMode, NoticeConstants.Mode.API)) { + String projectId = (String) noticeModel.getParamMap().get("projectId"); + messageDetails = noticeService.searchMessageByTypeBySend(NoticeConstants.TaskType.JENKINS_TASK, projectId); + } + + // 异步发送通知 + messageDetails.stream() + .filter(messageDetail -> StringUtils.equals(messageDetail.getEvent(), noticeModel.getEvent())) + .forEach(messageDetail -> { + this.getNoticeSender(messageDetail).send(messageDetail, noticeModel); + }); + + } catch (Exception e) { + LogUtil.error(e.getMessage(), e); + } + } + public void send(Organization organization, String taskType, NoticeModel noticeModel) { try { List messageDetails = noticeService.searchMessageByTypeAndOrganizationId(taskType, organization.getId()); diff --git a/backend/src/main/java/io/metersphere/notice/service/NoticeService.java b/backend/src/main/java/io/metersphere/notice/service/NoticeService.java index a5dccadfbb..231e0fd2ad 100644 --- a/backend/src/main/java/io/metersphere/notice/service/NoticeService.java +++ b/backend/src/main/java/io/metersphere/notice/service/NoticeService.java @@ -4,7 +4,6 @@ import com.alibaba.fastjson.JSON; import io.metersphere.base.domain.MessageTask; import io.metersphere.base.domain.MessageTaskExample; import io.metersphere.base.domain.Organization; -import io.metersphere.base.mapper.LoadTestReportMapper; import io.metersphere.base.mapper.MessageTaskMapper; import io.metersphere.base.mapper.ext.ExtProjectMapper; import io.metersphere.commons.exception.MSException; @@ -32,8 +31,6 @@ public class NoticeService { @Resource private MessageTaskMapper messageTaskMapper; @Resource - private LoadTestReportMapper loadTestReportMapper; - @Resource private ExtProjectMapper extProjectMapper; public void saveMessageTask(MessageDetail messageDetail) { @@ -134,11 +131,10 @@ public class NoticeService { } } - public List searchMessageByTypeBySend(String type, String id) { + public List searchMessageByTypeBySend(String type, String projectId) { try { String orgId = ""; if (null == SessionUtils.getUser()) { - String projectId = loadTestReportMapper.selectByPrimaryKey(id).getProjectId(); Organization organization = extProjectMapper.getOrganizationByProjectId(projectId); orgId = organization.getId(); } else { diff --git a/backend/src/main/java/io/metersphere/performance/notice/PerformanceNoticeEvent.java b/backend/src/main/java/io/metersphere/performance/notice/PerformanceNoticeEvent.java index e612116f50..36c52ee34e 100644 --- a/backend/src/main/java/io/metersphere/performance/notice/PerformanceNoticeEvent.java +++ b/backend/src/main/java/io/metersphere/performance/notice/PerformanceNoticeEvent.java @@ -72,23 +72,23 @@ public class PerformanceNoticeEvent implements LoadTestFinishEvent { paramMap.put("type", "performance"); paramMap.put("url", baseSystemConfigDTO.getUrl()); paramMap.putAll(new BeanMap(loadTestDTO)); - NoticeModel noticeModel = NoticeModel.builder() - .operator(loadTestReport.getUserId()) - .successContext(successContext) - .successMailTemplate("PerformanceApiSuccessNotification") - .failedContext(failedContext) - .failedMailTemplate("PerformanceFailedNotification") - .testId(loadTestReport.getTestId()) - .status(loadTestReport.getStatus()) - .subject(subject) - .event(event) - .paramMap(paramMap) - .build(); if (StringUtils.equals(ReportTriggerMode.API.name(), loadTestReport.getTriggerMode()) || StringUtils.equals(ReportTriggerMode.SCHEDULE.name(), loadTestReport.getTriggerMode())) { - noticeSendService.send(loadTestReport.getTriggerMode(), noticeModel); + NoticeModel noticeModel = NoticeModel.builder() + .operator(loadTestReport.getUserId()) + .successContext(successContext) + .successMailTemplate("PerformanceApiSuccessNotification") + .failedContext(failedContext) + .failedMailTemplate("PerformanceFailedNotification") + .testId(loadTestReport.getTestId()) + .status(loadTestReport.getStatus()) + .subject(subject) + .event(event) + .paramMap(paramMap) + .build(); + noticeSendService.send(loadTestReport.getTriggerMode(), NoticeConstants.TaskType.PERFORMANCE_TEST_TASK, noticeModel); } else { Organization organization = projectService.getOrganizationByProjectId(loadTestReport.getProjectId()); String context = "${operator}执行性能测试完成: ${name}"; @@ -101,6 +101,7 @@ public class PerformanceNoticeEvent implements LoadTestFinishEvent { .subject(subject) .event(NoticeConstants.Event.EXECUTE_COMPLETED) .paramMap(paramMap) + .excludeSelf(true) .build(); noticeSendService.send(organization, NoticeConstants.TaskType.PERFORMANCE_TEST_TASK, noticeModel2); }