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 9bf2b5ae19..58f2d4e90a 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java @@ -1206,15 +1206,26 @@ public class ApiAutomationService { ApiScenarioWithBLOBs scenarioWithBLOBs, ApiTestImportRequest apiTestImportRequest, ApiTestCaseMapper apiTestCaseMapper, ApiDefinitionMapper apiDefinitionMapper) { if (CollectionUtils.isEmpty(sameRequest)) { scenarioWithBLOBs.setId(UUID.randomUUID().toString()); - scenarioWithBLOBs.setOrder(getImportNextOrder(apiTestImportRequest.getProjectId())); - // 导入时设置版本 - scenarioWithBLOBs.setRefId(scenarioWithBLOBs.getId()); - if (StringUtils.isNotEmpty(apiTestImportRequest.getVersionId())) { - scenarioWithBLOBs.setVersionId(apiTestImportRequest.getVersionId()); + scenarioWithBLOBs.setCreateTime(System.currentTimeMillis()); + if (!scenarioWithBLOBs.getVersionId().equals("update")) { + scenarioWithBLOBs.setOrder(getImportNextOrder(apiTestImportRequest.getProjectId())); + // 导入时设置版本 + scenarioWithBLOBs.setRefId(scenarioWithBLOBs.getId()); + if (StringUtils.isNotEmpty(apiTestImportRequest.getVersionId())) { + scenarioWithBLOBs.setVersionId(apiTestImportRequest.getVersionId()); + } else { + scenarioWithBLOBs.setVersionId(apiTestImportRequest.getDefaultVersion()); + } + scenarioWithBLOBs.setLatest(true); } else { - scenarioWithBLOBs.setVersionId(apiTestImportRequest.getDefaultVersion()); + if (StringUtils.isNotEmpty(apiTestImportRequest.getUpdateVersionId())) { + scenarioWithBLOBs.setVersionId(apiTestImportRequest.getUpdateVersionId()); + } else { + scenarioWithBLOBs.setVersionId(apiTestImportRequest.getDefaultVersion()); + } + scenarioWithBLOBs.setLatest(apiTestImportRequest.getVersionId().equals(apiTestImportRequest.getDefaultVersion())); } - scenarioWithBLOBs.setLatest(true); + checkReferenceCase(scenarioWithBLOBs, apiTestCaseMapper, apiDefinitionMapper); batchMapper.insert(scenarioWithBLOBs); apiScenarioReferenceIdService.saveApiAndScenarioRelation(scenarioWithBLOBs); @@ -1230,10 +1241,14 @@ public class ApiAutomationService { // 新增对应的版本 if (!scenarioOp.isPresent()) { scenarioWithBLOBs.setId(UUID.randomUUID().toString()); - scenarioWithBLOBs.setRefId(sameRequest.get(0).getRefId()); + scenarioWithBLOBs.setCreateTime(System.currentTimeMillis()); scenarioWithBLOBs.setVersionId(apiTestImportRequest.getUpdateVersionId()); - scenarioWithBLOBs.setNum(sameRequest.get(0).getNum()); // 使用第一个num当作本次的num - scenarioWithBLOBs.setOrder(sameRequest.get(0).getOrder()); + scenarioWithBLOBs.setLatest(apiTestImportRequest.getVersionId().equals(apiTestImportRequest.getDefaultVersion())); + if (!scenarioWithBLOBs.getVersionId().equals("update")) { + scenarioWithBLOBs.setRefId(sameRequest.get(0).getRefId()); + scenarioWithBLOBs.setNum(sameRequest.get(0).getNum()); // 使用第一个num当作本次的num + scenarioWithBLOBs.setOrder(sameRequest.get(0).getOrder()); + } batchMapper.insert(scenarioWithBLOBs); } else { ApiScenarioWithBLOBs existScenario = scenarioOp.get(); @@ -1255,7 +1270,6 @@ public class ApiAutomationService { ApiTestImportRequest apiTestImportRequest, List sameList, ApiTestCaseMapper apiTestCaseMapper, ApiDefinitionMapper apiDefinitionMapper) { final ApiScenarioWithBLOBs scenarioWithBLOBs = new ApiScenarioWithBLOBs(); BeanUtils.copyBean(scenarioWithBLOBs, request); - scenarioWithBLOBs.setCreateTime(System.currentTimeMillis()); scenarioWithBLOBs.setUpdateTime(System.currentTimeMillis()); if (StringUtils.isEmpty(scenarioWithBLOBs.getStatus())) { scenarioWithBLOBs.setStatus(APITestStatus.Underway.name()); @@ -1302,6 +1316,7 @@ public class ApiAutomationService { _importCreate(sameList, batchMapper, extApiScenarioMapper, scenarioWithBLOBs, apiTestImportRequest, apiTestCaseMapper, apiDefinitionMapper); } else if (StringUtils.equals("incrementalMerge", apiTestImportRequest.getModeId())) { scenarioWithBLOBs.setId(UUID.randomUUID().toString()); + scenarioWithBLOBs.setCreateTime(System.currentTimeMillis()); if (CollectionUtils.isEmpty(sameList)) { if (scenarioWithBLOBs.getVersionId() != null && scenarioWithBLOBs.getVersionId().equals("new")) { scenarioWithBLOBs.setLatest(apiTestImportRequest.getVersionId().equals(apiTestImportRequest.getDefaultVersion())); @@ -1378,7 +1393,7 @@ public class ApiAutomationService { if (item.getName().length() > 255) { item.setName(item.getName().substring(0, 255)); } - if (item.getVersionId() == null || !item.getVersionId().equals("new")) { + if (item.getVersionId() == null || (!item.getVersionId().equals("new") && !item.getVersionId().equals("update"))) { item.setNum(num); } if (BooleanUtils.isFalse(request.getOpenCustomNum())) { diff --git a/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java b/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java index 484d402252..39e311ed48 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java @@ -790,8 +790,7 @@ public class ApiDefinitionService { List updateList, ApiTestCaseMapper apiTestCaseMapper, List caseList) { SaveApiDefinitionRequest saveReq = new SaveApiDefinitionRequest(); BeanUtils.copyBean(saveReq, apiDefinition); - apiDefinition.setCreateTime(System.currentTimeMillis()); - apiDefinition.setUpdateTime(System.currentTimeMillis()); + if (StringUtils.isEmpty(apiDefinition.getStatus())) { apiDefinition.setStatus(APITestStatus.Underway.name()); } @@ -809,6 +808,8 @@ public class ApiDefinitionService { if (CollectionUtils.isEmpty(collect)) { String originId = apiDefinition.getId(); apiDefinition.setId(UUID.randomUUID().toString()); + apiDefinition.setCreateTime(System.currentTimeMillis()); + apiDefinition.setUpdateTime(System.currentTimeMillis()); //postman 可能含有前置脚本,接口定义去掉脚本 if (apiDefinition.getVersionId() != null && apiDefinition.getVersionId().equals("new")) { apiDefinition.setLatest(apiTestImportRequest.getVersionId().equals(apiTestImportRequest.getDefaultVersion())); @@ -850,9 +851,7 @@ public class ApiDefinitionService { if (apiTestCaseWithBLOBs.getCreateTime() == null) { apiTestCaseWithBLOBs.setCreateTime(System.currentTimeMillis()); } - if (apiTestCaseWithBLOBs.getUpdateTime() == null) { - apiTestCaseWithBLOBs.setUpdateTime(System.currentTimeMillis()); - } + apiTestCaseWithBLOBs.setUpdateTime(System.currentTimeMillis()); if (StringUtils.isNotBlank(apiTestCaseWithBLOBs.getId())) { apiTestCaseMapper.updateByPrimaryKeyWithBLOBs(apiTestCaseWithBLOBs); } else { @@ -895,14 +894,27 @@ public class ApiDefinitionService { if (CollectionUtils.isEmpty(sameRequest)) { // 没有这个接口 新增 apiDefinition.setId(UUID.randomUUID().toString()); - apiDefinition.setRefId(apiDefinition.getId()); - if (StringUtils.isNotEmpty(apiTestImportRequest.getVersionId())) { - apiDefinition.setVersionId(apiTestImportRequest.getVersionId()); + + apiDefinition.setCreateTime(System.currentTimeMillis()); + apiDefinition.setUpdateTime(System.currentTimeMillis()); + if (apiDefinition.getVersionId().equals("update")) { + if (StringUtils.isNotEmpty(apiTestImportRequest.getUpdateVersionId())) { + apiDefinition.setVersionId(apiTestImportRequest.getUpdateVersionId()); + } else { + apiDefinition.setVersionId(apiTestImportRequest.getDefaultVersion()); + } + apiDefinition.setLatest(apiTestImportRequest.getVersionId().equals(apiTestImportRequest.getDefaultVersion())); } else { - apiDefinition.setVersionId(apiTestImportRequest.getDefaultVersion()); + apiDefinition.setRefId(apiDefinition.getId()); + apiDefinition.setLatest(true); // 新增接口 latest = true + apiDefinition.setOrder(getImportNextOrder(apiTestImportRequest.getProjectId())); + if (StringUtils.isNotEmpty(apiTestImportRequest.getVersionId())) { + apiDefinition.setVersionId(apiTestImportRequest.getVersionId()); + } else { + apiDefinition.setVersionId(apiTestImportRequest.getDefaultVersion()); + } } - apiDefinition.setLatest(true); // 新增接口 latest = true - apiDefinition.setOrder(getImportNextOrder(apiTestImportRequest.getProjectId())); + reSetImportMocksApiId(mocks, originId, apiDefinition.getId(), apiDefinition.getNum()); if (StringUtils.equalsIgnoreCase(apiDefinition.getProtocol(), RequestType.HTTP)) { batchMapper.insert(apiDefinition); @@ -925,14 +937,20 @@ public class ApiDefinitionService { if (!apiOp.isPresent()) { apiDefinition.setId(UUID.randomUUID().toString()); - if (sameRequest.get(0).getRefId() != null) { - apiDefinition.setRefId(sameRequest.get(0).getRefId()); - } else { - apiDefinition.setRefId(apiDefinition.getId()); + apiDefinition.setCreateTime(System.currentTimeMillis()); + apiDefinition.setUpdateTime(System.currentTimeMillis()); + if (!apiDefinition.getVersionId().equals("update")) { + if (sameRequest.get(0).getRefId() != null) { + apiDefinition.setRefId(sameRequest.get(0).getRefId()); + } else { + apiDefinition.setRefId(apiDefinition.getId()); + } + apiDefinition.setNum(sameRequest.get(0).getNum()); // 使用第一个num当作本次的num + apiDefinition.setOrder(sameRequest.get(0).getOrder()); } + apiDefinition.setLatest(apiTestImportRequest.getVersionId().equals(apiTestImportRequest.getDefaultVersion())); apiDefinition.setVersionId(apiTestImportRequest.getUpdateVersionId()); - apiDefinition.setNum(sameRequest.get(0).getNum()); // 使用第一个num当作本次的num - apiDefinition.setOrder(sameRequest.get(0).getOrder()); + if (sameRequest.get(0).getUserId() != null) { apiDefinition.setUserId(sameRequest.get(0).getUserId()); } @@ -956,9 +974,11 @@ public class ApiDefinitionService { //Check whether the content has changed, if not, do not change the creation time if (apiDefinition.getProtocol().equals("HTTP")) { Boolean toChangeTime = checkIsSynchronize(existApi, apiDefinition); - if (!toChangeTime) { - apiDefinition.setUpdateTime(existApi.getUpdateTime()); + if (toChangeTime) { + apiDefinition.setUpdateTime(System.currentTimeMillis()); } + } else { + apiDefinition.setUpdateTime(System.currentTimeMillis()); } if (!StringUtils.equalsIgnoreCase(apiTestImportRequest.getPlatform(), ApiImportPlatform.Metersphere.name())) { @@ -1509,7 +1529,7 @@ public class ApiDefinitionService { item.setName(item.getName().substring(0, 255)); } //如果是创建版本数据,则num和其他版本数据一致 - if (item.getVersionId() == null || !item.getVersionId().equals("new")) { + if (item.getVersionId() == null || (!item.getVersionId().equals("new") && !item.getVersionId().equals("update"))) { item.setNum(num++); } //如果EsbData需要存储,则需要进行接口是否更新的判断 diff --git a/backend/src/main/java/io/metersphere/api/service/ApiModuleService.java b/backend/src/main/java/io/metersphere/api/service/ApiModuleService.java index 04d20570d1..02c7bb2fba 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiModuleService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiModuleService.java @@ -854,18 +854,20 @@ public class ApiModuleService extends NodeTreeService { //不覆盖选择版本,如果被选版本有同接口,不导入,否则创建新版本接口 if (v.getVersionId().equals(versionId)) { optionData.remove(apiDefinitionWithBLOBs); - distinctNameCases.forEach(optionDataCases::remove); + if (distinctNameCases != null && !distinctNameCases.isEmpty()) { + distinctNameCases.forEach(optionDataCases::remove); + } } else { //这里是为了标识当前数据是需要创建版本的,不是全新增的数据 - addNewVersionApi(apiDefinitionWithBLOBs, v); + addNewVersionApi(apiDefinitionWithBLOBs, v, "new"); } } }); } } - private void addNewVersionApi(ApiDefinitionWithBLOBs apiDefinitionWithBLOBs, ApiDefinitionWithBLOBs v) { - apiDefinitionWithBLOBs.setVersionId("new"); + private void addNewVersionApi(ApiDefinitionWithBLOBs apiDefinitionWithBLOBs, ApiDefinitionWithBLOBs v, String version) { + apiDefinitionWithBLOBs.setVersionId(version); apiDefinitionWithBLOBs.setNum(v.getNum()); apiDefinitionWithBLOBs.setStatus(v.getStatus()); apiDefinitionWithBLOBs.setOrder(v.getOrder()); @@ -884,6 +886,11 @@ public class ApiModuleService extends NodeTreeService { repeatDataMap.forEach((k, v) -> { ApiDefinitionWithBLOBs apiDefinitionWithBLOBs = nameModuleMap.get(k); if (apiDefinitionWithBLOBs != null) { + //系统内重复的数据的版本如果不是选择的数据更新版本,则在数据更新版本新增,否则更新这个版本的数据 + if (!v.getVersionId().equals(updateVersionId)) { + addNewVersionApi(apiDefinitionWithBLOBs, v, "update"); + return; + } //该接口的case Map caseNameMap = getDistinctCaseNameMap(definitionIdCaseMAp, apiDefinitionWithBLOBs); apiDefinitionWithBLOBs.setId(v.getId()); @@ -896,6 +903,7 @@ public class ApiModuleService extends NodeTreeService { apiDefinitionWithBLOBs.setRefId(v.getRefId()); apiDefinitionWithBLOBs.setLatest(v.getLatest()); apiDefinitionWithBLOBs.setCreateTime(v.getCreateTime()); + apiDefinitionWithBLOBs.setUpdateTime(v.getUpdateTime()); //组合case if (caseNameMap != null) { buildCaseList(oldCaseMap, caseNameMap, v); @@ -931,6 +939,11 @@ public class ApiModuleService extends NodeTreeService { repeatDataMap.forEach((k, v) -> { ApiDefinitionWithBLOBs apiDefinitionWithBLOBs = nameModuleMap.get(k); if (apiDefinitionWithBLOBs != null) { + //系统内重复的数据的版本如果不是选择的数据更新版本,则在数据更新版本新增,否则更新这个版本的数据 + if (!v.getVersionId().equals(updateVersionId)) { + addNewVersionApi(apiDefinitionWithBLOBs, v, "update"); + return; + } //该接口的case Map caseNameMap = getDistinctCaseNameMap(definitionIdCaseMAp, apiDefinitionWithBLOBs); apiDefinitionWithBLOBs.setId(v.getId()); @@ -1024,10 +1037,12 @@ public class ApiModuleService extends NodeTreeService { List sameVersionList = v.stream().filter(t -> t.getVersionId().equals(versionId)).collect(Collectors.toList()); if (!sameVersionList.isEmpty()) { optionData.remove(apiDefinitionWithBLOBs); - distinctNameCases.forEach(optionDataCases::remove); + if (distinctNameCases != null && !distinctNameCases.isEmpty()) { + distinctNameCases.forEach(optionDataCases::remove); + } } else { for (ApiDefinitionWithBLOBs definitionWithBLOBs : v) { - addNewVersionApi(apiDefinitionWithBLOBs, definitionWithBLOBs); + addNewVersionApi(apiDefinitionWithBLOBs, definitionWithBLOBs, "new"); } } } @@ -1050,6 +1065,7 @@ public class ApiModuleService extends NodeTreeService { apiDefinitionWithBLOBs.setRefId(definitionWithBLOBs.getRefId()); apiDefinitionWithBLOBs.setLatest(definitionWithBLOBs.getLatest()); apiDefinitionWithBLOBs.setCreateTime(definitionWithBLOBs.getCreateTime()); + apiDefinitionWithBLOBs.setUpdateTime(definitionWithBLOBs.getUpdateTime()); } private void removeModulePath(Map moduleMap, Map> moduleOptionData, String modulePath) { @@ -1078,7 +1094,12 @@ public class ApiModuleService extends NodeTreeService { //该接口的case Map caseNameMap = getDistinctCaseNameMap(definitionIdCaseMAp, apiDefinitionWithBLOBs); //循环系统内重复接口 + int i = 0; for (ApiDefinitionWithBLOBs definitionWithBLOBs : v) { + if (!definitionWithBLOBs.getVersionId().equals(updateVersionId)) { + i += 1; + continue; + } //组合case if (caseNameMap != null) { buildCaseList(oldCaseMap, caseNameMap, definitionWithBLOBs); @@ -1094,9 +1115,16 @@ public class ApiModuleService extends NodeTreeService { api.setNum(definitionWithBLOBs.getNum()); api.setStatus(definitionWithBLOBs.getStatus()); api.setCreateTime(definitionWithBLOBs.getCreateTime()); + api.setUpdateTime(definitionWithBLOBs.getUpdateTime()); coverApiList.add(api); } - optionData.remove(apiDefinitionWithBLOBs); + if (i == v.size()) { + //如果系统内的所有版本都不是当前选择的数据更新版本,则在数据更新版本这里新建数据 + addNewVersionApi(apiDefinitionWithBLOBs, v.get(0), "update"); + + } else { + optionData.remove(apiDefinitionWithBLOBs); + } } }); buildOtherParam(toUpdateList, optionData, coverApiList); @@ -1110,7 +1138,6 @@ public class ApiModuleService extends NodeTreeService { Map oldCaseNameMap; if (apiTestCases != null && !apiTestCases.isEmpty()) { oldCaseNameMap = apiTestCases.stream().collect(Collectors.toMap(ApiTestCase::getName, testCase -> testCase)); - caseNameMap.forEach((name, caseWithBLOBs1) -> { //如果导入的有重名,覆盖,接口ID替换成系统内的 caseWithBLOBs1.setApiDefinitionId(definitionWithBLOBs.getId()); @@ -1147,7 +1174,12 @@ public class ApiModuleService extends NodeTreeService { if (apiDefinitionWithBLOBs != null) { //该接口的case Map caseNameMap = getDistinctCaseNameMap(definitionIdCaseMAp, apiDefinitionWithBLOBs); + int i = 0; for (ApiDefinitionWithBLOBs definitionWithBLOBs : v) { + if (!definitionWithBLOBs.getVersionId().equals(updateVersionId)) { + i += 1; + continue; + } //组合case if (caseNameMap != null) { buildCaseList(oldCaseMap, caseNameMap, definitionWithBLOBs); @@ -1165,9 +1197,16 @@ public class ApiModuleService extends NodeTreeService { api.setRefId(apiDefinitionWithBLOBs.getRefId()); api.setLatest(apiDefinitionWithBLOBs.getLatest()); api.setCreateTime(definitionWithBLOBs.getCreateTime()); + api.setUpdateTime(definitionWithBLOBs.getUpdateTime()); coverApiList.add(api); } - optionData.remove(apiDefinitionWithBLOBs); + if (i == v.size()) { + //如果系统内的所有版本都不是当前选择的数据更新版本,则在数据更新版本这里新建数据 + addNewVersionApi(apiDefinitionWithBLOBs, v.get(0), "update"); + + } else { + optionData.remove(apiDefinitionWithBLOBs); + } } }); buildOtherParam(toUpdateList, optionData, coverApiList); diff --git a/backend/src/main/java/io/metersphere/api/service/ApiScenarioModuleService.java b/backend/src/main/java/io/metersphere/api/service/ApiScenarioModuleService.java index 36a515bd5a..04a862edd9 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiScenarioModuleService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiScenarioModuleService.java @@ -592,19 +592,23 @@ public class ApiScenarioModuleService extends NodeTreeService moduleMap, Map> moduleOptionData, String modulePath) { if (StringUtils.isBlank(modulePath)) { return; @@ -623,6 +627,10 @@ public class ApiScenarioModuleService extends NodeTreeService { ApiScenarioWithBLOBs apiScenarioWithBLOBs = nameModuleMap.get(k); if (apiScenarioWithBLOBs != null) { + if (!v.getVersionId().equals(updateVersionId)) { + addNewVersionScenario(apiScenarioWithBLOBs, v, "update"); + return; + } apiScenarioWithBLOBs.setId(v.getId()); apiScenarioWithBLOBs.setVersionId(updateVersionId); apiScenarioWithBLOBs.setApiScenarioModuleId(v.getApiScenarioModuleId()); @@ -633,6 +641,7 @@ public class ApiScenarioModuleService extends NodeTreeService { ApiScenarioWithBLOBs apiScenarioWithBLOBs = nameModuleMap.get(k); if (apiScenarioWithBLOBs != null) { + if (!v.getVersionId().equals(updateVersionId)) { + addNewVersionScenario(apiScenarioWithBLOBs, v, "update"); + return; + } apiScenarioWithBLOBs.setId(v.getId()); apiScenarioWithBLOBs.setVersionId(updateVersionId); apiScenarioWithBLOBs.setNum(v.getNum()); @@ -669,6 +682,7 @@ public class ApiScenarioModuleService extends NodeTreeService