refactor: 清理项目相关资源

This commit is contained in:
fit2-zhao 2023-07-19 16:48:34 +08:00 committed by fit2-zhao
parent 61bae0ec14
commit 2a2c2959be
17 changed files with 260 additions and 19 deletions

View File

@ -0,0 +1,23 @@
package io.metersphere.sdk.invoker;
import io.metersphere.sdk.service.CleanupProjectResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public class ProjectServiceInvoker {
private final List<CleanupProjectResourceService> cleanupProjectResourceServices;
@Autowired
public ProjectServiceInvoker(List<CleanupProjectResourceService> services) {
this.cleanupProjectResourceServices = services;
}
public void invokeServices(String projectId) {
for (CleanupProjectResourceService service : cleanupProjectResourceServices) {
service.deleteResources(projectId);
}
}
}

View File

@ -0,0 +1,8 @@
package io.metersphere.sdk.service;
/**
* 清理项目资源
*/
public interface CleanupProjectResourceService {
void deleteResources(String projectId);
}

View File

@ -1,8 +1,8 @@
package io.metersphere.api.controller;
import io.metersphere.api.dto.definition.ApiDefinitionDTO;
import io.metersphere.api.service.ApiDefinitionLogService;
import io.metersphere.api.service.ApiDefinitionService;
import io.metersphere.api.service.APIDefinitionLogService;
import io.metersphere.api.service.APIDefinitionService;
import io.metersphere.sdk.constants.PermissionConstants;
import io.metersphere.sdk.log.annotation.Log;
import io.metersphere.sdk.log.constants.OperationLogType;
@ -22,12 +22,12 @@ import java.util.List;
@RequestMapping(value = "/api/definition")
public class ApiDefinitionController {
@Resource
private ApiDefinitionService apiDefinitionService;
private APIDefinitionService apiDefinitionService;
@PostMapping(value = "/add", consumes = MediaType.APPLICATION_JSON_VALUE)
@RequiresPermissions(PermissionConstants.PROJECT_API_DEFINITION_READ_ADD_API)
// 添加接口Log示例
@Log(type = OperationLogType.ADD, expression = "#msClass.addLog(#request)", msClass = ApiDefinitionLogService.class)
@Log(type = OperationLogType.ADD, expression = "#msClass.addLog(#request)", msClass = APIDefinitionLogService.class)
public ApiDefinitionDTO add(@Validated({Created.class}) @RequestBody ApiDefinitionDTO request,
@RequestParam(value = "files") List<MultipartFile> bodyFiles) {
return apiDefinitionService.create(request, bodyFiles);
@ -36,7 +36,7 @@ public class ApiDefinitionController {
@PostMapping(value = "/update", consumes = MediaType.APPLICATION_JSON_VALUE)
@RequiresPermissions(PermissionConstants.PROJECT_API_DEFINITION_READ_ADD_API)
// 添加修改Log示例
@Log(type = OperationLogType.UPDATE, expression = "#msClass.updateLog(#request)", msClass = ApiDefinitionLogService.class)
@Log(type = OperationLogType.UPDATE, expression = "#msClass.updateLog(#request)", msClass = APIDefinitionLogService.class)
public ApiDefinitionDTO update(@Validated({Updated.class}) @RequestBody ApiDefinitionDTO request) {
return request;
}
@ -44,20 +44,20 @@ public class ApiDefinitionController {
@PostMapping(value = "/batch-update", consumes = MediaType.APPLICATION_JSON_VALUE)
@RequiresPermissions(PermissionConstants.PROJECT_API_DEFINITION_READ_ADD_API)
// 添加修改Log示例
@Log(type = OperationLogType.UPDATE, expression = "#msClass.batchUpdateLog(#ids)", msClass = ApiDefinitionLogService.class)
@Log(type = OperationLogType.UPDATE, expression = "#msClass.batchUpdateLog(#ids)", msClass = APIDefinitionLogService.class)
public ApiDefinitionDTO batchUpdate(@RequestBody List<String> ids) {
return null;
}
@PostMapping(value = "/delete")
@RequiresPermissions(PermissionConstants.PROJECT_API_REPORT_READ_DELETE)
@Log(type = OperationLogType.DELETE, expression = "#msClass.delLog(#id)", msClass = ApiDefinitionLogService.class)
@Log(type = OperationLogType.DELETE, expression = "#msClass.delLog(#id)", msClass = APIDefinitionLogService.class)
public void batchDelete(@RequestBody String id) {
}
@PostMapping(value = "/batch-del")
@RequiresPermissions(PermissionConstants.PROJECT_API_REPORT_READ_DELETE)
@Log(type = OperationLogType.DELETE, expression = "#msClass.batchDelLog(#ids)", msClass = ApiDefinitionLogService.class)
@Log(type = OperationLogType.DELETE, expression = "#msClass.batchDelLog(#ids)", msClass = APIDefinitionLogService.class)
public void batchDelete(@RequestBody List<String> ids) {
apiDefinitionService.batchDelete(ids);
}

View File

@ -16,7 +16,7 @@ import java.util.ArrayList;
import java.util.List;
@Service
public class ApiDefinitionLogService {
public class APIDefinitionLogService {
@Resource
private ApiDefinitionMapper apiDefinitionMapper;

View File

@ -14,7 +14,7 @@ import org.springframework.web.multipart.MultipartFile;
import java.util.List;
@Service
public class ApiDefinitionService {
public class APIDefinitionService {
@Resource
private ApiDefinitionMapper apiDefinitionMapper;

View File

@ -0,0 +1,14 @@
package io.metersphere.api.service;
import io.metersphere.sdk.service.CleanupProjectResourceService;
import io.metersphere.sdk.util.LogUtils;
import org.springframework.stereotype.Component;
@Component
public class CleanupAPIResourceService implements CleanupProjectResourceService {
@Override
public void deleteResources(String projectId) {
LogUtils.info("删除当前项目[" + projectId + "]相关接口测试资源");
}
}

View File

@ -0,0 +1,26 @@
package io.metersphere.api.controller;
import io.metersphere.api.service.CleanupAPIResourceService;
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.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@AutoConfigureMockMvc
public class CleanupResourceTests {
@Resource
private CleanupAPIResourceService resourceService;
@Test
@Order(1)
public void testCleanupResource() throws Exception {
resourceService.deleteResources("test");
}
}

View File

@ -0,0 +1,14 @@
package io.metersphere.bug.service;
import io.metersphere.sdk.service.CleanupProjectResourceService;
import io.metersphere.sdk.util.LogUtils;
import org.springframework.stereotype.Component;
@Component
public class CleanupBugResourceService implements CleanupProjectResourceService {
@Override
public void deleteResources(String projectId) {
LogUtils.info("删除当前项目[" + projectId + "]相关缺陷资源");
}
}

View File

@ -0,0 +1,26 @@
package io.metersphere.bug.controller;
import io.metersphere.bug.service.CleanupBugResourceService;
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.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@AutoConfigureMockMvc
public class CleanupResourceTests {
@Resource
private CleanupBugResourceService resourceService;
@Test
@Order(1)
public void testCleanupResource() throws Exception {
resourceService.deleteResources("test");
}
}

View File

@ -0,0 +1,14 @@
package io.metersphere.functional.service;
import io.metersphere.sdk.service.CleanupProjectResourceService;
import io.metersphere.sdk.util.LogUtils;
import org.springframework.stereotype.Component;
@Component
public class CleanupCaseResourceService implements CleanupProjectResourceService {
@Override
public void deleteResources(String projectId) {
LogUtils.info("删除当前项目[" + projectId + "]相关功能用例资源");
}
}

View File

@ -0,0 +1,26 @@
package io.metersphere.functional.controller;
import io.metersphere.functional.service.CleanupCaseResourceService;
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.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@AutoConfigureMockMvc
public class CleanupResourceTests {
@Resource
private CleanupCaseResourceService resourceService;
@Test
@Order(1)
public void testCleanupResource() throws Exception {
resourceService.deleteResources("test");
}
}

View File

@ -26,7 +26,7 @@ public class CleanProjectJob {
/**
* 清理状态为删除的项目 每天凌晨三点执行
*/
@QuartzScheduled(cron = "0 3 0 * * ?")
@QuartzScheduled(cron = "0 0 3 * * ?")
public void cleanupProject() {
LoggerUtil.info("clean up project start.");
try {
@ -47,6 +47,4 @@ public class CleanProjectJob {
systemProjectService.deleteProject(projects);
}
}
}

View File

@ -8,13 +8,13 @@ import io.metersphere.sdk.dto.AddProjectRequest;
import io.metersphere.sdk.dto.ProjectDTO;
import io.metersphere.sdk.dto.UpdateProjectRequest;
import io.metersphere.sdk.exception.MSException;
import io.metersphere.sdk.log.service.OperationLogService;
import io.metersphere.sdk.util.Translator;
import io.metersphere.system.domain.User;
import io.metersphere.system.domain.UserRoleRelation;
import io.metersphere.system.domain.UserRoleRelationExample;
import io.metersphere.system.dto.OrganizationProjectOptionsDto;
import io.metersphere.system.dto.UserExtend;
import io.metersphere.sdk.invoker.ProjectServiceInvoker;
import io.metersphere.system.mapper.ExtSystemProjectMapper;
import io.metersphere.system.mapper.UserMapper;
import io.metersphere.system.mapper.UserRoleRelationMapper;
@ -26,6 +26,7 @@ import jakarta.annotation.Resource;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -45,8 +46,13 @@ public class SystemProjectService {
private UserRoleRelationMapper userRoleRelationMapper;
@Resource
private ExtSystemProjectMapper extSystemProjectMapper;
@Resource
private OperationLogService operationLogService;
private final ProjectServiceInvoker serviceInvoker;
@Autowired
public SystemProjectService(ProjectServiceInvoker serviceInvoker) {
this.serviceInvoker = serviceInvoker;
}
public Project get(String id) {
return projectMapper.selectByPrimaryKey(id);
@ -218,12 +224,10 @@ public class SystemProjectService {
public void deleteProject(List<Project> projects) {
// 删除项目
projects.forEach(project -> {
serviceInvoker.invokeServices(project.getId());
LoggerUtil.info("send delete_project message, project id: " + project.getId());
//删除项目关联的自定义组织
deleteProjectUserGroup(project.getId());
//TODO 需要删除环境文件管理各个资源涉及到的定时任务
// delete project
projectMapper.deleteByPrimaryKey(project.getId());
});

View File

@ -0,0 +1,34 @@
package io.metersphere.system.controller;
import io.metersphere.sdk.invoker.ProjectServiceInvoker;
import io.metersphere.system.mock.CleanupTestResourceService;
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 CleanupResourceTests {
private final ProjectServiceInvoker serviceInvoker;
@Resource
private CleanupTestResourceService cleanupTestResourceService;
@Autowired
public CleanupResourceTests(ProjectServiceInvoker serviceInvoker) {
this.serviceInvoker = serviceInvoker;
}
@Test
@Order(1)
public void testCleanupResource() throws Exception {
serviceInvoker.invokeServices("test");
cleanupTestResourceService.deleteResources("test");
}
}

View File

@ -0,0 +1,14 @@
package io.metersphere.system.mock;
import io.metersphere.sdk.service.CleanupProjectResourceService;
import io.metersphere.sdk.util.LogUtils;
import org.springframework.stereotype.Component;
@Component
public class CleanupTestResourceService implements CleanupProjectResourceService {
@Override
public void deleteResources(String projectId) {
LogUtils.info("删除当前项目[" + projectId + "]TEST资源");
}
}

View File

@ -0,0 +1,14 @@
package io.metersphere.plan.service;
import io.metersphere.sdk.service.CleanupProjectResourceService;
import io.metersphere.sdk.util.LogUtils;
import org.springframework.stereotype.Component;
@Component
public class CleanupPlanResourceService implements CleanupProjectResourceService {
@Override
public void deleteResources(String projectId) {
LogUtils.info("删除当前项目[" + projectId + "]相关测试计划资源");
}
}

View File

@ -0,0 +1,26 @@
package io.metersphere.plan.controller;
import io.metersphere.plan.service.CleanupPlanResourceService;
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.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@AutoConfigureMockMvc
public class CleanupResourceTests {
@Resource
private CleanupPlanResourceService resourceService;
@Test
@Order(1)
public void testCleanupResource() throws Exception {
resourceService.deleteResources("test");
}
}