From 812f0c9147bdff10bbdfe66bf211ca3e61681c61 Mon Sep 17 00:00:00 2001 From: "Captain.B" Date: Fri, 27 Aug 2021 16:57:21 +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 --- .../notice/service/NoticeSendService.java | 22 +++++- .../track/service/TestPlanReportService.java | 71 ++++++++++--------- .../notice/components/MentionedMeData.vue | 2 +- .../notice/components/SystemNoticeData.vue | 2 +- 4 files changed, 58 insertions(+), 39 deletions(-) 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 04cf730631..716217d274 100644 --- a/backend/src/main/java/io/metersphere/notice/service/NoticeSendService.java +++ b/backend/src/main/java/io/metersphere/notice/service/NoticeSendService.java @@ -83,8 +83,11 @@ public class NoticeSendService { } } + /** + * jenkins 和定时任务触发的发送 + */ public void send(String triggerMode, String taskType, NoticeModel noticeModel) { - // api和定时任务调用排除自己 + // api和定时任务调用不排除自己 noticeModel.setExcludeSelf(false); try { List messageDetails = new ArrayList<>(); @@ -117,9 +120,24 @@ public class NoticeSendService { } } + /** + * 后台触发的发送,没有session + */ public void send(Organization organization, String taskType, NoticeModel noticeModel) { try { - List messageDetails = noticeService.searchMessageByTypeAndOrganizationId(taskType, organization.getId()); + List messageDetails; +// switch (taskType) { +// case NoticeConstants.Mode.API: +// 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()); +// break; +// default: +// break; +// } + messageDetails = noticeService.searchMessageByTypeAndOrganizationId(taskType, organization.getId()); // 异步发送通知 messageDetails.stream() diff --git a/backend/src/main/java/io/metersphere/track/service/TestPlanReportService.java b/backend/src/main/java/io/metersphere/track/service/TestPlanReportService.java index cd896df4a5..6688838c81 100644 --- a/backend/src/main/java/io/metersphere/track/service/TestPlanReportService.java +++ b/backend/src/main/java/io/metersphere/track/service/TestPlanReportService.java @@ -31,6 +31,7 @@ import io.metersphere.track.request.report.QueryTestPlanReportRequest; import io.metersphere.track.request.report.TestPlanReportSaveRequest; import io.metersphere.track.request.testcase.QueryTestPlanRequest; import io.metersphere.track.request.testplan.LoadCaseRequest; +import org.apache.commons.beanutils.BeanMap; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.MapUtils; import org.apache.commons.lang3.StringUtils; @@ -138,18 +139,18 @@ public class TestPlanReportService { String planReportId = UUID.randomUUID().toString(); Map apiCaseInfoMap = new HashMap<>(); - if(!apiTestCaseIdMap.isEmpty()){ + if (!apiTestCaseIdMap.isEmpty()) { ApiTestCaseExample apiTestCaseExample = new ApiTestCaseExample(); apiTestCaseExample.createCriteria().andIdIn(new ArrayList<>(apiTestCaseIdMap.keySet())); List apiCaseList = apiTestCaseMapper.selectByExampleWithBLOBs(apiTestCaseExample); - Map apiCaseDataMap = new HashMap<>(); - if(!apiCaseList.isEmpty()){ + Map apiCaseDataMap = new HashMap<>(); + if (!apiCaseList.isEmpty()) { apiCaseDataMap = apiCaseList.stream().collect(Collectors.toMap(ApiTestCaseWithBLOBs::getId, k -> k)); for (String id : apiCaseDataMap.keySet()) { - apiCaseInfoMap.put(id,TestPlanApiExecuteStatus.PREPARE.name()); + apiCaseInfoMap.put(id, TestPlanApiExecuteStatus.PREPARE.name()); String testPlanApiCaseId = apiTestCaseIdMap.get(id); - if(StringUtils.isNotEmpty(testPlanApiCaseId)){ - apiTestCaseDataMap.put(apiCaseDataMap.get(id),testPlanApiCaseId); + if (StringUtils.isNotEmpty(testPlanApiCaseId)) { + apiTestCaseDataMap.put(apiCaseDataMap.get(id), testPlanApiCaseId); } } } @@ -195,7 +196,8 @@ public class TestPlanReportService { testPlanReport.setUpdateTime(System.currentTimeMillis()); try { testPlanReport.setName(testPlan.getName() + "-" + DateUtils.getTimeString(new Date())); - } catch (Exception ignored) {} + } catch (Exception ignored) { + } testPlanReport.setTriggerMode(saveRequest.getTriggerMode()); testPlanReport.setCreator(saveRequest.getUserId()); @@ -246,7 +248,7 @@ public class TestPlanReportService { performanceInfoMap = saveRequest.getPerformanceIdMap(); } - TestPlanReportExecuteCatch.addApiTestPlanExecuteInfo(testPlanReportID,apiCaseInfoMap,scenarioInfoMap,performanceInfoMap); + TestPlanReportExecuteCatch.addApiTestPlanExecuteInfo(testPlanReportID, apiCaseInfoMap, scenarioInfoMap, performanceInfoMap); testPlanReport.setPrincipal(testPlan.getPrincipal()); if (testPlanReport.getIsScenarioExecuting() || testPlanReport.getIsApiCaseExecuting() || testPlanReport.getIsPerformanceExecuting()) { @@ -510,7 +512,7 @@ public class TestPlanReportService { return returnDTO; } - public TestPlanReport updateReport(TestPlanReport testPlanReport,TestPlanReportContentWithBLOBs reportContent,boolean updateTime,TestPlanExecuteInfo executeInfo) { + public TestPlanReport updateReport(TestPlanReport testPlanReport, TestPlanReportContentWithBLOBs reportContent, boolean updateTime, TestPlanExecuteInfo executeInfo) { if (testPlanReport == null || executeInfo == null) { return null; @@ -538,7 +540,7 @@ public class TestPlanReportService { testPlanReport.setIsPerformanceExecuting(false); } - if(apiCaseIsOk && scenarioIsOk && performanceIsOk){ + if (apiCaseIsOk && scenarioIsOk && performanceIsOk) { TestPlanReportExecuteCatch.remove(testPlanReport.getId()); } @@ -560,7 +562,7 @@ public class TestPlanReportService { Map> testPlanExecuteResult = executeInfo.getExecutedResult(); testPlanLog.info("ReportId[" + testPlanReport.getId() + "] COUNT OVER. COUNT RESULT :" + JSONObject.toJSONString(testPlanExecuteResult)); - TestPlanSimpleReportDTO reportDTO = testPlanService.buildPlanReport(executeInfo, testPlanReport.getTestPlanId(),false); + TestPlanSimpleReportDTO reportDTO = testPlanService.buildPlanReport(executeInfo, testPlanReport.getTestPlanId(), false); //更新执行时间 reportDTO.setStartTime(testPlanReport.getStartTime()); reportDTO.setEndTime(System.currentTimeMillis()); @@ -762,7 +764,7 @@ public class TestPlanReportService { } if (testPlan != null && StringUtils.equalsAny(report.getTriggerMode(), ReportTriggerMode.API.name(), ReportTriggerMode.SCHEDULE.name())) { //发送通知 - sendMessage(report,testPlan.getProjectId()); + sendMessage(report, testPlan.getProjectId()); } } catch (Exception e) { @@ -774,7 +776,7 @@ public class TestPlanReportService { return report; } - public void sendMessage(TestPlanReport testPlanReport,String projectId) { + public void sendMessage(TestPlanReport testPlanReport, String projectId) { TestPlan testPlan = testPlanMapper.selectByPrimaryKey(testPlanReport.getTestPlanId()); assert testPlan != null; SystemParameterService systemParameterService = CommonBeanFactory.getBean(SystemParameterService.class); @@ -803,12 +805,11 @@ public class TestPlanReportService { } else { event = NoticeConstants.Event.EXECUTE_SUCCESSFUL; } - Map paramMap = new HashMap<>(); - paramMap.put("testName", testPlan.getName()); - paramMap.put("id", testPlanReport.getId()); + Map paramMap = new HashMap(); paramMap.put("type", "testPlan"); paramMap.put("url", url); - paramMap.put("status", testPlanReport.getStatus()); + paramMap.put("projectId", projectId); + paramMap.putAll(new BeanMap(testPlanReport)); String successfulMailTemplate = ""; String errfoMailTemplate = ""; @@ -831,8 +832,8 @@ public class TestPlanReportService { .paramMap(paramMap) .build(); // noticeSendService.send(testPlanReport.getTriggerMode(), noticeModel); - Organization organization = projectService.getOrganizationByProjectId(projectId); - noticeSendService.send(organization, testPlanReport.getTriggerMode(), noticeModel); +// Organization organization = projectService.getOrganizationByProjectId(projectId); + noticeSendService.send(testPlanReport.getTriggerMode(), noticeModel); } public TestPlanReport getTestPlanReport(String planId) { @@ -851,7 +852,7 @@ public class TestPlanReportService { * 同时如果执行过程中报告删除,那么此时也应当记为失败。 */ Map finishLoadTestId = new HashMap<>(); - Map caseReportMap = new HashMap<>(); + Map caseReportMap = new HashMap<>(); executorService.submit(() -> { //错误数据检查集合。 如果错误数据出现超过20次,则取消该条数据的检查 Map errorDataCheckMap = new HashMap<>(); @@ -906,8 +907,8 @@ public class TestPlanReportService { this.updatePerformanceTestStatus(eventDTO); } } - TestPlanReportExecuteCatch.updateApiTestPlanExecuteInfo(testPlanReport.getId(),null,null,finishLoadTestId); - TestPlanReportExecuteCatch.updateTestPlanExecuteResultInfo(testPlanReport.getId(),null,null,caseReportMap); + TestPlanReportExecuteCatch.updateApiTestPlanExecuteInfo(testPlanReport.getId(), null, null, finishLoadTestId); + TestPlanReportExecuteCatch.updateTestPlanExecuteResultInfo(testPlanReport.getId(), null, null, caseReportMap); } else { try { //查询定时任务是否关闭 @@ -1006,9 +1007,9 @@ public class TestPlanReportService { TestPlanExecuteInfo executeInfo = TestPlanReportExecuteCatch.getTestPlanExecuteInfo(planReportId); - Map executeApiCaseIdMap = executeInfo.getApiCaseExecInfo(); - Map executeScenarioCaseIdMap = executeInfo.getApiScenarioCaseExecInfo(); - Map executePerformanceIdMap = executeInfo.getLoadCaseExecInfo(); + Map executeApiCaseIdMap = executeInfo.getApiCaseExecInfo(); + Map executeScenarioCaseIdMap = executeInfo.getApiScenarioCaseExecInfo(); + Map executePerformanceIdMap = executeInfo.getLoadCaseExecInfo(); if (executeApiCaseIdMap == null) { executeApiCaseIdMap = new HashMap<>(); } @@ -1029,12 +1030,12 @@ public class TestPlanReportService { TestPlanReport report = null; if (!reportDataList.isEmpty()) { - TestPlanReportExecuteCatch.setReportDataCheckResult(planReportId,true); + TestPlanReportExecuteCatch.setReportDataCheckResult(planReportId, true); TestPlanReportContentWithBLOBs reportData = reportDataList.get(0); report = testPlanReportMapper.selectByPrimaryKey(planReportId); - report = this.updateReport(report,reportData,updateTime,executeInfo); + report = this.updateReport(report, reportData, updateTime, executeInfo); } else { - TestPlanReportExecuteCatch.setReportDataCheckResult(planReportId,false); + TestPlanReportExecuteCatch.setReportDataCheckResult(planReportId, false); } return report; @@ -1045,7 +1046,7 @@ public class TestPlanReportService { example.createCriteria().andTestPlanIdEqualTo(planId); List reportList = this.testPlanReportMapper.selectByExample(example); List testPlanReportIdList = new ArrayList<>(); - for (TestPlanReport report:reportList) { + for (TestPlanReport report : reportList) { testPlanReportIdList.add(report.getId()); } this.delete(testPlanReportIdList); @@ -1054,14 +1055,14 @@ public class TestPlanReportService { public void countReport(String planReportId) { TestPlanExecuteInfo executeInfo = TestPlanReportExecuteCatch.getTestPlanExecuteInfo(planReportId); int unFinishNum = executeInfo.countUnFinishedNum(); - if(unFinishNum > 0){ + if (unFinishNum > 0) { //如果间隔超过5分钟没有案例执行完成,则把执行结果变成false - long lastCountTime = executeInfo.getLastFinishedNumCountTime(); - long nowTime = System.currentTimeMillis(); - if(nowTime - lastCountTime > 300000){ - TestPlanReportExecuteCatch.finishAllTask(planReportId); + long lastCountTime = executeInfo.getLastFinishedNumCountTime(); + long nowTime = System.currentTimeMillis(); + if (nowTime - lastCountTime > 300000) { + TestPlanReportExecuteCatch.finishAllTask(planReportId); + } } - } this.updateExecuteApis(planReportId); } diff --git a/frontend/src/business/components/notice/components/MentionedMeData.vue b/frontend/src/business/components/notice/components/MentionedMeData.vue index baf90b3d53..f33a6ef6bd 100644 --- a/frontend/src/business/components/notice/components/MentionedMeData.vue +++ b/frontend/src/business/components/notice/components/MentionedMeData.vue @@ -82,7 +82,7 @@ export default { this.totalCount = response.data.itemCount; this.systemNoticeData.forEach(n => { - n.user = this.userMap[n.operator]; + n.user = this.userMap[n.operator] || {name: "MS"}; }); }); }, diff --git a/frontend/src/business/components/notice/components/SystemNoticeData.vue b/frontend/src/business/components/notice/components/SystemNoticeData.vue index 41d6c64828..0e2eb1316a 100644 --- a/frontend/src/business/components/notice/components/SystemNoticeData.vue +++ b/frontend/src/business/components/notice/components/SystemNoticeData.vue @@ -81,7 +81,7 @@ export default { this.totalPage = response.data.pageCount; this.totalCount = response.data.itemCount; this.systemNoticeData.forEach(n => { - n.user = this.userMap[n.operator]; + n.user = this.userMap[n.operator] || {name: "MS"}; }); }); },