refactor(测试跟踪): 修复功能用例上传附件没有记录到操作日志问题

--bug=1023259 --user=郭雨琦
https://www.tapd.cn/55049933/bugtrace/bugs/view/1155049933001023259
This commit is contained in:
guoyuqi 2023-02-22 17:59:20 +08:00 committed by jianxing
parent 9f43af1a78
commit 1b67bc455b
3 changed files with 87 additions and 2 deletions

View File

@ -21,7 +21,7 @@ import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature; import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.core.LocalVariableTableParameterNameDiscoverer; import org.springframework.core.StandardReflectionParameterNameDiscoverer;
import org.springframework.expression.EvaluationContext; import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression; import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser; import org.springframework.expression.ExpressionParser;
@ -50,7 +50,7 @@ public class MsLogAspect {
/** /**
* 将方法参数纳入Spring管理 * 将方法参数纳入Spring管理
*/ */
LocalVariableTableParameterNameDiscoverer discoverer = new LocalVariableTableParameterNameDiscoverer(); StandardReflectionParameterNameDiscoverer discoverer = new StandardReflectionParameterNameDiscoverer();
@Autowired @Autowired
private ApplicationContext applicationContext; private ApplicationContext applicationContext;

View File

@ -1,6 +1,9 @@
package io.metersphere.controller; package io.metersphere.controller;
import io.metersphere.base.domain.FileAttachmentMetadata; import io.metersphere.base.domain.FileAttachmentMetadata;
import io.metersphere.commons.constants.OperLogConstants;
import io.metersphere.commons.constants.OperLogModule;
import io.metersphere.log.annotation.MsAuditLog;
import io.metersphere.metadata.service.FileMetadataService; import io.metersphere.metadata.service.FileMetadataService;
import io.metersphere.request.attachment.AttachmentDumpRequest; import io.metersphere.request.attachment.AttachmentDumpRequest;
import io.metersphere.xpack.track.dto.AttachmentRequest; import io.metersphere.xpack.track.dto.AttachmentRequest;
@ -27,6 +30,7 @@ public class AttachmentController {
@Resource @Resource
private FileMetadataService fileMetadataService; private FileMetadataService fileMetadataService;
@MsAuditLog(module = OperLogModule.TRACK_TEST_CASE, type = OperLogConstants.UPDATE, title = "#request.belongType", content = "#msClass.getLogDetails(#request.belongId, #request.belongType, #file.getOriginalFilename(), false)", msClass = AttachmentService.class)
@PostMapping(value = "/upload", consumes = {"multipart/form-data"}) @PostMapping(value = "/upload", consumes = {"multipart/form-data"})
public void uploadAttachment(@RequestPart("request") AttachmentRequest request, @RequestPart(value = "file", required = false) MultipartFile file) { public void uploadAttachment(@RequestPart("request") AttachmentRequest request, @RequestPart(value = "file", required = false) MultipartFile file) {
attachmentService.uploadAttachment(request, file); attachmentService.uploadAttachment(request, file);
@ -57,6 +61,7 @@ public class AttachmentController {
} }
} }
@MsAuditLog(module = OperLogModule.TRACK_TEST_CASE, type = OperLogConstants.UPDATE, beforeEvent = "#msClass.getLogDetails(#attachmentId, #attachmentType)", title = "#request.belongType", msClass = AttachmentService.class)
@GetMapping("/delete/{attachmentType}/{attachmentId}") @GetMapping("/delete/{attachmentType}/{attachmentId}")
public void deleteAttachment(@PathVariable String attachmentId, @PathVariable String attachmentType) { public void deleteAttachment(@PathVariable String attachmentId, @PathVariable String attachmentType) {
attachmentService.deleteAttachment(attachmentId, attachmentType); attachmentService.deleteAttachment(attachmentId, attachmentType);

View File

@ -7,9 +7,12 @@ import io.metersphere.commons.constants.FileAssociationType;
import io.metersphere.commons.exception.MSException; import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.BeanUtils; import io.metersphere.commons.utils.BeanUtils;
import io.metersphere.commons.utils.FileUtils; import io.metersphere.commons.utils.FileUtils;
import io.metersphere.commons.utils.JSON;
import io.metersphere.commons.utils.SessionUtils; import io.metersphere.commons.utils.SessionUtils;
import io.metersphere.constants.AttachmentType; import io.metersphere.constants.AttachmentType;
import io.metersphere.i18n.Translator; import io.metersphere.i18n.Translator;
import io.metersphere.log.vo.DetailColumn;
import io.metersphere.log.vo.OperatingLogDetails;
import io.metersphere.metadata.service.FileMetadataService; import io.metersphere.metadata.service.FileMetadataService;
import io.metersphere.metadata.utils.MetadataUtils; import io.metersphere.metadata.utils.MetadataUtils;
import io.metersphere.platform.domain.SyncIssuesAttachmentRequest; import io.metersphere.platform.domain.SyncIssuesAttachmentRequest;
@ -578,4 +581,81 @@ public class AttachmentService {
return type.toUpperCase(); return type.toUpperCase();
} }
/**
* 上传文件的操作记录
*
* @param sourceId
* @param type
* @return
*/
public String getLogDetails(String sourceId, String type, String fileName, Boolean isDelete) {
String projectId = null;
String createUser = null;
if (StringUtils.isBlank(sourceId) || StringUtils.isBlank(type) || StringUtils.isBlank(fileName)) {
return null;
}
if (AttachmentType.ISSUE.type().equals(type)) {
IssuesWithBLOBs issues = issuesMapper.selectByPrimaryKey(sourceId);
if (issues == null) {
return null;
}
projectId = issues.getProjectId();
createUser = issues.getCreator();
} else if (AttachmentType.TEST_CASE.type().equals(type)) {
TestCaseWithBLOBs testCase = testCaseMapper.selectByPrimaryKey(sourceId);
if (testCase == null) {
return null;
}
projectId = testCase.getProjectId();
createUser = testCase.getCreateUser();
}
AttachmentRequest attachmentRequest = new AttachmentRequest();
attachmentRequest.setBelongId(sourceId);
attachmentRequest.setBelongType(type);
List<FileAttachmentMetadata> originFiles = listMetadata(attachmentRequest);
List<String> fileNames = new LinkedList<>();
if (CollectionUtils.isNotEmpty(originFiles)) {
fileNames = originFiles.stream().map(FileAttachmentMetadata::getName).collect(Collectors.toList());
}
String after;
String before;
if (fileNames.contains(fileName)) {
after = String.join(",", fileNames);
fileNames.remove(fileName);
before = String.join(",", fileNames);
} else {
after = String.join(",", fileNames);
fileNames.add(fileName);
before = String.join(",", fileNames);
}
List<DetailColumn> columns = new ArrayList<>();
if (isDelete) {
DetailColumn column = new DetailColumn("附件", "files", after, before);
columns.add(column);
} else {
DetailColumn column = new DetailColumn("附件", "files", before, after);
columns.add(column);
}
OperatingLogDetails details = new OperatingLogDetails(JSON.toJSONString(sourceId), projectId, createUser, columns);
return JSON.toJSONString(details);
}
public String getLogDetails(String attachmentId, String attachmentType) {
FileAttachmentMetadata fileAttachmentMetadata = fileAttachmentMetadataMapper.selectByPrimaryKey(attachmentId);
if (fileAttachmentMetadata == null) {
return null;
}
String fileName = fileAttachmentMetadata.getName();
AttachmentModuleRelationExample example = new AttachmentModuleRelationExample();
example.createCriteria().andAttachmentIdEqualTo(attachmentId).andRelationTypeEqualTo(attachmentType);
List<AttachmentModuleRelation> attachmentModuleRelations = attachmentModuleRelationMapper.selectByExample(example);
if (CollectionUtils.isEmpty(attachmentModuleRelations)) {
return null;
}
String relationId = attachmentModuleRelations.get(0).getRelationId();
return this.getLogDetails(relationId, attachmentType, fileName, true);
}
} }