fix(接口测试): 导入的用例和mock现在可以同步发送通知

--bug=1046249 --user=宋天阳 【接口测试】接口导入-MeterSphere格式-接口带用例-消息通知-未发送创建/更新用例的消息(站内信/邮件/企业微信/钉钉/飞书) https://www.tapd.cn/55049933/s/1578213
This commit is contained in:
Jianguo-Genius 2024-09-13 16:11:12 +08:00 committed by Craftsman
parent d7b94fea51
commit 80f40388d9
2 changed files with 78 additions and 8 deletions

View File

@ -1,7 +1,5 @@
package io.metersphere.api.service.definition;
import io.metersphere.api.domain.ApiDefinitionModule;
import io.metersphere.api.domain.ApiDefinitionModuleExample;
import io.metersphere.api.dto.definition.*;
import io.metersphere.api.dto.export.ApiExportResponse;
import io.metersphere.api.mapper.ApiDefinitionModuleMapper;
@ -24,6 +22,7 @@ import io.metersphere.sdk.util.MsFileUtils;
import io.metersphere.sdk.util.Translator;
import io.metersphere.system.constants.ExportConstants;
import io.metersphere.system.domain.User;
import io.metersphere.system.dto.sdk.BaseTreeNode;
import io.metersphere.system.log.dto.LogDTO;
import io.metersphere.system.log.service.OperationLogService;
import io.metersphere.system.manager.ExportTaskManager;
@ -44,6 +43,7 @@ import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@ -76,8 +76,19 @@ public class ApiDefinitionExportService {
private ApiDefinitionLogService apiDefinitionLogService;
@Resource
private OperationLogService operationLogService;
@Resource
private ApiDefinitionImportService apiDefinitionImportService;
private static final String EXPORT_CASE_TMP_DIR = "tmp";
private Map<String, String> buildModuleIdPathMap(String projectId) {
List<BaseTreeNode> apiModules = apiDefinitionImportService.buildTreeData(projectId, null);
Map<String, String> modulePathMap = new HashMap<>();
apiModules.forEach(item -> {
modulePathMap.put(item.getId(), item.getPath());
});
return modulePathMap;
}
public ApiExportResponse genApiExportResponse(ApiDefinitionBatchExportRequest request, String type, String userId) {
List<String> ids = this.getBatchExportApiIds(request, request.getProjectId(), userId);
if (CollectionUtils.isEmpty(ids)) {
@ -85,10 +96,7 @@ public class ApiDefinitionExportService {
}
List<ApiDefinitionWithBlob> list = this.selectAndSortByIds(ids);
List<String> moduleIds = list.stream().map(ApiDefinitionWithBlob::getModuleId).toList();
ApiDefinitionModuleExample example = new ApiDefinitionModuleExample();
example.createCriteria().andIdIn(moduleIds);
List<ApiDefinitionModule> definitionModules = apiDefinitionModuleMapper.selectByExample(example);
Map<String, String> moduleMap = definitionModules.stream().collect(Collectors.toMap(ApiDefinitionModule::getId, ApiDefinitionModule::getName));
Map<String, String> moduleMap = this.buildModuleIdPathMap(request.getProjectId());
return switch (type.toLowerCase()) {
case "swagger" -> exportSwagger(request, list, moduleMap);
case "metersphere" -> exportMetersphere(request, list, moduleMap);

View File

@ -139,6 +139,7 @@ public class ApiDefinitionImportService {
if (apiImportDataAnalysisResult.isEmpty()) {
throw new MSException(Translator.get("parse_empty_data"));
}
try {
//初始化版本信息用于保存以及以后真对具体版本导入进行拓展
String defaultVersion = extBaseProjectVersionMapper.getDefaultVersion(request.getProjectId());
@ -219,6 +220,11 @@ public class ApiDefinitionImportService {
Project project = projectMapper.selectByPrimaryKey(request.getProjectId());
List<ApiDefinitionCaseDTO> noticeCreateLists = new ArrayList<>();
List<ApiDefinitionCaseDTO> noticeUpdateLists = new ArrayList<>();
List<ApiDefinitionCaseDTO> noticeCaseCreateLists = new ArrayList<>();
List<ApiDefinitionCaseDTO> noticeCaseUpdateLists = new ArrayList<>();
List<ApiDefinitionCaseDTO> noticeMockCreateLists = new ArrayList<>();
List<ApiDefinitionCaseDTO> noticeMockUpdateLists = new ArrayList<>();
apiDefinitionPreImportAnalysisResult.getInsertModuleList().forEach(t -> {
operationLogs.add(ApiDefinitionImportUtils.genImportLog(project, t.getId(), t.getName(), t, OperationLogModule.API_TEST_MANAGEMENT_MODULE, request.getUserId(), OperationLogType.ADD.name()));
});
@ -248,12 +254,66 @@ public class ApiDefinitionImportService {
operationLogs.add(ApiDefinitionImportUtils.genImportLog(project, t.getId(), t.getName(), apiDefinitionDTO, OperationLogModule.API_TEST_MANAGEMENT_DEFINITION, request.getUserId(), OperationLogType.IMPORT.name()));
});
// 更新修改用例
apiDefinitionPreImportAnalysisResult.getInsertApiCaseList().forEach(t -> {
ApiTestCaseDTO apiTestCaseDTO = new ApiTestCaseDTO();
BeanUtils.copyBean(apiTestCaseDTO, t);
ApiDefinitionCaseDTO apiDefinitionCaseDTO = new ApiDefinitionCaseDTO();
BeanUtils.copyBean(apiDefinitionCaseDTO, apiTestCaseDTO);
noticeCaseCreateLists.add(apiDefinitionCaseDTO);
operationLogs.add(ApiDefinitionImportUtils.genImportLog(project, t.getId(), t.getName(), apiTestCaseDTO, OperationLogModule.API_TEST_MANAGEMENT_CASE, request.getUserId(), OperationLogType.IMPORT.name()));
});
apiDefinitionPreImportAnalysisResult.getUpdateApiCaseList().forEach(t -> {
ApiTestCaseDTO apiTestCaseDTO = new ApiTestCaseDTO();
BeanUtils.copyBean(apiTestCaseDTO, t);
ApiDefinitionCaseDTO apiDefinitionCaseDTO = new ApiDefinitionCaseDTO();
BeanUtils.copyBean(apiDefinitionCaseDTO, apiTestCaseDTO);
noticeCaseUpdateLists.add(apiDefinitionCaseDTO);
operationLogs.add(ApiDefinitionImportUtils.genImportLog(project, t.getId(), t.getName(), apiTestCaseDTO, OperationLogModule.API_TEST_MANAGEMENT_CASE, request.getUserId(), OperationLogType.UPDATE.name()));
});
//更新修改Mock
apiDefinitionPreImportAnalysisResult.getInsertApiMockList().forEach(t -> {
ApiDefinitionMockDTO apiMockDTO = new ApiDefinitionMockDTO();
BeanUtils.copyBean(apiMockDTO, t);
ApiDefinitionCaseDTO noticeMockDTO = new ApiDefinitionCaseDTO();
BeanUtils.copyBean(noticeMockDTO, apiMockDTO);
noticeMockCreateLists.add(noticeMockDTO);
operationLogs.add(ApiDefinitionImportUtils.genImportLog(project, t.getId(), t.getName(), apiMockDTO, OperationLogModule.API_TEST_MANAGEMENT_MOCK, request.getUserId(), OperationLogType.IMPORT.name()));
});
apiDefinitionPreImportAnalysisResult.getUpdateApiMockList().forEach(t -> {
ApiDefinitionMockDTO apiMockDTO = new ApiDefinitionMockDTO();
BeanUtils.copyBean(apiMockDTO, t);
ApiDefinitionCaseDTO apiDefinitionCaseDTO = new ApiDefinitionCaseDTO();
BeanUtils.copyBean(apiDefinitionCaseDTO, apiMockDTO);
noticeMockUpdateLists.add(apiDefinitionCaseDTO);
operationLogs.add(ApiDefinitionImportUtils.genImportLog(project, t.getId(), t.getName(), apiMockDTO, OperationLogModule.API_TEST_MANAGEMENT_MOCK, request.getUserId(), OperationLogType.UPDATE.name()));
});
//发送通知
List<Map> createResources = new ArrayList<>(JSON.parseArray(JSON.toJSONString(noticeCreateLists), Map.class));
User user = userMapper.selectByPrimaryKey(request.getUserId());
List<Map> createResources = new ArrayList<>(JSON.parseArray(JSON.toJSONString(noticeCreateLists), Map.class));
commonNoticeSendService.sendNotice(NoticeConstants.TaskType.API_DEFINITION_TASK, NoticeConstants.Event.CREATE, createResources, user, request.getProjectId());
List<Map> updateResources = new ArrayList<>(JSON.parseArray(JSON.toJSONString(noticeUpdateLists), Map.class));
commonNoticeSendService.sendNotice(NoticeConstants.TaskType.API_DEFINITION_TASK, NoticeConstants.Event.UPDATE, updateResources, user, request.getProjectId());
if (CollectionUtils.isNotEmpty(noticeCaseCreateLists)) {
List<Map> rsources = new ArrayList<>(JSON.parseArray(JSON.toJSONString(noticeCaseCreateLists), Map.class));
commonNoticeSendService.sendNotice(NoticeConstants.TaskType.API_DEFINITION_TASK, NoticeConstants.Event.CASE_CREATE, rsources, user, request.getProjectId());
}
if (CollectionUtils.isNotEmpty(noticeCaseUpdateLists)) {
List<Map> rsources = new ArrayList<>(JSON.parseArray(JSON.toJSONString(noticeCaseUpdateLists), Map.class));
commonNoticeSendService.sendNotice(NoticeConstants.TaskType.API_DEFINITION_TASK, NoticeConstants.Event.CASE_UPDATE, rsources, user, request.getProjectId());
}
if (CollectionUtils.isNotEmpty(noticeMockCreateLists)) {
List<Map> rsources = new ArrayList<>(JSON.parseArray(JSON.toJSONString(noticeMockCreateLists), Map.class));
commonNoticeSendService.sendNotice(NoticeConstants.TaskType.API_DEFINITION_TASK, NoticeConstants.Event.MOCK_CREATE, rsources, user, request.getProjectId());
}
if (CollectionUtils.isNotEmpty(noticeMockUpdateLists)) {
List<Map> rsources = new ArrayList<>(JSON.parseArray(JSON.toJSONString(noticeMockUpdateLists), Map.class));
commonNoticeSendService.sendNotice(NoticeConstants.TaskType.API_DEFINITION_TASK, NoticeConstants.Event.MOCK_UPDATE, rsources, user, request.getProjectId());
}
}
return operationLogs;
}
@ -1015,7 +1075,9 @@ public class ApiDefinitionImportService {
public List<BaseTreeNode> buildTreeData(String projectId, String protocol) {
ApiModuleRequest request = new ApiModuleRequest();
request.setProjectId(projectId);
if (StringUtils.isNotEmpty(protocol)) {
request.setProtocols(List.of(protocol));
}
List<BaseTreeNode> apiModuleList = extApiDefinitionModuleMapper.selectBaseByRequest(request);
return this.buildTreeAndCountResource(apiModuleList, true, Translator.get(UNPLANNED_API));
}