From ae4e05a713947d1c4a97bf70c2922dd0bd9e5ec4 Mon Sep 17 00:00:00 2001 From: "Captain.B" Date: Tue, 12 Oct 2021 16:18:52 +0800 Subject: [PATCH] =?UTF-8?q?revert:=20=E6=8E=A5=E5=8F=A3=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E5=8C=96=E5=AE=9A=E6=97=B6=E4=BB=BB=E5=8A=A1=E5=92=8Capi?= =?UTF-8?q?=E8=B0=83=E7=94=A8=E5=8F=91=E9=80=81=E9=80=9A=E7=9F=A5=E5=9B=9E?= =?UTF-8?q?=E9=80=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/service/TestResultService.java | 64 ++++++++++++++++++- .../notice/service/NoticeService.java | 2 +- 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/service/TestResultService.java b/backend/src/main/java/io/metersphere/api/service/TestResultService.java index 1d6274f33f..536f800fe8 100644 --- a/backend/src/main/java/io/metersphere/api/service/TestResultService.java +++ b/backend/src/main/java/io/metersphere/api/service/TestResultService.java @@ -18,6 +18,7 @@ import io.metersphere.track.request.testcase.TrackCount; import io.metersphere.track.service.TestPlanApiCaseService; import io.metersphere.track.service.TestPlanScenarioCaseService; import io.metersphere.track.service.TestPlanTestCaseService; +import org.apache.commons.beanutils.BeanMap; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; @@ -150,7 +151,12 @@ public class TestResultService { testPlanTestCaseService.updateTestCaseStates(ids, TestPlanTestCaseStatus.Failure.name()); } } - + if (reportTask != null) { + if (StringUtils.equals(ReportTriggerMode.API.name(), reportTask.getTriggerMode()) + || StringUtils.equals(ReportTriggerMode.SCHEDULE.name(), reportTask.getTriggerMode())) { + sendTask(reportTask, reportUrl, testResult); + } + } } catch (Exception e) { e.printStackTrace(); LogUtil.error(e.getMessage(), e); @@ -189,4 +195,60 @@ public class TestResultService { } } + private static void sendTask(ApiTestReportVariable report, String reportUrl, TestResult testResult) { + if (report == null) { + return; + } + SystemParameterService systemParameterService = CommonBeanFactory.getBean(SystemParameterService.class); + NoticeSendService noticeSendService = CommonBeanFactory.getBean(NoticeSendService.class); + assert systemParameterService != null; + assert noticeSendService != null; + BaseSystemConfigDTO baseSystemConfigDTO = systemParameterService.getBaseInfo(); + String url = baseSystemConfigDTO.getUrl() + "/#/api/report/view/" + report.getId(); + String url2 = baseSystemConfigDTO.getUrl() + "/#/api/automation/report/view/" + report.getId(); + + String successContext = ""; + String failedContext = ""; + String subject = ""; + String event = ""; + if (StringUtils.equals(ReportTriggerMode.API.name(), report.getTriggerMode())) { + successContext = "接口测试 API任务通知:jenkins所执行的" + report.getName() + "'执行成功" + "\n" + "执行环境:" + report.getExecutionEnvironment() + "\n" + "[接口定义暂无报告链接]" + "\n" + "请点击下面链接进入测试报告页面" + "\n" + "(旧版)接口测试路径" + url + "\n" + "(新版)接口测试路径" + url2; + failedContext = "接口测试 API任务通知:jenkins所执行的" + report.getName() + "'执行失败" + "\n" + "执行环境:" + report.getExecutionEnvironment() + "\n" + "[接口定义暂无报告链接]" + "\n" + "请点击下面链接进入测试报告页面" + "\n" + "(旧版)接口测试路径" + url + "\n" + "(新版)接口测试路径" + url2; + subject = Translator.get("task_notification_jenkins"); + } + if (StringUtils.equals(ReportTriggerMode.SCHEDULE.name(), report.getTriggerMode())) { + successContext = "接口测试定时任务通知:定时任务所执行的" + report.getName() + "'执行成功" + "\n" + "执行环境:" + report.getExecutionEnvironment() + "\n" + "[接口定义暂无报告链接]" + "\n" + "请点击下面链接进入测试报告页面" + "\n" + "(旧版)接口测试路径" + url + "\n" + "(新版)接口测试路径" + url2; + failedContext = "接口测试定时任务通知:定时任务所执行的" + report.getName() + "'执行失败" + "\n" + "执行环境:" + report.getExecutionEnvironment() + "\n" + "[接口定义暂无报告链接]" + "\n" + "请点击下面链接进入测试报告页面" + "\n" + "(旧版)接口测试路径" + url + "\n" + "(新版)接口测试路径" + url2; + subject = Translator.get("task_notification"); + } + if (StringUtils.equals("Success", report.getStatus())) { + event = NoticeConstants.Event.EXECUTE_SUCCESSFUL; + } + if (StringUtils.equals("success", report.getStatus())) { + event = NoticeConstants.Event.EXECUTE_SUCCESSFUL; + } + if (StringUtils.equals("Error", report.getStatus())) { + event = NoticeConstants.Event.EXECUTE_FAILED; + } + if (StringUtils.equals("error", report.getStatus())) { + event = NoticeConstants.Event.EXECUTE_FAILED; + } + Map paramMap = new HashMap<>(); + paramMap.put("type", "api"); + paramMap.put("url", baseSystemConfigDTO.getUrl()); + paramMap.putAll(new BeanMap(report)); + NoticeModel noticeModel = NoticeModel.builder() + .operator(SessionUtils.getUserId()) + .successContext(successContext) + .successMailTemplate("ApiSuccessfulNotification") + .failedContext(failedContext) + .failedMailTemplate("ApiFailedNotification") + .testId(testResult.getTestId()) + .status(report.getStatus()) + .event(event) + .subject(subject) + .paramMap(paramMap) + .build(); + noticeSendService.send(report.getTriggerMode(), NoticeConstants.TaskType.API_DEFINITION_TASK, noticeModel); + } } 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 3b17088b5a..231e0fd2ad 100644 --- a/backend/src/main/java/io/metersphere/notice/service/NoticeService.java +++ b/backend/src/main/java/io/metersphere/notice/service/NoticeService.java @@ -134,7 +134,7 @@ public class NoticeService { public List searchMessageByTypeBySend(String type, String projectId) { try { String orgId = ""; - if (StringUtils.isNotEmpty(projectId)) { + if (null == SessionUtils.getUser()) { Organization organization = extProjectMapper.getOrganizationByProjectId(projectId); orgId = organization.getId(); } else {