feat(消息管理): 增加新增项目时同步新增其他资源功能 (#26749)

* feat(消息管理): 增加新增项目时同步新增其他资源功能

---------

Co-authored-by: guoyuqi <xiaomeinvG@126.com>
This commit is contained in:
MeterSphere Bot 2023-09-13 16:44:05 +08:00 committed by GitHub
parent 7864d555f9
commit 80cc5940c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 215 additions and 3 deletions

View File

@ -1,6 +1,7 @@
package io.metersphere.sdk.invoker; package io.metersphere.sdk.invoker;
import io.metersphere.sdk.service.CleanupProjectResourceService; import io.metersphere.sdk.service.CleanupProjectResourceService;
import io.metersphere.sdk.service.CreateProjectResourceService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -10,9 +11,13 @@ import java.util.List;
public class ProjectServiceInvoker { public class ProjectServiceInvoker {
private final List<CleanupProjectResourceService> cleanupProjectResourceServices; private final List<CleanupProjectResourceService> cleanupProjectResourceServices;
private final List<CreateProjectResourceService> createProjectResourceServices;
@Autowired @Autowired
public ProjectServiceInvoker(List<CleanupProjectResourceService> services) { public ProjectServiceInvoker(List<CleanupProjectResourceService> services, List<CreateProjectResourceService> createProjectResourceServices) {
this.cleanupProjectResourceServices = services; this.cleanupProjectResourceServices = services;
this.createProjectResourceServices = createProjectResourceServices;
} }
public void invokeServices(String projectId) { public void invokeServices(String projectId) {
@ -20,4 +25,10 @@ public class ProjectServiceInvoker {
service.deleteResources(projectId); service.deleteResources(projectId);
} }
} }
public void invokeCreateServices(String projectId) {
for (CreateProjectResourceService service : createProjectResourceServices) {
service.createResources(projectId);
}
}
} }

View File

@ -0,0 +1,8 @@
package io.metersphere.sdk.service;
/**
* 新增项目资源
*/
public interface CreateProjectResourceService {
void createResources(String projectId);
}

View File

@ -1,15 +1,22 @@
package io.metersphere.project.dto; package io.metersphere.project.dto;
import io.metersphere.validation.groups.Created;
import io.metersphere.validation.groups.Updated;
import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode;
@Data @Data
@EqualsAndHashCode(callSuper = false)
public class ProjectRobotDTO { public class ProjectRobotDTO {
@Schema(description = "id", requiredMode = Schema.RequiredMode.REQUIRED) @Schema(description = "id", requiredMode = Schema.RequiredMode.REQUIRED)
@NotBlank(message = "{project_robot.id.not_blank}", groups = {Updated.class})
private String id; private String id;
@Schema(description = "名称", requiredMode = Schema.RequiredMode.REQUIRED) @Schema(description = "名称", requiredMode = Schema.RequiredMode.REQUIRED)
@NotBlank(message = "{project_robot.name.not_blank}", groups = {Created.class, Updated.class})
private String name; private String name;
@Schema(description = "所属平台(飞书,钉钉,企业微信,自定义)", requiredMode = Schema.RequiredMode.REQUIRED) @Schema(description = "所属平台(飞书,钉钉,企业微信,自定义)", requiredMode = Schema.RequiredMode.REQUIRED)
@ -34,5 +41,6 @@ public class ProjectRobotDTO {
private String description; private String description;
@Schema(description = "项目id", requiredMode = Schema.RequiredMode.REQUIRED) @Schema(description = "项目id", requiredMode = Schema.RequiredMode.REQUIRED)
@NotBlank(message = "{project_robot.project_id.not_blank}", groups = {Created.class, Updated.class})
private String projectId; private String projectId;
} }

View File

@ -6,6 +6,10 @@ import lombok.Data;
@Data @Data
public class ProjectRobotRequest extends BasePageRequest { public class ProjectRobotRequest extends BasePageRequest {
@Schema(description = "是否禁用")
private String projectId;
@Schema(description = "是否禁用") @Schema(description = "是否禁用")
private Boolean enable; private Boolean enable;
} }

View File

@ -0,0 +1,47 @@
package io.metersphere.project.service;
import io.metersphere.project.domain.ProjectRobot;
import io.metersphere.project.enums.ProjectRobotPlatform;
import io.metersphere.project.mapper.ProjectRobotMapper;
import io.metersphere.sdk.service.CreateProjectResourceService;
import io.metersphere.sdk.uid.UUID;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
@Component
public class CreateRobotResourceService implements CreateProjectResourceService {
@Resource
private ProjectRobotMapper robotMapper;
@Override
public void createResources(String projectId) {
List<ProjectRobot> list = new ArrayList<>();
ProjectRobot projectRobot = new ProjectRobot();
projectRobot.setId(UUID.randomUUID().toString());
projectRobot.setProjectId(projectId);
projectRobot.setName("站内信");
projectRobot.setPlatform(ProjectRobotPlatform.IN_SITE.toString());
projectRobot.setWebhook("NONE");
projectRobot.setCreateUser("admin");
projectRobot.setCreateTime(System.currentTimeMillis());
projectRobot.setUpdateUser("admin");
projectRobot.setUpdateTime(System.currentTimeMillis());
list.add(projectRobot);
ProjectRobot projectRobotMail = new ProjectRobot();
projectRobotMail.setId(UUID.randomUUID().toString());
projectRobotMail.setProjectId(projectId);
projectRobotMail.setName("邮件");
projectRobotMail.setPlatform(ProjectRobotPlatform.MAIL.toString());
projectRobotMail.setWebhook("NONE");
projectRobotMail.setCreateUser("admin");
projectRobotMail.setCreateTime(System.currentTimeMillis());
projectRobotMail.setUpdateUser("admin");
projectRobotMail.setUpdateTime(System.currentTimeMillis());
list.add(projectRobotMail);
robotMapper.batchInsert(list);
}
}

View File

@ -16,6 +16,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.util.List; import java.util.List;
import io.metersphere.sdk.uid.UUID; import io.metersphere.sdk.uid.UUID;
@Service @Service
@ -95,6 +96,10 @@ public class ProjectRobotService {
criteria.andEnableEqualTo(request.getEnable()); criteria.andEnableEqualTo(request.getEnable());
} }
if (request.getProjectId() != null) {
criteria.andProjectIdEqualTo(request.getProjectId());
}
projectExample.setOrderByClause("create_time desc"); projectExample.setOrderByClause("create_time desc");
return robotMapper.selectByExample(projectExample); return robotMapper.selectByExample(projectExample);

View File

@ -0,0 +1,64 @@
package io.metersphere.project.controller;
import io.metersphere.project.domain.ProjectRobot;
import io.metersphere.project.request.ProjectRobotRequest;
import io.metersphere.sdk.base.BaseTest;
import io.metersphere.sdk.constants.SessionConstants;
import io.metersphere.sdk.controller.handler.ResultHolder;
import io.metersphere.sdk.invoker.ProjectServiceInvoker;
import io.metersphere.sdk.util.JSON;
import io.metersphere.sdk.util.Pager;
import org.junit.jupiter.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import java.nio.charset.StandardCharsets;
import java.util.List;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@SpringBootTest
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@AutoConfigureMockMvc
public class CreateRobotResourceTests extends BaseTest {
private final ProjectServiceInvoker serviceInvoker;
public static final String ROBOT_LIST = "/project/robot/list/page";
@Autowired
public CreateRobotResourceTests(ProjectServiceInvoker serviceInvoker) {
this.serviceInvoker = serviceInvoker;
}
@Test
@Order(1)
public void testCleanupResource() throws Exception {
serviceInvoker.invokeCreateServices("test");
ProjectRobotRequest request = new ProjectRobotRequest();
request.setCurrent(1);
request.setPageSize(5);
request.setProjectId("test");
Pager<?> sortPageDataAfter = getPager(request);
List<ProjectRobot> projectRobotAfters = JSON.parseArray(JSON.toJSONString(sortPageDataAfter.getList()), ProjectRobot.class);
Assertions.assertEquals(2, projectRobotAfters.size());
}
private Pager<?> getPager(ProjectRobotRequest request) throws Exception {
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post(ROBOT_LIST)
.header(SessionConstants.HEADER_TOKEN, sessionId)
.header(SessionConstants.CSRF_TOKEN, csrfToken)
.content(JSON.toJSONString(request))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON)).andReturn();
String sortData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8);
ResultHolder sortHolder = JSON.parseObject(sortData, ResultHolder.class);
return JSON.parseObject(JSON.toJSONString(sortHolder.getData()), Pager.class);
}
}

View File

@ -183,7 +183,7 @@ public class ProjectRobotControllerTests extends BaseTest {
setCustomRobot("测试ID空失败"); setCustomRobot("测试ID空失败");
ProjectRobot projectRobot = getRobot("测试ID空失败"); ProjectRobot projectRobot = getRobot("测试ID空失败");
projectRobot.setId(null); projectRobot.setId(null);
checkUpdate(projectRobot, "测试ID空失败", status().is5xxServerError()); checkUpdate(projectRobot, "测试ID空失败", status().isBadRequest());
} }
@Test @Test
@ -295,6 +295,21 @@ public class ProjectRobotControllerTests extends BaseTest {
@Test @Test
@Order(19) @Order(19)
void getListSuccessByProject() throws Exception {
setCustomRobot("测试集合");
ProjectRobotRequest request = new ProjectRobotRequest();
request.setCurrent(1);
request.setPageSize(5);
request.setProjectId("test_project");
request.setEnable(true);
Pager<?> sortPageData = getPager(request);
List<ProjectRobot> projectRobots = JSON.parseArray(JSON.toJSONString(sortPageData.getList()), ProjectRobot.class);
ProjectRobot projectRobot = projectRobots.get(0);
Assertions.assertTrue(projectRobot.getEnable());
}
@Test
@Order(20)
void setEnableSuccess() throws Exception { void setEnableSuccess() throws Exception {
setCustomRobot("测试Enable"); setCustomRobot("测试Enable");
ProjectRobot projectRobot = getRobot("测试Enable"); ProjectRobot projectRobot = getRobot("测试Enable");
@ -309,7 +324,7 @@ public class ProjectRobotControllerTests extends BaseTest {
} }
@Test @Test
@Order(20) @Order(21)
void setEnableFail() throws Exception { void setEnableFail() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get(ROBOT_ENABLE+"/no_id") mockMvc.perform(MockMvcRequestBuilders.get(ROBOT_ENABLE+"/no_id")
.header(SessionConstants.HEADER_TOKEN, sessionId) .header(SessionConstants.HEADER_TOKEN, sessionId)

View File

@ -0,0 +1,35 @@
package io.metersphere.system.controller;
import io.metersphere.sdk.invoker.ProjectServiceInvoker;
import io.metersphere.system.mock.CreateTestResourceService;
import jakarta.annotation.Resource;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@AutoConfigureMockMvc
public class CreateResourceTests {
private final ProjectServiceInvoker serviceInvoker;
@Resource
private CreateTestResourceService createTestResourceService;
@Autowired
public CreateResourceTests(ProjectServiceInvoker serviceInvoker) {
this.serviceInvoker = serviceInvoker;
}
@Test
@Order(1)
public void testCreateResource() {
serviceInvoker.invokeCreateServices("test");
createTestResourceService.createResources("test");
}
}

View File

@ -0,0 +1,15 @@
package io.metersphere.system.mock;
import io.metersphere.sdk.service.CreateProjectResourceService;
import io.metersphere.sdk.util.LogUtils;
import org.springframework.stereotype.Component;
@Component
public class CreateTestResourceService implements CreateProjectResourceService {
@Override
public void createResources(String projectId) {
LogUtils.info("默认增加当前项目[" + projectId + "]TEST资源");
}
}