大声道撒旦

This commit is contained in:
dengqichen 2025-01-09 16:59:43 +08:00
parent c92c157864
commit 465d71d74e
2 changed files with 26 additions and 7 deletions

View File

@ -119,7 +119,7 @@ public class GitServiceIntegrationImpl implements IGitServiceIntegration {
@Override @Override
public List<GitBranchResponse> branches(ExternalSystem system, Long projectId) { public List<GitBranchResponse> branches(ExternalSystem system, Long projectId) {
try { try {
String url = String.format("%s/api/v4/projects/%d/repository/branches", system.getUrl(), projectId); String url = String.format("%s/api/v4/projects/%d/repository/branches?per_page=100", system.getUrl(), projectId);
HttpHeaders headers = createHeaders(system); HttpHeaders headers = createHeaders(system);
HttpEntity<String> entity = new HttpEntity<>(headers); HttpEntity<String> entity = new HttpEntity<>(headers);
@ -130,8 +130,7 @@ public class GitServiceIntegrationImpl implements IGitServiceIntegration {
entity, entity,
String.class String.class
); );
log.info("First branch from GitLab API: {}", log.info("GitLab API response for branches: {}", rawResponse.getBody());
rawResponse.getBody() != null ? rawResponse.getBody().split("},")[0] + "}" : null);
// 然后解析为对象 // 然后解析为对象
ResponseEntity<List<GitBranchResponse>> response = restTemplate.exchange( ResponseEntity<List<GitBranchResponse>> response = restTemplate.exchange(
@ -141,7 +140,14 @@ public class GitServiceIntegrationImpl implements IGitServiceIntegration {
new ParameterizedTypeReference<>() {} new ParameterizedTypeReference<>() {}
); );
return response.getBody() != null ? response.getBody() : Collections.emptyList(); List<GitBranchResponse> branches = response.getBody();
if (branches != null && !branches.isEmpty()) {
log.info("Found {} branches, first branch: {}", branches.size(), branches.get(0).getName());
} else {
log.warn("No branches found for project: {}", projectId);
}
return branches != null ? branches : Collections.emptyList();
} catch (Exception e) { } catch (Exception e) {
log.error("Failed to fetch git branches for system: {} and project: {}", system.getName(), projectId, e); log.error("Failed to fetch git branches for system: {} and project: {}", system.getName(), projectId, e);
return Collections.emptyList(); return Collections.emptyList();

View File

@ -110,6 +110,7 @@ public class RepositoryBranchServiceImpl extends BaseServiceImpl<RepositoryBranc
// 2. 获取外部系统信息 // 2. 获取外部系统信息
ExternalSystem externalSystem = externalSystemRepository.findById(externalSystemId) ExternalSystem externalSystem = externalSystemRepository.findById(externalSystemId)
.orElseThrow(() -> new BusinessException(ResponseCode.EXTERNAL_SYSTEM_NOT_FOUND)); .orElseThrow(() -> new BusinessException(ResponseCode.EXTERNAL_SYSTEM_NOT_FOUND));
log.info("Starting branch sync for external system: {} (ID: {})", externalSystem.getName(), externalSystemId);
// 3. 获取所有项目 // 3. 获取所有项目
List<RepositoryProject> projects = repositoryProjectRepository.findByExternalSystemIdAndDeletedFalse(externalSystemId); List<RepositoryProject> projects = repositoryProjectRepository.findByExternalSystemIdAndDeletedFalse(externalSystemId);
@ -118,6 +119,7 @@ public class RepositoryBranchServiceImpl extends BaseServiceImpl<RepositoryBranc
repositorySyncHistoryService.updateSyncHistory(syncHistory.getId(), ExternalSystemSyncStatus.SUCCESS, null); repositorySyncHistoryService.updateSyncHistory(syncHistory.getId(), ExternalSystemSyncStatus.SUCCESS, null);
return 0; return 0;
} }
log.info("Found {} projects to sync", projects.size());
AtomicInteger totalSyncedBranches = new AtomicInteger(0); AtomicInteger totalSyncedBranches = new AtomicInteger(0);
List<CompletableFuture<Void>> futures = new ArrayList<>(); List<CompletableFuture<Void>> futures = new ArrayList<>();
@ -127,17 +129,24 @@ public class RepositoryBranchServiceImpl extends BaseServiceImpl<RepositoryBranc
for (RepositoryProject project : projects) { for (RepositoryProject project : projects) {
CompletableFuture<Void> future = CompletableFuture.runAsync(() -> { CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
try { try {
log.info("Syncing branches for project: {} (ID: {})", project.getName(), project.getProjectId());
// 4.1 获取当前项目在GitLab上的所有分支 // 4.1 获取当前项目在GitLab上的所有分支
List<GitBranchResponse> remoteBranches = gitServiceIntegration.branches(externalSystem, project.getProjectId()); List<GitBranchResponse> remoteBranches = gitServiceIntegration.branches(externalSystem, project.getProjectId());
log.info("Found {} remote branches for project: {}", remoteBranches.size(), project.getName());
Set<String> remoteBranchNames = remoteBranches.stream() Set<String> remoteBranchNames = remoteBranches.stream()
.map(GitBranchResponse::getName) .map(GitBranchResponse::getName)
.collect(Collectors.toSet()); .collect(Collectors.toSet());
log.info("Remote branch names: {}", remoteBranchNames);
// 4.2 获取数据库中的所有分支 // 4.2 获取数据库中的所有分支
Map<String, RepositoryBranch> existingBranchMap = repositoryBranchRepository Map<String, RepositoryBranch> existingBranchMap = repositoryBranchRepository
.findByExternalSystemIdAndProjectIdAndDeletedFalse(externalSystemId, project.getId()) .findByExternalSystemIdAndProjectIdAndDeletedFalse(externalSystemId, project.getId())
.stream() .stream()
.collect(Collectors.toMap(RepositoryBranch::getName, Function.identity())); .collect(Collectors.toMap(RepositoryBranch::getName, Function.identity()));
log.info("Found {} existing branches in database for project: {}", existingBranchMap.size(), project.getName());
log.info("Existing branch names: {}", existingBranchMap.keySet());
// 4.3 处理所有远程分支新增或更新 // 4.3 处理所有远程分支新增或更新
for (GitBranchResponse remoteBranch : remoteBranches) { for (GitBranchResponse remoteBranch : remoteBranches) {
@ -145,6 +154,7 @@ public class RepositoryBranchServiceImpl extends BaseServiceImpl<RepositoryBranc
branchesToSave.add(branch); branchesToSave.add(branch);
totalSyncedBranches.incrementAndGet(); totalSyncedBranches.incrementAndGet();
existingBranchMap.remove(remoteBranch.getName()); existingBranchMap.remove(remoteBranch.getName());
log.info("Processed remote branch: {}", remoteBranch.getName());
} }
// 4.4 处理已删除的分支 // 4.4 处理已删除的分支
@ -152,11 +162,12 @@ public class RepositoryBranchServiceImpl extends BaseServiceImpl<RepositoryBranc
branch.setDeleted(true); branch.setDeleted(true);
branchesToSave.add(branch); branchesToSave.add(branch);
totalSyncedBranches.incrementAndGet(); totalSyncedBranches.incrementAndGet();
log.info("Marked branch as deleted: {}", branchName);
}); });
log.info("Processed {} branches for project: {}", remoteBranches.size(), project.getName()); log.info("Completed processing {} branches for project: {}", remoteBranches.size(), project.getName());
} catch (Exception e) { } catch (Exception e) {
log.error("Failed to sync branches for project: {}", project.getName(), e); log.error("Failed to sync branches for project: {} (ID: {})", project.getName(), project.getProjectId(), e);
throw new RuntimeException(e); throw new RuntimeException(e);
} }
}, executor); }, executor);
@ -170,6 +181,7 @@ public class RepositoryBranchServiceImpl extends BaseServiceImpl<RepositoryBranc
// 批量保存分支 // 批量保存分支
if (!branchesToSave.isEmpty()) { if (!branchesToSave.isEmpty()) {
try { try {
log.info("Saving {} branches to database", branchesToSave.size());
saveBatch(branchesToSave); saveBatch(branchesToSave);
} catch (Exception e) { } catch (Exception e) {
log.error("Error saving branches in batch", e); log.error("Error saving branches in batch", e);
@ -180,11 +192,12 @@ public class RepositoryBranchServiceImpl extends BaseServiceImpl<RepositoryBranc
// 5. 更新同步历史为成功 // 5. 更新同步历史为成功
repositorySyncHistoryService.updateSyncHistory(syncHistory.getId(), ExternalSystemSyncStatus.SUCCESS, null); repositorySyncHistoryService.updateSyncHistory(syncHistory.getId(), ExternalSystemSyncStatus.SUCCESS, null);
log.info("Successfully synchronized total {} branches for external system: {}", log.info("Successfully synchronized {} branches for external system: {}",
totalSyncedBranches.get(), externalSystem.getName()); totalSyncedBranches.get(), externalSystem.getName());
return totalSyncedBranches.get(); return totalSyncedBranches.get();
} catch (Exception e) { } catch (Exception e) {
// 6. 更新同步历史为失败 // 6. 更新同步历史为失败
log.error("Failed to sync branches", e);
repositorySyncHistoryService.updateSyncHistory(syncHistory.getId(), ExternalSystemSyncStatus.FAILED, e.getMessage()); repositorySyncHistoryService.updateSyncHistory(syncHistory.getId(), ExternalSystemSyncStatus.FAILED, e.getMessage());
throw new BusinessException(ResponseCode.REPOSITORY_BRANCH_SYNC_FAILED); throw new BusinessException(ResponseCode.REPOSITORY_BRANCH_SYNC_FAILED);
} }