refactor(消息管理): 增加机器人日志

This commit is contained in:
guoyuqi 2023-10-23 13:52:37 +08:00 committed by 刘瑞斌
parent c1c11501c3
commit 889ac1d1f9
4 changed files with 107 additions and 5 deletions

View File

@ -3,9 +3,12 @@ package io.metersphere.project.controller;
import io.metersphere.project.domain.ProjectRobot;
import io.metersphere.project.dto.ProjectRobotDTO;
import io.metersphere.project.service.MessageTaskLogService;
import io.metersphere.project.service.ProjectRobotService;
import io.metersphere.sdk.constants.PermissionConstants;
import io.metersphere.sdk.util.BeanUtils;
import io.metersphere.system.log.annotation.Log;
import io.metersphere.system.log.constants.OperationLogType;
import io.metersphere.system.utils.SessionUtils;
import io.metersphere.validation.groups.Created;
import io.metersphere.validation.groups.Updated;
@ -37,6 +40,7 @@ public class ProjectRobotController {
@PostMapping("add")
@Operation(summary = "项目管理-消息管理-新增机器人")
@RequiresPermissions(PermissionConstants.PROJECT_MESSAGE_READ_ADD)
@Log(type = OperationLogType.ADD, expression = "#msClass.addRobotLog(#projectRobotDTO)", msClass = MessageTaskLogService.class)
public void add(@Validated({Created.class}) @RequestBody ProjectRobotDTO projectRobotDTO) {
ProjectRobot projectRobot = new ProjectRobot();
BeanUtils.copyBean(projectRobot, projectRobotDTO);
@ -50,6 +54,7 @@ public class ProjectRobotController {
@PostMapping("update")
@Operation(summary = "项目管理-消息管理-更新机器人")
@RequiresPermissions(PermissionConstants.PROJECT_MESSAGE_READ_UPDATE)
@Log(type = OperationLogType.UPDATE, expression = "#msClass.updateRobotLog(#projectRobotDTO)", msClass = MessageTaskLogService.class)
public void update(@Validated({Updated.class}) @RequestBody ProjectRobotDTO projectRobotDTO) {
ProjectRobot projectRobot = new ProjectRobot();
BeanUtils.copyBean(projectRobot, projectRobotDTO);
@ -70,6 +75,7 @@ public class ProjectRobotController {
@GetMapping("delete/{id}")
@Operation(summary = "项目管理-消息管理-删除机器人")
@RequiresPermissions(PermissionConstants.PROJECT_MESSAGE_READ_DELETE)
@Log(type = OperationLogType.DELETE, expression = "#msClass.delRobotLog(#id)", msClass = MessageTaskLogService.class)
public void delete(@PathVariable(value = "id") String id) {
projectRobotService.delete(id);
}

View File

@ -4,6 +4,7 @@ import io.metersphere.project.domain.MessageTask;
import io.metersphere.project.domain.MessageTaskExample;
import io.metersphere.project.domain.ProjectRobot;
import io.metersphere.project.domain.ProjectRobotExample;
import io.metersphere.project.dto.ProjectRobotDTO;
import io.metersphere.project.enums.ProjectRobotPlatform;
import io.metersphere.project.mapper.MessageTaskMapper;
import io.metersphere.project.mapper.ProjectRobotMapper;
@ -13,6 +14,7 @@ import io.metersphere.sdk.dto.request.MessageTaskRequest;
import io.metersphere.sdk.util.JSON;
import io.metersphere.system.log.constants.OperationLogModule;
import io.metersphere.system.log.constants.OperationLogType;
import io.metersphere.system.notice.utils.MessageTemplateUtils;
import jakarta.annotation.Resource;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
@ -20,6 +22,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Map;
@Service
@Transactional(rollbackFor = Exception.class)
@ -39,6 +42,7 @@ public class MessageTaskLogService {
*/
public LogDTO addLog(MessageTaskRequest messageTaskRequest) {
MessageTaskExample messageTaskExample = new MessageTaskExample();
Map<String, String> taskTypeMap = MessageTemplateUtils.getTaskTypeMap();
//如果只选了用户没有选机器人默认机器人为站内信
String robotId = setDefaultRobot(messageTaskRequest.getProjectId(), messageTaskRequest.getRobotId());
messageTaskExample.createCriteria().andProjectIdEqualTo(messageTaskRequest.getProjectId())
@ -50,8 +54,8 @@ public class MessageTaskLogService {
messageTaskRequest.getTaskType()+messageTaskRequest.getEvent(),
null,
OperationLogType.UPDATE.name(),
OperationLogModule.PROJECT_MANAGEMENT_MESSAGE,
messageTaskRequest.getTaskType());
OperationLogModule.PROJECT_MANAGEMENT_MESSAGE_MANAGEMENT_CONFIG,
taskTypeMap.get(messageTaskRequest.getTaskType()));
dto.setPath("/notice/message/task/save");
dto.setMethod(HttpMethodConstants.POST.name());
@ -61,6 +65,65 @@ public class MessageTaskLogService {
return dto;
}
public LogDTO addRobotLog(ProjectRobotDTO projectRobotDTO){
LogDTO dto = new LogDTO(
projectRobotDTO.getProjectId(),
"",
projectRobotDTO.getId(),
null,
OperationLogType.ADD.name(),
OperationLogModule.PROJECT_MANAGEMENT_MESSAGE_MANAGEMENT_ROBOT,
projectRobotDTO.getName());
dto.setPath("/project/robot/add");
dto.setMethod(HttpMethodConstants.POST.name());
dto.setOriginalValue(JSON.toJSONBytes(projectRobotDTO));
return dto;
}
public LogDTO updateRobotLog(ProjectRobotDTO projectRobotDTO){
ProjectRobot projectRobot = projectRobotMapper.selectByPrimaryKey(projectRobotDTO.getId());
if (projectRobot != null) {
LogDTO dto = new LogDTO(
projectRobotDTO.getProjectId(),
"",
projectRobotDTO.getId(),
projectRobot.getCreateUser(),
OperationLogType.UPDATE.name(),
OperationLogModule.PROJECT_MANAGEMENT_MESSAGE_MANAGEMENT_ROBOT,
projectRobotDTO.getName()+"webhook:"+projectRobot.getWebhook()+ "——>" +"webhook:"+ projectRobotDTO.getWebhook() );
dto.setPath("/project/robot/update");
dto.setMethod(HttpMethodConstants.POST.name());
dto.setOriginalValue(JSON.toJSONBytes(projectRobot));
return dto;
}
return null;
}
public LogDTO delRobotLog(String id){
ProjectRobot projectRobot = projectRobotMapper.selectByPrimaryKey(id);
if (projectRobot != null) {
LogDTO dto = new LogDTO(
projectRobot.getProjectId(),
"",
id,
projectRobot.getCreateUser(),
OperationLogType.DELETE.name(),
OperationLogModule.PROJECT_MANAGEMENT_MESSAGE_MANAGEMENT_ROBOT,
projectRobot.getName());
dto.setPath("/project/robot/delete");
dto.setMethod(HttpMethodConstants.GET.name());
dto.setOriginalValue(JSON.toJSONBytes(projectRobot));
return dto;
}
return null;
}
/**
* 查询默认机器人id
*

View File

@ -12,12 +12,15 @@ import io.metersphere.project.mapper.MessageTaskMapper;
import io.metersphere.project.mapper.ProjectMapper;
import io.metersphere.project.mapper.ProjectRobotMapper;
import io.metersphere.sdk.constants.SessionConstants;
import io.metersphere.sdk.domain.OperationLogExample;
import io.metersphere.sdk.dto.OptionDTO;
import io.metersphere.sdk.dto.request.MessageTaskRequest;
import io.metersphere.sdk.mapper.OperationLogMapper;
import io.metersphere.sdk.util.BeanUtils;
import io.metersphere.sdk.util.JSON;
import io.metersphere.system.base.BaseTest;
import io.metersphere.system.controller.handler.ResultHolder;
import io.metersphere.system.log.constants.OperationLogType;
import io.metersphere.system.notice.constants.NoticeConstants;
import io.metersphere.system.uid.IDGenerator;
import jakarta.annotation.Resource;
@ -71,6 +74,9 @@ public class ProjectRobotControllerTests extends BaseTest {
@Resource
private SqlSessionFactory sqlSessionFactory;
@Resource
private OperationLogMapper operationLogMapper;
@Test
@Order(1)
@ -94,7 +100,6 @@ public class ProjectRobotControllerTests extends BaseTest {
projectRobotDTO.setWebhook("https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=2b67ccf4-e0da-4cd6-ae74-8d42657865f8");
getPostResult(projectRobotDTO, ROBOT_ADD, status().isOk());
checkName("test_project", "企业微信机器人");
}
private void checkName(String projectId, String name) throws Exception {
@ -113,7 +118,8 @@ public class ProjectRobotControllerTests extends BaseTest {
projectRobotDTO.setWebhook("https://open.feishu.cn/open-apis/bot/v2/hook/a6024229-9d9d-41c2-8662-7bc3da1092cb");
getPostResult(projectRobotDTO, ROBOT_ADD, status().isOk());
checkName("test_project", "飞书机器人");
ProjectRobot robot = getRobot("test_project", "飞书机器人");
checkContentLog(robot.getName(),OperationLogType.ADD);
}
@Test
@ -217,6 +223,7 @@ public class ProjectRobotControllerTests extends BaseTest {
setCustomRobot("用于更新自定义机器人","test_project");
ProjectRobot projectRobot = getRobot("test_project", "用于更新自定义机器人");
checkUpdate(projectRobot, "更新自定义机器人", status().isOk());
checkLog(projectRobot.getId(),OperationLogType.UPDATE);
}
@Test
@ -291,6 +298,7 @@ public class ProjectRobotControllerTests extends BaseTest {
.header(SessionConstants.CSRF_TOKEN, csrfToken))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON));
checkLog(projectRobotId,OperationLogType.DELETE);
mockMvc.perform(MockMvcRequestBuilders.get(ROBOT_DETAIL + "/" + projectRobotId)
.header(SessionConstants.HEADER_TOKEN, sessionId)
.header(SessionConstants.CSRF_TOKEN, csrfToken))
@ -585,4 +593,28 @@ public class ProjectRobotControllerTests extends BaseTest {
ResultHolder resultHolder = JSON.parseObject(contentAsString, ResultHolder.class);
return JSON.parseArray(JSON.toJSONString(resultHolder.getData()), MessageTaskDTO.class);
}
protected void checkLog(String resourceId, OperationLogType operationLogType) throws Exception {
OperationLogExample example = new OperationLogExample();
example.createCriteria().andSourceIdEqualTo(resourceId).andTypeEqualTo(operationLogType.name());
operationLogMapper.selectByExample(example).stream()
.filter(operationLog -> operationLog.getSourceId().equalsIgnoreCase(resourceId))
.filter(operationLog -> operationLog.getType().equalsIgnoreCase(operationLogType.name()))
.filter(operationLog -> StringUtils.isNotBlank(operationLog.getProjectId()))
.filter(operationLog -> StringUtils.isNotBlank(operationLog.getModule()))
.findFirst()
.orElseThrow(() -> new Exception("日志不存在,请补充操作日志"));
}
protected void checkContentLog(String content, OperationLogType operationLogType) throws Exception {
OperationLogExample example = new OperationLogExample();
example.createCriteria().andContentEqualTo(content).andTypeEqualTo(operationLogType.name());
operationLogMapper.selectByExample(example).stream()
.filter(operationLog -> operationLog.getContent().equalsIgnoreCase(content))
.filter(operationLog -> operationLog.getType().equalsIgnoreCase(operationLogType.name()))
.filter(operationLog -> StringUtils.isNotBlank(operationLog.getProjectId()))
.filter(operationLog -> StringUtils.isNotBlank(operationLog.getModule()))
.findFirst()
.orElseThrow(() -> new Exception("日志不存在,请补充操作日志"));
}
}

View File

@ -90,7 +90,8 @@ public class OperationLogModule {
// 项目管理-版本管理
public static final String PROJECT_MANAGEMENT_PERMISSION_VERSION = "PROJECT_MANAGEMENT_PERMISSION_VERSION";
//项目管理-消息设置
public static final String PROJECT_MANAGEMENT_MESSAGE = "PROJECT_MANAGEMENT_MESSAGE";
public static final String PROJECT_MANAGEMENT_MESSAGE_MANAGEMENT_CONFIG = "PROJECT_MANAGEMENT_MESSAGE_MANAGEMENT_CONFIG";
public static final String PROJECT_MANAGEMENT_MESSAGE_MANAGEMENT_ROBOT = "PROJECT_MANAGEMENT_MESSAGE_MANAGEMENT_ROBOT";
public static final String PROJECT_TEMPLATE = "PROJECT_TEMPLATE";// 项目模板
public static final String PROJECT_CUSTOM_FIELD = "PROJECT_CUSTOM_FIELD";// 项目字段
}