fix(消息管理): 修复发通知的内容的语言环境

This commit is contained in:
guoyuqi 2024-01-31 16:06:40 +08:00 committed by 刘瑞斌
parent 254b29f373
commit a482f3f57b
11 changed files with 80 additions and 94 deletions

View File

@ -1,5 +0,0 @@
package io.metersphere.project.enums;
public enum ProjectRobotPlatform {
DING_TALK, LARK, WE_COM, CUSTOM, IN_SITE, MAIL
}

View File

@ -1,5 +0,0 @@
package io.metersphere.project.enums;
public enum ProjectRobotType {
CUSTOM, ENTERPRISE
}

View File

@ -6,12 +6,11 @@ import io.metersphere.project.domain.ProjectRobot;
import io.metersphere.project.dto.MessageTaskDTO; import io.metersphere.project.dto.MessageTaskDTO;
import io.metersphere.project.dto.MessageTaskDetailDTO; import io.metersphere.project.dto.MessageTaskDetailDTO;
import io.metersphere.project.dto.MessageTaskTypeDTO; import io.metersphere.project.dto.MessageTaskTypeDTO;
import io.metersphere.project.enums.ProjectRobotPlatform;
import io.metersphere.project.mapper.MessageTaskBlobMapper; import io.metersphere.project.mapper.MessageTaskBlobMapper;
import io.metersphere.project.mapper.MessageTaskMapper; import io.metersphere.project.mapper.MessageTaskMapper;
import io.metersphere.project.mapper.ProjectRobotMapper; import io.metersphere.project.mapper.ProjectRobotMapper;
import io.metersphere.system.dto.sdk.OptionDTO;
import io.metersphere.sdk.util.JSON; import io.metersphere.sdk.util.JSON;
import io.metersphere.system.dto.sdk.OptionDTO;
import io.metersphere.system.notice.constants.NoticeConstants; import io.metersphere.system.notice.constants.NoticeConstants;
import io.metersphere.system.service.CreateProjectResourceService; import io.metersphere.system.service.CreateProjectResourceService;
import io.metersphere.system.uid.IDGenerator; import io.metersphere.system.uid.IDGenerator;
@ -50,7 +49,7 @@ public class CreateRobotResourceService implements CreateProjectResourceService
projectRobot.setId(inSiteId); projectRobot.setId(inSiteId);
projectRobot.setProjectId(projectId); projectRobot.setProjectId(projectId);
projectRobot.setName("robot_in_site"); projectRobot.setName("robot_in_site");
projectRobot.setPlatform(ProjectRobotPlatform.IN_SITE.toString()); projectRobot.setPlatform(NoticeConstants.Type.IN_SITE);
projectRobot.setWebhook("NONE"); projectRobot.setWebhook("NONE");
projectRobot.setCreateUser("admin"); projectRobot.setCreateUser("admin");
projectRobot.setCreateTime(System.currentTimeMillis()); projectRobot.setCreateTime(System.currentTimeMillis());
@ -63,7 +62,7 @@ public class CreateRobotResourceService implements CreateProjectResourceService
projectRobotMail.setId(IDGenerator.nextStr()); projectRobotMail.setId(IDGenerator.nextStr());
projectRobotMail.setProjectId(projectId); projectRobotMail.setProjectId(projectId);
projectRobotMail.setName("robot_mail"); projectRobotMail.setName("robot_mail");
projectRobotMail.setPlatform(ProjectRobotPlatform.MAIL.toString()); projectRobotMail.setPlatform(NoticeConstants.Type.MAIL);
projectRobotMail.setWebhook("NONE"); projectRobotMail.setWebhook("NONE");
projectRobotMail.setCreateUser("admin"); projectRobotMail.setCreateUser("admin");
projectRobotMail.setEnable(false); projectRobotMail.setEnable(false);

View File

@ -5,15 +5,15 @@ import io.metersphere.project.domain.MessageTaskExample;
import io.metersphere.project.domain.ProjectRobot; import io.metersphere.project.domain.ProjectRobot;
import io.metersphere.project.domain.ProjectRobotExample; import io.metersphere.project.domain.ProjectRobotExample;
import io.metersphere.project.dto.ProjectRobotDTO; import io.metersphere.project.dto.ProjectRobotDTO;
import io.metersphere.project.enums.ProjectRobotPlatform;
import io.metersphere.project.mapper.MessageTaskMapper; import io.metersphere.project.mapper.MessageTaskMapper;
import io.metersphere.project.mapper.ProjectRobotMapper; import io.metersphere.project.mapper.ProjectRobotMapper;
import io.metersphere.sdk.constants.HttpMethodConstants; import io.metersphere.sdk.constants.HttpMethodConstants;
import io.metersphere.system.log.dto.LogDTO;
import io.metersphere.system.dto.sdk.request.MessageTaskRequest;
import io.metersphere.sdk.util.JSON; import io.metersphere.sdk.util.JSON;
import io.metersphere.system.dto.sdk.request.MessageTaskRequest;
import io.metersphere.system.log.constants.OperationLogModule; import io.metersphere.system.log.constants.OperationLogModule;
import io.metersphere.system.log.constants.OperationLogType; import io.metersphere.system.log.constants.OperationLogType;
import io.metersphere.system.log.dto.LogDTO;
import io.metersphere.system.notice.constants.NoticeConstants;
import io.metersphere.system.notice.utils.MessageTemplateUtils; import io.metersphere.system.notice.utils.MessageTemplateUtils;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
@ -134,7 +134,7 @@ public class MessageTaskLogService {
private String setDefaultRobot(String projectId, String robotId) { private String setDefaultRobot(String projectId, String robotId) {
if (StringUtils.isBlank(robotId)) { if (StringUtils.isBlank(robotId)) {
ProjectRobotExample projectRobotExample = new ProjectRobotExample(); ProjectRobotExample projectRobotExample = new ProjectRobotExample();
projectRobotExample.createCriteria().andProjectIdEqualTo(projectId).andPlatformEqualTo(ProjectRobotPlatform.IN_SITE.toString()); projectRobotExample.createCriteria().andProjectIdEqualTo(projectId).andPlatformEqualTo(NoticeConstants.Type.IN_SITE);
List<ProjectRobot> projectRobots = projectRobotMapper.selectByExample(projectRobotExample); List<ProjectRobot> projectRobots = projectRobotMapper.selectByExample(projectRobotExample);
robotId = projectRobots.get(0).getId(); robotId = projectRobots.get(0).getId();
} }

View File

@ -3,18 +3,17 @@ package io.metersphere.project.service;
import io.metersphere.project.domain.*; import io.metersphere.project.domain.*;
import io.metersphere.project.dto.*; import io.metersphere.project.dto.*;
import io.metersphere.project.enums.ProjectRobotPlatform;
import io.metersphere.project.enums.result.ProjectResultCode; import io.metersphere.project.enums.result.ProjectResultCode;
import io.metersphere.project.mapper.*; import io.metersphere.project.mapper.*;
import io.metersphere.sdk.constants.TemplateScene; import io.metersphere.sdk.constants.TemplateScene;
import io.metersphere.system.dto.sdk.OptionDTO;
import io.metersphere.system.dto.sdk.request.MessageTaskRequest;
import io.metersphere.sdk.exception.MSException; import io.metersphere.sdk.exception.MSException;
import io.metersphere.sdk.util.BeanUtils; import io.metersphere.sdk.util.BeanUtils;
import io.metersphere.sdk.util.JSON; import io.metersphere.sdk.util.JSON;
import io.metersphere.sdk.util.Translator; import io.metersphere.sdk.util.Translator;
import io.metersphere.system.controller.handler.ResultHolder; import io.metersphere.system.controller.handler.ResultHolder;
import io.metersphere.system.domain.*; import io.metersphere.system.domain.*;
import io.metersphere.system.dto.sdk.OptionDTO;
import io.metersphere.system.dto.sdk.request.MessageTaskRequest;
import io.metersphere.system.mapper.CustomFieldMapper; import io.metersphere.system.mapper.CustomFieldMapper;
import io.metersphere.system.mapper.UserMapper; import io.metersphere.system.mapper.UserMapper;
import io.metersphere.system.mapper.UserRoleRelationMapper; import io.metersphere.system.mapper.UserRoleRelationMapper;
@ -159,7 +158,7 @@ public class NoticeMessageTaskService {
private ProjectRobot getDefaultRobot(String projectId, String robotId) { private ProjectRobot getDefaultRobot(String projectId, String robotId) {
if (StringUtils.isBlank(robotId)) { if (StringUtils.isBlank(robotId)) {
ProjectRobotExample projectRobotExample = new ProjectRobotExample(); ProjectRobotExample projectRobotExample = new ProjectRobotExample();
projectRobotExample.createCriteria().andProjectIdEqualTo(projectId).andPlatformEqualTo(ProjectRobotPlatform.IN_SITE.toString()); projectRobotExample.createCriteria().andProjectIdEqualTo(projectId).andPlatformEqualTo(NoticeConstants.Type.IN_SITE);
List<ProjectRobot> projectRobots = projectRobotMapper.selectByExample(projectRobotExample); List<ProjectRobot> projectRobots = projectRobotMapper.selectByExample(projectRobotExample);
return projectRobots.get(0); return projectRobots.get(0);
} else { } else {
@ -400,7 +399,7 @@ public class NoticeMessageTaskService {
receivers.add(optionDTO); receivers.add(optionDTO);
String platform = robotMap.get(messageTask.getProjectRobotId()).getPlatform(); String platform = robotMap.get(messageTask.getProjectRobotId()).getPlatform();
String defaultSubject; String defaultSubject;
if (StringUtils.equalsIgnoreCase(platform, ProjectRobotPlatform.MAIL.toString())) { if (StringUtils.equalsIgnoreCase(platform, NoticeConstants.Type.MAIL)) {
defaultSubject = "MeterSphere " + defaultTemplateSubjectMap.get(messageTaskTypeDTO.getTaskType() + "_" + messageTaskDetailDTO.getEvent()); defaultSubject = "MeterSphere " + defaultTemplateSubjectMap.get(messageTaskTypeDTO.getTaskType() + "_" + messageTaskDetailDTO.getEvent());
} else { } else {
defaultSubject = defaultTemplateSubjectMap.get(messageTaskTypeDTO.getTaskType() + "_" + messageTaskDetailDTO.getEvent()); defaultSubject = defaultTemplateSubjectMap.get(messageTaskTypeDTO.getTaskType() + "_" + messageTaskDetailDTO.getEvent());
@ -454,7 +453,7 @@ public class NoticeMessageTaskService {
ProjectRobotConfigDTO projectRobotConfigDTO = new ProjectRobotConfigDTO(); ProjectRobotConfigDTO projectRobotConfigDTO = new ProjectRobotConfigDTO();
projectRobotConfigDTO.setRobotId(projectRobot.getId()); projectRobotConfigDTO.setRobotId(projectRobot.getId());
projectRobotConfigDTO.setRobotName(Translator.get(projectRobot.getName())); projectRobotConfigDTO.setRobotName(Translator.get(projectRobot.getName()));
projectRobotConfigDTO.setPlatform(ProjectRobotPlatform.IN_SITE.toString()); projectRobotConfigDTO.setPlatform(NoticeConstants.Type.IN_SITE);
projectRobotConfigDTO.setDingType(projectRobot.getType()); projectRobotConfigDTO.setDingType(projectRobot.getType());
projectRobotConfigDTO.setEnable(false); projectRobotConfigDTO.setEnable(false);
projectRobotConfigDTO.setTemplate(defaultTemplate); projectRobotConfigDTO.setTemplate(defaultTemplate);

View File

@ -2,8 +2,6 @@ package io.metersphere.project.service;
import io.metersphere.project.domain.*; import io.metersphere.project.domain.*;
import io.metersphere.project.dto.ProjectRobotDTO; import io.metersphere.project.dto.ProjectRobotDTO;
import io.metersphere.project.enums.ProjectRobotPlatform;
import io.metersphere.project.enums.ProjectRobotType;
import io.metersphere.project.mapper.MessageTaskBlobMapper; import io.metersphere.project.mapper.MessageTaskBlobMapper;
import io.metersphere.project.mapper.MessageTaskMapper; import io.metersphere.project.mapper.MessageTaskMapper;
import io.metersphere.project.mapper.ProjectRobotMapper; import io.metersphere.project.mapper.ProjectRobotMapper;
@ -44,38 +42,23 @@ public class ProjectRobotService {
public ProjectRobot add(ProjectRobot projectRobot) { public ProjectRobot add(ProjectRobot projectRobot) {
projectRobot.setId(IDGenerator.nextStr()); projectRobot.setId(IDGenerator.nextStr());
projectRobot.setEnable(projectRobot.getEnable()); projectRobot.setEnable(projectRobot.getEnable());
reSetPlatform(projectRobot); checkDingTalkAndSet(projectRobot);
robotMapper.insert(projectRobot); robotMapper.insert(projectRobot);
return projectRobot; return projectRobot;
} }
private void reSetPlatform(ProjectRobot projectRobot) {
switch (projectRobot.getPlatform()) {
case "DING_TALK" -> checkDingTalkAndSet(projectRobot);
case "LARK" -> projectRobot.setPlatform(NoticeConstants.Type.LARK_ROBOT);
case "WE_COM" -> projectRobot.setPlatform(NoticeConstants.Type.WECOM_ROBOT);
case "IN_SITE" -> projectRobot.setPlatform(NoticeConstants.Type.IN_SITE);
case "MAIL" -> projectRobot.setPlatform(NoticeConstants.Type.MAIL);
case "CUSTOM" -> projectRobot.setPlatform(NoticeConstants.Type.CUSTOM_WEBHOOK_ROBOT);
default -> projectRobot.setPlatform(NoticeConstants.Type.IN_SITE);
}
}
private static void checkDingTalkAndSet(ProjectRobot projectRobot) { private static void checkDingTalkAndSet(ProjectRobot projectRobot) {
if (StringUtils.equals(projectRobot.getPlatform(), ProjectRobotPlatform.DING_TALK.toString()) || StringUtils.equals(projectRobot.getPlatform(), NoticeConstants.Type.DING_CUSTOM_ROBOT) || StringUtils.equals(projectRobot.getPlatform(), NoticeConstants.Type.DING_ENTERPRISE_ROBOT)) { if (StringUtils.equals(projectRobot.getPlatform(), NoticeConstants.Type.DING_TALK)) {
if (StringUtils.isBlank(projectRobot.getType())) { if (StringUtils.isBlank(projectRobot.getType())) {
throw new MSException(Translator.get("ding_type_is_null")); throw new MSException(Translator.get("ding_type_is_null"));
} }
projectRobot.setPlatform(NoticeConstants.Type.DING_CUSTOM_ROBOT); if (StringUtils.equals(projectRobot.getType(), NoticeConstants.DingType.ENTERPRISE)) {
if (StringUtils.equals(projectRobot.getType(), ProjectRobotType.ENTERPRISE.toString()) || StringUtils.equals(projectRobot.getPlatform(), NoticeConstants.Type.DING_ENTERPRISE_ROBOT)) {
if (StringUtils.isBlank(projectRobot.getAppKey())) { if (StringUtils.isBlank(projectRobot.getAppKey())) {
throw new MSException(Translator.get("ding_app_key_is_null")); throw new MSException(Translator.get("ding_app_key_is_null"));
} }
if (StringUtils.isBlank(projectRobot.getAppSecret())) { if (StringUtils.isBlank(projectRobot.getAppSecret())) {
throw new MSException(Translator.get("ding_app_secret_is_null")); throw new MSException(Translator.get("ding_app_secret_is_null"));
} }
projectRobot.setPlatform(NoticeConstants.Type.DING_ENTERPRISE_ROBOT);
} }
} }
} }
@ -126,7 +109,7 @@ public class ProjectRobotService {
List<ProjectRobot> projectRobots = robotMapper.selectByExample(projectExample); List<ProjectRobot> projectRobots = robotMapper.selectByExample(projectExample);
Map<String, String> userMap = getUserMap(projectRobots); Map<String, String> userMap = getUserMap(projectRobots);
for (ProjectRobot projectRobot : projectRobots) { for (ProjectRobot projectRobot : projectRobots) {
if ((StringUtils.equalsIgnoreCase(projectRobot.getPlatform(), ProjectRobotPlatform.IN_SITE.toString()) || StringUtils.equalsIgnoreCase(projectRobot.getPlatform(), ProjectRobotPlatform.MAIL.toString())) && StringUtils.isNotBlank(projectRobot.getDescription())) { if ((StringUtils.equalsIgnoreCase(projectRobot.getPlatform(), NoticeConstants.Type.IN_SITE) || StringUtils.equalsIgnoreCase(projectRobot.getPlatform(), NoticeConstants.Type.MAIL)) && StringUtils.isNotBlank(projectRobot.getDescription())) {
projectRobot.setDescription(Translator.get(projectRobot.getDescription())); projectRobot.setDescription(Translator.get(projectRobot.getDescription()));
projectRobot.setName(Translator.get(projectRobot.getName())); projectRobot.setName(Translator.get(projectRobot.getName()));
} }
@ -138,24 +121,25 @@ public class ProjectRobotService {
} }
} }
Map<String, List<ProjectRobot>> collect = projectRobots.stream().collect(Collectors.groupingBy(ProjectRobot::getPlatform)); Map<String, List<ProjectRobot>> collect = projectRobots.stream().collect(Collectors.groupingBy(ProjectRobot::getPlatform));
List<String>defaultPlatForm = new ArrayList<>(); List<String> defaultPlatForm = new ArrayList<>();
defaultPlatForm.add(NoticeConstants.Type.MAIL); defaultPlatForm.add(NoticeConstants.Type.MAIL);
defaultPlatForm.add(NoticeConstants.Type.IN_SITE); defaultPlatForm.add(NoticeConstants.Type.IN_SITE);
List<ProjectRobot> list = projectRobots.stream().filter(t -> !defaultPlatForm.contains(t.getPlatform())).toList(); List<ProjectRobot> list = projectRobots.stream().filter(t -> !defaultPlatForm.contains(t.getPlatform())).toList();
List<ProjectRobot> newProjectRobots = new ArrayList<>(list.stream().sorted(Comparator.comparing(ProjectRobot::getCreateTime).reversed()).toList()); List<ProjectRobot> newProjectRobots = new ArrayList<>(list.stream().sorted(Comparator.comparing(ProjectRobot::getCreateTime).reversed()).toList());
List<ProjectRobot> mailRobot = collect.get(NoticeConstants.Type.MAIL); List<ProjectRobot> mailRobot = collect.get(NoticeConstants.Type.MAIL);
if (CollectionUtils.isNotEmpty(mailRobot)) { if (CollectionUtils.isNotEmpty(mailRobot)) {
newProjectRobots.add(0,mailRobot.get(0)); newProjectRobots.add(0, mailRobot.get(0));
} }
List<ProjectRobot> inSiteRobot = collect.get(NoticeConstants.Type.IN_SITE); List<ProjectRobot> inSiteRobot = collect.get(NoticeConstants.Type.IN_SITE);
if (CollectionUtils.isNotEmpty(inSiteRobot)) { if (CollectionUtils.isNotEmpty(inSiteRobot)) {
newProjectRobots.add(0,inSiteRobot.get(0)); newProjectRobots.add(0, inSiteRobot.get(0));
} }
return newProjectRobots; return newProjectRobots;
} }
private Map<String, String> getUserMap(List<ProjectRobot> projectRobots) { private Map<String, String> getUserMap(List<ProjectRobot> projectRobots) {
List<String> userIds = projectRobots.stream().flatMap(projectRobot -> Stream.of(projectRobot.getCreateUser(), projectRobot.getUpdateUser())).distinct().toList();; List<String> userIds = projectRobots.stream().flatMap(projectRobot -> Stream.of(projectRobot.getCreateUser(), projectRobot.getUpdateUser())).distinct().toList();
;
if (CollectionUtils.isEmpty(userIds)) { if (CollectionUtils.isEmpty(userIds)) {
return new HashMap<>(); return new HashMap<>();
} }

View File

@ -5,21 +5,19 @@ import io.metersphere.project.dto.MessageTaskDTO;
import io.metersphere.project.dto.MessageTaskDetailDTO; import io.metersphere.project.dto.MessageTaskDetailDTO;
import io.metersphere.project.dto.MessageTaskTypeDTO; import io.metersphere.project.dto.MessageTaskTypeDTO;
import io.metersphere.project.dto.ProjectRobotDTO; import io.metersphere.project.dto.ProjectRobotDTO;
import io.metersphere.project.enums.ProjectRobotPlatform;
import io.metersphere.project.enums.ProjectRobotType;
import io.metersphere.project.mapper.MessageTaskBlobMapper; import io.metersphere.project.mapper.MessageTaskBlobMapper;
import io.metersphere.project.mapper.MessageTaskMapper; import io.metersphere.project.mapper.MessageTaskMapper;
import io.metersphere.project.mapper.ProjectMapper; import io.metersphere.project.mapper.ProjectMapper;
import io.metersphere.project.mapper.ProjectRobotMapper; import io.metersphere.project.mapper.ProjectRobotMapper;
import io.metersphere.sdk.constants.SessionConstants; import io.metersphere.sdk.constants.SessionConstants;
import io.metersphere.sdk.domain.OperationLogExample; import io.metersphere.sdk.domain.OperationLogExample;
import io.metersphere.system.dto.sdk.OptionDTO;
import io.metersphere.system.dto.sdk.request.MessageTaskRequest;
import io.metersphere.sdk.mapper.OperationLogMapper; import io.metersphere.sdk.mapper.OperationLogMapper;
import io.metersphere.sdk.util.BeanUtils; import io.metersphere.sdk.util.BeanUtils;
import io.metersphere.sdk.util.JSON; import io.metersphere.sdk.util.JSON;
import io.metersphere.system.base.BaseTest; import io.metersphere.system.base.BaseTest;
import io.metersphere.system.controller.handler.ResultHolder; import io.metersphere.system.controller.handler.ResultHolder;
import io.metersphere.system.dto.sdk.OptionDTO;
import io.metersphere.system.dto.sdk.request.MessageTaskRequest;
import io.metersphere.system.log.constants.OperationLogType; import io.metersphere.system.log.constants.OperationLogType;
import io.metersphere.system.notice.constants.NoticeConstants; import io.metersphere.system.notice.constants.NoticeConstants;
import io.metersphere.system.uid.IDGenerator; import io.metersphere.system.uid.IDGenerator;
@ -83,19 +81,19 @@ public class ProjectRobotControllerTests extends BaseTest {
void addRobotSuccessWeCom() throws Exception { void addRobotSuccessWeCom() throws Exception {
ProjectRobotDTO projectRobotDTOInSite = new ProjectRobotDTO(); ProjectRobotDTO projectRobotDTOInSite = new ProjectRobotDTO();
projectRobotDTOInSite.setName("robot_in_site"); projectRobotDTOInSite.setName("robot_in_site");
projectRobotDTOInSite.setPlatform(ProjectRobotPlatform.IN_SITE.toString()); projectRobotDTOInSite.setPlatform(NoticeConstants.Type.IN_SITE);
projectRobotDTOInSite.setProjectId("test_project"); projectRobotDTOInSite.setProjectId("test_project");
projectRobotDTOInSite.setWebhook("https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=2b67ccf4-e0da-4cd6-ae74-8d42657865f8"); projectRobotDTOInSite.setWebhook("https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=2b67ccf4-e0da-4cd6-ae74-8d42657865f8");
getPostResult(projectRobotDTOInSite, ROBOT_ADD, status().isOk()); getPostResult(projectRobotDTOInSite, ROBOT_ADD, status().isOk());
ProjectRobotDTO projectRobotDTOMail = new ProjectRobotDTO(); ProjectRobotDTO projectRobotDTOMail = new ProjectRobotDTO();
projectRobotDTOMail.setName("robot_mail"); projectRobotDTOMail.setName("robot_mail");
projectRobotDTOMail.setPlatform(ProjectRobotPlatform.MAIL.toString()); projectRobotDTOMail.setPlatform(NoticeConstants.Type.MAIL);
projectRobotDTOMail.setProjectId("test_project"); projectRobotDTOMail.setProjectId("test_project");
projectRobotDTOMail.setWebhook("https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=2b67ccf4-e0da-4cd6-ae74-8d42657865f8"); projectRobotDTOMail.setWebhook("https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=2b67ccf4-e0da-4cd6-ae74-8d42657865f8");
getPostResult(projectRobotDTOMail, ROBOT_ADD, status().isOk()); getPostResult(projectRobotDTOMail, ROBOT_ADD, status().isOk());
ProjectRobotDTO projectRobotDTO = new ProjectRobotDTO(); ProjectRobotDTO projectRobotDTO = new ProjectRobotDTO();
projectRobotDTO.setName("企业微信机器人"); projectRobotDTO.setName("企业微信机器人");
projectRobotDTO.setPlatform(ProjectRobotPlatform.WE_COM.toString()); projectRobotDTO.setPlatform(NoticeConstants.Type.WE_COM);
projectRobotDTO.setProjectId("test_project"); projectRobotDTO.setProjectId("test_project");
projectRobotDTO.setWebhook("https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=2b67ccf4-e0da-4cd6-ae74-8d42657865f8"); projectRobotDTO.setWebhook("https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=2b67ccf4-e0da-4cd6-ae74-8d42657865f8");
getPostResult(projectRobotDTO, ROBOT_ADD, status().isOk()); getPostResult(projectRobotDTO, ROBOT_ADD, status().isOk());
@ -113,11 +111,11 @@ public class ProjectRobotControllerTests extends BaseTest {
void addRobotSuccessLark() throws Exception { void addRobotSuccessLark() throws Exception {
ProjectRobotDTO projectRobotDTO = new ProjectRobotDTO(); ProjectRobotDTO projectRobotDTO = new ProjectRobotDTO();
projectRobotDTO.setName("飞书机器人"); projectRobotDTO.setName("飞书机器人");
projectRobotDTO.setPlatform(ProjectRobotPlatform.LARK.toString()); projectRobotDTO.setPlatform(NoticeConstants.Type.LARK);
projectRobotDTO.setProjectId("test_project"); projectRobotDTO.setProjectId("test_project");
projectRobotDTO.setWebhook("https://open.feishu.cn/open-apis/bot/v2/hook/a6024229-9d9d-41c2-8662-7bc3da1092cb"); projectRobotDTO.setWebhook("https://open.feishu.cn/open-apis/bot/v2/hook/a6024229-9d9d-41c2-8662-7bc3da1092cb");
ProjectRobot postResult = getPostResult(projectRobotDTO, ROBOT_ADD, status().isOk()); ProjectRobot postResult = getPostResult(projectRobotDTO, ROBOT_ADD, status().isOk());
Assertions.assertTrue(StringUtils.equalsIgnoreCase(postResult.getPlatform(),NoticeConstants.Type.LARK_ROBOT)); Assertions.assertTrue(StringUtils.equalsIgnoreCase(postResult.getPlatform(),NoticeConstants.Type.LARK));
checkName("test_project", "飞书机器人"); checkName("test_project", "飞书机器人");
ProjectRobot robot = getRobot("test_project", "飞书机器人"); ProjectRobot robot = getRobot("test_project", "飞书机器人");
checkContentLog(robot.getName(),OperationLogType.ADD); checkContentLog(robot.getName(),OperationLogType.ADD);
@ -128,13 +126,13 @@ public class ProjectRobotControllerTests extends BaseTest {
void addRobotSuccessDingCustom() throws Exception { void addRobotSuccessDingCustom() throws Exception {
ProjectRobotDTO projectRobotDTOInSite = new ProjectRobotDTO(); ProjectRobotDTO projectRobotDTOInSite = new ProjectRobotDTO();
projectRobotDTOInSite.setName("robot_in_site"); projectRobotDTOInSite.setName("robot_in_site");
projectRobotDTOInSite.setPlatform(ProjectRobotPlatform.IN_SITE.toString()); projectRobotDTOInSite.setPlatform(NoticeConstants.Type.IN_SITE);
projectRobotDTOInSite.setProjectId("test_project3"); projectRobotDTOInSite.setProjectId("test_project3");
projectRobotDTOInSite.setWebhook("https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=2b67ccf4-e0da-4cd6-ae74-8d42657865f8"); projectRobotDTOInSite.setWebhook("https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=2b67ccf4-e0da-4cd6-ae74-8d42657865f8");
getPostResult(projectRobotDTOInSite, ROBOT_ADD, status().isOk()); getPostResult(projectRobotDTOInSite, ROBOT_ADD, status().isOk());
ProjectRobotDTO projectRobotDTOMail = new ProjectRobotDTO(); ProjectRobotDTO projectRobotDTOMail = new ProjectRobotDTO();
projectRobotDTOMail.setName("robot_mail"); projectRobotDTOMail.setName("robot_mail");
projectRobotDTOMail.setPlatform(ProjectRobotPlatform.MAIL.toString()); projectRobotDTOMail.setPlatform(NoticeConstants.Type.MAIL);
projectRobotDTOMail.setProjectId("test_project3"); projectRobotDTOMail.setProjectId("test_project3");
projectRobotDTOMail.setWebhook("https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=2b67ccf4-e0da-4cd6-ae74-8d42657865f8"); projectRobotDTOMail.setWebhook("https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=2b67ccf4-e0da-4cd6-ae74-8d42657865f8");
getPostResult(projectRobotDTOMail, ROBOT_ADD, status().isOk()); getPostResult(projectRobotDTOMail, ROBOT_ADD, status().isOk());
@ -145,7 +143,7 @@ public class ProjectRobotControllerTests extends BaseTest {
private void setInSiteCustom(String projectId,String name) throws Exception { private void setInSiteCustom(String projectId,String name) throws Exception {
ProjectRobotDTO projectRobotDTO = new ProjectRobotDTO(); ProjectRobotDTO projectRobotDTO = new ProjectRobotDTO();
projectRobotDTO.setName(name); projectRobotDTO.setName(name);
projectRobotDTO.setPlatform(ProjectRobotPlatform.IN_SITE.toString()); projectRobotDTO.setPlatform(NoticeConstants.Type.IN_SITE);
projectRobotDTO.setProjectId(projectId); projectRobotDTO.setProjectId(projectId);
projectRobotDTO.setEnable(true); projectRobotDTO.setEnable(true);
projectRobotDTO.setWebhook("NONE"); projectRobotDTO.setWebhook("NONE");
@ -155,13 +153,13 @@ public class ProjectRobotControllerTests extends BaseTest {
private void setDingCustom(String projectId,String name) throws Exception { private void setDingCustom(String projectId,String name) throws Exception {
ProjectRobotDTO projectRobotDTO = new ProjectRobotDTO(); ProjectRobotDTO projectRobotDTO = new ProjectRobotDTO();
projectRobotDTO.setName(name); projectRobotDTO.setName(name);
projectRobotDTO.setPlatform(ProjectRobotPlatform.DING_TALK.toString()); projectRobotDTO.setPlatform(NoticeConstants.Type.DING_TALK);
projectRobotDTO.setProjectId(projectId); projectRobotDTO.setProjectId(projectId);
projectRobotDTO.setEnable(true); projectRobotDTO.setEnable(true);
projectRobotDTO.setType(ProjectRobotType.CUSTOM.toString()); projectRobotDTO.setType(NoticeConstants.DingType.CUSTOM);
projectRobotDTO.setWebhook("https://oapi.dingtalk.com/robot/send?access_token=fd963136a4d7eebaaa68de261223089148e62d7519fbaf426626fe3157725b8a"); projectRobotDTO.setWebhook("https://oapi.dingtalk.com/robot/send?access_token=fd963136a4d7eebaaa68de261223089148e62d7519fbaf426626fe3157725b8a");
ProjectRobot postResult = getPostResult(projectRobotDTO, ROBOT_ADD, status().isOk()); ProjectRobot postResult = getPostResult(projectRobotDTO, ROBOT_ADD, status().isOk());
Assertions.assertTrue(StringUtils.equalsIgnoreCase(postResult.getPlatform(),NoticeConstants.Type.DING_CUSTOM_ROBOT)); Assertions.assertTrue(StringUtils.equalsIgnoreCase(postResult.getPlatform(),NoticeConstants.Type.DING_TALK));
} }
@ -175,14 +173,14 @@ public class ProjectRobotControllerTests extends BaseTest {
private void setDingEn(String name) throws Exception { private void setDingEn(String name) throws Exception {
ProjectRobotDTO projectRobotDTO = new ProjectRobotDTO(); ProjectRobotDTO projectRobotDTO = new ProjectRobotDTO();
projectRobotDTO.setName(name); projectRobotDTO.setName(name);
projectRobotDTO.setPlatform(ProjectRobotPlatform.DING_TALK.toString()); projectRobotDTO.setPlatform(NoticeConstants.Type.DING_TALK);
projectRobotDTO.setProjectId("test_project"); projectRobotDTO.setProjectId("test_project");
projectRobotDTO.setType(ProjectRobotType.ENTERPRISE.toString()); projectRobotDTO.setType(NoticeConstants.DingType.ENTERPRISE);
projectRobotDTO.setAppKey("dingxwd71o7kj4qoixo7"); projectRobotDTO.setAppKey("dingxwd71o7kj4qoixo7");
projectRobotDTO.setAppSecret("szmOD9bjGgKtfYk09-Xx2rPdX-xkW4R8Iic0eig_k1D3k95nG4TLKRSpUKUD_f0G"); projectRobotDTO.setAppSecret("szmOD9bjGgKtfYk09-Xx2rPdX-xkW4R8Iic0eig_k1D3k95nG4TLKRSpUKUD_f0G");
projectRobotDTO.setWebhook("https://oapi.dingtalk.com/robot/send?access_token=e971f376669334cd44c585d419f0fdfa1600f97f906109b377999d8a0986b11e"); projectRobotDTO.setWebhook("https://oapi.dingtalk.com/robot/send?access_token=e971f376669334cd44c585d419f0fdfa1600f97f906109b377999d8a0986b11e");
ProjectRobot postResult = getPostResult(projectRobotDTO, ROBOT_ADD, status().isOk()); ProjectRobot postResult = getPostResult(projectRobotDTO, ROBOT_ADD, status().isOk());
Assertions.assertTrue(StringUtils.equalsIgnoreCase(postResult.getPlatform(),NoticeConstants.Type.DING_ENTERPRISE_ROBOT)); Assertions.assertTrue(StringUtils.equalsIgnoreCase(postResult.getPlatform(),NoticeConstants.Type.DING_TALK));
} }
@Test @Test
@ -190,7 +188,7 @@ public class ProjectRobotControllerTests extends BaseTest {
void addRobotFailDingCustomByType() throws Exception { void addRobotFailDingCustomByType() throws Exception {
ProjectRobotDTO projectRobotDTO = new ProjectRobotDTO(); ProjectRobotDTO projectRobotDTO = new ProjectRobotDTO();
projectRobotDTO.setName("钉钉自定义机器人"); projectRobotDTO.setName("钉钉自定义机器人");
projectRobotDTO.setPlatform(ProjectRobotPlatform.DING_TALK.toString()); projectRobotDTO.setPlatform(NoticeConstants.Type.DING_TALK);
projectRobotDTO.setProjectId("test_project"); projectRobotDTO.setProjectId("test_project");
projectRobotDTO.setWebhook("https://oapi.dingtalk.com/robot/send?access_token=e971f376669334cd44c585d419f0fdfa1600f97f906109b377999d8a0986b11e"); projectRobotDTO.setWebhook("https://oapi.dingtalk.com/robot/send?access_token=e971f376669334cd44c585d419f0fdfa1600f97f906109b377999d8a0986b11e");
getPostResult(projectRobotDTO, ROBOT_ADD, status().is5xxServerError()); getPostResult(projectRobotDTO, ROBOT_ADD, status().is5xxServerError());
@ -201,9 +199,9 @@ public class ProjectRobotControllerTests extends BaseTest {
void addRobotFailDingCustomByKey() throws Exception { void addRobotFailDingCustomByKey() throws Exception {
ProjectRobotDTO projectRobotDTO = new ProjectRobotDTO(); ProjectRobotDTO projectRobotDTO = new ProjectRobotDTO();
projectRobotDTO.setName("钉钉自定义机器人"); projectRobotDTO.setName("钉钉自定义机器人");
projectRobotDTO.setPlatform(ProjectRobotPlatform.DING_TALK.toString()); projectRobotDTO.setPlatform(NoticeConstants.Type.DING_TALK);
projectRobotDTO.setProjectId("test_project"); projectRobotDTO.setProjectId("test_project");
projectRobotDTO.setType(ProjectRobotType.ENTERPRISE.toString()); projectRobotDTO.setType(NoticeConstants.DingType.ENTERPRISE);
projectRobotDTO.setWebhook("https://oapi.dingtalk.com/robot/send?access_token=e971f376669334cd44c585d419f0fdfa1600f97f906109b377999d8a0986b11e"); projectRobotDTO.setWebhook("https://oapi.dingtalk.com/robot/send?access_token=e971f376669334cd44c585d419f0fdfa1600f97f906109b377999d8a0986b11e");
getPostResult(projectRobotDTO, ROBOT_ADD, status().is5xxServerError()); getPostResult(projectRobotDTO, ROBOT_ADD, status().is5xxServerError());
} }
@ -213,9 +211,9 @@ public class ProjectRobotControllerTests extends BaseTest {
void addRobotFailDingCustomBySecret() throws Exception { void addRobotFailDingCustomBySecret() throws Exception {
ProjectRobotDTO projectRobotDTO = new ProjectRobotDTO(); ProjectRobotDTO projectRobotDTO = new ProjectRobotDTO();
projectRobotDTO.setName("钉钉自定义机器人"); projectRobotDTO.setName("钉钉自定义机器人");
projectRobotDTO.setPlatform(ProjectRobotPlatform.DING_TALK.toString()); projectRobotDTO.setPlatform(NoticeConstants.Type.DING_TALK);
projectRobotDTO.setProjectId("test_project"); projectRobotDTO.setProjectId("test_project");
projectRobotDTO.setType(ProjectRobotType.ENTERPRISE.toString()); projectRobotDTO.setType(NoticeConstants.DingType.ENTERPRISE);
projectRobotDTO.setAppKey("dingxwd71o7kj4qoixo7"); projectRobotDTO.setAppKey("dingxwd71o7kj4qoixo7");
projectRobotDTO.setWebhook("https://oapi.dingtalk.com/robot/send?access_token=e971f376669334cd44c585d419f0fdfa1600f97f906109b377999d8a0986b11e"); projectRobotDTO.setWebhook("https://oapi.dingtalk.com/robot/send?access_token=e971f376669334cd44c585d419f0fdfa1600f97f906109b377999d8a0986b11e");
getPostResult(projectRobotDTO, ROBOT_ADD, status().is5xxServerError()); getPostResult(projectRobotDTO, ROBOT_ADD, status().is5xxServerError());
@ -481,7 +479,7 @@ public class ProjectRobotControllerTests extends BaseTest {
private void setCustomRobot(String name, String projectId) throws Exception { private void setCustomRobot(String name, String projectId) throws Exception {
ProjectRobotDTO projectRobotDTO = new ProjectRobotDTO(); ProjectRobotDTO projectRobotDTO = new ProjectRobotDTO();
projectRobotDTO.setName(name); projectRobotDTO.setName(name);
projectRobotDTO.setPlatform(ProjectRobotPlatform.CUSTOM.toString()); projectRobotDTO.setPlatform(NoticeConstants.Type.CUSTOM);
projectRobotDTO.setProjectId(projectId); projectRobotDTO.setProjectId(projectId);
projectRobotDTO.setEnable(true); projectRobotDTO.setEnable(true);
projectRobotDTO.setWebhook("https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=2b67ccf4-e0da-4cd6-ae74-8d42657865f8"); projectRobotDTO.setWebhook("https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=2b67ccf4-e0da-4cd6-ae74-8d42657865f8");

View File

@ -14,6 +14,7 @@ public class MessageDetail implements Serializable {
private String taskType; private String taskType;
private String webhook; private String webhook;
private String type; private String type;
private String dingType;
private String testId; private String testId;
private Long createTime; private Long createTime;
private String subject; private String subject;

View File

@ -106,11 +106,15 @@ public interface NoticeConstants {
interface Type { interface Type {
String MAIL = "MAIL"; String MAIL = "MAIL";
String IN_SITE = "IN_SITE"; String IN_SITE = "IN_SITE";
String DING_CUSTOM_ROBOT = "DING_CUSTOM_ROBOT"; String DING_TALK= "DING_TALK";
String DING_ENTERPRISE_ROBOT = "DING_ENTERPRISE_ROBOT"; String WE_COM = "WE_COM";
String WECOM_ROBOT = "WECOM_ROBOT"; String LARK = "LARK";
String LARK_ROBOT = "LARK_ROBOT"; String CUSTOM = "CUSTOM";
String CUSTOM_WEBHOOK_ROBOT = "CUSTOM_WEBHOOK_ROBOT"; }
interface DingType {
String CUSTOM = "CUSTOM";
String ENTERPRISE = "ENTERPRISE";
} }
interface Event { interface Event {

View File

@ -97,6 +97,9 @@ public class MessageDetailService {
return; return;
} }
messageDetail.setType(projectRobot.getPlatform()); messageDetail.setType(projectRobot.getPlatform());
if (StringUtils.isNotBlank(projectRobot.getType())) {
messageDetail.setDingType(projectRobot.getType());
}
if (StringUtils.isNotBlank(projectRobot.getWebhook())) { if (StringUtils.isNotBlank(projectRobot.getWebhook())) {
messageDetail.setWebhook(projectRobot.getWebhook()); messageDetail.setWebhook(projectRobot.getWebhook());
} }

View File

@ -12,7 +12,6 @@ import jakarta.annotation.Resource;
import org.apache.commons.lang3.SerializationUtils; import org.apache.commons.lang3.SerializationUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.context.i18n.SimpleLocaleContext;
import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -43,28 +42,29 @@ public class NoticeSendService {
AbstractNoticeSender noticeSender; AbstractNoticeSender noticeSender;
switch (messageDetail.getType()) { switch (messageDetail.getType()) {
case NoticeConstants.Type.MAIL -> noticeSender = mailNoticeSender; case NoticeConstants.Type.MAIL -> noticeSender = mailNoticeSender;
case NoticeConstants.Type.WECOM_ROBOT -> noticeSender = weComNoticeSender; case NoticeConstants.Type.WE_COM -> noticeSender = weComNoticeSender;
case NoticeConstants.Type.DING_CUSTOM_ROBOT -> noticeSender = dingCustomNoticeSender; case NoticeConstants.Type.DING_TALK -> noticeSender = getDingSender(messageDetail.getDingType());
case NoticeConstants.Type.DING_ENTERPRISE_ROBOT -> noticeSender = dingEnterPriseNoticeSender; case NoticeConstants.Type.LARK -> noticeSender = larkNoticeSender;
case NoticeConstants.Type.LARK_ROBOT -> noticeSender = larkNoticeSender; case NoticeConstants.Type.CUSTOM -> noticeSender = webhookNoticeSender;
case NoticeConstants.Type.CUSTOM_WEBHOOK_ROBOT -> noticeSender = webhookNoticeSender;
default -> noticeSender = inSiteNoticeSender; default -> noticeSender = inSiteNoticeSender;
} }
return noticeSender; return noticeSender;
} }
private AbstractNoticeSender getDingSender(String dingType) {
if (StringUtils.equalsIgnoreCase(dingType, NoticeConstants.DingType.ENTERPRISE)) {
return dingEnterPriseNoticeSender;
}else{
return dingCustomNoticeSender;
}
}
/** /**
* 在线操作发送通知 * 在线操作发送通知
*/ */
@Async @Async
public void send(String taskType, NoticeModel noticeModel) { public void send(String taskType, NoticeModel noticeModel) {
String language = (String) noticeModel.getParamMap().get("Language"); setLanguage(noticeModel);
if (StringUtils.isBlank(language)) {
language = "zh-CN";
}
SimpleLocaleContext localeContext = new SimpleLocaleContext(Locale.of(language));
LocaleContextHolder.setLocaleContext(localeContext);
try { try {
String projectId = (String) noticeModel.getParamMap().get("projectId"); String projectId = (String) noticeModel.getParamMap().get("projectId");
List<MessageDetail> messageDetails = messageDetailService.searchMessageByTypeAndProjectId(taskType, projectId); List<MessageDetail> messageDetails = messageDetailService.searchMessageByTypeAndProjectId(taskType, projectId);
@ -79,12 +79,20 @@ public class NoticeSendService {
} catch (Exception e) { } catch (Exception e) {
LogUtils.error(e.getMessage(), e); LogUtils.error(e.getMessage(), e);
}finally {
//清理语言环境
LocaleContextHolder.resetLocaleContext();
} }
} }
private static void setLanguage(NoticeModel noticeModel) {
String language = (String) noticeModel.getParamMap().get("Language");
if (StringUtils.isBlank(language)) {
language = "zh_CN";
}
if (language.contains("-")) {
language = language.replace("-","_");
}
LocaleContextHolder.setLocale(Locale.of(language));
}
/** /**
* jenkins 和定时任务触发的发送 * jenkins 和定时任务触发的发送
*/ */