refactor: jenkins 通知发送

This commit is contained in:
Captain.B 2021-08-27 16:13:01 +08:00 committed by 刘瑞斌
parent 3ed0110b8c
commit e95cbfd496
8 changed files with 75 additions and 31 deletions

View File

@ -201,7 +201,7 @@ public class ApiAutomationController {
public String runByJenkins(@RequestBody RunScenarioRequest request) {
request.setExecuteType(ExecuteType.Saved.name());
request.setTriggerMode(TriggerMode.API.name());
request.setRunMode(ApiRunMode.SCENARIO.name());
request.setRunMode(ApiRunMode.JENKINS.name());
return apiAutomationService.run(request);
}

View File

@ -160,10 +160,15 @@ public class ApiDefinitionExecResultService {
.failedMailTemplate("api/CaseResult")
.paramMap(paramMap)
.event(event)
.excludeSelf(true)
.build();
String taskType = NoticeConstants.TaskType.API_DEFINITION_TASK;
noticeSendService.send(taskType, noticeModel);
if (StringUtils.equals(ReportTriggerMode.API.name(), result.getTriggerMode())) {
noticeSendService.send(ReportTriggerMode.API.name(), taskType, noticeModel);
} else {
noticeSendService.send(taskType, noticeModel);
}
}
private String getName(String type, String id, String status, Long time, String resourceId) {
@ -225,7 +230,7 @@ public class ApiDefinitionExecResultService {
String finalSaveResultType = saveResultType;
Map<String, String> apiIdResultMap = new HashMap<>();
Map<String,ApiDefinitionExecResult> caseReportMap = new HashMap<>();
Map<String, ApiDefinitionExecResult> caseReportMap = new HashMap<>();
if (CollectionUtils.isNotEmpty(result.getScenarios())) {
result.getScenarios().forEach(scenarioResult -> {
@ -235,7 +240,7 @@ public class ApiDefinitionExecResultService {
ApiDefinitionExecResult saveResult = new ApiDefinitionExecResult();
saveResult.setId(UUID.randomUUID().toString());
saveResult.setCreateTime(System.currentTimeMillis());
saveResult.setName(getName(type, item.getName(), status, saveResult.getCreateTime(),saveResult.getId()));
saveResult.setName(getName(type, item.getName(), status, saveResult.getCreateTime(), saveResult.getId()));
ApiDefinitionWithBLOBs apiDefinitionWithBLOBs = apiDefinitionMapper.selectByPrimaryKey(item.getName());
String caseId = null;
if (apiDefinitionWithBLOBs != null) {
@ -254,7 +259,7 @@ public class ApiDefinitionExecResultService {
}
}
}
if(StringUtils.isNotEmpty(caseId)){
if (StringUtils.isNotEmpty(caseId)) {
apiIdResultMap.put(caseId, item.isSuccess() ? TestPlanApiExecuteStatus.SUCCESS.name() : TestPlanApiExecuteStatus.FAILD.name());
}
@ -301,14 +306,14 @@ public class ApiDefinitionExecResultService {
apiDefinitionExecResultMapper.updateByPrimaryKeyWithBLOBs(prevResult);
}
apiDefinitionExecResultMapper.insert(saveResult);
caseReportMap.put(caseId,saveResult);
caseReportMap.put(caseId, saveResult);
});
}
});
}
testPlanLog.info("TestPlanReportId[" + testPlanReportId + "] APICASE OVER. API CASE STATUS:" + JSONObject.toJSONString(apiIdResultMap));
TestPlanReportExecuteCatch.updateApiTestPlanExecuteInfo(testPlanReportId, apiIdResultMap, null, null);
TestPlanReportExecuteCatch.updateTestPlanExecuteResultInfo(testPlanReportId,caseReportMap,null,null);
TestPlanReportExecuteCatch.updateTestPlanExecuteResultInfo(testPlanReportId, caseReportMap, null, null);
}
public void deleteByResourceId(String resourceId) {

View File

@ -21,7 +21,6 @@ import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@ -181,9 +180,12 @@ public abstract class AbstractNoticeSender implements NoticeSender {
}
}
// 排除自己
toUsers.removeIf(u -> StringUtils.equals(u.getUserId(), noticeModel.getOperator()));
if (noticeModel.isExcludeSelf()) {
toUsers.removeIf(u -> StringUtils.equals(u.getUserId(), noticeModel.getOperator()));
}
// 去重复
HashSet<Receiver> receivers = new HashSet<>(toUsers);
return new ArrayList<>(receivers);
return toUsers.stream()
.distinct()
.collect(Collectors.toList());
}
}

View File

@ -57,4 +57,8 @@ public class NoticeModel {
* 接收人
*/
private List<Receiver> receivers;
/**
*
*/
private boolean excludeSelf;
}

View File

@ -142,6 +142,7 @@ public class SendNoticeAspect {
.paramMap(paramMap)
.event(sendNotice.event())
.status((String) paramMap.get("status"))
.excludeSelf(true)
.build();
noticeSendService.send(sendNotice.taskType(), noticeModel);
}

View File

@ -11,6 +11,7 @@ import io.metersphere.notice.sender.impl.*;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
@Component
@ -59,8 +60,8 @@ public class NoticeSendService {
List<MessageDetail> messageDetails;
switch (taskType) {
case NoticeConstants.Mode.API:
String loadReportId = (String) noticeModel.getParamMap().get("id");
messageDetails = noticeService.searchMessageByTypeBySend(NoticeConstants.TaskType.JENKINS_TASK, loadReportId);
String projectId = (String) noticeModel.getParamMap().get("projectId");
messageDetails = noticeService.searchMessageByTypeBySend(NoticeConstants.TaskType.JENKINS_TASK, projectId);
break;
case NoticeConstants.Mode.SCHEDULE:
messageDetails = noticeService.searchMessageByTestId(noticeModel.getTestId());
@ -82,6 +83,40 @@ public class NoticeSendService {
}
}
public void send(String triggerMode, String taskType, NoticeModel noticeModel) {
// api和定时任务调用排除自己
noticeModel.setExcludeSelf(false);
try {
List<MessageDetail> messageDetails = new ArrayList<>();
if (StringUtils.equals(triggerMode, NoticeConstants.Mode.SCHEDULE)) {
switch (taskType) {
case NoticeConstants.TaskType.API_AUTOMATION_TASK:
case NoticeConstants.TaskType.PERFORMANCE_TEST_TASK:
messageDetails = noticeService.searchMessageByTestId(noticeModel.getTestId());
break;
default:
break;
}
}
if (StringUtils.equals(triggerMode, NoticeConstants.Mode.API)) {
String projectId = (String) noticeModel.getParamMap().get("projectId");
messageDetails = noticeService.searchMessageByTypeBySend(NoticeConstants.TaskType.JENKINS_TASK, projectId);
}
// 异步发送通知
messageDetails.stream()
.filter(messageDetail -> StringUtils.equals(messageDetail.getEvent(), noticeModel.getEvent()))
.forEach(messageDetail -> {
this.getNoticeSender(messageDetail).send(messageDetail, noticeModel);
});
} catch (Exception e) {
LogUtil.error(e.getMessage(), e);
}
}
public void send(Organization organization, String taskType, NoticeModel noticeModel) {
try {
List<MessageDetail> messageDetails = noticeService.searchMessageByTypeAndOrganizationId(taskType, organization.getId());

View File

@ -4,7 +4,6 @@ import com.alibaba.fastjson.JSON;
import io.metersphere.base.domain.MessageTask;
import io.metersphere.base.domain.MessageTaskExample;
import io.metersphere.base.domain.Organization;
import io.metersphere.base.mapper.LoadTestReportMapper;
import io.metersphere.base.mapper.MessageTaskMapper;
import io.metersphere.base.mapper.ext.ExtProjectMapper;
import io.metersphere.commons.exception.MSException;
@ -32,8 +31,6 @@ public class NoticeService {
@Resource
private MessageTaskMapper messageTaskMapper;
@Resource
private LoadTestReportMapper loadTestReportMapper;
@Resource
private ExtProjectMapper extProjectMapper;
public void saveMessageTask(MessageDetail messageDetail) {
@ -134,11 +131,10 @@ public class NoticeService {
}
}
public List<MessageDetail> searchMessageByTypeBySend(String type, String id) {
public List<MessageDetail> searchMessageByTypeBySend(String type, String projectId) {
try {
String orgId = "";
if (null == SessionUtils.getUser()) {
String projectId = loadTestReportMapper.selectByPrimaryKey(id).getProjectId();
Organization organization = extProjectMapper.getOrganizationByProjectId(projectId);
orgId = organization.getId();
} else {

View File

@ -72,23 +72,23 @@ public class PerformanceNoticeEvent implements LoadTestFinishEvent {
paramMap.put("type", "performance");
paramMap.put("url", baseSystemConfigDTO.getUrl());
paramMap.putAll(new BeanMap(loadTestDTO));
NoticeModel noticeModel = NoticeModel.builder()
.operator(loadTestReport.getUserId())
.successContext(successContext)
.successMailTemplate("PerformanceApiSuccessNotification")
.failedContext(failedContext)
.failedMailTemplate("PerformanceFailedNotification")
.testId(loadTestReport.getTestId())
.status(loadTestReport.getStatus())
.subject(subject)
.event(event)
.paramMap(paramMap)
.build();
if (StringUtils.equals(ReportTriggerMode.API.name(), loadTestReport.getTriggerMode())
|| StringUtils.equals(ReportTriggerMode.SCHEDULE.name(), loadTestReport.getTriggerMode())) {
noticeSendService.send(loadTestReport.getTriggerMode(), noticeModel);
NoticeModel noticeModel = NoticeModel.builder()
.operator(loadTestReport.getUserId())
.successContext(successContext)
.successMailTemplate("PerformanceApiSuccessNotification")
.failedContext(failedContext)
.failedMailTemplate("PerformanceFailedNotification")
.testId(loadTestReport.getTestId())
.status(loadTestReport.getStatus())
.subject(subject)
.event(event)
.paramMap(paramMap)
.build();
noticeSendService.send(loadTestReport.getTriggerMode(), NoticeConstants.TaskType.PERFORMANCE_TEST_TASK, noticeModel);
} else {
Organization organization = projectService.getOrganizationByProjectId(loadTestReport.getProjectId());
String context = "${operator}执行性能测试完成: ${name}";
@ -101,6 +101,7 @@ public class PerformanceNoticeEvent implements LoadTestFinishEvent {
.subject(subject)
.event(NoticeConstants.Event.EXECUTE_COMPLETED)
.paramMap(paramMap)
.excludeSelf(true)
.build();
noticeSendService.send(organization, NoticeConstants.TaskType.PERFORMANCE_TEST_TASK, noticeModel2);
}