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 33ea1ecb..397b2820 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 @@ -119,7 +119,7 @@ public class GitServiceIntegrationImpl implements IGitServiceIntegration { @Override public List 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 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> response = restTemplate.exchange( @@ -141,7 +140,14 @@ public class GitServiceIntegrationImpl implements IGitServiceIntegration { new ParameterizedTypeReference<>() {} ); - return response.getBody() != null ? response.getBody() : Collections.emptyList(); + List 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(); 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 a53fba85..966e1ca3 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 @@ -110,6 +110,7 @@ public class RepositoryBranchServiceImpl extends BaseServiceImpl new BusinessException(ResponseCode.EXTERNAL_SYSTEM_NOT_FOUND)); + log.info("Starting branch sync for external system: {} (ID: {})", externalSystem.getName(), externalSystemId); // 3. 获取所有项目 List projects = repositoryProjectRepository.findByExternalSystemIdAndDeletedFalse(externalSystemId); @@ -118,6 +119,7 @@ public class RepositoryBranchServiceImpl extends BaseServiceImpl> futures = new ArrayList<>(); @@ -127,17 +129,24 @@ public class RepositoryBranchServiceImpl extends BaseServiceImpl future = CompletableFuture.runAsync(() -> { try { + log.info("Syncing branches for project: {} (ID: {})", project.getName(), project.getProjectId()); + // 4.1 获取当前项目在GitLab上的所有分支 List remoteBranches = gitServiceIntegration.branches(externalSystem, project.getProjectId()); + log.info("Found {} remote branches for project: {}", remoteBranches.size(), project.getName()); + Set remoteBranchNames = remoteBranches.stream() .map(GitBranchResponse::getName) .collect(Collectors.toSet()); + log.info("Remote branch names: {}", remoteBranchNames); // 4.2 获取数据库中的所有分支 Map 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