diff --git a/backend/src/main/java/io/metersphere/api/dto/ApiTestImportRequest.java b/backend/src/main/java/io/metersphere/api/dto/ApiTestImportRequest.java index 94a518555e..5389d3dc82 100644 --- a/backend/src/main/java/io/metersphere/api/dto/ApiTestImportRequest.java +++ b/backend/src/main/java/io/metersphere/api/dto/ApiTestImportRequest.java @@ -21,4 +21,6 @@ public class ApiTestImportRequest { private String userId; //调用类型 private String type; + // 是否开启自定义ID + private Boolean openCustomNum = false; } 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 910c54b64a..1376b31f37 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java @@ -249,12 +249,16 @@ public class ApiAutomationService { public ApiScenario create(SaveApiScenarioRequest request, List bodyFiles, List scenarioFiles) { request.setId(UUID.randomUUID().toString()); checkNameExist(request); + int nextNum = getNextNum(request.getProjectId()); + if (StringUtils.isBlank(request.getCustomNum())) { + request.setCustomNum(String.valueOf(nextNum)); + } checkScenarioNum(request); final ApiScenarioWithBLOBs scenario = buildSaveScenario(request); scenario.setVersion(0); scenario.setCreateTime(System.currentTimeMillis()); - scenario.setNum(getNextNum(request.getProjectId())); + scenario.setNum(nextNum); List useUrl = this.parseUrl(scenario); scenario.setUseUrl(JSONArray.toJSONString(useUrl)); @@ -287,20 +291,16 @@ public class ApiAutomationService { } private void checkScenarioNum(SaveApiScenarioRequest request) { - if (StringUtils.isNotBlank(request.getCustomNum())) { - String projectId = request.getProjectId(); - Project project = projectMapper.selectByPrimaryKey(projectId); - if (project != null) { - Boolean customNum = project.getScenarioCustomNum(); - // 未开启自定义ID - if (!customNum) { - request.setCustomNum(null); - } else { - checkCustomNumExist(request); - } - } else { - MSException.throwException("add scenario fail, project is not find."); - } + String projectId = request.getProjectId(); + Project project = projectMapper.selectByPrimaryKey(projectId); + + if (project == null) { + MSException.throwException("add scenario fail, project is not find."); + } + + Boolean openCustomNum = project.getScenarioCustomNum(); + if (BooleanUtils.isTrue(openCustomNum)) { + checkCustomNumExist(request); } } @@ -312,7 +312,7 @@ public class ApiAutomationService { .andIdNotEqualTo(request.getId()); List list = apiScenarioMapper.selectByExample(example); if (CollectionUtils.isNotEmpty(list)) { - MSException.throwException("自定义ID已存在!"); + MSException.throwException("自定义ID "+ request.getCustomNum() +" 已存在!"); } } @@ -1671,6 +1671,34 @@ public class ApiAutomationService { scenarioWithBLOBs.setDescription(request.getDescription()); List sameRequest = getWithBLOBs(scenarioWithBLOBs); + + Boolean openCustomNum = apiTestImportRequest.getOpenCustomNum(); + List list = new ArrayList<>(); + if (BooleanUtils.isTrue(openCustomNum)) { + ApiScenarioExample example = new ApiScenarioExample(); + ApiScenarioExample.Criteria criteria = example.createCriteria(); + if (CollectionUtils.isEmpty(sameRequest)) { + criteria.andCustomNumEqualTo(scenarioWithBLOBs.getCustomNum()) + .andProjectIdEqualTo(scenarioWithBLOBs.getProjectId()); + } else { + if (StringUtils.equals("fullCoverage", apiTestImportRequest.getModeId())) { + criteria.andNameEqualTo(scenarioWithBLOBs.getName()) + .andCustomNumEqualTo(scenarioWithBLOBs.getCustomNum()) + .andProjectIdEqualTo(scenarioWithBLOBs.getProjectId()) + .andIdNotEqualTo(sameRequest.get(0).getId()); + } + + } + if (criteria.isValid()) { + list = apiScenarioMapper.selectByExample(example); + } + } + + if (CollectionUtils.isNotEmpty(list)) { + LogUtil.error("import scenario fail, custom num is exist: " + scenarioWithBLOBs.getCustomNum()); + return scenarioWithBLOBs; + } + if (StringUtils.equals("fullCoverage", apiTestImportRequest.getModeId())) { _importCreate(sameRequest, batchMapper, scenarioWithBLOBs, apiTestImportRequest); } else if (StringUtils.equals("incrementalMerge", apiTestImportRequest.getModeId())) { @@ -1695,16 +1723,23 @@ public class ApiAutomationService { if (!CollectionUtils.isEmpty(data) && data.get(0) != null && data.get(0).getProjectId() != null) { num = getNextNum(data.get(0).getProjectId()); project = projectMapper.selectByPrimaryKey(data.get(0).getProjectId()); + request.setOpenCustomNum(project.getScenarioCustomNum()); } for (int i = 0; i < data.size(); i++) { ApiScenarioWithBLOBs item = data.get(i); if (item.getName().length() > 255) { item.setName(item.getName().substring(0, 255)); } - item.setNum(num++); - if (BooleanUtils.isTrue(project.getScenarioCustomNum()) && StringUtils.isBlank(item.getCustomNum())) { + item.setNum(num); + if (BooleanUtils.isFalse(project.getScenarioCustomNum())) { + // 如果未开启,即使有自定值也直接覆盖 item.setCustomNum(String.valueOf(num)); + } else { + if (StringUtils.isBlank(item.getCustomNum())) { + item.setCustomNum(String.valueOf(num)); + } } + num++; importCreate(item, batchMapper, request); if (i % 300 == 0) { sqlSession.flushStatements(); diff --git a/backend/src/main/java/io/metersphere/service/ProjectService.java b/backend/src/main/java/io/metersphere/service/ProjectService.java index 6758474fbc..36ee06b207 100644 --- a/backend/src/main/java/io/metersphere/service/ProjectService.java +++ b/backend/src/main/java/io/metersphere/service/ProjectService.java @@ -239,9 +239,6 @@ public class ProjectService { if (BooleanUtils.isTrue(project.getCustomNum())) { testCaseService.updateTestCaseCustomNumByProjectId(project.getId()); } - if (BooleanUtils.isTrue(project.getScenarioCustomNum())) { - apiAutomationService.updateCustomNumByProjectId(project.getId()); - } projectMapper.updateByPrimaryKeySelective(project); } diff --git a/backend/src/main/resources/db/migration/V90__v1.10.5_release.sql b/backend/src/main/resources/db/migration/V90__v1.10.5_release.sql new file mode 100644 index 0000000000..1f11c8ee13 --- /dev/null +++ b/backend/src/main/resources/db/migration/V90__v1.10.5_release.sql @@ -0,0 +1 @@ +update api_scenario set custom_num = num where (custom_num is null or custom_num = ''); \ No newline at end of file diff --git a/backend/src/main/resources/db/migration/V90__v1.11_release.sql b/backend/src/main/resources/db/migration/V91__v1.11_release.sql similarity index 100% rename from backend/src/main/resources/db/migration/V90__v1.11_release.sql rename to backend/src/main/resources/db/migration/V91__v1.11_release.sql diff --git a/frontend/src/business/components/api/automation/scenario/common/ScenarioImport.vue b/frontend/src/business/components/api/automation/scenario/common/ScenarioImport.vue index 56045f55e5..796e3f37b4 100644 --- a/frontend/src/business/components/api/automation/scenario/common/ScenarioImport.vue +++ b/frontend/src/business/components/api/automation/scenario/common/ScenarioImport.vue @@ -134,7 +134,7 @@ formData: { file: undefined, swaggerUrl: '', - modeId: this.$t('commons.not_cover'), + modeId: 'incrementalMerge', moduleId: '', }, rules: {},