fix(接口自动化): 接口自动化通知支持显示环境

--bug=1007801 --user=刘瑞斌 【接口测试】接口自动化定时任务通知模版增加“测试环境”变量 https://www.tapd.cn/55049933/s/1073013
This commit is contained in:
CaptainB 2021-11-23 17:17:00 +08:00 committed by 刘瑞斌
parent 66680cab8b
commit f42f4aaf2d
5 changed files with 39 additions and 45 deletions

View File

@ -7,6 +7,6 @@ import lombok.Data;
public class ApiTestReportVariable extends ApiTestReport {
public String executionTime;
public String executor;
public String executionEnvironment;
public String environment;
public String principal;
}

View File

@ -168,7 +168,7 @@ public class ApiAutomationService {
return null;
}
public ApiTestEnvironment get(String id) {
public ApiTestEnvironment getEnvironment(String id) {
return apiTestEnvironmentMapper.selectByPrimaryKey(id);
}

View File

@ -612,6 +612,32 @@ public class ApiScenarioReportService {
return lastReport;
}
public String getEnvironment(ApiScenarioWithBLOBs apiScenario) {
String environment = "未配置";
String environmentType = apiScenario.getEnvironmentType();
if (StringUtils.equals(environmentType, EnvironmentType.JSON.name())) {
String environmentJson = apiScenario.getEnvironmentJson();
JSONObject jsonObject = JSON.parseObject(environmentJson);
ApiTestEnvironmentExample example = new ApiTestEnvironmentExample();
List<String> collect = jsonObject.values().stream().map(Object::toString).collect(Collectors.toList());
collect.add("-1");// 防止没有配置环境导致不能发送的问题
example.createCriteria().andIdIn(collect);
List<ApiTestEnvironment> envs = apiTestEnvironmentMapper.selectByExample(example);
String env = envs.stream().map(ApiTestEnvironment::getName).collect(Collectors.joining(","));
if (StringUtils.isNotBlank(env)) {
environment = env;
}
}
if (StringUtils.equals(environmentType, EnvironmentType.GROUP.name())) {
String environmentGroupId = apiScenario.getEnvironmentGroupId();
EnvironmentGroup environmentGroup = environmentGroupMapper.selectByPrimaryKey(environmentGroupId);
if (environmentGroup != null) {
environment = environmentGroup.getName();
}
}
return environment;
}
private void sendNotice(ApiScenarioWithBLOBs scenario, ApiScenarioReport result) {
@ -632,33 +658,7 @@ public class ApiScenarioReportService {
Map paramMap = new HashMap<>(beanMap);
paramMap.put("operator", userDTO.getName());
paramMap.put("status", scenario.getLastResult());
String environmentType = scenario.getEnvironmentType();
if (StringUtils.equals(environmentType, EnvironmentType.JSON.name())) {
String environmentJson = scenario.getEnvironmentJson();
JSONObject jsonObject = JSON.parseObject(environmentJson);
ApiTestEnvironmentExample example = new ApiTestEnvironmentExample();
List<String> collect = jsonObject.values().stream().map(Object::toString).collect(Collectors.toList());
collect.add("-1");// 防止没有配置环境导致不能发送的问题
example.createCriteria().andIdIn(collect);
List<ApiTestEnvironment> envs = apiTestEnvironmentMapper.selectByExample(example);
String env = envs.stream().map(ApiTestEnvironment::getName).collect(Collectors.joining(","));
if (StringUtils.isNotBlank(env)) {
paramMap.put("environment", env);
} else {
paramMap.put("environment", "未配置");
}
}
if (StringUtils.equals(environmentType, EnvironmentType.GROUP.name())) {
String environmentGroupId = scenario.getEnvironmentGroupId();
EnvironmentGroup environmentGroup = environmentGroupMapper.selectByPrimaryKey(environmentGroupId);
if (environmentGroup != null) {
paramMap.put("environment", environmentGroup.getName());
} else {
paramMap.put("environment", "未配置");
}
}
paramMap.put("environment", getEnvironment(scenario));
String context = "${operator}执行接口自动化" + status + ": ${name}";
NoticeModel noticeModel = NoticeModel.builder()

View File

@ -1,6 +1,5 @@
package io.metersphere.api.service;
import com.alibaba.fastjson.JSONObject;
import io.metersphere.api.dto.automation.ApiTestReportVariable;
import io.metersphere.api.jmeter.TestResult;
import io.metersphere.base.domain.*;
@ -63,7 +62,7 @@ public class TestResultService {
ApiTestCaseWithBLOBs apiTestCaseWithBLOBs = apiTestCaseService.getInfoJenkins(testResult.getTestId());
ApiDefinitionExecResult apiResult = apiDefinitionExecResultService.getInfo(apiTestCaseWithBLOBs.getLastResultId());
//环境
String name = apiAutomationService.get(debugReportId).getName();
String name = apiAutomationService.getEnvironment(debugReportId).getName();
//执行人
String userName = apiAutomationService.getUser(apiTestCaseWithBLOBs.getCreateUserId());
//报告内容
@ -74,7 +73,7 @@ public class TestResultService {
reportTask.setName(apiTestCaseWithBLOBs.getName());
reportTask.setExecutor(userName);
reportTask.setExecutionTime(DateUtils.getTimeString(apiTestCaseWithBLOBs.getCreateTime()));
reportTask.setExecutionEnvironment(name);
reportTask.setEnvironment(name);
//测试计划用例定时jenkins
} else if (StringUtils.equalsAny(runMode, ApiRunMode.API_PLAN.name(), ApiRunMode.SCHEDULE_API_PLAN.name(), ApiRunMode.JENKINS_API_PLAN.name(), ApiRunMode.MANUAL_PLAN.name())) {
//测试计划定时任务-接口执行逻辑的话需要同步测试计划的报告数据
@ -82,7 +81,7 @@ public class TestResultService {
apiDefinitionExecResultService.saveApiResultByScheduleTask(testResult, debugReportId, runMode);
} else {
String testResultTestId = testResult.getTestId();
if(testResultTestId.contains(":")){
if (testResultTestId.contains(":")) {
testResult.setTestId(testResultTestId.split(":")[0]);
}
apiDefinitionExecResultService.saveApiResult(testResult, ApiRunMode.API_PLAN.name(), TriggerMode.MANUAL.name());
@ -94,19 +93,13 @@ public class TestResultService {
//环境
if (scenarioReport != null) {
ApiScenarioWithBLOBs apiScenario = apiAutomationService.getDto(scenarioReport.getScenarioId());
String name = "";
String environment = "";
//执行人
String userName = "";
//负责人
String principal = "";
if (apiScenario != null) {
String executionEnvironment = apiScenario.getScenarioDefinition();
JSONObject json = JSONObject.parseObject(executionEnvironment);
if (json != null && json.getString("environmentMap") != null && json.getString("environmentMap").length() > 2) {
JSONObject environment = JSONObject.parseObject(json.getString("environmentMap"));
String environmentId = environment.get(apiScenario.getProjectId()).toString();
name = apiAutomationService.get(environmentId).getName();
}
environment = apiScenarioReportService.getEnvironment(apiScenario);
userName = apiAutomationService.getUser(apiScenario.getUserId());
principal = apiAutomationService.getUser(apiScenario.getPrincipal());
}
@ -120,7 +113,7 @@ public class TestResultService {
reportTask.setExecutor(userName);
reportTask.setPrincipal(principal);
reportTask.setExecutionTime(DateUtils.getTimeString(scenarioReport.getUpdateTime()));
reportTask.setExecutionEnvironment(name);
reportTask.setEnvironment(environment);
SystemParameterService systemParameterService = CommonBeanFactory.getBean(SystemParameterService.class);
assert systemParameterService != null;
}
@ -148,6 +141,7 @@ public class TestResultService {
}
}
/**
* 更新测试计划关联接口测试的功能用例的状态
*
@ -193,7 +187,7 @@ public class TestResultService {
String subject = "";
String event = "";
String successContext = "${operator}执行接口测成功: ${name}" + ", 报告: ${reportUrl}";
String successContext = "${operator}执行接口测成功: ${name}" + ", 报告: ${reportUrl}";
String failedContext = "${operator}执行接口测试失败: ${name}" + ", 报告: ${reportUrl}";
if (StringUtils.equals(ReportTriggerMode.API.name(), report.getTriggerMode())) {

View File

@ -153,13 +153,13 @@ export default {
'<body>\n' +
'<div>\n' +
' <div style="margin-left: 100px">\n' +
' ${operator}执行接口测成功: ${name}, 报告: ${reportUrl}' +
' ${operator}执行接口测成功: ${name}, 报告: ${reportUrl}' +
' </div>\n' +
'\n' +
'</div>\n' +
'</body>\n' +
'</html>',
robotTitle: "${operator}执行接口测成功: ${name}, 报告: ${reportUrl}",
robotTitle: "${operator}执行接口测成功: ${name}, 报告: ${reportUrl}",
scheduleTask: [{
taskType: "scheduleTask",
event: "",