diff --git a/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java b/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java index c03dcb39f3..6af3bab094 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java @@ -2029,12 +2029,8 @@ public class ApiAutomationService { Long nextOrder = ServiceUtils.getNextOrder(request.getPlanId(), extTestPlanScenarioCaseMapper::getLastOrder); for (String id : set) { Map newEnvMap = new HashMap<>(16); - if (envMap != null && !envMap.isEmpty()) { - List list = mapping.get(id); - list.forEach(l -> { - newEnvMap.put(l, envMap.get(l)); - }); - } + List list = mapping.get(id); + list.forEach(l -> newEnvMap.put(l, envMap == null ? "" : envMap.getOrDefault(l, ""))); TestPlanApiScenario testPlanApiScenario = new TestPlanApiScenario(); testPlanApiScenario.setId(UUID.randomUUID().toString()); testPlanApiScenario.setCreateUser(SessionUtils.getUserId()); @@ -2042,12 +2038,15 @@ public class ApiAutomationService { testPlanApiScenario.setTestPlanId(request.getPlanId()); testPlanApiScenario.setCreateTime(System.currentTimeMillis()); testPlanApiScenario.setUpdateTime(System.currentTimeMillis()); + String environmentJson = JSON.toJSONString(newEnvMap); if (StringUtils.equals(envType, EnvironmentType.JSON.name())) { - testPlanApiScenario.setEnvironment(JSON.toJSONString(newEnvMap)); + testPlanApiScenario.setEnvironment(environmentJson); testPlanApiScenario.setEnvironmentType(EnvironmentType.JSON.name()); } else if (StringUtils.equals(envType, EnvironmentType.GROUP.name())) { testPlanApiScenario.setEnvironmentType(EnvironmentType.GROUP.name()); testPlanApiScenario.setEnvironmentGroupId(envGroupId); + // JSON类型环境中也保存最新值 + testPlanApiScenario.setEnvironment(environmentJson); } testPlanApiScenario.setOrder(nextOrder); nextOrder += 5000; diff --git a/backend/src/main/java/io/metersphere/track/service/TestPlanService.java b/backend/src/main/java/io/metersphere/track/service/TestPlanService.java index be8335bd29..e860f130a1 100644 --- a/backend/src/main/java/io/metersphere/track/service/TestPlanService.java +++ b/backend/src/main/java/io/metersphere/track/service/TestPlanService.java @@ -1500,21 +1500,10 @@ public class TestPlanService { for (TestPlanApiScenario testPlanApiScenario : testPlanApiScenarios) { String env = testPlanApiScenario.getEnvironment(); - String envType = testPlanApiScenario.getEnvironmentType(); - String envGroupId = testPlanApiScenario.getEnvironmentGroupId(); - if (StringUtils.isBlank(envType)) { + if (StringUtils.isBlank(env)) { continue; } - if ((StringUtils.equals(envType, EnvironmentType.JSON.name()) && StringUtils.isBlank(env)) - || StringUtils.equals(envType, EnvironmentType.GROUP.name()) && StringUtils.isBlank(envGroupId)) { - continue; - } - Map map = new HashMap<>(); - if (StringUtils.equals(envType, EnvironmentType.JSON.name())) { - map = JSON.parseObject(env, Map.class); - } else if (StringUtils.equals(envType, EnvironmentType.GROUP.name())) { - map = environmentGroupProjectService.getEnvMap(envGroupId); - } + Map map = JSON.parseObject(env, Map.class); if (!map.isEmpty()) { Set set = map.keySet(); for (String s : set) { diff --git a/frontend/src/business/components/api/automation/api-automation.js b/frontend/src/business/components/api/automation/api-automation.js index 47d1bcf4be..ea3e7eaa29 100644 --- a/frontend/src/business/components/api/automation/api-automation.js +++ b/frontend/src/business/components/api/automation/api-automation.js @@ -106,3 +106,19 @@ export function saveScenario(url, scenario, scenarioDefinition, _this, success) export function editApiScenarioCaseOrder(request, callback) { return basePost('/api/automation/edit/order', request, callback); } + +export function savePreciseEnvProjectIds(projectIds, envMap) { + if (envMap != null && projectIds != null) { + let keys = envMap.keys(); + for (let key of keys) { + if (!projectIds.has(key)) { + envMap.delete(key); + } + } + for (let id of projectIds) { + if (!envMap.get(id)) { + envMap.set(id, ""); + } + } + } +} diff --git a/frontend/src/business/components/api/automation/scenario/EditApiScenario.vue b/frontend/src/business/components/api/automation/scenario/EditApiScenario.vue index cb617d84f1..0078f72c2b 100644 --- a/frontend/src/business/components/api/automation/scenario/EditApiScenario.vue +++ b/frontend/src/business/components/api/automation/scenario/EditApiScenario.vue @@ -344,7 +344,7 @@ import { } from "@/common/js/utils"; import "@/common/css/material-icons.css" import OutsideClick from "@/common/js/outside-click"; -import {saveScenario} from "@/business/components/api/automation/api-automation"; +import {savePreciseEnvProjectIds, saveScenario} from "@/business/components/api/automation/api-automation"; import MsComponentConfig from "./component/ComponentConfig"; import {ENV_TYPE} from "@/common/js/constants"; @@ -1315,9 +1315,9 @@ export default { } return new Promise((resolve) => { document.getElementById("inputDelay").focus(); // 保存前在input框自动失焦,以免保存失败 - this.$refs['currentScenario'].validate((valid) => { + this.$refs['currentScenario'].validate(async (valid) => { if (valid) { - this.setParameter(); + await this.setParameter(); saveScenario(this.path, this.currentScenario, this.scenarioDefinition, this, (response) => { this.$success(this.$t('commons.save_success')); this.$store.state.scenarioMap.delete(this.currentScenario.id); @@ -1458,7 +1458,7 @@ export default { } } }, - setParameter() { + async setParameter() { this.currentScenario.stepTotal = this.scenarioDefinition.length; if (!this.currentScenario.projectId) { this.currentScenario.projectId = this.projectId; @@ -1483,6 +1483,11 @@ export default { this.formatData(scenario.hashTree); } this.currentScenario.environmentType = this.environmentType; + let definition = JSON.parse(JSON.stringify(this.currentScenario)); + definition.hashTree = this.scenarioDefinition; + await this.getEnv(JSON.stringify(definition)); + // 保存时同步所需要的项目环境 + savePreciseEnvProjectIds(this.projectIds, this.projectEnvMap); this.currentScenario.environmentJson = JSON.stringify(strMapToObj(this.projectEnvMap)); this.currentScenario.environmentGroupId = this.envGroupId; this.currentScenario.scenarioDefinition = scenario;