From 2d90609edfd50392d1142e93c717bf4f7ca84616 Mon Sep 17 00:00:00 2001 From: dengqichen Date: Wed, 8 Jan 2025 15:03:59 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=A7=E5=A3=B0=E9=81=93=E6=92=92=E6=97=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/RepositoryBranchApiController.java | 28 ------- .../api/RepositoryGroupApiController.java | 23 ------ .../api/RepositoryManagerApiController.java | 19 +---- .../api/RepositoryProjectApiController.java | 25 ------ .../integration/IGitServiceIntegration.java | 7 +- .../impl/GitServiceIntegrationImpl.java | 6 +- .../IRepositoryProjectRepository.java | 2 + .../deploy/service/IGitManagerService.java | 13 +--- .../service/IRepositoryBranchService.java | 3 +- .../service/IRepositoryProjectService.java | 3 +- .../service/impl/GitManagerServiceImpl.java | 66 ++++------------ .../impl/RepositoryBranchServiceImpl.java | 75 +++++++++--------- .../impl/RepositoryProjectServiceImpl.java | 76 ++++++++----------- .../db/migration/V1.0.0__init_schema.sql | 9 +-- 14 files changed, 99 insertions(+), 256 deletions(-) diff --git a/backend/src/main/java/com/qqchen/deploy/backend/deploy/api/RepositoryBranchApiController.java b/backend/src/main/java/com/qqchen/deploy/backend/deploy/api/RepositoryBranchApiController.java index abdaf75c..17dbc5c6 100644 --- a/backend/src/main/java/com/qqchen/deploy/backend/deploy/api/RepositoryBranchApiController.java +++ b/backend/src/main/java/com/qqchen/deploy/backend/deploy/api/RepositoryBranchApiController.java @@ -25,34 +25,6 @@ import java.util.List; @Tag(name = "Git仓库分支管理", description = "Git仓库分支管理相关接口") public class RepositoryBranchApiController extends BaseController { - @Resource - private IRepositoryBranchService repositoryBranchService; - - @Operation(summary = "同步指定外部系统下指定项目的分支") - @PostMapping("/{externalSystemId}/projects/{projectId}/sync") - public Response syncBranches( - @Parameter(description = "外部系统ID", required = true) @PathVariable Long externalSystemId, - @Parameter(description = "项目ID", required = true) @PathVariable Long projectId - ) { - return Response.success(repositoryBranchService.syncBranches(externalSystemId, projectId)); - } - - @Operation(summary = "统计指定外部系统下的分支数量") - @GetMapping("/{externalSystemId}/count") - public Response countByExternalSystemId( - @Parameter(description = "外部系统ID", required = true) @PathVariable Long externalSystemId - ) { - return Response.success(repositoryBranchService.countByExternalSystemId(externalSystemId)); - } - - @Operation(summary = "统计指定项目下的分支数量") - @GetMapping("/projects/{projectId}/count") - public Response countByProjectId( - @Parameter(description = "项目ID", required = true) @PathVariable Long projectId - ) { - return Response.success(repositoryBranchService.countByProjectId(projectId)); - } - @Override protected void exportData(HttpServletResponse response, List data) { diff --git a/backend/src/main/java/com/qqchen/deploy/backend/deploy/api/RepositoryGroupApiController.java b/backend/src/main/java/com/qqchen/deploy/backend/deploy/api/RepositoryGroupApiController.java index 73ab4026..3a3ca9e2 100644 --- a/backend/src/main/java/com/qqchen/deploy/backend/deploy/api/RepositoryGroupApiController.java +++ b/backend/src/main/java/com/qqchen/deploy/backend/deploy/api/RepositoryGroupApiController.java @@ -3,13 +3,8 @@ package com.qqchen.deploy.backend.deploy.api; import com.qqchen.deploy.backend.deploy.entity.RepositoryGroup; import com.qqchen.deploy.backend.deploy.dto.RepositoryGroupDTO; import com.qqchen.deploy.backend.deploy.query.RepositoryGroupQuery; -import com.qqchen.deploy.backend.deploy.service.IRepositoryGroupService; -import com.qqchen.deploy.backend.framework.api.Response; import com.qqchen.deploy.backend.framework.controller.BaseController; -import io.swagger.v3.oas.annotations.Operation; -import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.tags.Tag; -import jakarta.annotation.Resource; import jakarta.servlet.http.HttpServletResponse; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.*; @@ -25,24 +20,6 @@ import java.util.List; @Tag(name = "Git仓库组管理", description = "Git仓库组管理相关接口") public class RepositoryGroupApiController extends BaseController { - @Resource - private IRepositoryGroupService repositoryGroupService; - - @Operation(summary = "同步仓库组") - @PostMapping("/{externalSystemId}/sync") - public Response syncGroups( - @Parameter(description = "外部系统ID", required = true) @PathVariable Long externalSystemId - ) { - return Response.success(repositoryGroupService.syncGroups(externalSystemId)); - } - - @Operation(summary = "获取仓库组数量") - @GetMapping("/{externalSystemId}/count") - public Response countGroups( - @Parameter(description = "外部系统ID", required = true) @PathVariable Long externalSystemId - ) { - return Response.success(repositoryGroupService.countByExternalSystemId(externalSystemId)); - } @Override protected void exportData(HttpServletResponse response, List data) { diff --git a/backend/src/main/java/com/qqchen/deploy/backend/deploy/api/RepositoryManagerApiController.java b/backend/src/main/java/com/qqchen/deploy/backend/deploy/api/RepositoryManagerApiController.java index 34672ebd..27a25d0c 100644 --- a/backend/src/main/java/com/qqchen/deploy/backend/deploy/api/RepositoryManagerApiController.java +++ b/backend/src/main/java/com/qqchen/deploy/backend/deploy/api/RepositoryManagerApiController.java @@ -22,15 +22,6 @@ public class RepositoryManagerApiController { @Resource private IGitManagerService gitManagerService; - @Operation(summary = "同步所有Git数据", description = "同步指定外部系统的所有Git数据,包括仓库组、项目和分支") - @PostMapping("/{externalSystemId}/sync-all") - public Response syncAll( - @Parameter(description = "外部系统ID", required = true) @PathVariable Long externalSystemId - ) { - gitManagerService.syncAll(externalSystemId); - return Response.success(); - } - @Operation(summary = "同步Git仓库组", description = "同步指定外部系统的所有仓库组") @PostMapping("/{externalSystemId}/sync-groups") public Response syncGroups( @@ -43,20 +34,18 @@ public class RepositoryManagerApiController { @Operation(summary = "同步Git项目", description = "同步指定外部系统下指定仓库组的所有项目") @PostMapping("/{externalSystemId}/groups/{groupId}/sync-projects") public Response syncProjects( - @Parameter(description = "外部系统ID", required = true) @PathVariable Long externalSystemId, - @Parameter(description = "仓库组ID", required = true) @PathVariable Long groupId + @Parameter(description = "外部系统ID", required = true) @PathVariable Long externalSystemId ) { - gitManagerService.syncProjects(externalSystemId, groupId); + gitManagerService.syncProjects(externalSystemId); return Response.success(); } @Operation(summary = "同步Git分支", description = "同步指定外部系统下指定项目的所有分支") @PostMapping("/{externalSystemId}/projects/{projectId}/sync-branches") public Response syncBranches( - @Parameter(description = "外部系统ID", required = true) @PathVariable Long externalSystemId, - @Parameter(description = "项目ID", required = true) @PathVariable Long projectId + @Parameter(description = "外部系统ID", required = true) @PathVariable Long externalSystemId ) { - gitManagerService.syncBranches(externalSystemId, projectId); + gitManagerService.syncBranches(externalSystemId); return Response.success(); } diff --git a/backend/src/main/java/com/qqchen/deploy/backend/deploy/api/RepositoryProjectApiController.java b/backend/src/main/java/com/qqchen/deploy/backend/deploy/api/RepositoryProjectApiController.java index 10ddf32e..70cd4e15 100644 --- a/backend/src/main/java/com/qqchen/deploy/backend/deploy/api/RepositoryProjectApiController.java +++ b/backend/src/main/java/com/qqchen/deploy/backend/deploy/api/RepositoryProjectApiController.java @@ -3,13 +3,8 @@ package com.qqchen.deploy.backend.deploy.api; import com.qqchen.deploy.backend.deploy.entity.RepositoryProject; import com.qqchen.deploy.backend.deploy.dto.RepositoryProjectDTO; import com.qqchen.deploy.backend.deploy.query.RepositoryProjectQuery; -import com.qqchen.deploy.backend.deploy.service.IRepositoryProjectService; -import com.qqchen.deploy.backend.framework.api.Response; import com.qqchen.deploy.backend.framework.controller.BaseController; -import io.swagger.v3.oas.annotations.Operation; -import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.tags.Tag; -import jakarta.annotation.Resource; import jakarta.servlet.http.HttpServletResponse; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.*; @@ -25,26 +20,6 @@ import java.util.List; @Tag(name = "Git仓库项目管理", description = "Git仓库项目管理相关接口") public class RepositoryProjectApiController extends BaseController { - @Resource - private IRepositoryProjectService repositoryProjectService; - - @Operation(summary = "同步指定外部系统下指定仓库组的项目") - @PostMapping("/{externalSystemId}/groups/{groupId}/sync") - public Response syncProjects( - @Parameter(description = "外部系统ID", required = true) @PathVariable Long externalSystemId, - @Parameter(description = "仓库组ID", required = true) @PathVariable Long groupId - ) { - return Response.success(repositoryProjectService.syncProjects(externalSystemId, groupId)); - } - - @Operation(summary = "统计指定外部系统下的项目数量") - @GetMapping("/{externalSystemId}/count") - public Response countByExternalSystemId( - @Parameter(description = "外部系统ID", required = true) @PathVariable Long externalSystemId - ) { - return Response.success(repositoryProjectService.countByExternalSystemId(externalSystemId)); - } - @Override protected void exportData(HttpServletResponse response, List data) { diff --git a/backend/src/main/java/com/qqchen/deploy/backend/deploy/integration/IGitServiceIntegration.java b/backend/src/main/java/com/qqchen/deploy/backend/deploy/integration/IGitServiceIntegration.java index e9df8c19..92c4fe68 100644 --- a/backend/src/main/java/com/qqchen/deploy/backend/deploy/integration/IGitServiceIntegration.java +++ b/backend/src/main/java/com/qqchen/deploy/backend/deploy/integration/IGitServiceIntegration.java @@ -23,13 +23,12 @@ public interface IGitServiceIntegration extends IExternalSystemIntegration { List groups(ExternalSystem system); /** - * 获取指定组下的所有项目 + * 获取所有项目 * - * @param system 外部系统配置 - * @param groupId 组ID + * @param system 外部系统配置 * @return 项目列表 */ - List projects(ExternalSystem system, Long groupId); + List projects(ExternalSystem system); /** * 获取指定项目的所有分支 diff --git a/backend/src/main/java/com/qqchen/deploy/backend/deploy/integration/impl/GitServiceIntegrationImpl.java b/backend/src/main/java/com/qqchen/deploy/backend/deploy/integration/impl/GitServiceIntegrationImpl.java index 3e06f9aa..724874d0 100644 --- a/backend/src/main/java/com/qqchen/deploy/backend/deploy/integration/impl/GitServiceIntegrationImpl.java +++ b/backend/src/main/java/com/qqchen/deploy/backend/deploy/integration/impl/GitServiceIntegrationImpl.java @@ -69,9 +69,9 @@ public class GitServiceIntegrationImpl implements IGitServiceIntegration { } @Override - public List projects(ExternalSystem system, Long groupId) { + public List projects(ExternalSystem system) { try { - String url = String.format("%s/api/v4/groups/%d/projects?per_page=100", system.getUrl(), groupId); + String url = String.format("%s/api/v4/projects?per_page=100", system.getUrl()); HttpHeaders headers = createHeaders(system); HttpEntity entity = new HttpEntity<>(headers); @@ -84,7 +84,7 @@ public class GitServiceIntegrationImpl implements IGitServiceIntegration { return response.getBody() != null ? response.getBody() : Collections.emptyList(); } catch (Exception e) { - log.error("Failed to fetch git projects for system: {} and group: {}", system.getName(), groupId, e); + log.error("Failed to fetch git projects for system: {}", system.getName(), e); return Collections.emptyList(); } } diff --git a/backend/src/main/java/com/qqchen/deploy/backend/deploy/repository/IRepositoryProjectRepository.java b/backend/src/main/java/com/qqchen/deploy/backend/deploy/repository/IRepositoryProjectRepository.java index 84f8f2d5..006251c3 100644 --- a/backend/src/main/java/com/qqchen/deploy/backend/deploy/repository/IRepositoryProjectRepository.java +++ b/backend/src/main/java/com/qqchen/deploy/backend/deploy/repository/IRepositoryProjectRepository.java @@ -28,4 +28,6 @@ public interface IRepositoryProjectRepository extends IBaseRepository findByExternalSystemId(Long externalSystemId); + + void deleteByExternalSystemId(Long externalSystemId); } \ No newline at end of file diff --git a/backend/src/main/java/com/qqchen/deploy/backend/deploy/service/IGitManagerService.java b/backend/src/main/java/com/qqchen/deploy/backend/deploy/service/IGitManagerService.java index b52eaec6..cf64d548 100644 --- a/backend/src/main/java/com/qqchen/deploy/backend/deploy/service/IGitManagerService.java +++ b/backend/src/main/java/com/qqchen/deploy/backend/deploy/service/IGitManagerService.java @@ -7,13 +7,6 @@ import com.qqchen.deploy.backend.deploy.dto.GitInstanceDTO; */ public interface IGitManagerService { - /** - * 同步Git所有数据(组、项目、分支) - * - * @param externalSystemId 外部系统ID - */ - void syncAll(Long externalSystemId); - /** * 同步Git组 * @@ -25,17 +18,15 @@ public interface IGitManagerService { * 同步Git项目 * * @param externalSystemId 外部系统ID - * @param groupId 组ID */ - void syncProjects(Long externalSystemId, Long groupId); + void syncProjects(Long externalSystemId); /** * 同步Git分支 * * @param externalSystemId 外部系统ID - * @param projectId 项目ID */ - void syncBranches(Long externalSystemId, Long projectId); + void syncBranches(Long externalSystemId); /** * 获取Git实例信息 diff --git a/backend/src/main/java/com/qqchen/deploy/backend/deploy/service/IRepositoryBranchService.java b/backend/src/main/java/com/qqchen/deploy/backend/deploy/service/IRepositoryBranchService.java index 422de524..42a51ad5 100644 --- a/backend/src/main/java/com/qqchen/deploy/backend/deploy/service/IRepositoryBranchService.java +++ b/backend/src/main/java/com/qqchen/deploy/backend/deploy/service/IRepositoryBranchService.java @@ -14,10 +14,9 @@ public interface IRepositoryBranchService extends IBaseService groups = repositoryGroupService.findByExternalSystemId(externalSystemId); - for (RepositoryGroupDTO group : groups) { - syncProjects(externalSystemId, group.getId()); - } - - // 4. 获取所有项目,同步每个项目的分支 - List projects = repositoryProjectService.findByExternalSystemId(externalSystemId); - for (RepositoryProjectDTO project : projects) { - syncBranches(externalSystemId, project.getId()); - } - - // 5. 更新同步历史记录为成功 - repositorySyncHistoryService.updateSyncHistory(allHistory.getId(), ExternalSystemSyncStatus.SUCCESS, null); - log.info("Successfully synchronized all Git data for external system: {}", externalSystemId); - } catch (Exception e) { - // 6. 更新同步历史记录为失败 - repositorySyncHistoryService.updateSyncHistory(allHistory.getId(), ExternalSystemSyncStatus.FAILED, e.getMessage()); - log.error("Failed to synchronize Git data for external system: {}", externalSystemId, e); - throw e; - } - } catch (Exception e) { - log.error("Failed to create sync history for external system: {}", externalSystemId, e); - throw new BusinessException(REPOSITORY_SYNC_FAILED); - } - } - @Override @Transactional(rollbackFor = Exception.class) public void syncGroups(Long externalSystemId) { try { // 1. 创建同步历史记录 RepositorySyncHistoryDTO groupHistory = repositorySyncHistoryService.createSyncHistory(externalSystemId, RepositorySyncType.GROUP); - + try { // 2. 同步组 Integer groupCount = repositoryGroupService.syncGroups(externalSystemId); - + // 3. 更新同步历史记录为成功 repositorySyncHistoryService.updateSyncHistory(groupHistory.getId(), ExternalSystemSyncStatus.SUCCESS, null); log.info("Successfully synchronized {} groups for external system: {}", groupCount, externalSystemId); @@ -110,22 +72,22 @@ public class GitManagerServiceImpl implements IGitManagerService { @Override @Transactional(rollbackFor = Exception.class) - public void syncProjects(Long externalSystemId, Long groupId) { + public void syncProjects(Long externalSystemId) { try { // 1. 创建同步历史记录 RepositorySyncHistoryDTO projectHistory = repositorySyncHistoryService.createSyncHistory(externalSystemId, RepositorySyncType.PROJECT); - + try { // 2. 同步项目 - Integer projectCount = repositoryProjectService.syncProjects(externalSystemId, groupId); - + Integer projectCount = repositoryProjectService.syncProjects(externalSystemId); + // 3. 更新同步历史记录为成功 repositorySyncHistoryService.updateSyncHistory(projectHistory.getId(), ExternalSystemSyncStatus.SUCCESS, null); - log.info("Successfully synchronized {} projects for group {} in external system: {}", projectCount, groupId, externalSystemId); + log.info("Successfully synchronized {} projects in external system: {}", projectCount, externalSystemId); } catch (Exception e) { // 4. 更新同步历史记录为失败 repositorySyncHistoryService.updateSyncHistory(projectHistory.getId(), ExternalSystemSyncStatus.FAILED, e.getMessage()); - log.error("Failed to synchronize projects for group {} in external system: {}", groupId, externalSystemId, e); + log.error("Failed to synchronize projects in external system: {}", externalSystemId, e); throw e; } } catch (Exception e) { @@ -136,22 +98,22 @@ public class GitManagerServiceImpl implements IGitManagerService { @Override @Transactional(rollbackFor = Exception.class) - public void syncBranches(Long externalSystemId, Long projectId) { + public void syncBranches(Long externalSystemId) { try { // 1. 创建同步历史记录 RepositorySyncHistoryDTO branchHistory = repositorySyncHistoryService.createSyncHistory(externalSystemId, RepositorySyncType.BRANCH); - + try { // 2. 同步分支 - Integer branchCount = repositoryBranchService.syncBranches(externalSystemId, projectId); - + Integer branchCount = repositoryBranchService.syncBranches(externalSystemId); + // 3. 更新同步历史记录为成功 repositorySyncHistoryService.updateSyncHistory(branchHistory.getId(), ExternalSystemSyncStatus.SUCCESS, null); - log.info("Successfully synchronized {} branches for project {} in external system: {}", branchCount, projectId, externalSystemId); + log.info("Successfully synchronized {} branches for external system: {}", branchCount, externalSystemId); } catch (Exception e) { // 4. 更新同步历史记录为失败 repositorySyncHistoryService.updateSyncHistory(branchHistory.getId(), ExternalSystemSyncStatus.FAILED, e.getMessage()); - log.error("Failed to synchronize branches for project {} in external system: {}", projectId, externalSystemId, e); + log.error("Failed to synchronize branches for external system: {}", externalSystemId, e); throw e; } } catch (Exception e) { diff --git a/backend/src/main/java/com/qqchen/deploy/backend/deploy/service/impl/RepositoryBranchServiceImpl.java b/backend/src/main/java/com/qqchen/deploy/backend/deploy/service/impl/RepositoryBranchServiceImpl.java index c09dede5..d8f7f0c7 100644 --- a/backend/src/main/java/com/qqchen/deploy/backend/deploy/service/impl/RepositoryBranchServiceImpl.java +++ b/backend/src/main/java/com/qqchen/deploy/backend/deploy/service/impl/RepositoryBranchServiceImpl.java @@ -46,43 +46,44 @@ public class RepositoryBranchServiceImpl extends BaseServiceImpl new BusinessException(ResponseCode.EXTERNAL_SYSTEM_NOT_FOUND)); - - // 2. 获取项目信息 - RepositoryProject project = repositoryProjectRepository.findById(projectId) - .orElseThrow(() -> new BusinessException(ResponseCode.REPOSITORY_PROJECT_NOT_FOUND)); - - // 3. 获取远程仓库分支信息 - List remoteBranches = gitServiceIntegration.branches(externalSystem, project.getProjectId()); - if (remoteBranches.isEmpty()) { - log.info("No branches found in remote git system: {}, project: {}", externalSystem.getName(), project.getName()); - return 0; - } - - // 4. 获取本地已存在的仓库分支 - List existingBranches = repositoryBranchRepository.findByExternalSystemIdAndProjectId(externalSystemId, projectId); - Map existingBranchMap = existingBranches.stream() - .collect(Collectors.toMap(RepositoryBranch::getName, Function.identity())); - - // 5. 更新或创建仓库分支 - List branchesToSave = remoteBranches.stream() - .map(remoteBranch -> updateOrCreateBranch(externalSystemId, projectId, remoteBranch, existingBranchMap)) - .collect(Collectors.toList()); - - // 6. 保存仓库分支 - repositoryBranchRepository.saveAll(branchesToSave); - - log.info("Successfully synchronized {} branches for external system: {}, project: {}", - branchesToSave.size(), externalSystem.getName(), project.getName()); - return branchesToSave.size(); - } catch (Exception e) { - log.error("Failed to sync repository branches for external system: {}, project: {}", externalSystemId, projectId, e); - throw new BusinessException(ResponseCode.REPOSITORY_SYNC_FAILED); - } + public Integer syncBranches(Long externalSystemId) { +// try { +// // 1. 获取外部系统信息 +// ExternalSystem externalSystem = externalSystemRepository.findById(externalSystemId) +// .orElseThrow(() -> new BusinessException(ResponseCode.EXTERNAL_SYSTEM_NOT_FOUND)); +// +// // 2. 获取项目信息 +// RepositoryProject project = repositoryProjectRepository.findById(projectId) +// .orElseThrow(() -> new BusinessException(ResponseCode.REPOSITORY_PROJECT_NOT_FOUND)); +// +// // 3. 获取远程仓库分支信息 +// List remoteBranches = gitServiceIntegration.branches(externalSystem, project.getProjectId()); +// if (remoteBranches.isEmpty()) { +// log.info("No branches found in remote git system: {}, project: {}", externalSystem.getName(), project.getName()); +// return 0; +// } +// +// // 4. 获取本地已存在的仓库分支 +// List existingBranches = repositoryBranchRepository.findByExternalSystemIdAndProjectId(externalSystemId, projectId); +// Map existingBranchMap = existingBranches.stream() +// .collect(Collectors.toMap(RepositoryBranch::getName, Function.identity())); +// +// // 5. 更新或创建仓库分支 +// List branchesToSave = remoteBranches.stream() +// .map(remoteBranch -> updateOrCreateBranch(externalSystemId, projectId, remoteBranch, existingBranchMap)) +// .collect(Collectors.toList()); +// +// // 6. 保存仓库分支 +// repositoryBranchRepository.saveAll(branchesToSave); +// +// log.info("Successfully synchronized {} branches for external system: {}, project: {}", +// branchesToSave.size(), externalSystem.getName(), project.getName()); +// return branchesToSave.size(); +// } catch (Exception e) { +// log.error("Failed to sync repository branches for external system: {}, project: {}", externalSystemId, projectId, e); +// throw new BusinessException(ResponseCode.REPOSITORY_SYNC_FAILED); +// } + return null; } private RepositoryBranch updateOrCreateBranch( diff --git a/backend/src/main/java/com/qqchen/deploy/backend/deploy/service/impl/RepositoryProjectServiceImpl.java b/backend/src/main/java/com/qqchen/deploy/backend/deploy/service/impl/RepositoryProjectServiceImpl.java index 8c204aa4..95e97464 100644 --- a/backend/src/main/java/com/qqchen/deploy/backend/deploy/service/impl/RepositoryProjectServiceImpl.java +++ b/backend/src/main/java/com/qqchen/deploy/backend/deploy/service/impl/RepositoryProjectServiceImpl.java @@ -49,70 +49,54 @@ public class RepositoryProjectServiceImpl extends BaseServiceImpl new BusinessException(ResponseCode.EXTERNAL_SYSTEM_NOT_FOUND)); - // 2. 获取仓库组信息 - RepositoryGroup group = repositoryGroupRepository.findById(groupId) - .orElseThrow(() -> new BusinessException(ResponseCode.REPOSITORY_GROUP_NOT_FOUND)); - - // 3. 获取远程仓库项目信息 - List remoteProjects = gitServiceIntegration.projects(externalSystem, group.getGroupId()); + // 2. 获取远程仓库项目信息 + List remoteProjects = gitServiceIntegration.projects(externalSystem); if (remoteProjects.isEmpty()) { - log.info("No projects found in remote git system: {}, group: {}", externalSystem.getName(), group.getName()); + log.info("No projects found in remote git system: {}", externalSystem.getName()); return 0; } - // 4. 获取本地已存在的仓库项目 - List existingProjects = repositoryProjectRepository.findByExternalSystemIdAndGroupId(externalSystemId, groupId); - Map existingProjectMap = existingProjects.stream() - .collect(Collectors.toMap(RepositoryProject::getProjectId, Function.identity())); + // 3. 清空当前系统中该外部系统的所有项目 + repositoryProjectRepository.deleteByExternalSystemId(externalSystemId); + repositoryProjectRepository.flush(); - // 5. 更新或创建仓库项目 + // 4. 批量保存从Git API获取的项目到数据库 List projectsToSave = remoteProjects.stream() - .map(remoteProject -> updateOrCreateProject(externalSystemId, groupId, remoteProject, existingProjectMap)) + .map(remoteProject -> { + RepositoryProject project = new RepositoryProject(); + project.setExternalSystemId(externalSystemId); + project.setProjectId(remoteProject.getId()); + project.setName(remoteProject.getName()); + project.setPath(remoteProject.getPath()); + project.setDescription(remoteProject.getDescription()); + project.setVisibility(remoteProject.getVisibility()); + project.setIsDefaultBranch(remoteProject.getDefaultBranch()); + project.setWebUrl(remoteProject.getWebUrl()); + project.setSshUrl(remoteProject.getSshUrl()); + project.setHttpUrl(remoteProject.getHttpUrl()); + project.setLastActivityAt(remoteProject.getLastActivityAt()); + return project; + }) .collect(Collectors.toList()); - // 6. 保存仓库项目 - repositoryProjectRepository.saveAll(projectsToSave); - - log.info("Successfully synchronized {} projects for external system: {}, group: {}", - projectsToSave.size(), externalSystem.getName(), group.getName()); - return projectsToSave.size(); + List savedProjects = repositoryProjectRepository.saveAll(projectsToSave); + + log.info("Successfully synchronized {} projects for external system: {}", + savedProjects.size(), externalSystem.getName()); + return savedProjects.size(); + } catch (Exception e) { - log.error("Failed to sync repository projects for external system: {}, group: {}", externalSystemId, groupId, e); + log.error("Failed to sync repository projects for external system: {}", externalSystemId, e); throw new BusinessException(ResponseCode.REPOSITORY_SYNC_FAILED); } } - private RepositoryProject updateOrCreateProject( - Long externalSystemId, - Long groupId, - GitProjectResponse remoteProject, - Map existingProjectMap) { - - RepositoryProject project = existingProjectMap.getOrDefault(remoteProject.getId(), new RepositoryProject()); - - // 更新基本信息 - project.setExternalSystemId(externalSystemId); - project.setGroupId(groupId); - project.setProjectId(remoteProject.getId()); - project.setName(remoteProject.getName()); - project.setPath(remoteProject.getPath()); - project.setDescription(remoteProject.getDescription()); - project.setVisibility(remoteProject.getVisibility()); - project.setIsDefaultBranch(remoteProject.getDefaultBranch()); - project.setWebUrl(remoteProject.getWebUrl()); - project.setSshUrl(remoteProject.getSshUrl()); - project.setHttpUrl(remoteProject.getHttpUrl()); - project.setLastActivityAt(remoteProject.getLastActivityAt()); - - return project; - } - @Override public Long countByExternalSystemId(Long externalSystemId) { return repositoryProjectRepository.countByExternalSystemId(externalSystemId); diff --git a/backend/src/main/resources/db/migration/V1.0.0__init_schema.sql b/backend/src/main/resources/db/migration/V1.0.0__init_schema.sql index 753b84f4..efe32b3f 100644 --- a/backend/src/main/resources/db/migration/V1.0.0__init_schema.sql +++ b/backend/src/main/resources/db/migration/V1.0.0__init_schema.sql @@ -298,15 +298,8 @@ CREATE TABLE deploy_repo_group avatar_url VARCHAR(255) NULL COMMENT '头像URL', web_url VARCHAR(255) NULL COMMENT '网页URL', visibility ENUM('private', 'internal', 'public') NOT NULL DEFAULT 'private' COMMENT '可见性:private-私有,internal-内部,public-公开', - sort INT DEFAULT 0 COMMENT '排序号', + sort INT DEFAULT 0 COMMENT '排序号' - -- 索引 - INDEX idx_external_system_group_id (external_system_id, group_id) COMMENT '外部系统组ID索引', - INDEX idx_parent_id (parent_id) COMMENT '父级ID索引', - INDEX idx_path ( PATH) COMMENT '路径索引', - - -- 外键约束 - CONSTRAINT fk_group_external_system FOREIGN KEY (external_system_id)REFERENCES sys_external_system (id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='代码仓库组表'; -- 代码仓库项目表