refactor(接口测试): 接口导出只导出关联的模块信息

--bug=1014652 --user=陈建星 【接口测试】接口定义-选中一个接口导出MS文件-查看该文件中包含该项目的所有模块列表 {#_orginal_url#}
This commit is contained in:
chenjianxing 2022-07-07 14:10:47 +08:00 committed by jianxing
parent befa6abd2e
commit 0629e803ac
2 changed files with 48 additions and 19 deletions

View File

@ -1907,34 +1907,32 @@ public class ApiDefinitionService {
} }
public ApiExportResult export(ApiBatchRequest request, String type) { public ApiExportResult export(ApiBatchRequest request, String type) {
ApiExportResult apiExportResult;
ServiceUtils.getSelectAllIds(request, request.getCondition(), ServiceUtils.getSelectAllIds(request, request.getCondition(),
(query) -> extApiDefinitionMapper.selectIds(query)); (query) -> extApiDefinitionMapper.selectIds(query));
ApiDefinitionExample example = new ApiDefinitionExample();
example.createCriteria().andIdIn(request.getIds()); List<ApiDefinitionWithBLOBs> apiDefinitions = getByIds(request.getIds());
if (StringUtils.equals(type, "MS")) { // 导出为 Metersphere 格式 if (StringUtils.equals(type, "MS")) { // 导出为 Metersphere 格式
MockConfigService mockConfigService = CommonBeanFactory.getBean(MockConfigService.class); MockConfigService mockConfigService = CommonBeanFactory.getBean(MockConfigService.class);
apiExportResult = new MsApiExportResult(); MsApiExportResult msApiExportResult = new MsApiExportResult();
((MsApiExportResult) apiExportResult).setData(apiDefinitionMapper.selectByExampleWithBLOBs(example)); msApiExportResult.setData(apiDefinitions);
((MsApiExportResult) apiExportResult).setCases(apiTestCaseService.selectCasesBydApiIds(request.getIds())); msApiExportResult.setCases(apiTestCaseService.selectCasesBydApiIds(request.getIds()));
((MsApiExportResult) apiExportResult).setMocks(mockConfigService.selectMockExpectConfigByApiIdIn(request.getIds())); msApiExportResult.setMocks(mockConfigService.selectMockExpectConfigByApiIdIn(request.getIds()));
((MsApiExportResult) apiExportResult).setProjectName(request.getProjectId()); msApiExportResult.setProjectName(request.getProjectId());
((MsApiExportResult) apiExportResult).setProtocol(request.getProtocol()); msApiExportResult.setProtocol(request.getProtocol());
((MsApiExportResult) apiExportResult).setProjectId(request.getProjectId()); msApiExportResult.setProjectId(request.getProjectId());
((MsApiExportResult) apiExportResult).setVersion(System.getenv("MS_VERSION")); msApiExportResult.setVersion(System.getenv("MS_VERSION"));
if (CollectionUtils.isNotEmpty(((MsApiExportResult) apiExportResult).getData())) { if (CollectionUtils.isNotEmpty((msApiExportResult).getData())) {
List<String> names = ((MsApiExportResult) apiExportResult).getData().stream().map(ApiDefinitionWithBLOBs::getName).collect(Collectors.toList()); List<String> names = (msApiExportResult).getData().stream().map(ApiDefinitionWithBLOBs::getName).collect(Collectors.toList());
request.setName(String.join(",", names)); request.setName(String.join(",", names));
List<String> ids = ((MsApiExportResult) apiExportResult).getData().stream().map(ApiDefinitionWithBLOBs::getId).collect(Collectors.toList()); List<String> ids = msApiExportResult.getData().stream().map(ApiDefinitionWithBLOBs::getId).collect(Collectors.toList());
request.setId(JSON.toJSONString(ids)); request.setId(JSON.toJSONString(ids));
} }
return msApiExportResult;
} else { // 导出为 Swagger 格式 } else { // 导出为 Swagger 格式
Swagger3Parser swagger3Parser = new Swagger3Parser(); Swagger3Parser swagger3Parser = new Swagger3Parser();
apiExportResult = swagger3Parser.swagger3Export(apiDefinitionMapper.selectByExampleWithBLOBs(example)); return swagger3Parser.swagger3Export(apiDefinitions);
} }
return apiExportResult;
} }
public List<ApiDefinition> selectEffectiveIdByProjectId(String projectId) { public List<ApiDefinition> selectEffectiveIdByProjectId(String projectId) {

View File

@ -243,7 +243,7 @@ import {API_DEFINITION_CONFIGS} from "@/business/components/common/components/se
import MsTipButton from "@/business/components/common/components/MsTipButton"; import MsTipButton from "@/business/components/common/components/MsTipButton";
import CaseBatchMove from "@/business/components/api/definition/components/basis/BatchMove"; import CaseBatchMove from "@/business/components/api/definition/components/basis/BatchMove";
import { import {
buildBatchParam, buildBatchParam, deepClone,
getCustomTableHeader, getCustomTableHeader,
getCustomTableWidth, getCustomTableWidth,
getLastTableSortField, getLastTableSortField,
@ -952,13 +952,44 @@ export default {
let obj = response.data; let obj = response.data;
if (type == 'MS') { if (type == 'MS') {
obj.protocol = this.currentProtocol; obj.protocol = this.currentProtocol;
obj.nodeTree = nodeTree; obj.nodeTree = this.getExportNodeTree(nodeTree, obj.data);
downloadFile("Metersphere_Api_" + this.projectName + ".json", JSON.stringify(obj)); downloadFile("Metersphere_Api_" + this.projectName + ".json", JSON.stringify(obj));
} else { } else {
downloadFile("Swagger_Api_" + this.projectName + ".json", JSON.stringify(obj)); downloadFile("Swagger_Api_" + this.projectName + ".json", JSON.stringify(obj));
} }
}); });
}, },
getExportNodeTree(nodeTree, apis) {
let idSet = new Set();
apis.forEach((item) => {
idSet.add(item.moduleId);
});
let exportTree = deepClone(nodeTree);
for (let i = exportTree.length - 1; i >= 0; i--) {
if (!this.cutDownTree(exportTree[i], idSet)) {
exportTree.splice(i, 1);
}
}
return exportTree;
},
//
cutDownTree(nodeTree, nodeIdSet) {
let hasData = false;
if (nodeIdSet.has(nodeTree.id)) {
hasData = true;
}
let children = nodeTree.children;
if (children) {
for (let i = children.length - 1; i >= 0; i--) {
if (!this.cutDownTree(children[i], nodeIdSet)) {
children.splice(i, 1);
} else {
hasData = true;
}
}
}
return hasData;
},
headerDragend(newWidth, oldWidth, column, event) { headerDragend(newWidth, oldWidth, column, event) {
let finalWidth = newWidth; let finalWidth = newWidth;
if (column.minWidth > finalWidth) { if (column.minWidth > finalWidth) {