From 25ec1b4a5e6bca70e64273eef15219f2782a4142 Mon Sep 17 00:00:00 2001 From: dengqichen Date: Wed, 8 Jan 2025 15:57:38 +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 --- .../IRepositoryBranchRepository.java | 2 + .../IRepositoryProjectRepository.java | 2 + .../impl/RepositoryBranchServiceImpl.java | 89 +++++++++++-------- 3 files changed, 56 insertions(+), 37 deletions(-) diff --git a/backend/src/main/java/com/qqchen/deploy/backend/deploy/repository/IRepositoryBranchRepository.java b/backend/src/main/java/com/qqchen/deploy/backend/deploy/repository/IRepositoryBranchRepository.java index eabd6e66..926d6dbe 100644 --- a/backend/src/main/java/com/qqchen/deploy/backend/deploy/repository/IRepositoryBranchRepository.java +++ b/backend/src/main/java/com/qqchen/deploy/backend/deploy/repository/IRepositoryBranchRepository.java @@ -33,4 +33,6 @@ public interface IRepositoryBranchRepository extends IBaseRepository findByExternalSystemIdAndProjectIdAndDeletedFalse(Long externalSystemId, Long id); } \ No newline at end of file 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 006251c3..450eb13d 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 @@ -30,4 +30,6 @@ public interface IRepositoryProjectRepository extends IBaseRepository findByExternalSystemId(Long externalSystemId); void deleteByExternalSystemId(Long externalSystemId); + + List findByExternalSystemIdAndDeletedFalse(Long externalSystemId); } \ No newline at end of file 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 d8f7f0c7..16b8b97b 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 @@ -47,43 +47,58 @@ 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); -// } - return null; + try { + // 1. 获取外部系统信息 + ExternalSystem externalSystem = externalSystemRepository.findById(externalSystemId) + .orElseThrow(() -> new BusinessException(ResponseCode.EXTERNAL_SYSTEM_NOT_FOUND)); + + // 2. 获取所有项目 + List projects = repositoryProjectRepository.findByExternalSystemIdAndDeletedFalse(externalSystemId); + if (projects.isEmpty()) { + log.info("No projects found for external system: {}", externalSystem.getName()); + return 0; + } + + int totalSyncedBranches = 0; + // 3. 同步每个项目的分支 + for (RepositoryProject project : projects) { + try { + // 3.1 获取远程分支信息 + List remoteBranches = gitServiceIntegration.branches(externalSystem, project.getProjectId()); + if (remoteBranches.isEmpty()) { + log.info("No branches found for project: {}", project.getName()); + continue; + } + + // 3.2 获取本地已存在的分支 + List existingBranches = repositoryBranchRepository.findByExternalSystemIdAndProjectIdAndDeletedFalse( + externalSystemId, project.getId()); + Map existingBranchMap = existingBranches.stream() + .collect(Collectors.toMap(RepositoryBranch::getName, Function.identity())); + + // 3.3 更新或创建分支 + List branchesToSave = remoteBranches.stream() + .map(remoteBranch -> updateOrCreateBranch(externalSystemId, project.getId(), remoteBranch, existingBranchMap)) + .collect(Collectors.toList()); + + // 3.4 保存分支 + repositoryBranchRepository.saveAll(branchesToSave); + totalSyncedBranches += branchesToSave.size(); + + log.info("Successfully synchronized {} branches for project: {}", + branchesToSave.size(), project.getName()); + } catch (Exception e) { + log.error("Failed to sync branches for project: {}", project.getName(), e); + } + } + + log.info("Successfully synchronized total {} branches for external system: {}", + totalSyncedBranches, externalSystem.getName()); + return totalSyncedBranches; + } catch (Exception e) { + log.error("Failed to sync branches for external system: {}", externalSystemId, e); + throw new BusinessException(ResponseCode.REPOSITORY_BRANCH_SYNC_FAILED); + } } private RepositoryBranch updateOrCreateBranch(