diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/controller/NoticeMessageTaskController.java b/backend/services/project-management/src/main/java/io/metersphere/project/controller/NoticeMessageTaskController.java index 6183f614c2..7ed2e6951b 100644 --- a/backend/services/project-management/src/main/java/io/metersphere/project/controller/NoticeMessageTaskController.java +++ b/backend/services/project-management/src/main/java/io/metersphere/project/controller/NoticeMessageTaskController.java @@ -2,13 +2,13 @@ package io.metersphere.project.controller; import io.metersphere.project.dto.MessageTaskDTO; +import io.metersphere.project.dto.MessageTemplateConfigDTO; import io.metersphere.project.service.MessageTaskLogService; import io.metersphere.project.service.NoticeMessageTaskService; import io.metersphere.sdk.constants.PermissionConstants; import io.metersphere.sdk.dto.OptionDTO; -import io.metersphere.system.controller.handler.ResultHolder; import io.metersphere.sdk.dto.request.MessageTaskRequest; - +import io.metersphere.system.controller.handler.ResultHolder; import io.metersphere.system.log.annotation.Log; import io.metersphere.system.log.constants.OperationLogType; import io.metersphere.system.utils.SessionUtils; @@ -57,5 +57,15 @@ public class NoticeMessageTaskController { return noticeMessageTaskService.getUserList(projectId, keyword); } + @GetMapping("message/template/detail/{projectId}") + @Operation(summary = "项目管理-消息管理-消息设置-查看消息模版详情") + @RequiresPermissions(PermissionConstants.PROJECT_MESSAGE_READ_ADD) + public MessageTemplateConfigDTO getTemplateDetail(@PathVariable String projectId, @Schema(description = "消息配置功能类型") + @RequestParam(value = "taskType") String taskType, @Schema(description = "消息配置场景") + @RequestParam(value = "event") String event, @Schema(description = "消息配置机器人id") + @RequestParam(value = "robotId") String robotId) { + return noticeMessageTaskService.getTemplateDetail(projectId, taskType, event, robotId); + } + } diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/dto/MessageTemplateConfigDTO.java b/backend/services/project-management/src/main/java/io/metersphere/project/dto/MessageTemplateConfigDTO.java new file mode 100644 index 0000000000..f287783275 --- /dev/null +++ b/backend/services/project-management/src/main/java/io/metersphere/project/dto/MessageTemplateConfigDTO.java @@ -0,0 +1,18 @@ +package io.metersphere.project.dto; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; +import lombok.EqualsAndHashCode; + +import java.util.List; + +@Data +@EqualsAndHashCode(callSuper = false) +public class MessageTemplateConfigDTO extends ProjectRobotConfigDTO{ + + @Schema(description = "消息配置接收人") + private List receiverIds; + + @Schema(description = "具体测试的ID") + public String testId; +} diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/service/NoticeMessageTaskService.java b/backend/services/project-management/src/main/java/io/metersphere/project/service/NoticeMessageTaskService.java index 24f2a97e1d..8ca69c3ddb 100644 --- a/backend/services/project-management/src/main/java/io/metersphere/project/service/NoticeMessageTaskService.java +++ b/backend/services/project-management/src/main/java/io/metersphere/project/service/NoticeMessageTaskService.java @@ -2,28 +2,26 @@ package io.metersphere.project.service; import io.metersphere.project.domain.*; -import io.metersphere.project.dto.MessageTaskDTO; -import io.metersphere.project.dto.MessageTaskDetailDTO; -import io.metersphere.project.dto.MessageTaskTypeDTO; -import io.metersphere.project.dto.ProjectRobotConfigDTO; +import io.metersphere.project.dto.*; import io.metersphere.project.enums.ProjectRobotPlatform; import io.metersphere.project.enums.result.ProjectResultCode; import io.metersphere.project.mapper.*; import io.metersphere.sdk.dto.OptionDTO; -import io.metersphere.sdk.util.JSON; -import io.metersphere.system.controller.handler.ResultHolder; import io.metersphere.sdk.dto.request.MessageTaskRequest; import io.metersphere.sdk.exception.MSException; -import io.metersphere.system.domain.UserExample; -import io.metersphere.system.notice.constants.NoticeConstants; -import io.metersphere.system.notice.utils.MessageTemplateUtils; -import io.metersphere.system.uid.UUID; +import io.metersphere.sdk.util.BeanUtils; +import io.metersphere.sdk.util.JSON; import io.metersphere.sdk.util.Translator; +import io.metersphere.system.controller.handler.ResultHolder; import io.metersphere.system.domain.User; +import io.metersphere.system.domain.UserExample; import io.metersphere.system.domain.UserRoleRelation; import io.metersphere.system.domain.UserRoleRelationExample; import io.metersphere.system.mapper.UserMapper; import io.metersphere.system.mapper.UserRoleRelationMapper; +import io.metersphere.system.notice.constants.NoticeConstants; +import io.metersphere.system.notice.utils.MessageTemplateUtils; +import io.metersphere.system.uid.UUID; import jakarta.annotation.Resource; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; @@ -31,7 +29,6 @@ import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionUtils; - import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -39,7 +36,10 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; -import java.util.*; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import java.util.stream.Collectors; @Service @@ -323,7 +323,7 @@ public class NoticeMessageTaskService { Map taskTypeMap = MessageTemplateUtils.getTaskTypeMap(); Map eventMap = MessageTemplateUtils.getEventMap(); Map defaultTemplateMap = MessageTemplateUtils.getDefaultTemplateMap(); - Map defaultTemplateTitleMap = MessageTemplateUtils.getDefaultTemplateTitleMap(); + Map defaultTemplateSubjectMap = MessageTemplateUtils.getDefaultTemplateSubjectMap(); ProjectRobot projectRobot = setDefaultRobot(projectId, null); for (MessageTaskDTO messageTaskDTO : messageTaskDTOList) { messageTaskDTO.setProjectId(projectId); @@ -340,9 +340,9 @@ public class NoticeMessageTaskService { List messageTaskList = messageEventMap.get(messageTaskDetailDTO.getEvent()); List receivers = new ArrayList<>(); Map projectRobotConfigMap = new HashMap<>(); - String defaultTemplate = defaultTemplateMap.get(messageTaskTypeDTO.taskType + "_" + messageTaskDetailDTO.getEvent()); + String defaultTemplate = defaultTemplateMap.get(messageTaskTypeDTO.getTaskType() + "_" + messageTaskDetailDTO.getEvent()); if (CollectionUtils.isEmpty(messageTaskList)) { - String defaultSubject = defaultTemplateTitleMap.get(messageTaskTypeDTO.taskType + "_" + messageTaskDetailDTO.getEvent()); + String defaultSubject = defaultTemplateSubjectMap.get(messageTaskTypeDTO.getTaskType() + "_" + messageTaskDetailDTO.getEvent()); ProjectRobotConfigDTO projectRobotConfigDTO = getDefaultProjectRobotConfigDTO(defaultTemplate, defaultSubject, projectRobot); projectRobotConfigMap.put(projectRobot.getId(), projectRobotConfigDTO); } else { @@ -355,11 +355,11 @@ public class NoticeMessageTaskService { String platform = robotMap.get(messageTask.getProjectRobotId()).getPlatform(); String defaultSubject; if (StringUtils.equalsIgnoreCase(platform, ProjectRobotPlatform.MAIL.toString())) { - defaultSubject = "MeterSphere " + defaultTemplateTitleMap.get(messageTaskTypeDTO.taskType + "_" + messageTaskDetailDTO.getEvent()); + defaultSubject = "MeterSphere " + defaultTemplateSubjectMap.get(messageTaskTypeDTO.getTaskType() + "_" + messageTaskDetailDTO.getEvent()); } else { - defaultSubject = defaultTemplateTitleMap.get(messageTaskTypeDTO.taskType + "_" + messageTaskDetailDTO.getEvent()); + defaultSubject = defaultTemplateSubjectMap.get(messageTaskTypeDTO.getTaskType() + "_" + messageTaskDetailDTO.getEvent()); } - ProjectRobotConfigDTO projectRobotConfigDTO = getProjectRobotConfigDTO(defaultTemplate, defaultSubject, robotMap, messageTask, messageTaskBlob); + ProjectRobotConfigDTO projectRobotConfigDTO = getProjectRobotConfigDTO(defaultTemplate, defaultSubject, robotMap.get(messageTask.getProjectRobotId()), messageTask, messageTaskBlob); projectRobotConfigMap.put(messageTask.getProjectRobotId(), projectRobotConfigDTO); } } @@ -372,11 +372,10 @@ public class NoticeMessageTaskService { return messageTaskDTOList; } - private ProjectRobotConfigDTO getProjectRobotConfigDTO(String defaultTemplate, String defaultSubject, Map robotMap, MessageTask messageTask, MessageTaskBlob messageTaskBlob) { + private ProjectRobotConfigDTO getProjectRobotConfigDTO(String defaultTemplate, String defaultSubject, ProjectRobot projectRobot, MessageTask messageTask, MessageTaskBlob messageTaskBlob) { ProjectRobotConfigDTO projectRobotConfigDTO = new ProjectRobotConfigDTO(); - ProjectRobot projectRobot = robotMap.get(messageTask.getProjectRobotId()); projectRobotConfigDTO.setRobotName(projectRobot.getName()); - projectRobotConfigDTO.setRobotId(messageTask.getProjectRobotId()); + projectRobotConfigDTO.setRobotId(projectRobot.getId()); projectRobotConfigDTO.setPlatform(projectRobot.getPlatform()); projectRobotConfigDTO.setDingType(projectRobot.getType()); projectRobotConfigDTO.setEnable(messageTask.getEnable()); @@ -408,4 +407,35 @@ public class NoticeMessageTaskService { public List getUserList(String projectId, String keyword) { return extProjectUserRoleMapper.getProjectUserSelectList(projectId, keyword); } + + public MessageTemplateConfigDTO getTemplateDetail(String projectId, String taskType, String event, String robotId) { + MessageTaskExample messageTaskExample = new MessageTaskExample(); + messageTaskExample.createCriteria().andProjectIdEqualTo(projectId).andTaskTypeEqualTo(taskType).andEventEqualTo(event); + List messageTasks = messageTaskMapper.selectByExample(messageTaskExample); + List receiverIds = messageTasks.stream().map(MessageTask::getReceiver).distinct().toList(); + Map> messageRobotMap = messageTasks.stream().collect(Collectors.groupingBy(MessageTask::getProjectRobotId)); + MessageTask messageTask; + if (CollectionUtils.isNotEmpty(messageRobotMap.get(robotId))) { + messageTask = messageRobotMap.get(robotId).get(0); + } else { + messageTask = messageTasks.get(0); + messageTask.setEnable(false); + messageTask.setUseDefaultTemplate(true); + messageTask.setUseDefaultSubject(true); + } + MessageTaskBlob messageTaskBlob = messageTaskBlobMapper.selectByPrimaryKey(messageTask.getId()); + Map defaultTemplateMap = MessageTemplateUtils.getDefaultTemplateMap(); + Map defaultTemplateSubjectMap = MessageTemplateUtils.getDefaultTemplateSubjectMap(); + String defaultTemplate = defaultTemplateMap.get(messageTask.getTaskType() + "_" + messageTask.getEvent()); + String defaultSubject = defaultTemplateSubjectMap.get(messageTask.getTaskType() + "_" + messageTask.getEvent()); + ProjectRobot projectRobot = projectRobotMapper.selectByPrimaryKey(robotId); + if (projectRobot == null) { + throw new MSException(Translator.get("robot_is_null")); + } + ProjectRobotConfigDTO projectRobotConfigDTO = getProjectRobotConfigDTO(defaultTemplate, defaultSubject, projectRobot, messageTask, messageTaskBlob); + MessageTemplateConfigDTO messageTemplateConfigDTO = new MessageTemplateConfigDTO(); + BeanUtils.copyBean(messageTemplateConfigDTO,projectRobotConfigDTO); + messageTemplateConfigDTO.setReceiverIds(receiverIds); + return messageTemplateConfigDTO; + } } diff --git a/backend/services/project-management/src/test/java/io/metersphere/project/controller/NoticeMessageTaskControllerTests.java b/backend/services/project-management/src/test/java/io/metersphere/project/controller/NoticeMessageTaskControllerTests.java index fb60e646a4..af08514864 100644 --- a/backend/services/project-management/src/test/java/io/metersphere/project/controller/NoticeMessageTaskControllerTests.java +++ b/backend/services/project-management/src/test/java/io/metersphere/project/controller/NoticeMessageTaskControllerTests.java @@ -7,6 +7,7 @@ import io.metersphere.project.domain.MessageTaskExample; import io.metersphere.project.dto.MessageTaskDTO; import io.metersphere.project.dto.MessageTaskDetailDTO; +import io.metersphere.project.dto.MessageTemplateConfigDTO; import io.metersphere.project.dto.ProjectRobotConfigDTO; import io.metersphere.project.mapper.MessageTaskBlobMapper; import io.metersphere.project.mapper.MessageTaskMapper; @@ -483,4 +484,54 @@ public class NoticeMessageTaskControllerTests extends BaseTest { List userDtoList = JSON.parseArray(JSON.toJSONString(resultHolder.getData()), OptionDTO.class); Assertions.assertEquals(0, userDtoList.size()); } + + @Test + @Order(19) + public void getTemplateDetailWithRobot() throws Exception { + MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/notice/message/template/detail/project-message-test") + .header(SessionConstants.HEADER_TOKEN, sessionId) + .header(SessionConstants.CSRF_TOKEN, csrfToken) + .param("taskType", NoticeConstants.TaskType.API_DEFINITION_TASK) + .param("event", NoticeConstants.Event.CREATE) + .param("robotId", "test_message_robot1") + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)).andReturn(); + String contentAsString = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8); + ResultHolder resultHolder = JSON.parseObject(contentAsString, ResultHolder.class); + MessageTemplateConfigDTO messageTemplateConfigDTO = JSON.parseObject(JSON.toJSONString(resultHolder.getData()), MessageTemplateConfigDTO.class); + Assertions.assertTrue(messageTemplateConfigDTO.getReceiverIds().size()>0); + } + + @Test + @Order(20) + public void getTemplateDetailWithOutRobot() throws Exception { + MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/notice/message/template/detail/project-message-test") + .header(SessionConstants.HEADER_TOKEN, sessionId) + .header(SessionConstants.CSRF_TOKEN, csrfToken) + .param("taskType", NoticeConstants.TaskType.API_DEFINITION_TASK) + .param("event", NoticeConstants.Event.CREATE) + .param("robotId", "test_message_robot3") + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)).andReturn(); + String contentAsString = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8); + ResultHolder resultHolder = JSON.parseObject(contentAsString, ResultHolder.class); + MessageTemplateConfigDTO messageTemplateConfigDTO = JSON.parseObject(JSON.toJSONString(resultHolder.getData()), MessageTemplateConfigDTO.class); + Assertions.assertTrue(messageTemplateConfigDTO.getReceiverIds().size()>0); + } + + @Test + @Order(21) + public void getTemplateDetailNotExistRobot() throws Exception { + mockMvc.perform(MockMvcRequestBuilders.get("/notice/message/template/detail/project-message-test") + .header(SessionConstants.HEADER_TOKEN, sessionId) + .header(SessionConstants.CSRF_TOKEN, csrfToken) + .param("taskType", NoticeConstants.TaskType.API_DEFINITION_TASK) + .param("event", NoticeConstants.Event.CREATE) + .param("robotId", "test_message_robotX") + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().is5xxServerError()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)).andReturn(); + } } diff --git a/backend/services/project-management/src/test/resources/dml/init_project_message.sql b/backend/services/project-management/src/test/resources/dml/init_project_message.sql index c4c8042283..b2904f4ae6 100644 --- a/backend/services/project-management/src/test/resources/dml/init_project_message.sql +++ b/backend/services/project-management/src/test/resources/dml/init_project_message.sql @@ -35,7 +35,8 @@ INSERT INTO user_role_relation (id, user_id, role_id, source_id, organization_id INSERT INTO project_robot(id, project_id, name, platform, webhook, type, app_key, app_secret, enable, create_user, create_time, update_user, update_time, description) VALUES ('test_message_robot1', 'project-message-test-1', '测试机器人1', 'IN_SITE', 'NONE', null, null, null, true, 'admin', unix_timestamp() * 1000,'admin', unix_timestamp() * 1000, null), - ('test_message_robot2', 'project-message-test-1', '测试机器人2', 'MAIL', 'NONE', null, null, null, true, 'admin', unix_timestamp() * 1000,'admin', unix_timestamp() * 1000, null); + ('test_message_robot2', 'project-message-test-1', '测试机器人2', 'MAIL', 'NONE', null, null, null, true, 'admin', unix_timestamp() * 1000,'admin', unix_timestamp() * 1000, null), + ('test_message_robot3', 'project-message-test-1', '测试机器人3', 'WE_COM', 'NONE', null, null, null, true, 'admin', unix_timestamp() * 1000,'admin', unix_timestamp() * 1000, null); diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/notice/constants/NoticeConstants.java b/backend/services/system-setting/src/main/java/io/metersphere/system/notice/constants/NoticeConstants.java index 465b898023..9a12267935 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/notice/constants/NoticeConstants.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/notice/constants/NoticeConstants.java @@ -290,7 +290,7 @@ public interface NoticeConstants { String JENKINS_TASK_EXECUTE_FAILED = "JENKINS_TASK_EXECUTE_FAILED";//Jenkins执行了测试计划:${name} } - interface TemplateTitle { + interface TemplateSubject { @Schema(description = "message.title.test_plan_task_create") String TEST_PLAN_TASK_CREATE = "TEST_PLAN_TASK_CREATE"; // 测试计划创建通知 @Schema(description = "message.title.test_plan_task_update") diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/notice/sender/AfterReturningNoticeSendService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/notice/sender/AfterReturningNoticeSendService.java index f52b352533..5ed4ea02ba 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/notice/sender/AfterReturningNoticeSendService.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/notice/sender/AfterReturningNoticeSendService.java @@ -56,7 +56,7 @@ public class AfterReturningNoticeSendService { } private String getSubject(SendNotice sendNotice) { - Map defaultTemplateTitleMap = MessageTemplateUtils.getDefaultTemplateTitleMap(); + Map defaultTemplateTitleMap = MessageTemplateUtils.getDefaultTemplateSubjectMap(); return defaultTemplateTitleMap.get(sendNotice.taskType() + "_" + sendNotice.event()); } diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/notice/utils/MessageTemplateUtils.java b/backend/services/system-setting/src/main/java/io/metersphere/system/notice/utils/MessageTemplateUtils.java index bff9af049c..819e7bfc12 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/notice/utils/MessageTemplateUtils.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/notice/utils/MessageTemplateUtils.java @@ -77,11 +77,11 @@ public class MessageTemplateUtils { * * @return Map defaultTemplateTitleMap */ - public static Map getDefaultTemplateTitleMap() { - Map defaultTemplateTitleMap = new HashMap<>(); - Field[] defaultTemplateTitleFields = FieldUtils.getAllFields(NoticeConstants.TemplateTitle.class); - MessageTemplateUtils.setFieldNameMap(defaultTemplateTitleFields, defaultTemplateTitleMap); - return defaultTemplateTitleMap; + public static Map getDefaultTemplateSubjectMap() { + Map defaultTemplateSubjectMap = new HashMap<>(); + Field[] defaultTemplateSubjectFields = FieldUtils.getAllFields(NoticeConstants.TemplateSubject.class); + MessageTemplateUtils.setFieldNameMap(defaultTemplateSubjectFields, defaultTemplateSubjectMap); + return defaultTemplateSubjectMap; } /** diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/MessageDetailService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/MessageDetailService.java index 21681343cc..c1cf494f87 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/service/MessageDetailService.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/MessageDetailService.java @@ -127,7 +127,7 @@ public class MessageDetailService { } private String getSubject(String taskType, String event) { - Map defaultTemplateTitleMap = MessageTemplateUtils.getDefaultTemplateTitleMap(); + Map defaultTemplateTitleMap = MessageTemplateUtils.getDefaultTemplateSubjectMap(); return "MeterSphere " + defaultTemplateTitleMap.get(taskType + "_" + event); }