diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/invoker/ProjectServiceInvoker.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/invoker/ProjectServiceInvoker.java new file mode 100644 index 0000000000..b284cad0a4 --- /dev/null +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/invoker/ProjectServiceInvoker.java @@ -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 cleanupProjectResourceServices; + + @Autowired + public ProjectServiceInvoker(List services) { + this.cleanupProjectResourceServices = services; + } + + public void invokeServices(String projectId) { + for (CleanupProjectResourceService service : cleanupProjectResourceServices) { + service.deleteResources(projectId); + } + } +} diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/CleanupProjectResourceService.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/CleanupProjectResourceService.java new file mode 100644 index 0000000000..ae8a1b3f9e --- /dev/null +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/CleanupProjectResourceService.java @@ -0,0 +1,8 @@ +package io.metersphere.sdk.service; + +/** + * 清理项目资源 + */ +public interface CleanupProjectResourceService { + void deleteResources(String projectId); +} diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/controller/ApiDefinitionController.java b/backend/services/api-test/src/main/java/io/metersphere/api/controller/ApiDefinitionController.java index c34b2f2961..4569183295 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/controller/ApiDefinitionController.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/controller/ApiDefinitionController.java @@ -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 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 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 ids) { apiDefinitionService.batchDelete(ids); } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiDefinitionLogService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/APIDefinitionLogService.java similarity index 99% rename from backend/services/api-test/src/main/java/io/metersphere/api/service/ApiDefinitionLogService.java rename to backend/services/api-test/src/main/java/io/metersphere/api/service/APIDefinitionLogService.java index 7c1d03bfc8..5f7144f0a1 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiDefinitionLogService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/APIDefinitionLogService.java @@ -16,7 +16,7 @@ import java.util.ArrayList; import java.util.List; @Service -public class ApiDefinitionLogService { +public class APIDefinitionLogService { @Resource private ApiDefinitionMapper apiDefinitionMapper; diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiDefinitionService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/APIDefinitionService.java similarity index 97% rename from backend/services/api-test/src/main/java/io/metersphere/api/service/ApiDefinitionService.java rename to backend/services/api-test/src/main/java/io/metersphere/api/service/APIDefinitionService.java index 3801efb323..11a8edbdbd 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiDefinitionService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/APIDefinitionService.java @@ -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; diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/CleanupAPIResourceService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/CleanupAPIResourceService.java new file mode 100644 index 0000000000..d21fb33652 --- /dev/null +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/CleanupAPIResourceService.java @@ -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 + "]相关接口测试资源"); + } +} diff --git a/backend/services/api-test/src/test/java/io/metersphere/api/controller/CleanupResourceTests.java b/backend/services/api-test/src/test/java/io/metersphere/api/controller/CleanupResourceTests.java new file mode 100644 index 0000000000..e0e63ecf50 --- /dev/null +++ b/backend/services/api-test/src/test/java/io/metersphere/api/controller/CleanupResourceTests.java @@ -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"); + } + +} diff --git a/backend/services/bug-management/src/main/java/io/metersphere/bug/service/CleanupBugResourceService.java b/backend/services/bug-management/src/main/java/io/metersphere/bug/service/CleanupBugResourceService.java new file mode 100644 index 0000000000..8b831a9d0e --- /dev/null +++ b/backend/services/bug-management/src/main/java/io/metersphere/bug/service/CleanupBugResourceService.java @@ -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 + "]相关缺陷资源"); + } +} diff --git a/backend/services/bug-management/src/test/java/io/metersphere/bug/controller/CleanupResourceTests.java b/backend/services/bug-management/src/test/java/io/metersphere/bug/controller/CleanupResourceTests.java new file mode 100644 index 0000000000..e5e166a662 --- /dev/null +++ b/backend/services/bug-management/src/test/java/io/metersphere/bug/controller/CleanupResourceTests.java @@ -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"); + } + +} diff --git a/backend/services/case-management/src/main/java/io/metersphere/functional/service/CleanupCaseResourceService.java b/backend/services/case-management/src/main/java/io/metersphere/functional/service/CleanupCaseResourceService.java new file mode 100644 index 0000000000..79d3bff4a1 --- /dev/null +++ b/backend/services/case-management/src/main/java/io/metersphere/functional/service/CleanupCaseResourceService.java @@ -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 + "]相关功能用例资源"); + } +} diff --git a/backend/services/case-management/src/test/java/io/metersphere/functional/controller/CleanupResourceTests.java b/backend/services/case-management/src/test/java/io/metersphere/functional/controller/CleanupResourceTests.java new file mode 100644 index 0000000000..d09eb90254 --- /dev/null +++ b/backend/services/case-management/src/test/java/io/metersphere/functional/controller/CleanupResourceTests.java @@ -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"); + } + +} diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/job/CleanProjectJob.java b/backend/services/system-setting/src/main/java/io/metersphere/system/job/CleanProjectJob.java index 9e44204aae..015106a926 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/job/CleanProjectJob.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/job/CleanProjectJob.java @@ -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); } } - - } diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/SystemProjectService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/SystemProjectService.java index ec823bb4bf..4258d17dba 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/service/SystemProjectService.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/SystemProjectService.java @@ -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 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()); }); diff --git a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/CleanupResourceTests.java b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/CleanupResourceTests.java new file mode 100644 index 0000000000..0b367e60d5 --- /dev/null +++ b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/CleanupResourceTests.java @@ -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"); + } + +} diff --git a/backend/services/system-setting/src/test/java/io/metersphere/system/mock/CleanupTestResourceService.java b/backend/services/system-setting/src/test/java/io/metersphere/system/mock/CleanupTestResourceService.java new file mode 100644 index 0000000000..99649a1084 --- /dev/null +++ b/backend/services/system-setting/src/test/java/io/metersphere/system/mock/CleanupTestResourceService.java @@ -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资源"); + } +} diff --git a/backend/services/test-plan/src/main/java/io/metersphere/plan/service/CleanupPlanResourceService.java b/backend/services/test-plan/src/main/java/io/metersphere/plan/service/CleanupPlanResourceService.java new file mode 100644 index 0000000000..0281788d46 --- /dev/null +++ b/backend/services/test-plan/src/main/java/io/metersphere/plan/service/CleanupPlanResourceService.java @@ -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 + "]相关测试计划资源"); + } +} diff --git a/backend/services/test-plan/src/test/java/io/metersphere/plan/controller/CleanupResourceTests.java b/backend/services/test-plan/src/test/java/io/metersphere/plan/controller/CleanupResourceTests.java new file mode 100644 index 0000000000..890fa62c51 --- /dev/null +++ b/backend/services/test-plan/src/test/java/io/metersphere/plan/controller/CleanupResourceTests.java @@ -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"); + } + +}