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.service.NoticeSendService;
import io.metersphere.service.SystemParameterService;
import io.metersphere.track.service.TestPlanApiCaseService;
import io.metersphere.track.service.TestPlanReportService;
import io.metersphere.track.service.TestPlanTestCaseService;
import org.apache.commons.collections4.CollectionUtils;
@ -62,6 +63,8 @@ public class APIBackendListenerClient extends AbstractBackendListenerClient impl
private ApiAutomationService apiAutomationService;
private TestPlanApiCaseService testPlanApiCaseService;
public String runMode = ApiRunMode.RUN.name();
// 测试ID
@ -122,6 +125,10 @@ public class APIBackendListenerClient extends AbstractBackendListenerClient impl
if (apiAutomationService == null) {
LogUtil.error("apiAutomationService is required");
}
testPlanApiCaseService = CommonBeanFactory.getBean(TestPlanApiCaseService.class);
if (testPlanApiCaseService == null) {
LogUtil.error("testPlanApiCaseService is required");
}
super.setupTest(context);
}
@ -195,16 +202,60 @@ public class APIBackendListenerClient extends AbstractBackendListenerClient impl
} else if (StringUtils.equals(this.runMode, ApiRunMode.JENKINS.name())) {
apiDefinitionService.addResult(testResult);
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())) {
apiDefinitionService.addResult(testResult);
apiDefinitionExecResultService.saveApiResult(testResult, ApiRunMode.API_PLAN.name());
ApiDefinitionExecResult result = new ApiDefinitionExecResult();
result = apiDefinitionService.getResultByJenkins(debugReportId, ApiRunMode.API_PLAN.name());
report = new ApiTestReport();
report.setStatus(result.getStatus());
report.setId(result.getId());
report.setTriggerMode(ApiRunMode.API.name());
report.setName(apiDefinitionService.getApiCaseInfo(testId).getName());
ApiTestCaseWithBLOBs apiTestCaseWithBLOBs = apiTestCaseService.getInfoJenkins(testResult.getTestId());
ApiDefinitionExecResult apiResult = apiDefinitionExecResultService.getInfo(apiTestCaseWithBLOBs.getLastResultId());
//环境
TestPlanApiCase testPlanApiCase = testPlanApiCaseService.getInfo(testResult.getTestId(), debugReportId);
String name = apiAutomationService.get(testPlanApiCase.getEnvironmentId()).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())) {
apiDefinitionService.addResult(testResult);

View File

@ -41,10 +41,13 @@ public class ApiDefinitionExecResultService {
private ApiTestCaseMapper apiTestCaseMapper;
@Resource
private TestCaseReviewApiCaseService testCaseReviewApiCaseService;
@Resource
SqlSessionFactory sqlSessionFactory;
public ApiDefinitionExecResult getInfo(String id) {
return apiDefinitionExecResultMapper.selectByPrimaryKey(id);
}
public void saveApiResult(TestResult result, String type) {
if (CollectionUtils.isNotEmpty(result.getScenarios())) {
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;
//查询测试用例详情
public ApiTestCaseWithBLOBs getInfoJenkins(String id) {
ApiTestCaseWithBLOBs apiTest = apiTestCaseMapper.selectByPrimaryKey(id);
return apiTest;
}
public List<ApiTestCaseResult> list(ApiTestCaseRequest request) {
request.setOrders(ServiceUtils.getDefaultOrder(request.getOrders()));
List<ApiTestCaseResult> returnList = extApiTestCaseMapper.list(request);
for (ApiTestCaseResult res : returnList) {
if(StringUtils.equalsIgnoreCase(res.getApiMethod(),"esb")){
if (StringUtils.equalsIgnoreCase(res.getApiMethod(), "esb")) {
esbApiParamService.handleApiEsbParams(res);
}
}
@ -558,14 +564,21 @@ public class ApiTestCaseService {
}
public String run(RunCaseRequest request) {
ApiTestCaseWithBLOBs testCaseWithBLOBs=new ApiTestCaseWithBLOBs();
if(StringUtils.equals(request.getRunMode(), ApiRunMode.JENKINS_API_PLAN.name())){
testCaseWithBLOBs= apiTestCaseMapper.selectByPrimaryKey(request.getReportId());
ApiTestCaseWithBLOBs testCaseWithBLOBs = new ApiTestCaseWithBLOBs();
if (StringUtils.equals(request.getRunMode(), ApiRunMode.JENKINS_API_PLAN.name())) {
testCaseWithBLOBs = apiTestCaseMapper.selectByPrimaryKey(request.getReportId());
request.setCaseId(request.getReportId());
}else{
testCaseWithBLOBs= apiTestCaseMapper.selectByPrimaryKey(request.getCaseId());
} else {
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 获取
if (testCaseWithBLOBs != null && StringUtils.isNotEmpty(testCaseWithBLOBs.getRequest())) {
try {

View File

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

View File

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

View File

@ -44,6 +44,12 @@ public class TestPlanApiCaseService {
@Resource
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) {
request.setProjectId(null);
request.setOrders(ServiceUtils.getDefaultOrder(request.getOrders()));

View File

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