大声道撒旦
This commit is contained in:
parent
b49c21942a
commit
25ec1b4a5e
@ -33,4 +33,6 @@ public interface IRepositoryBranchRepository extends IBaseRepository<RepositoryB
|
||||
* @return 分支数量
|
||||
*/
|
||||
Long countByProjectId(Long projectId);
|
||||
|
||||
List<RepositoryBranch> findByExternalSystemIdAndProjectIdAndDeletedFalse(Long externalSystemId, Long id);
|
||||
}
|
||||
@ -30,4 +30,6 @@ public interface IRepositoryProjectRepository extends IBaseRepository<Repository
|
||||
List<RepositoryProject> findByExternalSystemId(Long externalSystemId);
|
||||
|
||||
void deleteByExternalSystemId(Long externalSystemId);
|
||||
|
||||
List<RepositoryProject> findByExternalSystemIdAndDeletedFalse(Long externalSystemId);
|
||||
}
|
||||
@ -47,43 +47,58 @@ public class RepositoryBranchServiceImpl extends BaseServiceImpl<RepositoryBranc
|
||||
@Override
|
||||
@Transactional
|
||||
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<GitBranchResponse> 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<RepositoryBranch> existingBranches = repositoryBranchRepository.findByExternalSystemIdAndProjectId(externalSystemId, projectId);
|
||||
// Map<String, RepositoryBranch> existingBranchMap = existingBranches.stream()
|
||||
// .collect(Collectors.toMap(RepositoryBranch::getName, Function.identity()));
|
||||
//
|
||||
// // 5. 更新或创建仓库分支
|
||||
// List<RepositoryBranch> 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<RepositoryProject> 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<GitBranchResponse> remoteBranches = gitServiceIntegration.branches(externalSystem, project.getProjectId());
|
||||
if (remoteBranches.isEmpty()) {
|
||||
log.info("No branches found for project: {}", project.getName());
|
||||
continue;
|
||||
}
|
||||
|
||||
// 3.2 获取本地已存在的分支
|
||||
List<RepositoryBranch> existingBranches = repositoryBranchRepository.findByExternalSystemIdAndProjectIdAndDeletedFalse(
|
||||
externalSystemId, project.getId());
|
||||
Map<String, RepositoryBranch> existingBranchMap = existingBranches.stream()
|
||||
.collect(Collectors.toMap(RepositoryBranch::getName, Function.identity()));
|
||||
|
||||
// 3.3 更新或创建分支
|
||||
List<RepositoryBranch> 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(
|
||||
|
||||
Loading…
Reference in New Issue
Block a user