From be4870155e98e870db831543f1bd40f547508b30 Mon Sep 17 00:00:00 2001 From: AgAngle <1323481023@qq.com> Date: Wed, 23 Oct 2024 16:17:27 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E7=8E=AF=E5=A2=83=E5=BC=95=E7=94=A8=E5=85=AC=E5=85=B1=E8=84=9A?= =?UTF-8?q?=E6=9C=AC=EF=BC=8C=E6=89=A7=E8=A1=8C=E6=97=B6=E6=B2=A1=E6=9C=89?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E6=9C=80=E6=96=B0=E7=9A=84=E8=84=9A=E6=9C=AC?= =?UTF-8?q?=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1045655 --user=陈建星 [项目管理]github #32810环境的前置里引入了全局脚本,全局脚本变更后,环境里的未变更 https://www.tapd.cn/55049933/s/1596334 --- .../jmeter/MsCommonElementConverter.java | 2 +- .../api/service/ApiCommonService.java | 85 +++++++++++++++++++ .../api/service/ApiExecuteService.java | 12 +++ 3 files changed, 98 insertions(+), 1 deletion(-) diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/MsCommonElementConverter.java b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/MsCommonElementConverter.java index 05ffa6c636..ae13eb5c5a 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/MsCommonElementConverter.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/MsCommonElementConverter.java @@ -139,7 +139,7 @@ public class MsCommonElementConverter extends AbstractJmeterElementConverter, MsProcessorConverter> getConverterFunc = diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiCommonService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiCommonService.java index 6467c08aa9..4a0aa3e604 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiCommonService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiCommonService.java @@ -22,6 +22,9 @@ import io.metersphere.project.domain.CustomFunctionBlob; import io.metersphere.project.domain.FileAssociation; import io.metersphere.project.domain.FileMetadata; import io.metersphere.project.dto.CommonScriptInfo; +import io.metersphere.project.dto.environment.EnvironmentInfoDTO; +import io.metersphere.project.dto.environment.MsEnvAssertionConfig; +import io.metersphere.project.dto.environment.processors.*; import io.metersphere.project.service.CustomFunctionService; import io.metersphere.project.service.FileAssociationService; import io.metersphere.project.service.FileMetadataService; @@ -29,6 +32,7 @@ import io.metersphere.sdk.constants.ApplicationNumScope; import io.metersphere.sdk.constants.ExecStatus; import io.metersphere.sdk.util.BeanUtils; import io.metersphere.sdk.util.JSON; +import io.metersphere.sdk.util.LogUtils; import io.metersphere.system.domain.ExecTask; import io.metersphere.system.domain.ExecTaskItem; import io.metersphere.system.uid.IDGenerator; @@ -254,6 +258,87 @@ public class ApiCommonService { setEnableCommonScriptInfo(commonScriptInfos); } + /** + * 设置环境前后置的公共脚本信息 + * + * @param envConfig + */ + public void setEnvCommonScriptInfo(EnvironmentInfoDTO envConfig) { + if (envConfig == null || envConfig.getConfig() == null) { + return; + } + try { + // 获取脚本 + List scriptsProcessors = getEnableCommonScriptProcessors(envConfig.getConfig().getPreProcessorConfig()); + scriptsProcessors.addAll(getEnableCommonScriptProcessors(envConfig.getConfig().getPostProcessorConfig())); + + // 获取断言 + List scriptAssertions = getEnableCommonScriptAssertion(envConfig.getConfig().getAssertionConfig()); + + List commonScriptInfos = scriptsProcessors.stream() + .map(ScriptProcessor::getCommonScriptInfo) + .collect(Collectors.toList()); + + List assertionsCommonScriptInfos = scriptAssertions.stream() + .map(MsScriptAssertion::getCommonScriptInfo) + .toList(); + + commonScriptInfos.addAll(assertionsCommonScriptInfos); + // 设置最新的公共脚本信息 + setEnableCommonScriptInfo(commonScriptInfos); + } catch (Exception e) { + LogUtils.error(e); + } + } + + /** + * 获取环境使用公共脚本的前后置 + * + * @param envProcessorConfig + * @return + */ + private List getEnableCommonScriptProcessors(EnvProcessorConfig envProcessorConfig) { + if (envProcessorConfig == null) { + return new ArrayList<>(0); + } + ApiEnvProcessorConfig apiProcessorConfig = envProcessorConfig.getApiProcessorConfig(); + ApiEnvPlanProcessorConfig planProcessorConfig = apiProcessorConfig.getPlanProcessorConfig(); + ApiEnvScenarioProcessorConfig scenarioProcessorConfig = apiProcessorConfig.getScenarioProcessorConfig(); + ApiEnvRequestProcessorConfig requestProcessorConfig = apiProcessorConfig.getRequestProcessorConfig(); + + List processors = new ArrayList<>(); + processors.addAll(planProcessorConfig.getProcessors()); + processors.addAll(scenarioProcessorConfig.getProcessors()); + processors.addAll(requestProcessorConfig.getProcessors()); + + // 获取使用公共脚本的前后置 + return processors.stream() + .filter(processor -> processor instanceof ScriptProcessor) + .map(processor -> (ScriptProcessor) processor) + .filter(ScriptProcessor::isEnableCommonScript) + .filter(ScriptProcessor::isValid) + .collect(Collectors.toList()); + } + + /** + * 获取使用公共脚本的前后置 + * + * @param envAssertionConfig + * @return + */ + private List getEnableCommonScriptAssertion(MsEnvAssertionConfig envAssertionConfig) { + if (envAssertionConfig == null || CollectionUtils.isEmpty(envAssertionConfig.getAssertions())) { + return List.of(); + } + return envAssertionConfig.getAssertions() + .stream() + .filter(assertion -> assertion instanceof MsScriptAssertion) + .map(msAssertion -> (MsScriptAssertion) msAssertion) + .filter(MsScriptAssertion::isEnableCommonScript) + .filter(MsScriptAssertion::isValid) + .toList(); + } + private void setEnableCommonScriptInfo(List commonScriptInfos) { List commonScriptIds = commonScriptInfos.stream() .map(CommonScriptInfo::getId) 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 7e80fb864c..a637938e85 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 @@ -4,6 +4,7 @@ import io.metersphere.api.config.JmeterProperties; import io.metersphere.api.config.KafkaConfig; import io.metersphere.api.controller.result.ApiResultCode; 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.request.controller.MsScriptElement; @@ -18,6 +19,7 @@ import io.metersphere.project.domain.FileMetadata; import io.metersphere.project.domain.ProjectApplication; import io.metersphere.project.dto.CommonScriptInfo; import io.metersphere.project.dto.customfunction.request.CustomFunctionRunRequest; +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.service.*; @@ -648,6 +650,16 @@ public class ApiExecuteService { public String parseExecuteScript(AbstractMsTestElement msTestElement, ParameterConfig config) { // 解析生成脚本 TestElementParser defaultParser = TestElementParserFactory.getDefaultParser(); + // 环境获取最新的公共脚本信息 + if (config instanceof ApiScenarioParamConfig apiScenarioParamConfig) { + Map projectEnvMap = apiScenarioParamConfig.getProjectEnvMap(); + if (projectEnvMap != null) { + projectEnvMap.values().forEach(apiCommonService::setEnvCommonScriptInfo); + } + apiCommonService.setEnvCommonScriptInfo(apiScenarioParamConfig.getEnvConfig()); + } else if (config instanceof ApiParamConfig apiParamConfig) { + apiCommonService.setEnvCommonScriptInfo(apiParamConfig.getEnvConfig()); + } return defaultParser.parse(msTestElement, config); }