fix:Jenkins飞书消息通知-执行测试计划中的性能测试结果未通知,创建完测试计划,负责人是空

This commit is contained in:
wenyann 2021-04-19 19:30:21 +08:00 committed by 刘瑞斌
parent 27b545d8d8
commit aae13f0342
7 changed files with 92 additions and 18 deletions

View File

@ -14,6 +14,7 @@ import io.metersphere.i18n.Translator;
import io.metersphere.notice.sender.NoticeModel; import io.metersphere.notice.sender.NoticeModel;
import io.metersphere.notice.service.NoticeSendService; import io.metersphere.notice.service.NoticeSendService;
import io.metersphere.service.SystemParameterService; import io.metersphere.service.SystemParameterService;
import io.metersphere.track.service.TestPlanApiCaseService;
import io.metersphere.track.service.TestPlanReportService; import io.metersphere.track.service.TestPlanReportService;
import io.metersphere.track.service.TestPlanTestCaseService; import io.metersphere.track.service.TestPlanTestCaseService;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
@ -62,6 +63,8 @@ public class APIBackendListenerClient extends AbstractBackendListenerClient impl
private ApiAutomationService apiAutomationService; private ApiAutomationService apiAutomationService;
private TestPlanApiCaseService testPlanApiCaseService;
public String runMode = ApiRunMode.RUN.name(); public String runMode = ApiRunMode.RUN.name();
// 测试ID // 测试ID
@ -122,6 +125,10 @@ public class APIBackendListenerClient extends AbstractBackendListenerClient impl
if (apiAutomationService == null) { if (apiAutomationService == null) {
LogUtil.error("apiAutomationService is required"); LogUtil.error("apiAutomationService is required");
} }
testPlanApiCaseService = CommonBeanFactory.getBean(TestPlanApiCaseService.class);
if (testPlanApiCaseService == null) {
LogUtil.error("testPlanApiCaseService is required");
}
super.setupTest(context); super.setupTest(context);
} }
@ -195,16 +202,60 @@ public class APIBackendListenerClient extends AbstractBackendListenerClient impl
} else if (StringUtils.equals(this.runMode, ApiRunMode.JENKINS.name())) { } else if (StringUtils.equals(this.runMode, ApiRunMode.JENKINS.name())) {
apiDefinitionService.addResult(testResult); apiDefinitionService.addResult(testResult);
apiDefinitionExecResultService.saveApiResult(testResult, ApiRunMode.DEFINITION.name()); apiDefinitionExecResultService.saveApiResult(testResult, ApiRunMode.DEFINITION.name());
ApiTestCaseWithBLOBs apiTestCaseWithBLOBs = apiTestCaseService.getInfoJenkins(testResult.getTestId());
ApiDefinitionExecResult apiResult = apiDefinitionExecResultService.getInfo(apiTestCaseWithBLOBs.getLastResultId());
//环境
String name = apiAutomationService.get(debugReportId).getName();
//时间
Long time = apiTestCaseWithBLOBs.getUpdateTime();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String executionTime = null;
String time_ = String.valueOf(time);
if (!time_.equals("null")) {
executionTime = sdf.format(new Date(Long.parseLong(time_)));
}
//执行人
String userName = apiAutomationService.getUser(apiTestCaseWithBLOBs.getCreateUserId()).getName();
//报告内容
reportTask = new ApiTestReportVariable();
reportTask.setStatus(apiResult.getStatus());
reportTask.setId(apiResult.getId());
reportTask.setTriggerMode("API");
reportTask.setName(apiTestCaseWithBLOBs.getName());
reportTask.setExecutor(userName);
reportTask.setExecutionTime(executionTime);
reportTask.setExecutionEnvironment(name);
} else if (StringUtils.equals(this.runMode, ApiRunMode.JENKINS_API_PLAN.name())) { } else if (StringUtils.equals(this.runMode, ApiRunMode.JENKINS_API_PLAN.name())) {
apiDefinitionService.addResult(testResult); apiDefinitionService.addResult(testResult);
apiDefinitionExecResultService.saveApiResult(testResult, ApiRunMode.API_PLAN.name()); apiDefinitionExecResultService.saveApiResult(testResult, ApiRunMode.API_PLAN.name());
ApiDefinitionExecResult result = new ApiDefinitionExecResult(); ApiTestCaseWithBLOBs apiTestCaseWithBLOBs = apiTestCaseService.getInfoJenkins(testResult.getTestId());
result = apiDefinitionService.getResultByJenkins(debugReportId, ApiRunMode.API_PLAN.name()); ApiDefinitionExecResult apiResult = apiDefinitionExecResultService.getInfo(apiTestCaseWithBLOBs.getLastResultId());
report = new ApiTestReport(); //环境
report.setStatus(result.getStatus()); TestPlanApiCase testPlanApiCase = testPlanApiCaseService.getInfo(testResult.getTestId(), debugReportId);
report.setId(result.getId()); String name = apiAutomationService.get(testPlanApiCase.getEnvironmentId()).getName();
report.setTriggerMode(ApiRunMode.API.name()); //时间
report.setName(apiDefinitionService.getApiCaseInfo(testId).getName()); Long time = apiTestCaseWithBLOBs.getUpdateTime();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String executionTime = null;
String time_ = String.valueOf(time);
if (!time_.equals("null")) {
executionTime = sdf.format(new Date(Long.parseLong(time_)));
}
//执行人
String userName = apiAutomationService.getUser(apiTestCaseWithBLOBs.getCreateUserId()).getName();
//报告内容
reportTask = new ApiTestReportVariable();
reportTask.setStatus(apiResult.getStatus());
reportTask.setId(apiResult.getId());
reportTask.setTriggerMode("API");
reportTask.setName(apiTestCaseWithBLOBs.getName());
reportTask.setExecutor(userName);
reportTask.setExecutionTime(executionTime);
reportTask.setExecutionEnvironment(name);
} else if (StringUtils.equalsAny(this.runMode, ApiRunMode.API_PLAN.name(), ApiRunMode.SCHEDULE_API_PLAN.name())) { } else if (StringUtils.equalsAny(this.runMode, ApiRunMode.API_PLAN.name(), ApiRunMode.SCHEDULE_API_PLAN.name())) {
apiDefinitionService.addResult(testResult); apiDefinitionService.addResult(testResult);

View File

@ -41,10 +41,13 @@ public class ApiDefinitionExecResultService {
private ApiTestCaseMapper apiTestCaseMapper; private ApiTestCaseMapper apiTestCaseMapper;
@Resource @Resource
private TestCaseReviewApiCaseService testCaseReviewApiCaseService; private TestCaseReviewApiCaseService testCaseReviewApiCaseService;
@Resource @Resource
SqlSessionFactory sqlSessionFactory; SqlSessionFactory sqlSessionFactory;
public ApiDefinitionExecResult getInfo(String id) {
return apiDefinitionExecResultMapper.selectByPrimaryKey(id);
}
public void saveApiResult(TestResult result, String type) { public void saveApiResult(TestResult result, String type) {
if (CollectionUtils.isNotEmpty(result.getScenarios())) { if (CollectionUtils.isNotEmpty(result.getScenarios())) {
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH); SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);

View File

@ -81,12 +81,18 @@ public class ApiTestCaseService {
private static final String BODY_FILE_DIR = FileUtils.BODY_FILE_DIR; private static final String BODY_FILE_DIR = FileUtils.BODY_FILE_DIR;
//查询测试用例详情
public ApiTestCaseWithBLOBs getInfoJenkins(String id) {
ApiTestCaseWithBLOBs apiTest = apiTestCaseMapper.selectByPrimaryKey(id);
return apiTest;
}
public List<ApiTestCaseResult> list(ApiTestCaseRequest request) { public List<ApiTestCaseResult> list(ApiTestCaseRequest request) {
request.setOrders(ServiceUtils.getDefaultOrder(request.getOrders())); request.setOrders(ServiceUtils.getDefaultOrder(request.getOrders()));
List<ApiTestCaseResult> returnList = extApiTestCaseMapper.list(request); List<ApiTestCaseResult> returnList = extApiTestCaseMapper.list(request);
for (ApiTestCaseResult res : returnList) { for (ApiTestCaseResult res : returnList) {
if(StringUtils.equalsIgnoreCase(res.getApiMethod(),"esb")){ if (StringUtils.equalsIgnoreCase(res.getApiMethod(), "esb")) {
esbApiParamService.handleApiEsbParams(res); esbApiParamService.handleApiEsbParams(res);
} }
} }
@ -558,14 +564,21 @@ public class ApiTestCaseService {
} }
public String run(RunCaseRequest request) { public String run(RunCaseRequest request) {
ApiTestCaseWithBLOBs testCaseWithBLOBs=new ApiTestCaseWithBLOBs(); ApiTestCaseWithBLOBs testCaseWithBLOBs = new ApiTestCaseWithBLOBs();
if(StringUtils.equals(request.getRunMode(), ApiRunMode.JENKINS_API_PLAN.name())){ if (StringUtils.equals(request.getRunMode(), ApiRunMode.JENKINS_API_PLAN.name())) {
testCaseWithBLOBs= apiTestCaseMapper.selectByPrimaryKey(request.getReportId()); testCaseWithBLOBs = apiTestCaseMapper.selectByPrimaryKey(request.getReportId());
request.setCaseId(request.getReportId()); request.setCaseId(request.getReportId());
}else{ } else {
testCaseWithBLOBs= apiTestCaseMapper.selectByPrimaryKey(request.getCaseId()); testCaseWithBLOBs = apiTestCaseMapper.selectByPrimaryKey(request.getCaseId());
} }
if (StringUtils.equals(request.getRunMode(), ApiRunMode.JENKINS.name())) {
request.setReportId(request.getEnvironmentId());
}
if (StringUtils.equals(request.getRunMode(), ApiRunMode.JENKINS_API_PLAN.name())) {
//通过测试计划id查询环境
request.setReportId(request.getTestPlanId());
}
// 多态JSON普通转换会丢失内容需要通过 ObjectMapper 获取 // 多态JSON普通转换会丢失内容需要通过 ObjectMapper 获取
if (testCaseWithBLOBs != null && StringUtils.isNotEmpty(testCaseWithBLOBs.getRequest())) { if (testCaseWithBLOBs != null && StringUtils.isNotEmpty(testCaseWithBLOBs.getRequest())) {
try { try {

View File

@ -33,14 +33,14 @@ public class LarkClient {
httpPost.setEntity(entity); httpPost.setEntity(entity);
// 执行http请求 // 执行http请求
response = httpClient.execute(httpPost); response = httpClient.execute(httpPost);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { /* if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String result = EntityUtils.toString(response.getEntity()); String result = EntityUtils.toString(response.getEntity());
JSONObject obj = JSONObject.parseObject(result); JSONObject obj = JSONObject.parseObject(result);
Integer errcode = obj.getInteger("errcode"); Integer errcode = obj.getInteger("errcode");
sendResult.setErrorCode(errcode); sendResult.setErrorCode(errcode);
sendResult.setErrorMsg(obj.getString("errmsg")); sendResult.setErrorMsg(obj.getString("errmsg"));
sendResult.setIsSuccess(errcode.equals(0)); sendResult.setIsSuccess(errcode.equals(0));
} }*/
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} finally { } finally {

View File

@ -26,6 +26,7 @@ public class PerformanceNoticeEvent implements LoadTestFinishEvent {
private NoticeSendService noticeSendService; private NoticeSendService noticeSendService;
public void sendNotice(LoadTestReport loadTestReport) { public void sendNotice(LoadTestReport loadTestReport) {
BaseSystemConfigDTO baseSystemConfigDTO = systemParameterService.getBaseInfo(); BaseSystemConfigDTO baseSystemConfigDTO = systemParameterService.getBaseInfo();
String url = baseSystemConfigDTO.getUrl() + "/#/performance/report/view/" + loadTestReport.getId(); String url = baseSystemConfigDTO.getUrl() + "/#/performance/report/view/" + loadTestReport.getId();
String successContext = ""; String successContext = "";
@ -71,7 +72,7 @@ public class PerformanceNoticeEvent implements LoadTestFinishEvent {
@Override @Override
public void execute(LoadTestReport loadTestReport) { public void execute(LoadTestReport loadTestReport) {
LogUtil.info("PerformanceNoticeEvent OVER:" + loadTestReport.getTriggerMode()+";"+loadTestReport.getStatus()); LogUtil.info("PerformanceNoticeEvent OVER:" + loadTestReport.getTriggerMode() + ";" + loadTestReport.getStatus() + ";" + loadTestReport.getName());
if (StringUtils.equals(ReportTriggerMode.API.name(), loadTestReport.getTriggerMode()) if (StringUtils.equals(ReportTriggerMode.API.name(), loadTestReport.getTriggerMode())
|| StringUtils.equals(ReportTriggerMode.SCHEDULE.name(), loadTestReport.getTriggerMode())) { || StringUtils.equals(ReportTriggerMode.SCHEDULE.name(), loadTestReport.getTriggerMode())) {
if (StringUtils.equalsAny(loadTestReport.getStatus(), if (StringUtils.equalsAny(loadTestReport.getStatus(),

View File

@ -44,6 +44,12 @@ public class TestPlanApiCaseService {
@Resource @Resource
ApiDefinitionExecResultService apiDefinitionExecResultService; ApiDefinitionExecResultService apiDefinitionExecResultService;
public TestPlanApiCase getInfo(String caseId, String testPlanId) {
TestPlanApiCaseExample example = new TestPlanApiCaseExample();
example.createCriteria().andApiCaseIdEqualTo(caseId).andTestPlanIdEqualTo(testPlanId);
return testPlanApiCaseMapper.selectByExample(example).get(0);
}
public List<TestPlanApiCaseDTO> list(ApiTestCaseRequest request) { public List<TestPlanApiCaseDTO> list(ApiTestCaseRequest request) {
request.setProjectId(null); request.setProjectId(null);
request.setOrders(ServiceUtils.getDefaultOrder(request.getOrders())); request.setOrders(ServiceUtils.getDefaultOrder(request.getOrders()));

View File

@ -26,7 +26,7 @@
</el-table-column> </el-table-column>
<el-table-column <el-table-column
v-if="item.id == 'userName'" v-if="item.id == 'userName'"
prop="userName" prop="principal"
:label="$t('test_track.plan.plan_principal')" :label="$t('test_track.plan.plan_principal')"
show-overflow-tooltip show-overflow-tooltip
:key="index"> :key="index">