大声道撒旦
This commit is contained in:
parent
c92c157864
commit
465d71d74e
@ -119,7 +119,7 @@ public class GitServiceIntegrationImpl implements IGitServiceIntegration {
|
||||
@Override
|
||||
public List<GitBranchResponse> branches(ExternalSystem system, Long projectId) {
|
||||
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);
|
||||
HttpEntity<String> entity = new HttpEntity<>(headers);
|
||||
|
||||
@ -130,8 +130,7 @@ public class GitServiceIntegrationImpl implements IGitServiceIntegration {
|
||||
entity,
|
||||
String.class
|
||||
);
|
||||
log.info("First branch from GitLab API: {}",
|
||||
rawResponse.getBody() != null ? rawResponse.getBody().split("},")[0] + "}" : null);
|
||||
log.info("GitLab API response for branches: {}", rawResponse.getBody());
|
||||
|
||||
// 然后解析为对象
|
||||
ResponseEntity<List<GitBranchResponse>> response = restTemplate.exchange(
|
||||
@ -141,7 +140,14 @@ public class GitServiceIntegrationImpl implements IGitServiceIntegration {
|
||||
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) {
|
||||
log.error("Failed to fetch git branches for system: {} and project: {}", system.getName(), projectId, e);
|
||||
return Collections.emptyList();
|
||||
|
||||
@ -110,6 +110,7 @@ public class RepositoryBranchServiceImpl extends BaseServiceImpl<RepositoryBranc
|
||||
// 2. 获取外部系统信息
|
||||
ExternalSystem externalSystem = externalSystemRepository.findById(externalSystemId)
|
||||
.orElseThrow(() -> new BusinessException(ResponseCode.EXTERNAL_SYSTEM_NOT_FOUND));
|
||||
log.info("Starting branch sync for external system: {} (ID: {})", externalSystem.getName(), externalSystemId);
|
||||
|
||||
// 3. 获取所有项目
|
||||
List<RepositoryProject> projects = repositoryProjectRepository.findByExternalSystemIdAndDeletedFalse(externalSystemId);
|
||||
@ -118,6 +119,7 @@ public class RepositoryBranchServiceImpl extends BaseServiceImpl<RepositoryBranc
|
||||
repositorySyncHistoryService.updateSyncHistory(syncHistory.getId(), ExternalSystemSyncStatus.SUCCESS, null);
|
||||
return 0;
|
||||
}
|
||||
log.info("Found {} projects to sync", projects.size());
|
||||
|
||||
AtomicInteger totalSyncedBranches = new AtomicInteger(0);
|
||||
List<CompletableFuture<Void>> futures = new ArrayList<>();
|
||||
@ -127,17 +129,24 @@ public class RepositoryBranchServiceImpl extends BaseServiceImpl<RepositoryBranc
|
||||
for (RepositoryProject project : projects) {
|
||||
CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
|
||||
try {
|
||||
log.info("Syncing branches for project: {} (ID: {})", project.getName(), project.getProjectId());
|
||||
|
||||
// 4.1 获取当前项目在GitLab上的所有分支
|
||||
List<GitBranchResponse> remoteBranches = gitServiceIntegration.branches(externalSystem, project.getProjectId());
|
||||
log.info("Found {} remote branches for project: {}", remoteBranches.size(), project.getName());
|
||||
|
||||
Set<String> remoteBranchNames = remoteBranches.stream()
|
||||
.map(GitBranchResponse::getName)
|
||||
.collect(Collectors.toSet());
|
||||
log.info("Remote branch names: {}", remoteBranchNames);
|
||||
|
||||
// 4.2 获取数据库中的所有分支
|
||||
Map<String, RepositoryBranch> existingBranchMap = repositoryBranchRepository
|
||||
.findByExternalSystemIdAndProjectIdAndDeletedFalse(externalSystemId, project.getId())
|
||||
.stream()
|
||||
.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 处理所有远程分支(新增或更新)
|
||||
for (GitBranchResponse remoteBranch : remoteBranches) {
|
||||
@ -145,6 +154,7 @@ public class RepositoryBranchServiceImpl extends BaseServiceImpl<RepositoryBranc
|
||||
branchesToSave.add(branch);
|
||||
totalSyncedBranches.incrementAndGet();
|
||||
existingBranchMap.remove(remoteBranch.getName());
|
||||
log.info("Processed remote branch: {}", remoteBranch.getName());
|
||||
}
|
||||
|
||||
// 4.4 处理已删除的分支
|
||||
@ -152,11 +162,12 @@ public class RepositoryBranchServiceImpl extends BaseServiceImpl<RepositoryBranc
|
||||
branch.setDeleted(true);
|
||||
branchesToSave.add(branch);
|
||||
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) {
|
||||
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);
|
||||
}
|
||||
}, executor);
|
||||
@ -170,6 +181,7 @@ public class RepositoryBranchServiceImpl extends BaseServiceImpl<RepositoryBranc
|
||||
// 批量保存分支
|
||||
if (!branchesToSave.isEmpty()) {
|
||||
try {
|
||||
log.info("Saving {} branches to database", branchesToSave.size());
|
||||
saveBatch(branchesToSave);
|
||||
} catch (Exception e) {
|
||||
log.error("Error saving branches in batch", e);
|
||||
@ -180,11 +192,12 @@ public class RepositoryBranchServiceImpl extends BaseServiceImpl<RepositoryBranc
|
||||
// 5. 更新同步历史为成功
|
||||
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());
|
||||
return totalSyncedBranches.get();
|
||||
} catch (Exception e) {
|
||||
// 6. 更新同步历史为失败
|
||||
log.error("Failed to sync branches", e);
|
||||
repositorySyncHistoryService.updateSyncHistory(syncHistory.getId(), ExternalSystemSyncStatus.FAILED, e.getMessage());
|
||||
throw new BusinessException(ResponseCode.REPOSITORY_BRANCH_SYNC_FAILED);
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user