fix(测试计划): 修复测试计划批量复制时日志名称记录错误的问题

--bug=1043150 --user=宋天阳 【测试计划】批量复制测试计划-日志生成的名称是复制前的名称 https://www.tapd.cn/55049933/s/1546084
This commit is contained in:
Jianguo-Genius 2024-07-10 10:59:51 +08:00 committed by 建国
parent 6ebb9148e5
commit 5c662aeb5c
8 changed files with 43 additions and 46 deletions

View File

@ -10,6 +10,7 @@ import io.metersphere.sdk.constants.PermissionConstants;
import io.metersphere.system.log.annotation.Log;
import io.metersphere.system.log.constants.OperationLogType;
import io.metersphere.system.security.CheckOwner;
import io.metersphere.system.uid.IDGenerator;
import io.metersphere.system.utils.SessionUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
@ -41,7 +42,11 @@ public class TestPlanExecuteController {
@Log(type = OperationLogType.EXECUTE, expression = "#msClass.executeLog(#request)", msClass = TestPlanLogService.class)
public String startExecute(@Validated @RequestBody TestPlanExecuteRequest request) {
testPlanManagementService.checkModuleIsOpen(request.getExecuteId(), TestPlanResourceConfig.CONFIG_TEST_PLAN, Collections.singletonList(TestPlanResourceConfig.CONFIG_TEST_PLAN));
return testPlanExecuteService.singleExecuteTestPlan(request, SessionUtils.getUserId());
String reportId = IDGenerator.nextStr();
Thread.startVirtualThread(() ->
testPlanExecuteService.singleExecuteTestPlan(request, reportId, SessionUtils.getUserId())
);
return reportId;
}
@PostMapping("/batch")

View File

@ -8,6 +8,7 @@ import io.metersphere.sdk.util.CommonBeanFactory;
import io.metersphere.sdk.util.JSON;
import io.metersphere.sdk.util.LogUtils;
import io.metersphere.system.schedule.BaseScheduleJob;
import io.metersphere.system.uid.IDGenerator;
import org.quartz.JobExecutionContext;
import org.quartz.JobKey;
import org.quartz.TriggerKey;
@ -28,7 +29,7 @@ public class TestPlanScheduleJob extends BaseScheduleJob {
this.setExecuteId(resourceId);
this.setRunMode(runMode);
this.setExecutionSource(ApiExecuteRunMode.SCHEDULE.name());
}}, userId)
}}, IDGenerator.nextStr(), userId)
);
}

View File

@ -125,27 +125,26 @@ public class TestPlanBatchOperationService extends TestPlanBaseUtilsService {
}
public long batchCopy(List<TestPlan> copyPlanList, String targetId, String targetType, String userId) {
long copyCount = 0;
public List<TestPlan> batchCopy(List<TestPlan> originalPlanList, String targetId, String targetType, String userId) {
List<TestPlan> copyPlanResult = new ArrayList<>();
long operatorTime = System.currentTimeMillis();
//如果目标ID是测试计划组 需要进行容量校验
if (!StringUtils.equalsIgnoreCase(targetType, ModuleConstants.NODE_TYPE_DEFAULT)) {
testPlanGroupService.validateGroupCapacity(targetId, copyPlanList.size());
testPlanGroupService.validateGroupCapacity(targetId, originalPlanList.size());
}
/*
此处不进行批量处理原因有两点
1 测试计划内或者测试计划组内数据量不可控选择批量操作时更容易出现数据太多不走索引数据太多内存溢出等问题不批量操作可以减少这些问题出现的概率代价是速度会变慢
2 作为数据量不可控的操作如果数据量少不采用批量处理也不会消耗太多时间如果数据量多就会容易出现1的问题并且本人不建议针对不可控数据量的数据支持批量操作
*/
for (TestPlan copyPlan : copyPlanList) {
for (TestPlan copyPlan : originalPlanList) {
if (StringUtils.equalsIgnoreCase(copyPlan.getType(), TestPlanConstants.TEST_PLAN_TYPE_GROUP)) {
this.copyPlanGroup(copyPlan, targetId, targetType, operatorTime, userId);
copyPlanResult.add(this.copyPlanGroup(copyPlan, targetId, targetType, operatorTime, userId));
} else {
this.copyPlan(copyPlan, targetId, targetType, operatorTime, userId);
copyPlanResult.add(this.copyPlan(copyPlan, targetId, targetType, operatorTime, userId));
}
copyCount++;
}
return copyCount;
return copyPlanResult;
}

View File

@ -125,10 +125,7 @@ public class TestPlanExecuteService {
* 这里涉及到嵌套查询不使用事务中间涉及到的报告生成测试计划字段更改测试报告汇总等操作会开启子事务处理
*/
@Transactional(rollbackFor = Exception.class, propagation = Propagation.NOT_SUPPORTED)
public String singleExecuteTestPlan(TestPlanExecuteRequest request, String userId) {
String reportId = IDGenerator.nextStr();
Thread.startVirtualThread(() -> {
public String singleExecuteTestPlan(TestPlanExecuteRequest request, String reportId, String userId) {
String queueId = IDGenerator.nextStr();
TestPlanExecutionQueue singleExecuteRootQueue = new TestPlanExecutionQueue(
0,
@ -149,7 +146,6 @@ public class TestPlanExecuteService {
TestPlanExecutionQueue nextQueue = testPlanExecuteSupportService.getNextQueue(queueId, QUEUE_PREFIX_TEST_PLAN_BATCH_EXECUTE);
LogUtils.info("测试计划的单独执行start计划报告[{}] , 资源ID[{}]", singleExecuteRootQueue.getPrepareReportId(), singleExecuteRootQueue.getSourceID());
executeTestPlanOrGroup(nextQueue);
});
return reportId;
}

View File

@ -107,24 +107,24 @@ public class TestPlanGroupService extends TestPlanSortService {
}
public TestPlan validateGroupCapacity(String groupId, int size) {
// 判断测试计划组是否存在
TestPlan groupPlan = testPlanMapper.selectByPrimaryKey(groupId);
if (groupPlan == null) {
throw new MSException(Translator.get("test_plan.group.error"));
}
// 判断测试计划组是否存在
TestPlan groupPlan = testPlanMapper.selectByPrimaryKey(groupId);
if (groupPlan == null) {
throw new MSException(Translator.get("test_plan.group.error"));
}
if (!StringUtils.equalsIgnoreCase(groupPlan.getType(), TestPlanConstants.TEST_PLAN_TYPE_GROUP)) {
throw new MSException(Translator.get("test_plan.group.error"));
}
//判断并未归档
if (StringUtils.equalsIgnoreCase(groupPlan.getStatus(), TestPlanConstants.TEST_PLAN_STATUS_ARCHIVED)) {
throw new MSException(Translator.get("test_plan.group.error"));
}
//判断测试计划组下的测试计划数量是否超过20
TestPlanExample example = new TestPlanExample();
example.createCriteria().andGroupIdEqualTo(groupId);
if (testPlanMapper.countByExample(example) + size > 20) {
throw new MSException(Translator.getWithArgs("test_plan.group.children.max", MAX_CHILDREN_COUNT));
}
//判断并未归档
if (StringUtils.equalsIgnoreCase(groupPlan.getStatus(), TestPlanConstants.TEST_PLAN_STATUS_ARCHIVED)) {
throw new MSException(Translator.get("test_plan.group.error"));
}
//判断测试计划组下的测试计划数量是否超过20
TestPlanExample example = new TestPlanExample();
example.createCriteria().andGroupIdEqualTo(groupId);
if (testPlanMapper.countByExample(example) + size > 20) {
throw new MSException(Translator.getWithArgs("test_plan.group.children.max", MAX_CHILDREN_COUNT));
}
return groupPlan;
}
}

View File

@ -182,9 +182,8 @@ public class TestPlanLogService {
* @param requestUrl 请求URL
* @param requestMethod 请求方法
* @param requestType 请求类型
* @param typeKey 类型Key
*/
public void saveBatchLog(List<TestPlan> plans, String operator, String requestUrl, String requestMethod, String requestType, String typeKey) {
public void saveBatchLog(List<TestPlan> plans, String operator, String requestUrl, String requestMethod, String requestType) {
if (CollectionUtils.isEmpty(plans)) {
return;
}

View File

@ -314,7 +314,7 @@ public class TestPlanService extends TestPlanBaseUtilsService {
// 计划组的删除暂时预留
this.deleteGroupByList(testPlanGroupList);
//记录日志
testPlanLogService.saveBatchLog(deleteTestPlanList, operator, requestUrl, requestMethod, OperationLogType.DELETE.name(), "delete");
testPlanLogService.saveBatchLog(deleteTestPlanList, operator, requestUrl, requestMethod, OperationLogType.DELETE.name());
}
}
}
@ -366,7 +366,7 @@ public class TestPlanService extends TestPlanBaseUtilsService {
updateTestPlan.setPlannedEndTime(request.getPlannedEndTime());
updateTestPlan.setDescription(request.getDescription());
//判断有没有用户组的变化
if (StringUtils.isNotBlank(request.getGroupId())) {
if (StringUtils.isNotBlank(request.getGroupId()) && !StringUtils.equalsIgnoreCase(request.getGroupId(), TestPlanConstants.TEST_PLAN_DEFAULT_GROUP_ID)) {
if (!StringUtils.equalsIgnoreCase(originalTestPlan.getGroupId(), request.getGroupId())) {
//用户更换了测试计划组
TestPlan testPlanGroup = testPlanGroupService.validateGroupCapacity(request.getGroupId(), 1);
@ -518,7 +518,7 @@ public class TestPlanService extends TestPlanBaseUtilsService {
}
});
//日志
testPlanLogService.saveBatchLog(archivedPlanGroupList, currentUser, "/test-plan/batch-archived", HttpMethodConstants.POST.name(), OperationLogType.ARCHIVED.name(), "archive");
testPlanLogService.saveBatchLog(archivedPlanGroupList, currentUser, "/test-plan/batch-archived", HttpMethodConstants.POST.name(), OperationLogType.ARCHIVED.name());
}
}
@ -666,15 +666,16 @@ public class TestPlanService extends TestPlanBaseUtilsService {
if (CollectionUtils.isNotEmpty(copyIds)) {
TestPlanExample example = new TestPlanExample();
example.createCriteria().andIdIn(copyIds);
List<TestPlan> copyTestPlanList = testPlanMapper.selectByExample(example);
List<TestPlan> originalTestPlanList = testPlanMapper.selectByExample(example);
//批量复制时不允许存在测试计划组下的测试计划
copyTestPlanList = copyTestPlanList.stream().filter(item -> StringUtils.equalsIgnoreCase(item.getGroupId(), TestPlanConstants.TEST_PLAN_DEFAULT_GROUP_ID))
originalTestPlanList = originalTestPlanList.stream().filter(item -> StringUtils.equalsIgnoreCase(item.getGroupId(), TestPlanConstants.TEST_PLAN_DEFAULT_GROUP_ID))
.collect(Collectors.toList());
//日志
if (CollectionUtils.isNotEmpty(copyTestPlanList)) {
copyCount = testPlanBatchOperationService.batchCopy(copyTestPlanList, request.getTargetId(), request.getMoveType(), userId);
testPlanLogService.saveBatchLog(copyTestPlanList, userId, url, method, OperationLogType.ADD.name(), "copy");
if (CollectionUtils.isNotEmpty(originalTestPlanList)) {
List<TestPlan> copyPlanList = testPlanBatchOperationService.batchCopy(originalTestPlanList, request.getTargetId(), request.getMoveType(), userId);
copyCount = copyPlanList.size();
testPlanLogService.saveBatchLog(copyPlanList, userId, url, method, OperationLogType.ADD.name());
}
}
@ -713,7 +714,7 @@ public class TestPlanService extends TestPlanBaseUtilsService {
}
//日志
if (CollectionUtils.isNotEmpty(moveTestPlanList)) {
testPlanLogService.saveBatchLog(moveTestPlanList, userId, operationUrl, method, OperationLogType.UPDATE.name(), "update");
testPlanLogService.saveBatchLog(moveTestPlanList, userId, operationUrl, method, OperationLogType.UPDATE.name());
}
}
return moveCount;

View File

@ -48,8 +48,6 @@ public class TestPlanExecuteTests extends BaseTest {
private static TestPlan allParallelGroup;
private static TestPlan noGroupPlan;
private static final String URL_POST_TEST_PLAN_SINGLE_EXECUTE = "/test-plan-execute/single";
@Resource
private CommonProjectService commonProjectService;
@Resource
@ -280,9 +278,7 @@ public class TestPlanExecuteTests extends BaseTest {
executeRequest.setExecuteId(id);
executeRequest.setRunMode(runMode);
this.requestPostWithOk(URL_POST_TEST_PLAN_SINGLE_EXECUTE, executeRequest);
Thread.sleep(1000);
testPlanExecuteService.singleExecuteTestPlan(executeRequest, IDGenerator.nextStr(), "admin");
//检查队列
List<String> allQueueIds = new ArrayList<>();