From c0d40b1e761f79d4f7a0ccb387e05cb11d1419ff Mon Sep 17 00:00:00 2001 From: song-tianyang Date: Fri, 26 Nov 2021 11:27:55 +0800 Subject: [PATCH] =?UTF-8?q?fix(Jenkins=E6=89=A7=E8=A1=8C):=20=E4=BF=AE?= =?UTF-8?q?=E5=A4=8DJenkins=E8=B0=83=E7=94=A8=E6=8E=A5=E5=8F=A3=E6=89=A7?= =?UTF-8?q?=E8=A1=8C=E6=97=B6=E9=80=9A=E7=9F=A5=E4=B8=AD=E5=BF=83=E6=B2=A1?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复Jenkins调用接口执行时通知中心没数据的问题 --- .../api/controller/ApiTestCaseController.java | 2 +- .../api/service/ApiTestCaseService.java | 50 +++++++++++++++---- 2 files changed, 40 insertions(+), 12 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/controller/ApiTestCaseController.java b/backend/src/main/java/io/metersphere/api/controller/ApiTestCaseController.java index 9580937882..689588aa63 100644 --- a/backend/src/main/java/io/metersphere/api/controller/ApiTestCaseController.java +++ b/backend/src/main/java/io/metersphere/api/controller/ApiTestCaseController.java @@ -196,7 +196,7 @@ public class ApiTestCaseController { @PostMapping(value = "/jenkins/run") @MsAuditLog(module = "api_definition_case", type = OperLogConstants.EXECUTE, content = "#msClass.getLogDetails(#request.caseId)", msClass = ApiTestCaseService.class) public String jenkinsRun(@RequestBody RunCaseRequest request) { - return apiTestCaseService.run(request); + return apiTestCaseService.jenkinsRun(request); } @GetMapping(value = "/jenkins/exec/result/{id}") diff --git a/backend/src/main/java/io/metersphere/api/service/ApiTestCaseService.java b/backend/src/main/java/io/metersphere/api/service/ApiTestCaseService.java index f03862c509..423d0260c6 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiTestCaseService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiTestCaseService.java @@ -736,16 +736,20 @@ public class ApiTestCaseService { return ids; } - private ApiDefinitionExecResult addResult(String id, String status) { + private ApiDefinitionExecResult addResult(String resourceId, String status, String reportId) { ApiDefinitionExecResult apiResult = new ApiDefinitionExecResult(); - apiResult.setId(UUID.randomUUID().toString()); + if (StringUtils.isEmpty(reportId)) { + apiResult.setId(UUID.randomUUID().toString()); + } else { + apiResult.setId(reportId); + } apiResult.setCreateTime(System.currentTimeMillis()); apiResult.setStartTime(System.currentTimeMillis()); apiResult.setEndTime(System.currentTimeMillis()); apiResult.setTriggerMode(TriggerMode.BATCH.name()); apiResult.setActuator("LOCAL"); apiResult.setUserId(Objects.requireNonNull(SessionUtils.getUser()).getId()); - apiResult.setResourceId(id); + apiResult.setResourceId(resourceId); apiResult.setStartTime(System.currentTimeMillis()); apiResult.setType(ApiRunMode.DEFINITION.name()); apiResult.setStatus(status); @@ -765,7 +769,7 @@ public class ApiTestCaseService { ApiTestCaseMapper sqlSessionMapper = sqlSession.getMapper(ApiTestCaseMapper.class); for (ApiTestCaseWithBLOBs caseWithBLOBs : list) { - ApiDefinitionExecResult report = addResult(caseWithBLOBs.getId(), APITestStatus.Running.name()); + ApiDefinitionExecResult report = addResult(caseWithBLOBs.getId(), APITestStatus.Running.name(), null); report.setName(caseWithBLOBs.getName()); caseWithBLOBs.setLastResultId(report.getId()); caseWithBLOBs.setUpdateTime(System.currentTimeMillis()); @@ -792,6 +796,35 @@ public class ApiTestCaseService { } } + public String jenkinsRun(RunCaseRequest request) { + ApiTestCaseWithBLOBs caseWithBLOBs = null; + if (request.getBloBs() == null) { + caseWithBLOBs = apiTestCaseMapper.selectByPrimaryKey(request.getCaseId()); + if (caseWithBLOBs == null) { + return null; + } + request.setBloBs(caseWithBLOBs); + } else { + caseWithBLOBs = request.getBloBs(); + } + if (caseWithBLOBs == null) { + return null; + } + //提前生成报告 + ApiDefinitionExecResult report = addResult(caseWithBLOBs.getId(), APITestStatus.Running.name(), request.getReportId()); + report.setName(caseWithBLOBs.getName()); + report.setTriggerMode(ApiRunMode.API.name()); + report.setType(ApiRunMode.JENKINS.name()); + apiDefinitionExecResultMapper.insert(report); + //更新接口案例的最后执行状态等信息 + caseWithBLOBs.setLastResultId(report.getId()); + caseWithBLOBs.setUpdateTime(System.currentTimeMillis()); + caseWithBLOBs.setStatus(APITestStatus.Running.name()); + apiTestCaseMapper.updateByPrimaryKey(caseWithBLOBs); + request.setReport(report); + return this.run(request); + } + public String run(RunCaseRequest request) { ApiTestCaseWithBLOBs testCaseWithBLOBs = request.getBloBs(); if (StringUtils.equals(request.getRunMode(), ApiRunMode.JENKINS_API_PLAN.name())) { @@ -801,17 +834,12 @@ public class ApiTestCaseService { request.setReportId(request.getTestPlanId()); } - if (StringUtils.equals(request.getRunMode(), ApiRunMode.JENKINS.name())) { - testCaseWithBLOBs = apiTestCaseMapper.selectByPrimaryKey(request.getCaseId()); - request.setReportId(request.getEnvironmentId()); - } - // 多态JSON普通转换会丢失内容,需要通过 ObjectMapper 获取 if (testCaseWithBLOBs != null && StringUtils.isNotEmpty(testCaseWithBLOBs.getRequest())) { try { HashTree jmeterHashTree = this.generateHashTree(request, testCaseWithBLOBs); // 调用执行方法 - jMeterService.runLocal(request.getReportId(),null, jmeterHashTree, null, request.getRunMode()); + jMeterService.runLocal(request.getReportId(), null, jmeterHashTree, null, request.getRunMode()); } catch (Exception ex) { ApiDefinitionExecResult result = MessageCache.caseExecResourceLock.get(request.getReportId()); @@ -837,7 +865,7 @@ public class ApiTestCaseService { request.setTestPlanId(testPlanID); HashTree jmeterHashTree = this.generateHashTree(request, apiCaseBolbs); // 调用执行方法 - jMeterService.runLocal(id,null, jmeterHashTree, debugReportId, runMode); + jMeterService.runLocal(id, null, jmeterHashTree, debugReportId, runMode); } catch (Exception ex) { LogUtil.error(ex); }