diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiExecuteService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiExecuteService.java index 87b396786b..390d5f584e 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiExecuteService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiExecuteService.java @@ -7,9 +7,13 @@ import io.metersphere.api.dto.ApiParamConfig; import io.metersphere.api.dto.ApiScenarioParamConfig; import io.metersphere.api.dto.debug.ApiDebugRunRequest; import io.metersphere.api.dto.debug.ApiResourceRunRequest; +import io.metersphere.api.dto.definition.ApiModuleDTO; +import io.metersphere.api.dto.definition.EnvApiModuleRequest; +import io.metersphere.api.dto.definition.EnvApiTreeDTO; import io.metersphere.api.dto.request.controller.MsScriptElement; import io.metersphere.api.parser.TestElementParser; import io.metersphere.api.parser.TestElementParserFactory; +import io.metersphere.api.service.definition.ApiDefinitionModuleService; import io.metersphere.api.utils.ApiDataUtils; import io.metersphere.engine.EngineFactory; import io.metersphere.engine.MsHttpClient; @@ -22,6 +26,9 @@ import io.metersphere.project.dto.customfunction.request.CustomFunctionRunReques import io.metersphere.project.dto.environment.EnvironmentInfoDTO; import io.metersphere.project.dto.environment.GlobalParams; import io.metersphere.project.dto.environment.GlobalParamsDTO; +import io.metersphere.project.dto.environment.http.HttpConfig; +import io.metersphere.project.dto.environment.http.HttpConfigModuleMatchRule; +import io.metersphere.project.dto.environment.http.SelectModule; import io.metersphere.project.service.*; import io.metersphere.sdk.constants.*; import io.metersphere.sdk.dto.api.task.*; @@ -114,6 +121,8 @@ public class ApiExecuteService { private ExecTaskItemMapper execTaskItemMapper; @Resource private SqlSessionFactory sqlSessionFactory; + @Resource + private ApiDefinitionModuleService apiDefinitionModuleService; @PostConstruct private void init() { @@ -844,9 +853,55 @@ public class ApiExecuteService { public TaskRequestDTO apiExecute(ApiResourceRunRequest runRequest, TaskRequestDTO taskRequest, ApiParamConfig apiParamConfig) { // 设置使用脚本前后置的公共脚本信息 setTestElementParam(runRequest.getTestElement(), taskRequest.getTaskInfo().getProjectId(), taskRequest.getTaskItem()); + // 处理模块匹配 + handleHttpModuleMatchRule(apiParamConfig.getEnvConfig()); return execute(runRequest, taskRequest, apiParamConfig); } + /** + * 处理环境的 HTTP 配置模块匹配规则 + * 查询新增子模块 + * + * @param envInfoDTO + */ + public void handleHttpModuleMatchRule(EnvironmentInfoDTO envInfoDTO) { + if (envInfoDTO == null || envInfoDTO.getConfig() == null) { + return; + } + List httpConfigs = envInfoDTO.getConfig().getHttpConfig(); + for (HttpConfig httpConfig : httpConfigs) { + if (!httpConfig.isModuleMatchRule()) { + continue; + } + // 获取勾选了包含子模块的模块ID + HttpConfigModuleMatchRule moduleMatchRule = httpConfig.getModuleMatchRule(); + List selectModules = moduleMatchRule.getModules(); + + EnvApiModuleRequest envApiModuleRequest = new EnvApiModuleRequest(); + envApiModuleRequest.setProjectId(envInfoDTO.getProjectId()); + List apiModuleDTOS = selectModules.stream().map(selectModule -> { + ApiModuleDTO apiModuleDTO = new ApiModuleDTO(); + apiModuleDTO.setModuleId(selectModule.getModuleId()); + apiModuleDTO.setContainChildModule(selectModule.getContainChildModule()); + return apiModuleDTO; + }).toList(); + envApiModuleRequest.setSelectedModules(apiModuleDTOS); + EnvApiTreeDTO envApiTreeDTO = apiDefinitionModuleService.envTree(envApiModuleRequest); + List selectedModules = envApiTreeDTO.getSelectedModules(); + List moduleIds = selectedModules.stream().map(ApiModuleDTO::getModuleId).toList(); + + + // 重新设置选中的模块ID + moduleMatchRule.setModules(null); + List allSelectModules = moduleIds.stream().map(moduleId -> { + SelectModule module = new SelectModule(); + module.setModuleId(moduleId); + return module; + }).collect(Collectors.toList()); + moduleMatchRule.setModules(allSelectModules); + } + } + public void setTestElementParam(AbstractMsTestElement testElement, String projectId, TaskItem taskItem) { apiCommonService.setEnableCommonScriptProcessorInfo(testElement); testElement.setResourceId(taskItem.getResourceId()); diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiTestCaseRunService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiTestCaseRunService.java index 0552138fcc..0f1ff8eabe 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiTestCaseRunService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiTestCaseRunService.java @@ -218,6 +218,8 @@ public class ApiTestCaseRunService { apiCommonService.setApiDefinitionExecuteInfo(msTestElement, BeanUtils.copyBean(new ApiDefinitionExecuteInfo(), apiDefinition)); apiExecuteService.setTestElementParam(msTestElement, apiTestCase.getProjectId(), request.getTaskItem()); + // 处理模块匹配 + apiExecuteService.handleHttpModuleMatchRule(apiParamConfig.getEnvConfig()); // 设置环境信息 apiParamConfig.setEnvConfig(environmentService.get(getEnvId(request.getRunModeConfig(), apiTestCase.getEnvironmentId()))); diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioRunService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioRunService.java index 2ddc963612..3de5df2ba2 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioRunService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioRunService.java @@ -919,53 +919,11 @@ public class ApiScenarioRunService { envInfo.setEnvGroupMap(envGroupMap); envInfo.setEnvMap(envMap); - envMap.forEach((envId, envInfoDTO) -> handleHttpModuleMatchRule(envInfoDTO)); + envMap.forEach((envId, envInfoDTO) -> apiExecuteService.handleHttpModuleMatchRule(envInfoDTO)); return envInfo; } - - /** - * 处理环境的 HTTP 配置模块匹配规则 - * 查询新增子模块 - * - * @param envInfoDTO - */ - private void handleHttpModuleMatchRule(EnvironmentInfoDTO envInfoDTO) { - List httpConfigs = envInfoDTO.getConfig().getHttpConfig(); - for (HttpConfig httpConfig : httpConfigs) { - if (!httpConfig.isModuleMatchRule()) { - continue; - } - // 获取勾选了包含子模块的模块ID - HttpConfigModuleMatchRule moduleMatchRule = httpConfig.getModuleMatchRule(); - List selectModules = moduleMatchRule.getModules(); - - EnvApiModuleRequest envApiModuleRequest = new EnvApiModuleRequest(); - envApiModuleRequest.setProjectId(envInfoDTO.getProjectId()); - List apiModuleDTOS = selectModules.stream().map(selectModule -> { - ApiModuleDTO apiModuleDTO = new ApiModuleDTO(); - apiModuleDTO.setModuleId(selectModule.getModuleId()); - apiModuleDTO.setContainChildModule(selectModule.getContainChildModule()); - return apiModuleDTO; - }).toList(); - envApiModuleRequest.setSelectedModules(apiModuleDTOS); - EnvApiTreeDTO envApiTreeDTO = apiDefinitionModuleService.envTree(envApiModuleRequest); - List selectedModules = envApiTreeDTO.getSelectedModules(); - List moduleIds = selectedModules.stream().map(ApiModuleDTO::getModuleId).toList(); - - - // 重新设置选中的模块ID - moduleMatchRule.setModules(null); - List allSelectModules = moduleIds.stream().map(moduleId -> { - SelectModule module = new SelectModule(); - module.setModuleId(moduleId); - return module; - }).collect(Collectors.toList()); - moduleMatchRule.setModules(allSelectModules); - } - } - private List getApiScenarioByIds(List apiScenarioIds) { ApiScenarioExample example = new ApiScenarioExample(); example.createCriteria().andIdIn(apiScenarioIds);