From 3d3dd075bf0603501f1dd41cceee651642b835cc Mon Sep 17 00:00:00 2001 From: CaptainB Date: Wed, 12 Jan 2022 13:50:29 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E5=AF=BC=E5=85=A5=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E6=94=AF=E6=8C=81=E9=80=89=E6=8B=A9=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --story=1004981 --user=刘瑞斌 版本对比(X-Pack)-导入数据关于版本的改动 https://www.tapd.cn/55049933/s/1090837 --- .../api/dto/ApiTestImportRequest.java | 2 + .../api/service/ApiAutomationService.java | 47 +++++++++-- .../api/service/ApiDefinitionService.java | 81 +++++++++++++------ .../scenario/common/ScenarioImport.vue | 48 ++++++++++- .../components/import/ApiImport.vue | 50 +++++++++++- frontend/src/i18n/en-US.js | 4 + frontend/src/i18n/zh-CN.js | 4 + frontend/src/i18n/zh-TW.js | 4 + 8 files changed, 205 insertions(+), 35 deletions(-) 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 f23e1c766b..332ce92622 100644 --- a/backend/src/main/java/io/metersphere/api/dto/ApiTestImportRequest.java +++ b/backend/src/main/java/io/metersphere/api/dto/ApiTestImportRequest.java @@ -23,6 +23,8 @@ public class ApiTestImportRequest { //导入策略 private String modeId; private String userId; + private String versionId; // 新导入选择的版本 + private String updateVersionId; // 覆盖导入已存在的接口选择的版本 //调用类型 private String type; // 是否开启自定义ID 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 ea683728f1..138baf9c8b 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java @@ -1069,20 +1069,47 @@ public class ApiAutomationService { } private void _importCreate(List sameRequest, ApiScenarioMapper batchMapper, ApiScenarioWithBLOBs scenarioWithBLOBs, ApiTestImportRequest apiTestImportRequest) { + String defaultVersion = extProjectVersionMapper.getDefaultVersion(apiTestImportRequest.getProjectId()); if (CollectionUtils.isEmpty(sameRequest)) { scenarioWithBLOBs.setId(UUID.randomUUID().toString()); List useUrl = this.parseUrl(scenarioWithBLOBs); scenarioWithBLOBs.setUseUrl(JSONArray.toJSONString(useUrl)); scenarioWithBLOBs.setOrder(getImportNextOrder(apiTestImportRequest.getProjectId())); + // 导入时设置版本 + scenarioWithBLOBs.setRefId(scenarioWithBLOBs.getId()); + if (StringUtils.isNotEmpty(apiTestImportRequest.getVersionId())) { + scenarioWithBLOBs.setVersionId(apiTestImportRequest.getVersionId()); + } else { + scenarioWithBLOBs.setVersionId(defaultVersion); + } batchMapper.insert(scenarioWithBLOBs); apiScenarioReferenceIdService.saveByApiScenario(scenarioWithBLOBs); } else { //如果存在则修改 - scenarioWithBLOBs.setId(sameRequest.get(0).getId()); - scenarioWithBLOBs.setNum(sameRequest.get(0).getNum()); - List useUrl = this.parseUrl(scenarioWithBLOBs); - scenarioWithBLOBs.setUseUrl(JSONArray.toJSONString(useUrl)); - batchMapper.updateByPrimaryKeyWithBLOBs(scenarioWithBLOBs); + if (StringUtils.isEmpty(apiTestImportRequest.getUpdateVersionId())) { + apiTestImportRequest.setUpdateVersionId(defaultVersion); + } + Optional scenarioOp = sameRequest.stream() + .filter(api -> StringUtils.equals(api.getVersionId(), apiTestImportRequest.getUpdateVersionId())) + .findFirst(); + + // 新增对应的版本 + if (!scenarioOp.isPresent()) { + scenarioWithBLOBs.setId(UUID.randomUUID().toString()); + scenarioWithBLOBs.setRefId(sameRequest.get(0).getRefId()); + scenarioWithBLOBs.setVersionId(apiTestImportRequest.getUpdateVersionId()); + scenarioWithBLOBs.setNum(sameRequest.get(0).getNum()); // 使用第一个num当作本次的num + batchMapper.insert(scenarioWithBLOBs); + } else { + ApiScenarioWithBLOBs existScenario = scenarioOp.get(); + scenarioWithBLOBs.setId(existScenario.getId()); + scenarioWithBLOBs.setRefId(existScenario.getRefId()); + scenarioWithBLOBs.setVersionId(apiTestImportRequest.getUpdateVersionId()); + scenarioWithBLOBs.setNum(existScenario.getNum()); + List useUrl = this.parseUrl(scenarioWithBLOBs); + scenarioWithBLOBs.setUseUrl(JSONArray.toJSONString(useUrl)); + batchMapper.updateByPrimaryKeyWithBLOBs(scenarioWithBLOBs); + } apiScenarioReferenceIdService.saveByApiScenario(scenarioWithBLOBs); } } @@ -1143,6 +1170,13 @@ public class ApiAutomationService { scenarioWithBLOBs.setUseUrl(JSONArray.toJSONString(useUrl)); scenarioWithBLOBs.setOrder(getImportNextOrder(request.getProjectId())); scenarioWithBLOBs.setId(UUID.randomUUID().toString()); + scenarioWithBLOBs.setRefId(scenarioWithBLOBs.getId()); + if (StringUtils.isNotEmpty(apiTestImportRequest.getVersionId())) { + scenarioWithBLOBs.setVersionId(apiTestImportRequest.getVersionId()); + } else { + String defaultVersion = extProjectVersionMapper.getDefaultVersion(apiTestImportRequest.getProjectId()); + scenarioWithBLOBs.setVersionId(defaultVersion); + } batchMapper.insert(scenarioWithBLOBs); // 存储依赖关系 @@ -1189,9 +1223,6 @@ public class ApiAutomationService { if (StringUtils.isBlank(item.getId())) { item.setId(UUID.randomUUID().toString()); } - // 导入时设置版本 - item.setVersionId(extProjectVersionMapper.getDefaultVersion(project.getId())); - item.setRefId(item.getId()); importCreate(item, batchMapper, request); if (i % 300 == 0) { sqlSession.flushStatements(); 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 3759928be8..c389af6094 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java @@ -680,7 +680,12 @@ public class ApiDefinitionService { String originId = apiDefinition.getId(); apiDefinition.setId(UUID.randomUUID().toString()); apiDefinition.setRefId(apiDefinition.getId()); - apiDefinition.setVersionId(extProjectVersionMapper.getDefaultVersion(apiTestImportRequest.getProjectId())); + if (StringUtils.isNotEmpty(apiTestImportRequest.getVersionId())) { + apiDefinition.setVersionId(apiTestImportRequest.getVersionId()); + } else { + String defaultVersion = extProjectVersionMapper.getDefaultVersion(apiTestImportRequest.getProjectId()); + apiDefinition.setVersionId(defaultVersion); + } batchMapper.insert(apiDefinition); String requestStr = setImportHashTree(apiDefinition); reSetImportCasesApiId(cases, originId, apiDefinition.getId()); @@ -718,10 +723,15 @@ public class ApiDefinitionService { private void _importCreate(List sameRequest, ApiDefinitionMapper batchMapper, ApiDefinitionWithBLOBs apiDefinition, ApiTestCaseMapper apiTestCaseMapper, ApiTestImportRequest apiTestImportRequest, List cases, List mocks) { String originId = apiDefinition.getId(); + String defaultVersion = extProjectVersionMapper.getDefaultVersion(apiTestImportRequest.getProjectId()); if (CollectionUtils.isEmpty(sameRequest)) { apiDefinition.setId(UUID.randomUUID().toString()); apiDefinition.setRefId(apiDefinition.getId()); - apiDefinition.setVersionId(extProjectVersionMapper.getDefaultVersion(apiTestImportRequest.getProjectId())); + if (StringUtils.isNotEmpty(apiTestImportRequest.getVersionId())) { + apiDefinition.setVersionId(apiTestImportRequest.getVersionId()); + } else { + apiDefinition.setVersionId(defaultVersion); + } apiDefinition.setOrder(getImportNextOrder(apiTestImportRequest.getProjectId())); reSetImportCasesApiId(cases, originId, apiDefinition.getId()); reSetImportMocksApiId(mocks, originId, apiDefinition.getId(), apiDefinition.getNum()); @@ -738,34 +748,55 @@ public class ApiDefinitionService { } } else { - apiDefinition.setStatus(sameRequest.get(0).getStatus()); - apiDefinition.setOriginalState(sameRequest.get(0).getOriginalState()); - apiDefinition.setCaseStatus(sameRequest.get(0).getCaseStatus()); - apiDefinition.setNum(sameRequest.get(0).getNum()); //id 不变 - if (!StringUtils.equalsIgnoreCase(apiTestImportRequest.getPlatform(), ApiImportPlatform.Metersphere.name())) { - apiDefinition.setTags(sameRequest.get(0).getTags()); // 其他格式 tag 不变,MS 格式替换 + //如果存在则修改 + if (StringUtils.isEmpty(apiTestImportRequest.getUpdateVersionId())) { + apiTestImportRequest.setUpdateVersionId(defaultVersion); } - if (StringUtils.equalsIgnoreCase(apiDefinition.getProtocol(), RequestType.HTTP)) { - //如果存在则修改 - apiDefinition.setId(sameRequest.get(0).getId()); - String request = setImportHashTree(apiDefinition); - apiDefinition.setModuleId(sameRequest.get(0).getModuleId()); - apiDefinition.setModulePath(sameRequest.get(0).getModulePath()); - apiDefinition.setOrder(sameRequest.get(0).getOrder()); - apiDefinitionMapper.updateByPrimaryKeyWithBLOBs(apiDefinition); - apiDefinition.setRequest(request); - reSetImportCasesApiId(cases, originId, apiDefinition.getId()); - importApiCase(apiDefinition, apiTestImportRequest); + Optional apiOp = sameRequest.stream() + .filter(api -> StringUtils.equals(api.getVersionId(), apiTestImportRequest.getUpdateVersionId())) + .findFirst(); + + if (!apiOp.isPresent()) { + apiDefinition.setId(UUID.randomUUID().toString()); + apiDefinition.setRefId(sameRequest.get(0).getRefId()); + apiDefinition.setVersionId(apiTestImportRequest.getUpdateVersionId()); + apiDefinition.setNum(sameRequest.get(0).getNum()); // 使用第一个num当作本次的num + batchMapper.insert(apiDefinition); } else { - apiDefinition.setId(sameRequest.get(0).getId()); - if (StringUtils.equalsAnyIgnoreCase(apiDefinition.getProtocol(), RequestType.TCP)) { - setImportTCPHashTree(apiDefinition); + ApiDefinition existApi = apiOp.get(); + apiDefinition.setStatus(existApi.getStatus()); + apiDefinition.setOriginalState(existApi.getOriginalState()); + apiDefinition.setCaseStatus(existApi.getCaseStatus()); + apiDefinition.setNum(existApi.getNum()); //id 不变 + apiDefinition.setRefId(existApi.getRefId()); + apiDefinition.setVersionId(apiTestImportRequest.getUpdateVersionId()); + + if (!StringUtils.equalsIgnoreCase(apiTestImportRequest.getPlatform(), ApiImportPlatform.Metersphere.name())) { + apiDefinition.setTags(existApi.getTags()); // 其他格式 tag 不变,MS 格式替换 + } + if (StringUtils.equalsIgnoreCase(apiDefinition.getProtocol(), RequestType.HTTP)) { + //如果存在则修改 + apiDefinition.setId(existApi.getId()); + String request = setImportHashTree(apiDefinition); + apiDefinition.setModuleId(existApi.getModuleId()); + apiDefinition.setModulePath(existApi.getModulePath()); + apiDefinition.setOrder(existApi.getOrder()); + apiDefinitionMapper.updateByPrimaryKeyWithBLOBs(apiDefinition); + apiDefinition.setRequest(request); + reSetImportCasesApiId(cases, originId, apiDefinition.getId()); + importApiCase(apiDefinition, apiTestImportRequest); + } else { + apiDefinition.setId(existApi.getId()); + if (StringUtils.equalsAnyIgnoreCase(apiDefinition.getProtocol(), RequestType.TCP)) { + setImportTCPHashTree(apiDefinition); + } + apiDefinition.setOrder(existApi.getOrder()); + reSetImportCasesApiId(cases, originId, apiDefinition.getId()); + apiDefinitionMapper.updateByPrimaryKeyWithBLOBs(apiDefinition); } - apiDefinition.setOrder(sameRequest.get(0).getOrder()); - reSetImportCasesApiId(cases, originId, apiDefinition.getId()); - apiDefinitionMapper.updateByPrimaryKeyWithBLOBs(apiDefinition); } + } } 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 18db1b9535..200680930c 100644 --- a/frontend/src/business/components/api/automation/scenario/common/ScenarioImport.vue +++ b/frontend/src/business/components/api/automation/scenario/common/ScenarioImport.vue @@ -29,6 +29,24 @@ + + + + + + + + + + + + + + + @@ -75,7 +93,7 @@ diff --git a/frontend/src/business/components/api/definition/components/import/ApiImport.vue b/frontend/src/business/components/api/definition/components/import/ApiImport.vue index eb5b9ba00f..cb82cee63f 100644 --- a/frontend/src/business/components/api/definition/components/import/ApiImport.vue +++ b/frontend/src/business/components/api/definition/components/import/ApiImport.vue @@ -32,6 +32,24 @@ + + + + + + + + + + + + + + + { + this.versionOptions = response.data.filter(v => v.status === 'open'); + this.versionOptions.forEach(v => { + if (v.latest) { + v.name = v.name + ' ' + this.$t('api_test.api_import.current_version'); + } + }); + }); + } + }, + checkVersionEnable() { + if (!this.projectId) { + return; + } + if (hasLicense()) { + this.$get('/project/version/enable/' + this.projectId, response => { + this.projectVersionEnable = response.data; + }); + } } } } diff --git a/frontend/src/i18n/en-US.js b/frontend/src/i18n/en-US.js index 258ebbf589..8f0902821d 100644 --- a/frontend/src/i18n/en-US.js +++ b/frontend/src/i18n/en-US.js @@ -1671,6 +1671,10 @@ export default { cover_tip_1: "1. Add if the interface path does not exist", cover_tip_2: "2. The interface path is consistent with the original interface, if the content is inconsistent, the original interface will be overwritten", cover_tip_3: "3. If the interface path and content are consistent with the original interface, no change will be made", + import_version: 'Import version', + data_update_version: 'Api update version', + data_new_version: 'Api creation version', + current_version: 'Current version', }, home_page: { unit_of_measurement: "", diff --git a/frontend/src/i18n/zh-CN.js b/frontend/src/i18n/zh-CN.js index f29df0a8e9..de2efd9ccd 100644 --- a/frontend/src/i18n/zh-CN.js +++ b/frontend/src/i18n/zh-CN.js @@ -1676,6 +1676,10 @@ export default { cover_tip_1: "1. 接口路径不存在则新增", cover_tip_2: "2. 接口路径与原接口一致,内容不一致则覆盖原接口", cover_tip_3: "3. 接口路径、内容与原接口一致则不做变更", + import_version: '导入版本', + data_update_version: '数据更新版本', + data_new_version: '数据创建版本', + current_version: '当前版本', }, home_page: { unit_of_measurement: "个", diff --git a/frontend/src/i18n/zh-TW.js b/frontend/src/i18n/zh-TW.js index 5233d7eaac..d4a5171487 100644 --- a/frontend/src/i18n/zh-TW.js +++ b/frontend/src/i18n/zh-TW.js @@ -1676,6 +1676,10 @@ export default { cover_tip_1: "1. 接口路徑不存在則新增", cover_tip_2: "2. 接口路徑與原接口一致,內容不一致則覆蓋原接口", cover_tip_3: "3. 接口路徑、內容與原接口一致則不做變更", + import_version: '導入版本', + data_update_version: '數據更新版本', + data_new_version: '數據創建版本', + current_version: '當前版本', }, home_page: { unit_of_measurement: "個",