diff --git a/backend/src/main/java/io/metersphere/api/jmeter/APIBackendListenerClient.java b/backend/src/main/java/io/metersphere/api/jmeter/APIBackendListenerClient.java index 2c770f6f09..09aaa6847b 100644 --- a/backend/src/main/java/io/metersphere/api/jmeter/APIBackendListenerClient.java +++ b/backend/src/main/java/io/metersphere/api/jmeter/APIBackendListenerClient.java @@ -278,11 +278,12 @@ public class APIBackendListenerClient extends AbstractBackendListenerClient impl reportTask.setExecutor(userName); reportTask.setExecutionTime(executionTime); reportTask.setExecutionEnvironment(name); - } else if (StringUtils.equalsAny(this.runMode, ApiRunMode.API_PLAN.name(), ApiRunMode.SCHEDULE_API_PLAN.name())) { + //用例,定时,jenkins + } else if (StringUtils.equalsAny(this.runMode, ApiRunMode.API_PLAN.name(), ApiRunMode.SCHEDULE_API_PLAN.name(), ApiRunMode.JENKINS_API_PLAN.name())) { apiDefinitionService.addResult(testResult); //测试计划定时任务-接口执行逻辑的话,需要同步测试计划的报告数据 - if (StringUtils.equalsAny(this.runMode, ApiRunMode.SCHEDULE_API_PLAN.name(), ApiRunMode.JENKINS_API_PLAN.name())) { + if (StringUtils.equals(this.runMode, ApiRunMode.SCHEDULE_API_PLAN.name())) { apiDefinitionExecResultService.saveApiResultByScheduleTask(testResult, ApiRunMode.SCHEDULE_API_PLAN.name()); List testPlanReportIdList = new ArrayList<>(); testPlanReportIdList.add(debugReportId); @@ -290,17 +291,25 @@ public class APIBackendListenerClient extends AbstractBackendListenerClient impl testPlanReportService.checkTestPlanStatus(testPlanReportId); } testPlanReportService.updateReport(testPlanReportIdList, ApiRunMode.SCHEDULE_API_PLAN.name(), ReportTriggerMode.SCHEDULE.name()); + } else if (StringUtils.equals(this.runMode, ApiRunMode.JENKINS_API_PLAN.name())) { + apiDefinitionExecResultService.saveApiResult(testResult, ApiRunMode.JENKINS_API_PLAN.name(), TriggerMode.API.name()); + List testPlanReportIdList = new ArrayList<>(); + testPlanReportIdList.add(debugReportId); + for (String testPlanReportId : testPlanReportIdList) { // 更新每个测试计划的状态 + testPlanReportService.checkTestPlanStatus(testPlanReportId); + } + testPlanReportService.updateReport(testPlanReportIdList, ApiRunMode.JENKINS_API_PLAN.name(), TriggerMode.API.name()); } else { apiDefinitionExecResultService.saveApiResult(testResult, ApiRunMode.API_PLAN.name(), TriggerMode.MANUAL.name()); } - } else if (StringUtils.equalsAny(this.runMode, ApiRunMode.SCENARIO.name(), ApiRunMode.SCENARIO_PLAN.name(), ApiRunMode.SCHEDULE_SCENARIO_PLAN.name(), ApiRunMode.SCHEDULE_SCENARIO.name())) { + } else if (StringUtils.equalsAny(this.runMode, ApiRunMode.SCENARIO.name(), ApiRunMode.SCENARIO_PLAN.name(), ApiRunMode.SCHEDULE_SCENARIO_PLAN.name(), ApiRunMode.SCHEDULE_SCENARIO.name(), ApiRunMode.JENKINS_SCENARIO_PLAN.name())) { // 执行报告不需要存储,由用户确认后在存储 testResult.setTestId(testId); ApiScenarioReport scenarioReport = apiScenarioReportService.complete(testResult, this.runMode); //环境 ApiScenarioWithBLOBs apiScenario = apiAutomationService.getDto(scenarioReport.getScenarioId()); String name = ""; - if(apiScenario!= null ) { + if (apiScenario != null) { String executionEnvironment = apiScenario.getScenarioDefinition(); JSONObject json = JSONObject.parseObject(executionEnvironment); if (json != null && json.getString("environmentMap") != null && json.getString("environmentMap").length() > 2) { diff --git a/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java b/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java index 957b61d86e..d8d24cfcde 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java @@ -916,7 +916,7 @@ public class ApiAutomationService { APIScenarioReportResult report; Map planEnvMap = new HashMap<>(); //如果是测试计划页面触发的执行方式,生成报告时createScenarioReport第二个参数需要特殊处理 - if (StringUtils.equalsAny(request.getRunMode(), ApiRunMode.SCENARIO_PLAN.name(), ApiRunMode.SCHEDULE_SCENARIO_PLAN.name())) { + if (StringUtils.equalsAny(request.getRunMode(), ApiRunMode.SCENARIO_PLAN.name(), ApiRunMode.SCHEDULE_SCENARIO_PLAN.name(), ApiRunMode.JENKINS_SCENARIO_PLAN.name())) { String testPlanScenarioId = item.getId(); if (request.getScenarioTestPlanIdMap() != null && request.getScenarioTestPlanIdMap().containsKey(item.getId())) { testPlanScenarioId = request.getScenarioTestPlanIdMap().get(item.getId()); @@ -1091,7 +1091,7 @@ public class ApiAutomationService { if (reportIds != null) { //如果是测试计划页面触发的执行方式,生成报告时createScenarioReport第二个参数需要特殊处理 APIScenarioReportResult report = null; - if (StringUtils.equalsAny(request.getRunMode(), ApiRunMode.SCENARIO_PLAN.name(), ApiRunMode.SCHEDULE_SCENARIO_PLAN.name())) { + if (StringUtils.equalsAny(request.getRunMode(), ApiRunMode.SCENARIO_PLAN.name(), ApiRunMode.SCHEDULE_SCENARIO_PLAN.name(), ApiRunMode.JENKINS_SCENARIO_PLAN.name())) { String testPlanScenarioId = item.getId(); if (request.getScenarioTestPlanIdMap() != null && request.getScenarioTestPlanIdMap().containsKey(item.getId())) { testPlanScenarioId = request.getScenarioTestPlanIdMap().get(item.getId()); diff --git a/backend/src/main/java/io/metersphere/api/service/ApiScenarioReportService.java b/backend/src/main/java/io/metersphere/api/service/ApiScenarioReportService.java index 7b15d4e710..f028bf3a78 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiScenarioReportService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiScenarioReportService.java @@ -66,7 +66,7 @@ public class ApiScenarioReportService { if (result != null) { if (StringUtils.equals(runMode, ApiRunMode.SCENARIO_PLAN.name())) { return updatePlanCase(result, runMode); - } else if (StringUtils.equals(runMode, ApiRunMode.SCHEDULE_SCENARIO_PLAN.name())) { + } else if (StringUtils.equalsAny(runMode, ApiRunMode.SCHEDULE_SCENARIO_PLAN.name(), ApiRunMode.JENKINS_SCENARIO_PLAN.name())) { return updateSchedulePlanCase(result, runMode); } else { updateScenarioStatus(result.getTestId()); @@ -319,7 +319,7 @@ public class ApiScenarioReportService { // margeReport(result, scenarioIds, scenarioNames, runMode, projectId, userId, reportIds); TestPlanReportService testPlanReportService = CommonBeanFactory.getBean(TestPlanReportService.class); - testPlanReportService.updateReport(testPlanReportIdList, ApiRunMode.SCHEDULE_SCENARIO_PLAN.name(), ReportTriggerMode.SCHEDULE.name()); + testPlanReportService.updateReport(testPlanReportIdList, runMode, lastReport.getTriggerMode()); return lastReport; } diff --git a/backend/src/main/java/io/metersphere/commons/constants/ApiRunMode.java b/backend/src/main/java/io/metersphere/commons/constants/ApiRunMode.java index 15283c30ef..6a62390a97 100644 --- a/backend/src/main/java/io/metersphere/commons/constants/ApiRunMode.java +++ b/backend/src/main/java/io/metersphere/commons/constants/ApiRunMode.java @@ -1,5 +1,6 @@ package io.metersphere.commons.constants; public enum ApiRunMode { - RUN, DEBUG, DEFINITION, SCENARIO, API_PLAN, JENKINS_API_PLAN, JENKINS, SCENARIO_PLAN, API, SCHEDULE_API_PLAN, SCHEDULE_SCENARIO,SCHEDULE_SCENARIO_PLAN, SCHEDULE_PERFORMANCE_TEST + RUN, DEBUG, DEFINITION, SCENARIO, API_PLAN, JENKINS_API_PLAN, JENKINS_SCENARIO_PLAN, JENKINS_PERFORMANCE_TEST, JENKINS, + SCENARIO_PLAN, API, SCHEDULE_API_PLAN, SCHEDULE_SCENARIO, SCHEDULE_SCENARIO_PLAN, SCHEDULE_PERFORMANCE_TEST } diff --git a/backend/src/main/java/io/metersphere/commons/constants/ReportTriggerMode.java b/backend/src/main/java/io/metersphere/commons/constants/ReportTriggerMode.java index a202ba4ec4..d417e4a393 100644 --- a/backend/src/main/java/io/metersphere/commons/constants/ReportTriggerMode.java +++ b/backend/src/main/java/io/metersphere/commons/constants/ReportTriggerMode.java @@ -8,5 +8,6 @@ public enum ReportTriggerMode { * 性能测试用例执行触发报告 */ CASE, - TEST_PLAN_SCHEDULE + TEST_PLAN_SCHEDULE, + TEST_PLAN_API, } diff --git a/backend/src/main/java/io/metersphere/track/controller/TestPlanController.java b/backend/src/main/java/io/metersphere/track/controller/TestPlanController.java index be95796302..f9bd32b13b 100644 --- a/backend/src/main/java/io/metersphere/track/controller/TestPlanController.java +++ b/backend/src/main/java/io/metersphere/track/controller/TestPlanController.java @@ -157,6 +157,8 @@ public class TestPlanController { ApiRunConfigDTO api = new ApiRunConfigDTO(); api.setMode(testplanRunRequest.getMode()); api.setResourcePoolId(testplanRunRequest.getResourcePoolId()); + api.setOnSampleError(true); + api.setReportType("iddReport"); String apiRunConfig = JSONObject.toJSONString(api); testPlanService.run(testplanRunRequest.getTestPlanId(), testplanRunRequest.getProjectId(), testplanRunRequest.getUserId(), testplanRunRequest.getTriggerMode(), apiRunConfig); } diff --git a/backend/src/main/java/io/metersphere/track/controller/TestPlanReportController.java b/backend/src/main/java/io/metersphere/track/controller/TestPlanReportController.java index a7e81995c2..3a3c8b5f53 100644 --- a/backend/src/main/java/io/metersphere/track/controller/TestPlanReportController.java +++ b/backend/src/main/java/io/metersphere/track/controller/TestPlanReportController.java @@ -44,7 +44,14 @@ public class TestPlanReportController { public String sendTask(@PathVariable String planId) { TestPlanReport report = testPlanReportService.getTestPlanReport(planId); testPlanReportService.update(report); - return "sucess"; + return "sucess"; + } + + @GetMapping("/status/{planId}") + public String getStatus(@PathVariable String planId) { + TestPlanReport report = testPlanReportService.getTestPlanReport(planId); + String status = report.getStatus(); + return status; } @PostMapping("/delete") diff --git a/backend/src/main/java/io/metersphere/track/dto/ApiRunConfigDTO.java b/backend/src/main/java/io/metersphere/track/dto/ApiRunConfigDTO.java index 71881187fb..f97166c6c4 100644 --- a/backend/src/main/java/io/metersphere/track/dto/ApiRunConfigDTO.java +++ b/backend/src/main/java/io/metersphere/track/dto/ApiRunConfigDTO.java @@ -6,7 +6,7 @@ import lombok.Data; public class ApiRunConfigDTO { private String mode; private String reportType; - private String onSampleError; + private boolean onSampleError; private String runWithinResourcePool; private String resourcePoolId; } 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 3d0370fccf..99ffbad84c 100644 --- a/backend/src/main/java/io/metersphere/track/service/TestPlanReportService.java +++ b/backend/src/main/java/io/metersphere/track/service/TestPlanReportService.java @@ -168,7 +168,6 @@ public class TestPlanReportService { //更新TestPlan状态,改为进行中 testPlan.setStatus(TestPlanStatus.Underway.name()); testPlanMapper.updateByPrimaryKeySelective(testPlan); - return testPlanReport; } @@ -243,8 +242,7 @@ public class TestPlanReportService { String issuesInfo = null; //因为接口案例的定时任务是单个案例开线程运行, 所以要检查是否都执行完成。全部执行完成时才会进行统一整理 - if (StringUtils.equals(ReportTriggerMode.SCHEDULE.name(), triggerMode) - && StringUtils.equalsAny(resourceRunMode, ApiRunMode.SCHEDULE_API_PLAN.name())) { + if (StringUtils.equalsAny(triggerMode, ReportTriggerMode.SCHEDULE.name(), ReportTriggerMode.API.name()) && StringUtils.equalsAny(resourceRunMode, ApiRunMode.SCHEDULE_API_PLAN.name(), ApiRunMode.JENKINS_API_PLAN.name())) { List statusList = extTestPlanApiCaseMapper.getStatusByTestPlanId(testPlan.getId()); for (String status : statusList) { if (status == null) { @@ -280,14 +278,14 @@ public class TestPlanReportService { //只针对定时任务做处理 - if (StringUtils.equals(ReportTriggerMode.SCHEDULE.name(), triggerMode) - && StringUtils.equals(resourceRunMode, ApiRunMode.SCHEDULE_API_PLAN.name())) { + if (StringUtils.equalsAny(triggerMode, ReportTriggerMode.SCHEDULE.name(), ReportTriggerMode.API.name()) + && StringUtils.equalsAny(resourceRunMode, ApiRunMode.SCHEDULE_API_PLAN.name(), ApiRunMode.JENKINS_API_PLAN.name())) { testPlanReport.setIsApiCaseExecuting(false); - } else if (StringUtils.equals(ReportTriggerMode.SCHEDULE.name(), triggerMode) - && StringUtils.equals(resourceRunMode, ApiRunMode.SCHEDULE_SCENARIO_PLAN.name())) { + } else if (StringUtils.equalsAny(triggerMode, ReportTriggerMode.SCHEDULE.name(), ReportTriggerMode.API.name()) + && StringUtils.equalsAny(resourceRunMode, ApiRunMode.SCHEDULE_SCENARIO_PLAN.name(), ApiRunMode.SCHEDULE_SCENARIO_PLAN.name())) { testPlanReport.setIsScenarioExecuting(false); - } else if (StringUtils.equals(ReportTriggerMode.SCHEDULE.name(), triggerMode) - && StringUtils.equals(resourceRunMode, ApiRunMode.SCHEDULE_PERFORMANCE_TEST.name())) { + } else if (StringUtils.equalsAny(triggerMode, ReportTriggerMode.SCHEDULE.name(), ReportTriggerMode.API.name()) + && StringUtils.equalsAny(resourceRunMode, ApiRunMode.SCHEDULE_PERFORMANCE_TEST.name(), ApiRunMode.JENKINS_PERFORMANCE_TEST.name())) { testPlanReport.setIsPerformanceExecuting(false); } else { testPlanReport.setIsPerformanceExecuting(false); @@ -299,8 +297,8 @@ public class TestPlanReportService { component.afterBuild(testCaseReportMetricDTO); }); - if (StringUtils.equals(ReportTriggerMode.SCHEDULE.name(), triggerMode) - && StringUtils.equals(resourceRunMode, ApiRunMode.SCHEDULE_PERFORMANCE_TEST.name())) { + if (StringUtils.equalsAny(triggerMode, ReportTriggerMode.SCHEDULE.name(), ReportTriggerMode.API.name()) + && StringUtils.equalsAny(resourceRunMode, ApiRunMode.SCHEDULE_PERFORMANCE_TEST.name(), ApiRunMode.JENKINS_PERFORMANCE_TEST.name())) { //如果是性能测试作为触发,由于延迟原因可能会出现报告已经结束但是状态还是进行中的状态 List loadResult = testCaseReportMetricDTO.getExecuteResult().getLoadResult(); for (TestCaseReportStatusResultDTO dto : loadResult) { @@ -381,17 +379,16 @@ public class TestPlanReportService { testPlan.setStatus(TestPlanStatus.Completed.name()); testPlanMapper.updateByPrimaryKeySelective(testPlan); } - - if (StringUtils.equals(report.getTriggerMode(), ReportTriggerMode.API.name()) || StringUtils.equals(report.getTriggerMode(), ReportTriggerMode.SCHEDULE.name())) { + if (StringUtils.equalsAny(report.getTriggerMode(), ReportTriggerMode.API.name(), ReportTriggerMode.SCHEDULE.name())) { //发送通知 sendMessage(report); } - } catch (Exception e) { } } else { } + testPlanReportMapper.updateByPrimaryKey(report); } @@ -408,10 +405,16 @@ public class TestPlanReportService { String failedContext = ""; String subject = ""; String event = ""; + if (StringUtils.equals(testPlanReport.getTriggerMode(), ReportTriggerMode.API.name())) { + successContext = "测试计划jenkins任务通知:'" + testPlan.getName() + "'执行成功" + "\n" + "请点击下面链接进入测试报告页面" + "\n" + url; + failedContext = "测试计划jenkins任务通知:'" + testPlan.getName() + "'执行失败" + "\n" + "请点击下面链接进入测试报告页面" + "\n" + url; + subject = Translator.get("task_notification_jenkins"); + } else { + successContext = "测试计划定时任务通知:'" + testPlan.getName() + "'执行成功" + "\n" + "请点击下面链接进入测试报告页面" + "\n" + url; + failedContext = "测试计划定时任务通知:'" + testPlan.getName() + "'执行失败" + "\n" + "请点击下面链接进入测试报告页面" + "\n" + url; + subject = Translator.get("task_notification"); + } - successContext = "测试计划定时任务通知:'" + testPlan.getName() + "'执行成功" + "\n" + "请点击下面链接进入测试报告页面" + "\n" + url; - failedContext = "测试计划定时任务通知:'" + testPlan.getName() + "'执行失败" + "\n" + "请点击下面链接进入测试报告页面" + "\n" + url; - subject = Translator.get("task_notification"); if (StringUtils.equals(TestPlanReportStatus.FAILED.name(), testPlanReport.getStatus())) { event = NoticeConstants.Event.EXECUTE_FAILED; @@ -428,7 +431,7 @@ public class TestPlanReportService { String successfulMailTemplate = ""; String errfoMailTemplate = ""; - if (StringUtils.equals(testPlanReport.getTriggerMode(), ReportTriggerMode.SCHEDULE.name())) { + if (StringUtils.equalsAny(testPlanReport.getTriggerMode(), ReportTriggerMode.SCHEDULE.name(), ReportTriggerMode.API.name())) { successfulMailTemplate = "TestPlanSuccessfulNotification"; errfoMailTemplate = "TestPlanFailedNotification"; } @@ -498,7 +501,7 @@ public class TestPlanReportService { for (String string : updatePerformaneReportIDList) { TestPlanLoadCaseEventDTO eventDTO = new TestPlanLoadCaseEventDTO(); eventDTO.setReportId(string); - eventDTO.setTriggerMode(ReportTriggerMode.SCHEDULE.name()); + eventDTO.setTriggerMode(triggerMode); eventDTO.setStatus(PerformanceTestStatus.Completed.name()); this.updatePerformanceTestStatus(eventDTO); } @@ -516,7 +519,11 @@ public class TestPlanReportService { public void updatePerformanceTestStatus(TestPlanLoadCaseEventDTO eventDTO) { List testPlanReportId = extTestPlanMapper.findIdByPerformanceReportId(eventDTO.getReportId()); - this.updateReport(testPlanReportId, ApiRunMode.SCHEDULE_PERFORMANCE_TEST.name(), eventDTO.getTriggerMode()); + if (StringUtils.equals(eventDTO.getTriggerMode(), ReportTriggerMode.SCHEDULE.name())) { + this.updateReport(testPlanReportId, ApiRunMode.SCHEDULE_PERFORMANCE_TEST.name(), eventDTO.getTriggerMode()); + } else { + this.updateReport(testPlanReportId, ApiRunMode.JENKINS_PERFORMANCE_TEST.name(), eventDTO.getTriggerMode()); + } } public void delete(List testPlanReportIdList) { diff --git a/backend/src/main/java/io/metersphere/track/service/TestPlanService.java b/backend/src/main/java/io/metersphere/track/service/TestPlanService.java index 80d492d7f8..b04e8b9db8 100644 --- a/backend/src/main/java/io/metersphere/track/service/TestPlanService.java +++ b/backend/src/main/java/io/metersphere/track/service/TestPlanService.java @@ -1060,7 +1060,7 @@ public class TestPlanService { performanceRequest.setId(caseID); performanceRequest.setTestPlanLoadId(caseID); if (StringUtils.equals(ReportTriggerMode.API.name(), triggerMode)) { - performanceRequest.setTriggerMode(ReportTriggerMode.API.name()); + performanceRequest.setTriggerMode(ReportTriggerMode.TEST_PLAN_API.name()); } else { performanceRequest.setTriggerMode(ReportTriggerMode.TEST_PLAN_SCHEDULE.name()); } @@ -1095,7 +1095,7 @@ public class TestPlanService { if (!performaneReportIDList.isEmpty()) { //性能测试时保存性能测试报告ID,在结果返回时用于捕捉并进行 - testPlanReportService.updatePerformanceInfo(testPlanReport, performaneReportIDList, ReportTriggerMode.SCHEDULE.name()); + testPlanReportService.updatePerformanceInfo(testPlanReport, performaneReportIDList, triggerMode); } @@ -1106,7 +1106,12 @@ public class TestPlanService { String planCaseID = entry.getValue(); ApiTestCaseWithBLOBs blobs = apiTestCaseService.get(apiCaseID); //需要更新这里来保证PlanCase的状态能正常更改 - apiTestCaseService.run(blobs, UUID.randomUUID().toString(), planReportId, testPlanID, ApiRunMode.SCHEDULE_API_PLAN.name()); + if (StringUtils.equals(triggerMode, ReportTriggerMode.API.name())) { + apiTestCaseService.run(blobs, UUID.randomUUID().toString(), planReportId, testPlanID, ApiRunMode.JENKINS_API_PLAN.name()); + } else { + apiTestCaseService.run(blobs, UUID.randomUUID().toString(), planReportId, testPlanID, ApiRunMode.SCHEDULE_API_PLAN.name()); + } + apiCaseIsExcuting = true; } if (apiCaseIsExcuting) { @@ -1122,8 +1127,11 @@ public class TestPlanService { scenarioRequest.setProjectId(projectID); if (StringUtils.equals(triggerMode, ReportTriggerMode.API.name())) { scenarioRequest.setTriggerMode(ReportTriggerMode.API.name()); + scenarioRequest.setRunMode(ApiRunMode.JENKINS_SCENARIO_PLAN.name()); + } else { scenarioRequest.setTriggerMode(ReportTriggerMode.SCHEDULE.name()); + scenarioRequest.setRunMode(ApiRunMode.SCHEDULE_SCENARIO_PLAN.name()); } scenarioRequest.setExecuteType(ExecuteType.Saved.name()); Map> testPlanScenarioIdMap = new HashMap<>(); @@ -1131,7 +1139,7 @@ public class TestPlanService { scenarioRequest.setTestPlanScenarioIDMap(testPlanScenarioIdMap); scenarioRequest.setReportUserID(userId); scenarioRequest.setTestPlanID(testPlanID); - scenarioRequest.setRunMode(ApiRunMode.SCHEDULE_SCENARIO_PLAN.name()); + scenarioRequest.setTestPlanReportId(planReportId); RunModeConfig runModeConfig = JSONObject.parseObject(apiRunConfig, RunModeConfig.class); scenarioRequest.setConfig(runModeConfig);