feat(接口设置环境变量): 修复部分接口无法设置环境变量的问题,修复环境变量值等号结尾被舍弃的问题

修复部分接口无法设置环境变量的问题,修复环境变量值等号结尾被舍弃的问题
This commit is contained in:
song-tianyang 2021-07-07 14:58:38 +08:00 committed by 刘瑞斌
parent f4537b5bbc
commit 8d8073acfd
10 changed files with 25 additions and 17 deletions

View File

@ -108,7 +108,7 @@ public abstract class MsTestElement {
@JSONField(ordinal = 13) @JSONField(ordinal = 13)
private boolean isMockEnvironment; private boolean isMockEnvironment;
@JSONField(ordinal = 14) @JSONField(ordinal = 14)
private String useEnviroment; private String useEnvironment;
private MsTestElement parent; private MsTestElement parent;
private static final String BODY_FILE_DIR = FileUtils.BODY_FILE_DIR; private static final String BODY_FILE_DIR = FileUtils.BODY_FILE_DIR;

View File

@ -36,17 +36,17 @@ public class MsJSR223Processor extends MsTestElement {
@Override @Override
public void toHashTree(HashTree tree, List<MsTestElement> hashTree, ParameterConfig config) { public void toHashTree(HashTree tree, List<MsTestElement> hashTree, ParameterConfig config) {
//替换Metersphere环境变量 //替换Metersphere环境变量
if(StringUtils.isEmpty(this.getUseEnviroment())){ if(StringUtils.isEmpty(this.getUseEnvironment())){
if(config.getConfig() != null){ if(config.getConfig() != null){
if(config.getProjectId() != null){ if(config.getProjectId() != null){
String evnId = config.getConfig().get(config.getProjectId()).getApiEnvironmentid(); String evnId = config.getConfig().get(config.getProjectId()).getApiEnvironmentid();
this.setUseEnviroment(evnId); this.setUseEnvironment(evnId);
}else { }else {
Collection<EnvironmentConfig> evnConfigList = config.getConfig().values(); Collection<EnvironmentConfig> evnConfigList = config.getConfig().values();
if(evnConfigList!=null && !evnConfigList.isEmpty()){ if(evnConfigList!=null && !evnConfigList.isEmpty()){
for (EnvironmentConfig configItem : evnConfigList) { for (EnvironmentConfig configItem : evnConfigList) {
String evnId = configItem.getApiEnvironmentid(); String evnId = configItem.getApiEnvironmentid();
this.setUseEnviroment(evnId); this.setUseEnvironment(evnId);
break; break;
} }
} }
@ -54,7 +54,7 @@ public class MsJSR223Processor extends MsTestElement {
} }
} }
script = StringUtils.replace(script, RunningParamKeys.API_ENVIRONMENT_ID,"\""+RunningParamKeys.RUNNING_PARAMS_PREFIX+this.getUseEnviroment()+".\""); script = StringUtils.replace(script, RunningParamKeys.API_ENVIRONMENT_ID,"\""+RunningParamKeys.RUNNING_PARAMS_PREFIX+this.getUseEnvironment()+".\"");
// 非导出操作且不是启用状态则跳过执行 // 非导出操作且不是启用状态则跳过执行
if (!config.isOperating() && !this.isEnable()) { if (!config.isOperating() && !this.isEnable()) {

View File

@ -32,17 +32,17 @@ public class MsJSR223PostProcessor extends MsTestElement {
@Override @Override
public void toHashTree(HashTree tree, List<MsTestElement> hashTree, ParameterConfig config) { public void toHashTree(HashTree tree, List<MsTestElement> hashTree, ParameterConfig config) {
if(StringUtils.isEmpty(this.getUseEnviroment())){ if(StringUtils.isEmpty(this.getUseEnvironment())){
if(config.getConfig() != null){ if(config.getConfig() != null){
if(config.getProjectId() != null){ if(config.getProjectId() != null){
String evnId = config.getConfig().get(config.getProjectId()).getApiEnvironmentid(); String evnId = config.getConfig().get(config.getProjectId()).getApiEnvironmentid();
this.setUseEnviroment(evnId); this.setUseEnvironment(evnId);
}else { }else {
Collection<EnvironmentConfig> evnConfigList = config.getConfig().values(); Collection<EnvironmentConfig> evnConfigList = config.getConfig().values();
if(evnConfigList!=null && !evnConfigList.isEmpty()){ if(evnConfigList!=null && !evnConfigList.isEmpty()){
for (EnvironmentConfig configItem : evnConfigList) { for (EnvironmentConfig configItem : evnConfigList) {
String evnId = configItem.getApiEnvironmentid(); String evnId = configItem.getApiEnvironmentid();
this.setUseEnviroment(evnId); this.setUseEnvironment(evnId);
break; break;
} }
} }
@ -50,7 +50,7 @@ public class MsJSR223PostProcessor extends MsTestElement {
} }
} }
//替换Metersphere环境变量 //替换Metersphere环境变量
script = StringUtils.replace(script,RunningParamKeys.API_ENVIRONMENT_ID,"\""+RunningParamKeys.RUNNING_PARAMS_PREFIX+this.getUseEnviroment()+".\""); script = StringUtils.replace(script,RunningParamKeys.API_ENVIRONMENT_ID,"\""+RunningParamKeys.RUNNING_PARAMS_PREFIX+this.getUseEnvironment()+".\"");
// 非导出操作且不是启用状态则跳过执行 // 非导出操作且不是启用状态则跳过执行
if (!config.isOperating() && !this.isEnable()) { if (!config.isOperating() && !this.isEnable()) {

View File

@ -32,17 +32,17 @@ public class MsJSR223PreProcessor extends MsTestElement {
@Override @Override
public void toHashTree(HashTree tree, List<MsTestElement> hashTree, ParameterConfig config) { public void toHashTree(HashTree tree, List<MsTestElement> hashTree, ParameterConfig config) {
if(StringUtils.isEmpty(this.getUseEnviroment())){ if(StringUtils.isEmpty(this.getUseEnvironment())){
if(config.getConfig() != null){ if(config.getConfig() != null){
if(config.getProjectId() != null){ if(config.getProjectId() != null){
String evnId = config.getConfig().get(config.getProjectId()).getApiEnvironmentid(); String evnId = config.getConfig().get(config.getProjectId()).getApiEnvironmentid();
this.setUseEnviroment(evnId); this.setUseEnvironment(evnId);
}else { }else {
Collection<EnvironmentConfig> evnConfigList = config.getConfig().values(); Collection<EnvironmentConfig> evnConfigList = config.getConfig().values();
if(evnConfigList!=null && !evnConfigList.isEmpty()){ if(evnConfigList!=null && !evnConfigList.isEmpty()){
for (EnvironmentConfig configItem : evnConfigList) { for (EnvironmentConfig configItem : evnConfigList) {
String evnId = configItem.getApiEnvironmentid(); String evnId = configItem.getApiEnvironmentid();
this.setUseEnviroment(evnId); this.setUseEnvironment(evnId);
break; break;
} }
} }
@ -50,7 +50,7 @@ public class MsJSR223PreProcessor extends MsTestElement {
} }
} }
//替换Metersphere环境变量 //替换Metersphere环境变量
script = StringUtils.replace(script,RunningParamKeys.API_ENVIRONMENT_ID,"\""+RunningParamKeys.RUNNING_PARAMS_PREFIX+this.getUseEnviroment()+".\""); script = StringUtils.replace(script,RunningParamKeys.API_ENVIRONMENT_ID,"\""+RunningParamKeys.RUNNING_PARAMS_PREFIX+this.getUseEnvironment()+".\"");
// 非导出操作且不是启用状态则跳过执行 // 非导出操作且不是启用状态则跳过执行
if (!config.isOperating() && !this.isEnable()) { if (!config.isOperating() && !this.isEnable()) {

View File

@ -252,7 +252,7 @@ public class MsHTTPSamplerProxy extends MsTestElement {
if (CollectionUtils.isNotEmpty(hashTree)) { if (CollectionUtils.isNotEmpty(hashTree)) {
for (MsTestElement el : hashTree) { for (MsTestElement el : hashTree) {
el.setUseEnviroment(useEnvironment); el.setUseEnvironment(useEnvironment);
el.toHashTree(httpSamplerTree, el.getHashTree(), config); el.toHashTree(httpSamplerTree, el.getHashTree(), config);
} }
} }
@ -265,6 +265,10 @@ public class MsHTTPSamplerProxy extends MsTestElement {
private HttpConfig getHttpConfig(ParameterConfig config) { private HttpConfig getHttpConfig(ParameterConfig config) {
if (config.isEffective(this.getProjectId())) { if (config.isEffective(this.getProjectId())) {
String useEvnId = config.getConfig().get(this.getProjectId()).getApiEnvironmentid();
if(StringUtils.isNotEmpty(useEvnId) && !StringUtils.equals(useEvnId,this.getUseEnvironment())){
this.setUseEnvironment(useEvnId);
}
return getHttpConfig(config.getConfig().get(this.getProjectId()).getHttpConfig()); return getHttpConfig(config.getConfig().get(this.getProjectId()).getHttpConfig());
} }
return null; return null;

View File

@ -10,6 +10,7 @@ import java.util.stream.Collectors;
@Data @Data
public class HttpConfig { public class HttpConfig {
private String apiEnvironmentid;
private String socket; private String socket;
private String domain; private String domain;
private String protocol = "https"; private String protocol = "https";

View File

@ -102,8 +102,9 @@ public class ApiEnvironmentRunningParamService {
String envId = envAndKeyArr[0]; String envId = envAndKeyArr[0];
String [] keyArr = ArrayUtils.remove(envAndKeyArr,0); String [] keyArr = ArrayUtils.remove(envAndKeyArr,0);
String key = StringUtils.join(keyArr,"."); String key = StringUtils.join(keyArr,".");
String [] valueArr = ArrayUtils.remove(envItem,0); // String [] valueArr = ArrayUtils.remove(envItem,0);
String value = StringUtils.join(valueArr,"="); // String value = StringUtils.join(valueArr,"=");
String value = StringUtils.substring(env,jmeterVarKey.length()+1);
if(StringUtils.isNoneEmpty(envId,key,value)){ if(StringUtils.isNoneEmpty(envId,key,value)){
this.addParam(envId,key,value); this.addParam(envId,key,value);
} }

View File

@ -49,6 +49,7 @@ import {saveScenario} from "@/business/components/api/automation/api-automation"
this.$emit('runRefresh', {}); this.$emit('runRefresh', {});
this.$emit("changeDebugButton"); this.$emit("changeDebugButton");
}); });
alert("123");
}, },
} }
} }

View File

@ -902,6 +902,7 @@ export default {
promise.then(() => { promise.then(() => {
let sign = this.$refs.envPopover.checkEnv(this.isFullUrl); let sign = this.$refs.envPopover.checkEnv(this.isFullUrl);
if (!sign) { if (!sign) {
this.buttonIsLoading = false;
return; return;
} }
// //

View File

@ -43,7 +43,7 @@
}, },
{ {
title: this.$t('api_test.request.processor.param_environment_set_global_variable'), title: this.$t('api_test.request.processor.param_environment_set_global_variable'),
value: 'vars.put(${__metersphere_env_id}+"key","value");\n'+'vars.put("key","value");', value: 'vars.put(${__metersphere_env_id}+"key","value");\n'+'vars.put("key","value")',
}, },
{ {
title: this.$t('api_test.request.processor.code_add_report_length'), title: this.$t('api_test.request.processor.code_add_report_length'),