fix(测试计划): 执行通知问题

--bug=1044741 --user=宋昌昌 【Jenkins插件】MS消息管理中配置了Jenkins执行成功/执行失败,给接收人是创建人发送通知失败 https://www.tapd.cn/55049933/s/1558376
This commit is contained in:
song-cc-rock 2024-08-07 18:45:28 +08:00 committed by Craftsman
parent fc46239b5b
commit 209c5a9e44
5 changed files with 1292 additions and 1232 deletions

View File

@ -23,6 +23,7 @@ import org.springframework.web.multipart.MultipartFile;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
@Service
@Transactional(rollbackFor = Exception.class)
@ -43,6 +44,7 @@ public class BugLogService {
* @param files 文件
* @return 日志
*/
@SuppressWarnings("unused")
public LogDTO addLog(BugEditRequest request, List<MultipartFile> files) {
LogDTO dto = new LogDTO(request.getProjectId(), null, null, null, OperationLogType.ADD.name(), OperationLogModule.BUG_MANAGEMENT_INDEX, getPlatformTitle(request));
dto.setHistory(true);
@ -59,6 +61,7 @@ public class BugLogService {
* @param files 文件
* @return 日志
*/
@SuppressWarnings("unused")
public LogDTO updateLog(BugEditRequest request, List<MultipartFile> files) {
BugDTO history = getOriginalValue(request.getId());
LogDTO dto = new LogDTO(request.getProjectId(), null, request.getId(), null, OperationLogType.UPDATE.name(), OperationLogModule.BUG_MANAGEMENT_INDEX, getPlatformTitle(request));
@ -76,6 +79,7 @@ public class BugLogService {
* @param id 缺陷ID
* @return 日志
*/
@SuppressWarnings("unused")
public LogDTO deleteLog(String id) {
Bug bug = bugMapper.selectByPrimaryKey(id);
if (bug != null) {
@ -95,6 +99,7 @@ public class BugLogService {
* @param id 缺陷ID
* @return 日志
*/
@SuppressWarnings("unused")
public LogDTO recoverLog(String id) {
Bug bug = bugMapper.selectByPrimaryKey(id);
if (bug != null) {
@ -146,10 +151,7 @@ public class BugLogService {
bugs.forEach(bug -> {
BugDTO originalBug = new BugDTO();
BeanUtils.copyBean(originalBug, bug);
BugContent bugContent = bugContents.stream().filter(content -> StringUtils.equals(content.getBugId(), bug.getId())).findFirst().orElse(null);
if (bugContent != null) {
originalBug.setDescription(bugContent.getDescription());
}
bugContents.stream().filter(content -> StringUtils.equals(content.getBugId(), bug.getId())).findFirst().ifPresent(bugContent -> originalBug.setDescription(bugContent.getDescription()));
bugOriginalList.add(originalBug);
});
// 缺陷自定义字段
@ -162,7 +164,8 @@ public class BugLogService {
* @return 缺陷标题
*/
private String getPlatformTitle(BugEditRequest request) {
BugCustomFieldDTO platformTitle = request.getCustomFields().stream().filter(field -> StringUtils.equalsAny(field.getId(), "summary")).findFirst().get();
return StringUtils.isNotBlank(request.getTitle()) ? request.getTitle() : platformTitle.getValue();
Optional<BugCustomFieldDTO> find = request.getCustomFields().stream().filter(field -> StringUtils.equalsAny(field.getId(), "summary", "title")).findFirst();
BugCustomFieldDTO titleField = find.orElseGet(BugCustomFieldDTO::new);
return StringUtils.isNotBlank(request.getTitle()) ? request.getTitle() : titleField.getValue();
}
}

View File

@ -40,6 +40,7 @@ public class BugNoticeService {
* @param request 请求参数
* @return 缺陷通知
*/
@SuppressWarnings("unused")
public BugNoticeDTO getNoticeByRequest(BugEditRequest request) {
// 获取状态选项, 处理人选项
Map<String, String> statusMap = getStatusMap(request.getProjectId());
@ -88,22 +89,7 @@ public class BugNoticeService {
if (bugDTO == null) {
return null;
}
// 构建通知对象
BugNoticeDTO notice = new BugNoticeDTO();
BeanUtils.copyBean(notice, bugDTO);
// 自定义字段解析{name: value}
if (CollectionUtils.isNotEmpty(bugDTO.getCustomFields())) {
List<OptionDTO> fields = new ArrayList<>();
bugDTO.getCustomFields().forEach(field -> {
// 其他自定义字段
OptionDTO fieldDTO = new OptionDTO();
fieldDTO.setId(field.getName());
fieldDTO.setName(field.getValue());
fields.add(fieldDTO);
});
notice.setFields(fields);
}
return notice;
return buildNotice(bugDTO);
}
/**
@ -116,22 +102,7 @@ public class BugNoticeService {
return null;
}
List<BugNoticeDTO> notices = new ArrayList<>();
bugs.forEach(bug -> {
BugNoticeDTO notice = new BugNoticeDTO();
BeanUtils.copyBean(notice, bug);
if (CollectionUtils.isNotEmpty(bug.getCustomFields())) {
List<OptionDTO> fields = new ArrayList<>();
bug.getCustomFields().forEach(field -> {
// 其他自定义字段
OptionDTO fieldDTO = new OptionDTO();
fieldDTO.setId(field.getName());
fieldDTO.setName(field.getValue());
fields.add(fieldDTO);
});
notice.setFields(fields);
}
notices.add(notice);
});
bugs.forEach(bug -> notices.add(buildNotice(bug)));
return notices;
}
@ -140,6 +111,7 @@ public class BugNoticeService {
* @param request 批量请求参数
* @return 缺陷通知集合
*/
@SuppressWarnings("unused")
public List<BugNoticeDTO> getBatchNoticeByRequest(BugBatchRequest request) {
List<String> batchIds = bugService.getBatchIdsByRequest(request);
return getNoticeByIds(batchIds);
@ -164,4 +136,28 @@ public class BugNoticeService {
List<SelectOption> handlerOption = bugCommonService.getHeaderHandlerOption(projectId);
return handlerOption.stream().collect(Collectors.toMap(SelectOption::getValue, SelectOption::getText));
}
/**
* 构建通知对象
* @param bugDTO 缺陷DTO
* @return 通知对象
*/
private BugNoticeDTO buildNotice(BugDTO bugDTO) {
// 构建通知对象
BugNoticeDTO notice = new BugNoticeDTO();
BeanUtils.copyBean(notice, bugDTO);
// 自定义字段解析{name: value}
if (CollectionUtils.isNotEmpty(bugDTO.getCustomFields())) {
List<OptionDTO> fields = new ArrayList<>();
bugDTO.getCustomFields().forEach(field -> {
// 其他自定义字段
OptionDTO fieldDTO = new OptionDTO();
fieldDTO.setId(field.getName());
fieldDTO.setName(field.getValue());
fields.add(fieldDTO);
});
notice.setFields(fields);
}
return notice;
}
}

View File

@ -1,13 +1,17 @@
package io.metersphere.system.dto.sdk;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class TestPlanMessageDTO {
@Schema(description = "message.domain.test_plan_num")
private String num;

View File

@ -280,6 +280,7 @@ public class TestPlanReportService {
/**
* 自动生成报告 (计划 或者 )
*
* @param request 请求参数
* @param currentUser 当前用户
*/
@ -578,7 +579,7 @@ public class TestPlanReportService {
// 发送计划执行通知
if (!useManual) {
testPlanSendNoticeService.sendExecuteNotice(planReport.getCreateUser(), planReport.getTestPlanId(), planReport.getProjectId(), planReport.getResultStatus(), planReport.getTriggerMode());
testPlanSendNoticeService.sendExecuteNotice(planReport.getCreateUser(), planReport.getTestPlanId(), planReport);
}
}
@ -701,6 +702,7 @@ public class TestPlanReportService {
/**
* 返回功能用例执行结果
*
* @param executeHisId 执行历史ID
* @return 执行结果
*/

View File

@ -4,27 +4,36 @@ import io.metersphere.functional.domain.FunctionalCase;
import io.metersphere.functional.mapper.FunctionalCaseMapper;
import io.metersphere.plan.domain.*;
import io.metersphere.plan.dto.TestPlanDTO;
import io.metersphere.plan.dto.TestPlanShareInfo;
import io.metersphere.plan.dto.request.TestPlanCreateRequest;
import io.metersphere.plan.dto.request.TestPlanReportShareRequest;
import io.metersphere.plan.dto.request.TestPlanUpdateRequest;
import io.metersphere.plan.mapper.TestPlanConfigMapper;
import io.metersphere.plan.mapper.TestPlanFollowerMapper;
import io.metersphere.plan.mapper.TestPlanMapper;
import io.metersphere.project.domain.Project;
import io.metersphere.project.mapper.ProjectMapper;
import io.metersphere.sdk.constants.ResultStatus;
import io.metersphere.sdk.constants.TaskTriggerMode;
import io.metersphere.sdk.constants.TestPlanConstants;
import io.metersphere.sdk.util.BeanUtils;
import io.metersphere.sdk.util.CommonBeanFactory;
import io.metersphere.sdk.util.JSON;
import io.metersphere.system.domain.User;
import io.metersphere.system.dto.sdk.BaseSystemConfigDTO;
import io.metersphere.system.dto.sdk.TestPlanMessageDTO;
import io.metersphere.system.mapper.UserMapper;
import io.metersphere.system.notice.NoticeModel;
import io.metersphere.system.notice.constants.NoticeConstants;
import io.metersphere.system.notice.utils.MessageTemplateUtils;
import io.metersphere.system.service.CommonNoticeSendService;
import io.metersphere.system.service.NoticeSendService;
import io.metersphere.system.service.SystemParameterService;
import jakarta.annotation.Resource;
import org.apache.commons.beanutils.BeanMap;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
@ -51,6 +60,10 @@ public class TestPlanSendNoticeService {
private TestPlanConfigMapper testPlanConfigMapper;
@Resource
private TestPlanFollowerMapper testPlanFollowerMapper;
@Autowired
private ProjectMapper projectMapper;
@Resource
private TestPlanReportShareService testPlanReportShareService;
public void sendNoticeCase(List<String> relatedUsers, String userId, String caseId, String task, String event, String testPlanId) {
FunctionalCase functionalCase = functionalCaseMapper.selectByPrimaryKey(caseId);
@ -130,7 +143,7 @@ public class TestPlanSendNoticeService {
return testPlans.stream().collect(Collectors.toMap(TestPlan::getId, testPlan -> testPlan));
}
@SuppressWarnings("unused")
public TestPlanDTO sendAddNotice(TestPlanCreateRequest request) {
TestPlanDTO dto = new TestPlanDTO();
BeanUtils.copyBean(dto, request);
@ -138,6 +151,7 @@ public class TestPlanSendNoticeService {
return dto;
}
@SuppressWarnings("unused")
public TestPlanDTO sendUpdateNotice(TestPlanUpdateRequest request) {
TestPlanDTO dto = new TestPlanDTO();
BeanUtils.copyBean(dto, request);
@ -154,6 +168,7 @@ public class TestPlanSendNoticeService {
return dto;
}
@SuppressWarnings("unused")
public TestPlanDTO sendDeleteNotice(String id) {
TestPlan testPlan = testPlanMapper.selectByPrimaryKey(id);
TestPlanConfig testPlanConfig = testPlanConfigMapper.selectByPrimaryKey(id);
@ -173,34 +188,74 @@ public class TestPlanSendNoticeService {
/**
* 报告汇总-计划执行结束通知
*
* @param currentUser 当前用户
* @param planId 计划ID
* @param projectId 项目ID
* @param executeResult 执行结果
* @param report 报告
*/
@Async
public void sendExecuteNotice(String currentUser, String planId, String projectId, String executeResult, String triggerMode) {
public void sendExecuteNotice(String currentUser, String planId, TestPlanReport report) {
TestPlan testPlan = testPlanMapper.selectByPrimaryKey(planId);
if (testPlan != null) {
User user = userMapper.selectByPrimaryKey(currentUser);
setLanguage(user.getLanguage());
Map<String, String> defaultTemplateMap = MessageTemplateUtils.getDefaultTemplateMap();
String template = defaultTemplateMap.get(StringUtils.equals(executeResult, ResultStatus.SUCCESS.name()) ?
String template = defaultTemplateMap.get(StringUtils.equals(report.getResultStatus(), ResultStatus.SUCCESS.name()) ?
NoticeConstants.TemplateText.TEST_PLAN_TASK_EXECUTE_SUCCESSFUL : NoticeConstants.TemplateText.TEST_PLAN_TASK_EXECUTE_FAILED);
Map<String, String> defaultSubjectMap = MessageTemplateUtils.getDefaultTemplateSubjectMap();
String subject = defaultSubjectMap.get(StringUtils.equals(executeResult, ResultStatus.SUCCESS.name()) ?
String subject = defaultSubjectMap.get(StringUtils.equals(report.getResultStatus(), ResultStatus.SUCCESS.name()) ?
NoticeConstants.TemplateText.TEST_PLAN_TASK_EXECUTE_SUCCESSFUL : NoticeConstants.TemplateText.TEST_PLAN_TASK_EXECUTE_FAILED);
Map<String, Object> paramMap = new HashMap<>(4);
TestPlanMessageDTO messageDTO = buildMessageNotice(planId, report, currentUser);
BeanMap beanMap = new BeanMap(messageDTO);
Map paramMap = new HashMap<>(beanMap);
paramMap.put(NoticeConstants.RelatedUser.OPERATOR, user.getName());
paramMap.put("name", testPlan.getName());
paramMap.put("projectId", projectId);
paramMap.put("projectId", report.getProjectId());
paramMap.put("id", planId);
paramMap.put("Language", user.getLanguage());
paramMap.put("createUser", testPlan.getCreateUser());
NoticeModel noticeModel = NoticeModel.builder().operator(currentUser).excludeSelf(false)
.context(template).subject(subject).paramMap(paramMap).event(StringUtils.equals(executeResult, ResultStatus.SUCCESS.name()) ?
.context(template).subject(subject).paramMap(paramMap).event(StringUtils.equals(report.getResultStatus(), ResultStatus.SUCCESS.name()) ?
NoticeConstants.Event.EXECUTE_SUCCESSFUL : NoticeConstants.Event.EXECUTE_FAILED).build();
noticeSendService.send(StringUtils.equals(TaskTriggerMode.API.name(), triggerMode) ?
noticeSendService.send(StringUtils.equals(TaskTriggerMode.API.name(), report.getTriggerMode()) ?
NoticeConstants.TaskType.JENKINS_TASK : NoticeConstants.TaskType.TEST_PLAN_TASK, noticeModel);
}
}
/**
* 构建计划消息通知对象
*
* @param planId 计划ID
* @param report 报告
* @param currentUser 当前用户
* @return 计划消息通知对象
*/
private TestPlanMessageDTO buildMessageNotice(String planId, TestPlanReport report, String currentUser) {
TestPlan testPlan = testPlanMapper.selectByPrimaryKey(planId);
// 报告URL
Project project = projectMapper.selectByPrimaryKey(testPlan.getProjectId());
SystemParameterService systemParameterService = CommonBeanFactory.getBean(SystemParameterService.class);
assert systemParameterService != null;
BaseSystemConfigDTO baseSystemConfigDTO = systemParameterService.getBaseInfo();
String reportUrl = baseSystemConfigDTO.getUrl() + "/#/test-plan/testPlanReportDetail?id=%s&type=%s&orgId=%s&pId=%s";
reportUrl = String.format(reportUrl, report.getId(), report.getIntegrated() ? TestPlanConstants.TEST_PLAN_TYPE_GROUP : TestPlanConstants.TEST_PLAN_TYPE_PLAN,
project.getOrganizationId(), project.getId());
// 报告分享URL
TestPlanReportShareRequest shareRequest = new TestPlanReportShareRequest();
shareRequest.setReportId(report.getId());
shareRequest.setProjectId(project.getId());
shareRequest.setShareType("TEST_PLAN_SHARE_REPORT");
TestPlanShareInfo shareInfo = testPlanReportShareService.gen(shareRequest, currentUser);
String reportShareUrl = baseSystemConfigDTO.getUrl() + "/#/share/shareReportTestPlan?type=" +
(report.getIntegrated() ? TestPlanConstants.TEST_PLAN_TYPE_GROUP : TestPlanConstants.TEST_PLAN_TYPE_PLAN) + "shareId=" + shareInfo.getId();
return TestPlanMessageDTO.builder()
.num(testPlan.getNum().toString()).name(testPlan.getName()).status(testPlan.getStatus()).type(testPlan.getType()).tags(testPlan.getTags())
.createUser(testPlan.getCreateUser()).createTime(testPlan.getCreateTime()).updateUser(testPlan.getUpdateUser()).updateTime(testPlan.getUpdateTime())
.plannedStartTime(testPlan.getPlannedStartTime()).plannedEndTime(testPlan.getPlannedEndTime())
.actualStartTime(testPlan.getActualStartTime()).actualEndTime(testPlan.getActualEndTime())
.description(testPlan.getDescription()).reportName(report.getName()).reportUrl(reportUrl).reportShareUrl(reportShareUrl)
.startTime(report.getStartTime()).endTime(report.getEndTime()).execStatus(report.getExecStatus()).resultStatus(report.getResultStatus())
.passRate(report.getPassRate()).passThreshold(report.getPassThreshold()).executeRate(report.getExecuteRate())
.build();
}
}