feat(消息管理): add project messageTask resource

This commit is contained in:
guoyuqi 2023-09-28 10:32:04 +08:00 committed by fit2-zhao
parent d453bb93e0
commit 4ee10e11dc
4 changed files with 181 additions and 100 deletions

View File

@ -25,7 +25,7 @@ import java.io.IOException;
import java.util.List;
@Tag(name = "项目管理-项目与权限-消息管理-消息设置")
@Tag(name = "项目管理-消息管理-消息设置")
@RestController
@RequestMapping("notice/")
public class NoticeMessageTaskController {
@ -33,7 +33,7 @@ public class NoticeMessageTaskController {
private NoticeMessageTaskService noticeMessageTaskService;
@PostMapping("message/task/save")
@Operation(summary = "项目管理-项目与权限-消息管理-消息设置-保存消息设置")
@Operation(summary = "项目管理-消息管理-消息设置-保存消息设置")
@RequiresPermissions(value = {PermissionConstants.PROJECT_MESSAGE_READ_ADD, PermissionConstants.PROJECT_MESSAGE_READ_UPDATE}, logical = Logical.OR)
@Log(type = OperationLogType.UPDATE, expression = "#msClass.addLog(#messageTaskRequest)", msClass = MessageTaskLogService.class)
public ResultHolder saveMessage(@Validated({Created.class, Updated.class}) @RequestBody MessageTaskRequest messageTaskRequest) {
@ -41,7 +41,7 @@ public class NoticeMessageTaskController {
}
@GetMapping("message/task/get/{projectId}")
@Operation(summary = "项目管理-项目与权限-消息管理-消息设置-获取消息设置")
@Operation(summary = "项目管理-消息管理-消息设置-获取消息设置")
@RequiresPermissions(PermissionConstants.PROJECT_MESSAGE_READ_ADD)
public List<MessageTaskDTO> getMessageList(@PathVariable String projectId) throws IOException {
return noticeMessageTaskService.getMessageList(projectId);

View File

@ -1,13 +1,33 @@
package io.metersphere.project.service;
import io.metersphere.project.domain.MessageTask;
import io.metersphere.project.domain.MessageTaskBlob;
import io.metersphere.project.domain.ProjectRobot;
import io.metersphere.project.dto.MessageTaskDTO;
import io.metersphere.project.dto.MessageTaskDetailDTO;
import io.metersphere.project.dto.MessageTaskTypeDTO;
import io.metersphere.project.enums.ProjectRobotPlatform;
import io.metersphere.project.mapper.MessageTaskBlobMapper;
import io.metersphere.project.mapper.MessageTaskMapper;
import io.metersphere.project.mapper.ProjectRobotMapper;
import io.metersphere.sdk.dto.OptionDTO;
import io.metersphere.sdk.util.JSON;
import io.metersphere.system.notice.constants.NoticeConstants;
import io.metersphere.system.service.CreateProjectResourceService;
import io.metersphere.system.uid.UUID;
import jakarta.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
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.Component;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
@ -17,11 +37,15 @@ public class CreateRobotResourceService implements CreateProjectResourceService
@Resource
private ProjectRobotMapper robotMapper;
@Resource
private SqlSessionFactory sqlSessionFactory;
@Override
public void createResources(String projectId) {
List<ProjectRobot> list = new ArrayList<>();
ProjectRobot projectRobot = new ProjectRobot();
projectRobot.setId(UUID.randomUUID().toString());
String inSiteId = UUID.randomUUID().toString();
projectRobot.setId(inSiteId);
projectRobot.setProjectId(projectId);
projectRobot.setName("站内信");
projectRobot.setPlatform(ProjectRobotPlatform.IN_SITE.toString());
@ -43,5 +67,71 @@ public class CreateRobotResourceService implements CreateProjectResourceService
projectRobotMail.setUpdateTime(System.currentTimeMillis());
list.add(projectRobotMail);
robotMapper.batchInsert(list);
setMessageTask(projectId, inSiteId);
}
public void setMessageTask(String projectId, String defaultRobotId) {
StringBuilder jsonStr = new StringBuilder();
InputStream inputStream = getClass().getResourceAsStream("/message_task.json");
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
try {
while ((line = reader.readLine()) != null) {
jsonStr.append(line);
}
reader.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
MessageTaskMapper mapper = sqlSession.getMapper(MessageTaskMapper.class);
MessageTaskBlobMapper blobMapper = sqlSession.getMapper(MessageTaskBlobMapper.class);
List<MessageTaskDTO> messageTaskDTOList = JSON.parseArray(jsonStr.toString(), MessageTaskDTO.class);
for (MessageTaskDTO messageTaskDTO : messageTaskDTOList) {
List<MessageTaskTypeDTO> messageTaskTypeDTOList = messageTaskDTO.getMessageTaskTypeDTOList();
for (MessageTaskTypeDTO messageTaskTypeDTO : messageTaskTypeDTOList) {
String taskType = messageTaskTypeDTO.getTaskType();
if (taskType.contains(NoticeConstants.Mode.SCHEDULE) || taskType.contains("AT") || taskType.contains("JENKINS")) {
continue;
}
List<MessageTaskDetailDTO> messageTaskDetailDTOList = messageTaskTypeDTO.getMessageTaskDetailDTOList();
for (MessageTaskDetailDTO messageTaskDetailDTO : messageTaskDetailDTOList) {
String event = messageTaskDetailDTO.getEvent();
List<OptionDTO> receivers = messageTaskDetailDTO.getReceivers();
if (StringUtils.equalsIgnoreCase(event, NoticeConstants.Event.CREATE) || StringUtils.equalsIgnoreCase(event, NoticeConstants.Event.CASE_CREATE) || CollectionUtils.isEmpty(receivers)) {
continue;
}
for (OptionDTO receiver : receivers) {
String id = UUID.randomUUID().toString();
MessageTask messageTask = new MessageTask();
messageTask.setId(id);
messageTask.setEvent(event);
messageTask.setTaskType(taskType);
messageTask.setReceiver(receiver.getId());
messageTask.setProjectId(projectId);
messageTask.setProjectRobotId(defaultRobotId);
messageTask.setEnable(true);
messageTask.setTestId("NONE");
messageTask.setCreateUser("admin");
messageTask.setCreateTime(System.currentTimeMillis());
messageTask.setUpdateUser("admin");
messageTask.setUpdateTime(System.currentTimeMillis());
messageTask.setSubject("");
messageTask.setUseDefaultSubject(true);
messageTask.setUseDefaultTemplate(true);
MessageTaskBlob messageTaskBlob = new MessageTaskBlob();
messageTaskBlob.setId(id);
messageTaskBlob.setTemplate("");
mapper.insert(messageTask);
blobMapper.insert(messageTaskBlob);
}
}
}
}
sqlSession.flushStatements();
SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory);
}
}

View File

@ -35,7 +35,11 @@
"eventName":"",
"receivers":[
{
"id": "",
"id": "CREATE_USER",
"name": ""
},
{
"id": "FOLLOW_PEOPLE",
"name": ""
}
],
@ -57,7 +61,7 @@
"eventName":"",
"receivers":[
{
"id": "",
"id": "CREATE_USER",
"name": ""
}
],
@ -79,7 +83,7 @@
"eventName":"",
"receivers":[
{
"id": "",
"id": "OPERATOR",
"name": ""
}
],
@ -107,7 +111,7 @@
"eventName":"",
"receivers":[
{
"id": "",
"id": "CREATE_USER",
"name": ""
}
],
@ -164,7 +168,11 @@
"eventName":"",
"receivers":[
{
"id": "",
"id": "CREATE_USER",
"name": ""
},
{
"id": "FOLLOW_PEOPLE",
"name": ""
}
],
@ -186,7 +194,7 @@
"eventName":"",
"receivers":[
{
"id": "",
"id": "CREATE_USER",
"name": ""
}
],
@ -208,7 +216,7 @@
"eventName":"",
"receivers":[
{
"id": "",
"id": "CREATE_USER",
"name": ""
}
],
@ -236,35 +244,7 @@
"eventName":"",
"receivers":[
{
"id": "",
"name": ""
}
],
"projectRobotConfigList":[
{
"robotId":"",
"enable":"",
"template":"",
"defaultTemplate":"",
"useDefaultTemplate":true,
"subject":"",
"defaultSubject":"",
"useDefaultSubject":true
}
]
}
]
},
{
"taskType":"BUG_TASK_AT",
"taskTypeName":"",
"messageTaskDetailDTOList":[
{
"event":"COMMENT",
"eventName":"",
"receivers":[
{
"id": "",
"id": "OPERATOR",
"name": ""
}
],
@ -321,7 +301,11 @@
"eventName":"",
"receivers":[
{
"id": "",
"id": "CREATE_USER",
"name": ""
},
{
"id": "FOLLOW_PEOPLE",
"name": ""
}
],
@ -343,7 +327,7 @@
"eventName":"",
"receivers":[
{
"id": "",
"id": "CREATE_USER",
"name": ""
}
],
@ -365,35 +349,7 @@
"eventName":"",
"receivers":[
{
"id": "",
"name": ""
}
],
"projectRobotConfigList":[
{
"robotId":"",
"enable":"",
"template":"",
"defaultTemplate":"",
"useDefaultTemplate":true,
"subject":"",
"defaultSubject":"",
"useDefaultSubject":true
}
]
}
]
},
{
"taskType":"FUNCTIONAL_CASE_TASK_AT",
"taskTypeName":"",
"messageTaskDetailDTOList":[
{
"event":"COMMENT",
"eventName":"",
"receivers":[
{
"id": "",
"id": "CREATE_USER",
"name": ""
}
],
@ -443,7 +399,11 @@
"eventName":"",
"receivers":[
{
"id": "",
"id": "CREATE_USER",
"name": ""
},
{
"id": "FOLLOW_PEOPLE",
"name": ""
}
],
@ -465,7 +425,7 @@
"eventName":"",
"receivers":[
{
"id": "",
"id": "CREATE_USER",
"name": ""
}
],
@ -487,7 +447,7 @@
"eventName":"",
"receivers":[
{
"id": "",
"id": "CREATE_USER",
"name": ""
}
],
@ -509,7 +469,7 @@
"eventName":"",
"receivers":[
{
"id": "",
"id": "CREATE_USER",
"name": ""
}
],
@ -566,7 +526,7 @@
"eventName":"",
"receivers":[
{
"id": "",
"id": "CREATE_USER",
"name": ""
}
],
@ -588,7 +548,7 @@
"eventName":"",
"receivers":[
{
"id": "",
"id": "CREATE_USER",
"name": ""
}
],
@ -632,7 +592,7 @@
"eventName":"",
"receivers":[
{
"id": "",
"id": "CREATE_USER",
"name": ""
}
],
@ -654,7 +614,7 @@
"eventName":"",
"receivers":[
{
"id": "",
"id": "CREATE_USER",
"name": ""
}
],
@ -676,7 +636,7 @@
"eventName":"",
"receivers":[
{
"id": "",
"id": "CREATE_USER",
"name": ""
}
],
@ -698,7 +658,7 @@
"eventName":"",
"receivers":[
{
"id": "",
"id": "CREATE_USER",
"name": ""
}
],
@ -720,7 +680,7 @@
"eventName":"",
"receivers":[
{
"id": "",
"id": "CREATE_USER",
"name": ""
}
],
@ -770,7 +730,7 @@
"eventName":"",
"receivers":[
{
"id": "",
"id": "CREATE_USER",
"name": ""
}
],
@ -792,7 +752,7 @@
"eventName":"",
"receivers":[
{
"id": "",
"id": "CREATE_USER",
"name": ""
}
],
@ -814,7 +774,7 @@
"eventName":"",
"receivers":[
{
"id": "",
"id": "CREATE_USER",
"name": ""
}
],
@ -836,7 +796,7 @@
"eventName":"",
"receivers":[
{
"id": "",
"id": "CREATE_USER",
"name": ""
}
],
@ -858,7 +818,7 @@
"eventName":"",
"receivers":[
{
"id": "",
"id": "CREATE_USER",
"name": ""
}
],
@ -886,7 +846,7 @@
"eventName":"",
"receivers":[
{
"id": "",
"id": "CREATE_USER",
"name": ""
}
],
@ -943,7 +903,7 @@
"eventName":"",
"receivers":[
{
"id": "",
"id": "CREATE_USER",
"name": ""
}
],
@ -965,7 +925,7 @@
"eventName":"",
"receivers":[
{
"id": "",
"id": "CREATE_USER",
"name": ""
}
],
@ -987,7 +947,7 @@
"eventName":"",
"receivers":[
{
"id": "",
"id": "CREATE_USER",
"name": ""
}
],
@ -1009,7 +969,7 @@
"eventName":"",
"receivers":[
{
"id": "",
"id": "CREATE_USER",
"name": ""
}
],
@ -1035,7 +995,7 @@
"eventName":"",
"receivers":[
{
"id": "",
"id": "CREATE_USER",
"name": ""
}
],
@ -1094,7 +1054,7 @@
"eventName":"",
"receivers":[
{
"id": "",
"id": "CREATE_USER",
"name": ""
}
],
@ -1116,7 +1076,7 @@
"eventName":"",
"receivers":[
{
"id": "",
"id": "CREATE_USER",
"name": ""
}
],
@ -1138,7 +1098,7 @@
"eventName":"",
"receivers":[
{
"id": "",
"id": "OPERATOR",
"name": ""
}
],
@ -1166,7 +1126,7 @@
"eventName":"",
"receivers":[
{
"id": "",
"id": "CREATE_USER",
"name": ""
}
],

View File

@ -1,11 +1,15 @@
package io.metersphere.project.controller;
import io.metersphere.project.domain.Project;
import io.metersphere.project.domain.ProjectRobot;
import io.metersphere.project.dto.MessageTaskDTO;
import io.metersphere.project.mapper.ProjectMapper;
import io.metersphere.sdk.constants.SessionConstants;
import io.metersphere.sdk.util.JSON;
import io.metersphere.system.base.BaseTest;
import io.metersphere.system.controller.handler.ResultHolder;
import io.metersphere.system.invoker.ProjectServiceInvoker;
import jakarta.annotation.Resource;
import org.junit.jupiter.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
@ -21,7 +25,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@SpringBootTest(webEnvironment= SpringBootTest.WebEnvironment.RANDOM_PORT)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@AutoConfigureMockMvc
public class CreateRobotResourceTests extends BaseTest {
@ -34,16 +38,32 @@ public class CreateRobotResourceTests extends BaseTest {
this.serviceInvoker = serviceInvoker;
}
@Resource
private ProjectMapper projectMapper;
@Test
@Order(1)
public void testCleanupResource() throws Exception {
serviceInvoker.invokeCreateServices("test");
Project project = new Project();
project.setId("test_message");
project.setOrganizationId("organization-message-test");
project.setName("默认项目");
project.setDescription("系统默认创建的项目");
project.setCreateUser("admin");
project.setUpdateUser("admin");
project.setCreateTime(System.currentTimeMillis());
project.setUpdateTime(System.currentTimeMillis());
projectMapper.insertSelective(project);
serviceInvoker.invokeCreateServices("test_message");
List<ProjectRobot> projectRobotAfters = getList();
Assertions.assertEquals(2, projectRobotAfters.size());
List<MessageTaskDTO> messageList = getMessageList();
Assertions.assertTrue(messageList.size() > 0);
System.out.println(messageList);
}
private List<ProjectRobot> getList() throws Exception {
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get(ROBOT_LIST + "test")
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get(ROBOT_LIST + "test_message")
.header(SessionConstants.HEADER_TOKEN, sessionId)
.header(SessionConstants.CSRF_TOKEN, csrfToken)
.contentType(MediaType.APPLICATION_JSON))
@ -53,4 +73,15 @@ public class CreateRobotResourceTests extends BaseTest {
ResultHolder sortHolder = JSON.parseObject(sortData, ResultHolder.class);
return JSON.parseArray(JSON.toJSONString(sortHolder.getData()), ProjectRobot.class);
}
private List<MessageTaskDTO> getMessageList() throws Exception {
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/notice/message/task/get/test_message")
.header(SessionConstants.HEADER_TOKEN, sessionId)
.header(SessionConstants.CSRF_TOKEN, csrfToken))
.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);
return JSON.parseArray(JSON.toJSONString(resultHolder.getData()), MessageTaskDTO.class);
}
}