diff --git a/api-test/backend/src/main/java/io/metersphere/api/exec/scenario/ApiScenarioExecuteService.java b/api-test/backend/src/main/java/io/metersphere/api/exec/scenario/ApiScenarioExecuteService.java index e01b23b9b7..3619ed32ed 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/exec/scenario/ApiScenarioExecuteService.java +++ b/api-test/backend/src/main/java/io/metersphere/api/exec/scenario/ApiScenarioExecuteService.java @@ -281,6 +281,11 @@ public class ApiScenarioExecuteService { for (String testPlanScenarioId : request.getProcessVO().getTestPlanScenarioMap().keySet()) { TestPlanApiScenarioInfoDTO planApiScenario = request.getProcessVO().getTestPlanScenarioMap().get(testPlanScenarioId); ApiScenarioWithBLOBs scenario = scenarioMap.get(planApiScenario.getApiScenarioId()); + Set scenarioUsedProjectIdSet = null; + try { + scenarioUsedProjectIdSet = apiScenarioEnvService.getApiScenarioEnv(scenario.getScenarioDefinition()).getProjectIds(); + } catch (Exception ignore) { + } if (scenario.getStepTotal() == null || scenario.getStepTotal() == 0) { continue; } @@ -318,14 +323,17 @@ public class ApiScenarioExecuteService { if (MapUtils.isEmpty(runModeConfig.getEnvMap())) { apiCaseExecuteService.setRunModeConfigEnvironment(runModeConfig, planEnvMap); } - //对报告的envMap做过滤,过滤多余的key - Map diffEnvMap = new HashMap<>(); - planEnvMap.forEach((k, v) -> { - if (StringUtils.equals(planApiScenario.getProjectId(), k)) { - diffEnvMap.put(k, v); - } - }); - runModeConfig.setEnvMap(diffEnvMap); + //对报告的envMap做过滤,通过场景用到的项目来进行匹配,过滤掉使用不到的项目环境 + if (CollectionUtils.isNotEmpty(scenarioUsedProjectIdSet)) { + List scenarioUsedProjectIdList = new ArrayList<>(scenarioUsedProjectIdSet); + Map diffEnvMap = new HashMap<>(); + planEnvMap.forEach((k, v) -> { + if (scenarioUsedProjectIdList.contains(k)) { + diffEnvMap.put(k, v); + } + }); + runModeConfig.setEnvMap(diffEnvMap); + } report.setEnvConfig(JSON.toJSONString(runModeConfig)); } // 生成文档结构 diff --git a/test-track/backend/src/main/java/io/metersphere/plan/service/TestPlanReportService.java b/test-track/backend/src/main/java/io/metersphere/plan/service/TestPlanReportService.java index 75a2c11955..5ad87b486a 100644 --- a/test-track/backend/src/main/java/io/metersphere/plan/service/TestPlanReportService.java +++ b/test-track/backend/src/main/java/io/metersphere/plan/service/TestPlanReportService.java @@ -208,22 +208,12 @@ public class TestPlanReportService { List scenarios) { TestPlanReportRunInfoDTO runInfoDTO = new TestPlanReportRunInfoDTO(); - if (MapUtils.isNotEmpty(config.getEnvMap())) { - //判断记录选择的环境还是默认环境 - Map> requestEnvMap = new HashMap<>(); - for (Map.Entry entry : config.getEnvMap().entrySet()) { - requestEnvMap.put(entry.getKey(), new ArrayList<>() {{ - this.add(entry.getValue()); - }}); - } - runInfoDTO.setRequestEnvMap(requestEnvMap); - } else { - runInfoDTO.setRequestEnvMap(config.getTestPlanDefaultEnvMap()); - } - - final Map runEnvMap = MapUtils.isNotEmpty(config.getEnvMap()) ? config.getEnvMap() : new HashMap<>(); runInfoDTO.setRunMode(config.getMode()); + Map> projectInvMap = TestPlanReportUtil.getTestPlanExecutedEnvironments(config.getTestPlanDefaultEnvMap(), config.getEnvMap()); + runInfoDTO.setRequestEnvMap(projectInvMap); + + final Map runEnvMap = new HashMap<>(); if (StringUtils.equals(GROUP, config.getEnvironmentType()) && StringUtils.isNotEmpty(config.getEnvironmentGroupId())) { Map groupMap = baseEnvGroupProjectService.getEnvMap(config.getEnvironmentGroupId()); if (MapUtils.isNotEmpty(groupMap)) { @@ -1173,7 +1163,7 @@ public class TestPlanReportService { if (MapUtils.isEmpty(runInfoDTO.getRequestEnvMap())) { if (MapUtils.isNotEmpty(runInfoDTO.getApiCaseRunInfo())) { for (Map map : runInfoDTO.getApiCaseRunInfo().values()) { - requestEnvMap = TestPlanReportUtil.mergeApiCaseEnvMap(requestEnvMap, map); + requestEnvMap = TestPlanReportUtil.mergeEnvironmentMap(requestEnvMap, map); } } if (MapUtils.isNotEmpty(runInfoDTO.getScenarioRunInfo())) { diff --git a/test-track/backend/src/main/java/io/metersphere/plan/utils/TestPlanReportUtil.java b/test-track/backend/src/main/java/io/metersphere/plan/utils/TestPlanReportUtil.java index 3501c80fdd..55fe66449d 100644 --- a/test-track/backend/src/main/java/io/metersphere/plan/utils/TestPlanReportUtil.java +++ b/test-track/backend/src/main/java/io/metersphere/plan/utils/TestPlanReportUtil.java @@ -40,7 +40,24 @@ public class TestPlanReportUtil { return r; } - public static Map> mergeApiCaseEnvMap(Map> projectEnvMap, Map originProjectEnvMap) { + //获取测试计划执行时的环境 + public static Map> getTestPlanExecutedEnvironments(Map> defaultEnvMap, Map selectedEnvMap) { + Map> returnMap = new HashMap>(); + if (MapUtils.isNotEmpty(defaultEnvMap)) { + returnMap.putAll(defaultEnvMap); + } + if (MapUtils.isNotEmpty(selectedEnvMap)) { + for (Map.Entry entry : selectedEnvMap.entrySet()) { + String projectId = entry.getKey(); + returnMap.put(projectId, new ArrayList<>() {{ + this.add(entry.getValue()); + }}); + } + } + return returnMap; + } + + public static Map> mergeEnvironmentMap(Map> projectEnvMap, Map originProjectEnvMap) { if (projectEnvMap == null) { projectEnvMap = new HashMap>(); } diff --git a/test-track/frontend/src/business/plan/env/EnvSelectPopover.vue b/test-track/frontend/src/business/plan/env/EnvSelectPopover.vue index 104746cb77..9060c84c20 100644 --- a/test-track/frontend/src/business/plan/env/EnvSelectPopover.vue +++ b/test-track/frontend/src/business/plan/env/EnvSelectPopover.vue @@ -300,16 +300,20 @@ export default { let projectEnvMapElement = this.projectEnvMap[d]; if (projectEnvMapElement.length > 0) { projectEnvMapElement.forEach((envId) => { - let filter = envs.filter((e) => e.id === envId); + let filteredEnv = envs.filter((e) => e.id === envId); if (!this.selectedEnvName.has(d)) { let name = []; - name.push(filter[0].name); - this.selectedEnvName.set(d, name); + if (filteredEnv.length > 0) { + name.push(filteredEnv[0].name); + this.selectedEnvName.set(d, name); + this.$emit("setDefaultEnv", d, filteredEnv[0].id); + } } else { - this.selectedEnvName.get(d).push(filter[0].name); + if (filteredEnv.length > 0) { + this.selectedEnvName.get(d).push(filteredEnv[0].name); + this.$emit("setDefaultEnv", d, filteredEnv[0].id); + } } - - this.$emit("setDefaultEnv", d, filter[0].id); }); } }