fix(Jenkins执行): 修复Jenkins调用接口执行时通知中心没数据的问题

修复Jenkins调用接口执行时通知中心没数据的问题
This commit is contained in:
song-tianyang 2021-11-26 11:27:55 +08:00 committed by 刘瑞斌
parent 1770bd7a8e
commit 8b8392ddcf
2 changed files with 40 additions and 12 deletions

View File

@ -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}")

View File

@ -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);
}