大声道撒旦
This commit is contained in:
parent
25ec1b4a5e
commit
a7a6a1f5bb
@ -4,6 +4,8 @@ import com.qqchen.deploy.backend.deploy.enums.ExternalSystemSyncStatus;
|
|||||||
import com.qqchen.deploy.backend.deploy.enums.RepositorySyncType;
|
import com.qqchen.deploy.backend.deploy.enums.RepositorySyncType;
|
||||||
import com.qqchen.deploy.backend.framework.domain.Entity;
|
import com.qqchen.deploy.backend.framework.domain.Entity;
|
||||||
import jakarta.persistence.Column;
|
import jakarta.persistence.Column;
|
||||||
|
import jakarta.persistence.EnumType;
|
||||||
|
import jakarta.persistence.Enumerated;
|
||||||
import jakarta.persistence.Table;
|
import jakarta.persistence.Table;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.EqualsAndHashCode;
|
import lombok.EqualsAndHashCode;
|
||||||
@ -32,9 +34,11 @@ public class RepositorySyncHistory extends Entity<Long> {
|
|||||||
private String errorMessage;
|
private String errorMessage;
|
||||||
|
|
||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
|
@Enumerated(EnumType.STRING)
|
||||||
private ExternalSystemSyncStatus status;
|
private ExternalSystemSyncStatus status;
|
||||||
|
|
||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
|
@Enumerated(EnumType.STRING)
|
||||||
private RepositorySyncType syncType;
|
private RepositorySyncType syncType;
|
||||||
|
|
||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
|
|||||||
@ -4,6 +4,8 @@ import com.qqchen.deploy.backend.deploy.entity.ExternalSystem;
|
|||||||
import com.qqchen.deploy.backend.deploy.entity.RepositoryProject;
|
import com.qqchen.deploy.backend.deploy.entity.RepositoryProject;
|
||||||
import com.qqchen.deploy.backend.deploy.entity.RepositoryBranch;
|
import com.qqchen.deploy.backend.deploy.entity.RepositoryBranch;
|
||||||
import com.qqchen.deploy.backend.deploy.dto.RepositoryBranchDTO;
|
import com.qqchen.deploy.backend.deploy.dto.RepositoryBranchDTO;
|
||||||
|
import com.qqchen.deploy.backend.deploy.enums.ExternalSystemSyncStatus;
|
||||||
|
import com.qqchen.deploy.backend.deploy.enums.RepositorySyncType;
|
||||||
import com.qqchen.deploy.backend.deploy.integration.IGitServiceIntegration;
|
import com.qqchen.deploy.backend.deploy.integration.IGitServiceIntegration;
|
||||||
import com.qqchen.deploy.backend.deploy.integration.response.GitBranchResponse;
|
import com.qqchen.deploy.backend.deploy.integration.response.GitBranchResponse;
|
||||||
import com.qqchen.deploy.backend.deploy.query.RepositoryBranchQuery;
|
import com.qqchen.deploy.backend.deploy.query.RepositoryBranchQuery;
|
||||||
@ -14,6 +16,10 @@ import com.qqchen.deploy.backend.deploy.service.IRepositoryBranchService;
|
|||||||
import com.qqchen.deploy.backend.framework.enums.ResponseCode;
|
import com.qqchen.deploy.backend.framework.enums.ResponseCode;
|
||||||
import com.qqchen.deploy.backend.framework.exception.BusinessException;
|
import com.qqchen.deploy.backend.framework.exception.BusinessException;
|
||||||
import com.qqchen.deploy.backend.framework.service.impl.BaseServiceImpl;
|
import com.qqchen.deploy.backend.framework.service.impl.BaseServiceImpl;
|
||||||
|
import com.qqchen.deploy.backend.deploy.dto.RepositorySyncHistoryDTO;
|
||||||
|
import com.qqchen.deploy.backend.deploy.entity.RepositorySyncHistory;
|
||||||
|
import com.qqchen.deploy.backend.deploy.service.IRepositorySyncHistoryService;
|
||||||
|
import com.qqchen.deploy.backend.deploy.repository.IRepositorySyncHistoryRepository;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
@ -44,44 +50,75 @@ public class RepositoryBranchServiceImpl extends BaseServiceImpl<RepositoryBranc
|
|||||||
@Resource
|
@Resource
|
||||||
private IGitServiceIntegration gitServiceIntegration;
|
private IGitServiceIntegration gitServiceIntegration;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private IRepositorySyncHistoryService repositorySyncHistoryService;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private IRepositorySyncHistoryRepository repositorySyncHistoryRepository;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Transactional
|
@Transactional
|
||||||
public Integer syncBranches(Long externalSystemId) {
|
public Integer syncBranches(Long externalSystemId) {
|
||||||
|
// 1. 创建同步历史记录
|
||||||
|
RepositorySyncHistoryDTO syncHistory = repositorySyncHistoryService.createSyncHistory(
|
||||||
|
externalSystemId, RepositorySyncType.BRANCH);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 1. 获取外部系统信息
|
// 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));
|
||||||
|
|
||||||
// 2. 获取所有项目
|
// 3. 获取所有项目
|
||||||
List<RepositoryProject> projects = repositoryProjectRepository.findByExternalSystemIdAndDeletedFalse(externalSystemId);
|
List<RepositoryProject> projects = repositoryProjectRepository.findByExternalSystemIdAndDeletedFalse(externalSystemId);
|
||||||
if (projects.isEmpty()) {
|
if (projects.isEmpty()) {
|
||||||
log.info("No projects found for external system: {}", externalSystem.getName());
|
log.info("No projects found for external system: {}", externalSystem.getName());
|
||||||
|
repositorySyncHistoryService.updateSyncHistory(syncHistory.getId(), ExternalSystemSyncStatus.SUCCESS, null);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. 获取上次成功同步的历史记录
|
||||||
|
RepositorySyncHistory lastSuccessSync = repositorySyncHistoryRepository
|
||||||
|
.findTopByExternalSystemIdAndSyncTypeAndStatusOrderByStartTimeDesc(
|
||||||
|
externalSystemId, RepositorySyncType.BRANCH, ExternalSystemSyncStatus.SUCCESS);
|
||||||
|
|
||||||
|
// 5. 筛选需要同步的项目
|
||||||
|
List<RepositoryProject> projectsToSync = projects;
|
||||||
|
if (lastSuccessSync != null) {
|
||||||
|
projectsToSync = projects.stream()
|
||||||
|
.filter(project -> project.getLastActivityAt() == null ||
|
||||||
|
project.getLastActivityAt().isAfter(lastSuccessSync.getEndTime()))
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (projectsToSync.isEmpty()) {
|
||||||
|
log.info("No projects need to be synchronized for external system: {}", externalSystem.getName());
|
||||||
|
repositorySyncHistoryService.updateSyncHistory(syncHistory.getId(), ExternalSystemSyncStatus.SUCCESS, null);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int totalSyncedBranches = 0;
|
int totalSyncedBranches = 0;
|
||||||
// 3. 同步每个项目的分支
|
// 6. 同步需要更新的项目的分支
|
||||||
for (RepositoryProject project : projects) {
|
for (RepositoryProject project : projectsToSync) {
|
||||||
try {
|
try {
|
||||||
// 3.1 获取远程分支信息
|
// 6.1 获取远程分支信息
|
||||||
List<GitBranchResponse> remoteBranches = gitServiceIntegration.branches(externalSystem, project.getProjectId());
|
List<GitBranchResponse> remoteBranches = gitServiceIntegration.branches(externalSystem, project.getProjectId());
|
||||||
if (remoteBranches.isEmpty()) {
|
if (remoteBranches.isEmpty()) {
|
||||||
log.info("No branches found for project: {}", project.getName());
|
log.info("No branches found for project: {}", project.getName());
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3.2 获取本地已存在的分支
|
// 6.2 获取本地已存在的分支
|
||||||
List<RepositoryBranch> existingBranches = repositoryBranchRepository.findByExternalSystemIdAndProjectIdAndDeletedFalse(
|
List<RepositoryBranch> existingBranches = repositoryBranchRepository.findByExternalSystemIdAndProjectIdAndDeletedFalse(
|
||||||
externalSystemId, project.getId());
|
externalSystemId, project.getId());
|
||||||
Map<String, RepositoryBranch> existingBranchMap = existingBranches.stream()
|
Map<String, RepositoryBranch> existingBranchMap = existingBranches.stream()
|
||||||
.collect(Collectors.toMap(RepositoryBranch::getName, Function.identity()));
|
.collect(Collectors.toMap(RepositoryBranch::getName, Function.identity()));
|
||||||
|
|
||||||
// 3.3 更新或创建分支
|
// 6.3 更新或创建分支
|
||||||
List<RepositoryBranch> branchesToSave = remoteBranches.stream()
|
List<RepositoryBranch> branchesToSave = remoteBranches.stream()
|
||||||
.map(remoteBranch -> updateOrCreateBranch(externalSystemId, project.getId(), remoteBranch, existingBranchMap))
|
.map(remoteBranch -> updateOrCreateBranch(externalSystemId, project.getId(), remoteBranch, existingBranchMap))
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
// 3.4 保存分支
|
// 6.4 保存分支
|
||||||
repositoryBranchRepository.saveAll(branchesToSave);
|
repositoryBranchRepository.saveAll(branchesToSave);
|
||||||
totalSyncedBranches += branchesToSave.size();
|
totalSyncedBranches += branchesToSave.size();
|
||||||
|
|
||||||
@ -92,10 +129,15 @@ public class RepositoryBranchServiceImpl extends BaseServiceImpl<RepositoryBranc
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 7. 更新同步历史为成功
|
||||||
|
repositorySyncHistoryService.updateSyncHistory(syncHistory.getId(), ExternalSystemSyncStatus.SUCCESS, null);
|
||||||
|
|
||||||
log.info("Successfully synchronized total {} branches for external system: {}",
|
log.info("Successfully synchronized total {} branches for external system: {}",
|
||||||
totalSyncedBranches, externalSystem.getName());
|
totalSyncedBranches, externalSystem.getName());
|
||||||
return totalSyncedBranches;
|
return totalSyncedBranches;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
// 8. 更新同步历史为失败
|
||||||
|
repositorySyncHistoryService.updateSyncHistory(syncHistory.getId(), ExternalSystemSyncStatus.FAILED, e.getMessage());
|
||||||
log.error("Failed to sync branches for external system: {}", externalSystemId, e);
|
log.error("Failed to sync branches for external system: {}", externalSystemId, e);
|
||||||
throw new BusinessException(ResponseCode.REPOSITORY_BRANCH_SYNC_FAILED);
|
throw new BusinessException(ResponseCode.REPOSITORY_BRANCH_SYNC_FAILED);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user