fix(接口测试): 接口用例批量同步,消息通知没有去重

--bug=1045154 --user=陈建星 【接口测试】批量同步用例-开启了变更通知并且有设置消息通知-收到了两条站内信 https://www.tapd.cn/55049933/s/1563262
This commit is contained in:
AgAngle 2024-08-15 17:16:46 +08:00 committed by 刘瑞斌
parent 0c022fdecd
commit e37ab5b70b
3 changed files with 114 additions and 10 deletions

View File

@ -12,9 +12,11 @@ import io.metersphere.sdk.util.JSON;
import io.metersphere.sdk.util.SubListUtils;
import io.metersphere.system.domain.User;
import io.metersphere.system.dto.sdk.ApiDefinitionCaseDTO;
import io.metersphere.system.dto.sdk.BaseSystemConfigDTO;
import io.metersphere.system.mapper.UserMapper;
import io.metersphere.system.notice.constants.NoticeConstants;
import io.metersphere.system.service.CommonNoticeSendService;
import io.metersphere.system.service.SystemParameterService;
import jakarta.annotation.Resource;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.BooleanUtils;
@ -34,6 +36,8 @@ public class ApiTestCaseNoticeService {
private CommonNoticeSendService commonNoticeSendService;
@Resource
private ExtApiTestCaseMapper extApiTestCaseMapper;
@Resource
private SystemParameterService systemParameterService;
public ApiDefinitionCaseDTO addCaseDto(ApiTestCaseAddRequest request) {
ApiDefinitionCaseDTO caseDTO = new ApiDefinitionCaseDTO();
@ -104,26 +108,22 @@ public class ApiTestCaseNoticeService {
if (BooleanUtils.isTrue(notificationConfig.getScenarioCreator()) || BooleanUtils.isTrue(notificationConfig.getApiCaseCreator())) {
for (Map resource : resources) {
String caseId = (String) resource.get("id");
String relatedUsers = null;
List<String> extraUsers = new ArrayList<>();
if (BooleanUtils.isTrue(notificationConfig.getScenarioCreator())) {
// 添加引用该用例的场景的创建人
Set<String> userIds = Optional.ofNullable(caseRefApiScenarioCreatorMap.get(caseId)).orElse(new HashSet<>(1));
relatedUsers = userIds.stream().collect(Collectors.joining(";"));
extraUsers.addAll(userIds);
}
if (BooleanUtils.isTrue(notificationConfig.getApiCaseCreator())) {
// 添加用例创建人
String createUser = (String) resource.get("createUser");
if (relatedUsers == null) {
relatedUsers = createUser;
} else {
relatedUsers += ";" + createUser;
}
extraUsers.add(createUser);
}
// 添加特殊通知人
resource.put("relatedUsers", relatedUsers);
BaseSystemConfigDTO baseSystemConfigDTO = systemParameterService.getBaseInfo();
commonNoticeSendService.sendNotice(NoticeConstants.TaskType.API_DEFINITION_TASK, event, resource, user, projectId,
baseSystemConfigDTO, extraUsers, true);
}
}
commonNoticeSendService.sendNotice(NoticeConstants.TaskType.API_DEFINITION_TASK, event, resources, user, projectId);
}
}

View File

@ -58,6 +58,48 @@ public class CommonNoticeSendService {
}
}
/**
* 发送通知
* @param taskType
* @param event
* @param operator
* @param currentProjectId
* @param baseSystemConfig
* @param resource
* @param extraUsers 除了消息通知配置的用户需要额外通知的用户
* @param excludeSelf 是否排除自己
*/
public void sendNotice(String taskType, String event, Map resource, User operator, String currentProjectId, BaseSystemConfigDTO baseSystemConfig,
List<String> extraUsers, boolean excludeSelf) {
Map paramMap = new HashMap<>();
paramMap.put("url", baseSystemConfig.getUrl());
paramMap.put(NoticeConstants.RelatedUser.OPERATOR, operator.getName());
paramMap.put("Language", operator.getLanguage());
paramMap.putAll(resource);
paramMap.putIfAbsent("projectId", currentProjectId);
// 占位符
handleDefaultValues(paramMap);
String context = getContext(taskType, event);
String subject = getSubject(taskType, event);
List<String> relatedUsers = getRelatedUsers(resource.get("relatedUsers"));
NoticeModel noticeModel = NoticeModel.builder()
.operator(operator.getId())
.context(context)
.subject(subject)
.paramMap(paramMap)
.event(event)
.status((String) paramMap.get("status"))
.excludeSelf(true)
.relatedUsers(relatedUsers)
.build();
noticeSendService.sendOther(taskType, noticeModel, extraUsers, excludeSelf);
}
private List<String> getRelatedUsers(Object relatedUsers) {
String relatedUser = (String) relatedUsers;
List<String> relatedUserList = new ArrayList<>();

View File

@ -8,7 +8,9 @@ import io.metersphere.system.notice.NoticeModel;
import io.metersphere.system.notice.constants.NoticeConstants;
import io.metersphere.system.notice.sender.AbstractNoticeSender;
import io.metersphere.system.notice.sender.impl.*;
import io.metersphere.system.notice.utils.MessageTemplateUtils;
import jakarta.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.SerializationUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.i18n.LocaleContextHolder;
@ -17,6 +19,7 @@ import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Locale;
import java.util.Map;
@Component
public class NoticeSendService {
@ -168,4 +171,63 @@ public class NoticeSendService {
}
}
/**
* 其他类型
*/
@Async("threadPoolTaskExecutor")
public void sendOther(String taskType, NoticeModel noticeModel, List<String> users, boolean excludeSelf) {
//如果在线需要排除自己也需要选定当前环境选择的语言
if (excludeSelf) {
setLanguage(noticeModel);
}
// 定时任务调用不排除自己
noticeModel.setExcludeSelf(excludeSelf);
try {
String projectId = (String) noticeModel.getParamMap().get("projectId");
List<MessageDetail> messageDetails = messageDetailService.searchMessageByTypeAndProjectId(taskType, projectId)
.stream()
.filter(messageDetail -> StringUtils.equals(messageDetail.getEvent(), noticeModel.getEvent()))
.toList();
if (CollectionUtils.isEmpty(messageDetails)) {
NoticeModel n = SerializationUtils.clone(noticeModel);
MessageDetail m = buildMessageTails(taskType, noticeModel, users, projectId);
inSiteNoticeSender.send(m, n);
} else {
// 异步发送通知
messageDetails.stream()
.forEach(messageDetail -> {
MessageDetail m = SerializationUtils.clone(messageDetail);
if (CollectionUtils.isNotEmpty(users)) {
m.getReceiverIds().addAll(users);
}
NoticeModel n = SerializationUtils.clone(noticeModel);
try {
this.getNoticeSender(m).send(m, n);
} catch (Exception e) {
LogUtils.error(e);
}
});
}
} catch (Exception e) {
LogUtils.error(e.getMessage(), e);
}
}
private static MessageDetail buildMessageTails(String taskType, NoticeModel noticeModel, List<String> users, String projectId) {
MessageDetail m = new MessageDetail();
Map<String, String> defaultTemplateTitleMap = MessageTemplateUtils.getDefaultTemplateSubjectMap();
String defaultSubject = defaultTemplateTitleMap.get(taskType + "_" + noticeModel.getEvent());
Map<String, String> defaultTemplateMap = MessageTemplateUtils.getDefaultTemplateMap();
String defaultTemplate = defaultTemplateMap.get(taskType + "_" + noticeModel.getEvent());
m.setReceiverIds(users);
m.setProjectId(projectId);
m.setEvent(noticeModel.getEvent());
m.setTaskType(taskType);
m.setTemplate(defaultTemplate);
m.setSubject(defaultSubject);
m.setType(NoticeConstants.Type.IN_SITE);
return m;
}
}