大声道撒旦

This commit is contained in:
dengqichen 2025-01-08 15:03:59 +08:00
parent 39e40969b6
commit 2d90609edf
14 changed files with 99 additions and 256 deletions

View File

@ -25,34 +25,6 @@ import java.util.List;
@Tag(name = "Git仓库分支管理", description = "Git仓库分支管理相关接口") @Tag(name = "Git仓库分支管理", description = "Git仓库分支管理相关接口")
public class RepositoryBranchApiController extends BaseController<RepositoryBranch, RepositoryBranchDTO, Long, RepositoryBranchQuery> { public class RepositoryBranchApiController extends BaseController<RepositoryBranch, RepositoryBranchDTO, Long, RepositoryBranchQuery> {
@Resource
private IRepositoryBranchService repositoryBranchService;
@Operation(summary = "同步指定外部系统下指定项目的分支")
@PostMapping("/{externalSystemId}/projects/{projectId}/sync")
public Response<Integer> syncBranches(
@Parameter(description = "外部系统ID", required = true) @PathVariable Long externalSystemId,
@Parameter(description = "项目ID", required = true) @PathVariable Long projectId
) {
return Response.success(repositoryBranchService.syncBranches(externalSystemId, projectId));
}
@Operation(summary = "统计指定外部系统下的分支数量")
@GetMapping("/{externalSystemId}/count")
public Response<Long> countByExternalSystemId(
@Parameter(description = "外部系统ID", required = true) @PathVariable Long externalSystemId
) {
return Response.success(repositoryBranchService.countByExternalSystemId(externalSystemId));
}
@Operation(summary = "统计指定项目下的分支数量")
@GetMapping("/projects/{projectId}/count")
public Response<Long> countByProjectId(
@Parameter(description = "项目ID", required = true) @PathVariable Long projectId
) {
return Response.success(repositoryBranchService.countByProjectId(projectId));
}
@Override @Override
protected void exportData(HttpServletResponse response, List<RepositoryBranchDTO> data) { protected void exportData(HttpServletResponse response, List<RepositoryBranchDTO> data) {

View File

@ -3,13 +3,8 @@ package com.qqchen.deploy.backend.deploy.api;
import com.qqchen.deploy.backend.deploy.entity.RepositoryGroup; import com.qqchen.deploy.backend.deploy.entity.RepositoryGroup;
import com.qqchen.deploy.backend.deploy.dto.RepositoryGroupDTO; import com.qqchen.deploy.backend.deploy.dto.RepositoryGroupDTO;
import com.qqchen.deploy.backend.deploy.query.RepositoryGroupQuery; import com.qqchen.deploy.backend.deploy.query.RepositoryGroupQuery;
import com.qqchen.deploy.backend.deploy.service.IRepositoryGroupService;
import com.qqchen.deploy.backend.framework.api.Response;
import com.qqchen.deploy.backend.framework.controller.BaseController; import com.qqchen.deploy.backend.framework.controller.BaseController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
@ -25,24 +20,6 @@ import java.util.List;
@Tag(name = "Git仓库组管理", description = "Git仓库组管理相关接口") @Tag(name = "Git仓库组管理", description = "Git仓库组管理相关接口")
public class RepositoryGroupApiController extends BaseController<RepositoryGroup, RepositoryGroupDTO, Long, RepositoryGroupQuery> { public class RepositoryGroupApiController extends BaseController<RepositoryGroup, RepositoryGroupDTO, Long, RepositoryGroupQuery> {
@Resource
private IRepositoryGroupService repositoryGroupService;
@Operation(summary = "同步仓库组")
@PostMapping("/{externalSystemId}/sync")
public Response<Integer> syncGroups(
@Parameter(description = "外部系统ID", required = true) @PathVariable Long externalSystemId
) {
return Response.success(repositoryGroupService.syncGroups(externalSystemId));
}
@Operation(summary = "获取仓库组数量")
@GetMapping("/{externalSystemId}/count")
public Response<Long> countGroups(
@Parameter(description = "外部系统ID", required = true) @PathVariable Long externalSystemId
) {
return Response.success(repositoryGroupService.countByExternalSystemId(externalSystemId));
}
@Override @Override
protected void exportData(HttpServletResponse response, List<RepositoryGroupDTO> data) { protected void exportData(HttpServletResponse response, List<RepositoryGroupDTO> data) {

View File

@ -22,15 +22,6 @@ public class RepositoryManagerApiController {
@Resource @Resource
private IGitManagerService gitManagerService; private IGitManagerService gitManagerService;
@Operation(summary = "同步所有Git数据", description = "同步指定外部系统的所有Git数据包括仓库组、项目和分支")
@PostMapping("/{externalSystemId}/sync-all")
public Response<Void> syncAll(
@Parameter(description = "外部系统ID", required = true) @PathVariable Long externalSystemId
) {
gitManagerService.syncAll(externalSystemId);
return Response.success();
}
@Operation(summary = "同步Git仓库组", description = "同步指定外部系统的所有仓库组") @Operation(summary = "同步Git仓库组", description = "同步指定外部系统的所有仓库组")
@PostMapping("/{externalSystemId}/sync-groups") @PostMapping("/{externalSystemId}/sync-groups")
public Response<Void> syncGroups( public Response<Void> syncGroups(
@ -43,20 +34,18 @@ public class RepositoryManagerApiController {
@Operation(summary = "同步Git项目", description = "同步指定外部系统下指定仓库组的所有项目") @Operation(summary = "同步Git项目", description = "同步指定外部系统下指定仓库组的所有项目")
@PostMapping("/{externalSystemId}/groups/{groupId}/sync-projects") @PostMapping("/{externalSystemId}/groups/{groupId}/sync-projects")
public Response<Void> syncProjects( public Response<Void> syncProjects(
@Parameter(description = "外部系统ID", required = true) @PathVariable Long externalSystemId, @Parameter(description = "外部系统ID", required = true) @PathVariable Long externalSystemId
@Parameter(description = "仓库组ID", required = true) @PathVariable Long groupId
) { ) {
gitManagerService.syncProjects(externalSystemId, groupId); gitManagerService.syncProjects(externalSystemId);
return Response.success(); return Response.success();
} }
@Operation(summary = "同步Git分支", description = "同步指定外部系统下指定项目的所有分支") @Operation(summary = "同步Git分支", description = "同步指定外部系统下指定项目的所有分支")
@PostMapping("/{externalSystemId}/projects/{projectId}/sync-branches") @PostMapping("/{externalSystemId}/projects/{projectId}/sync-branches")
public Response<Void> syncBranches( public Response<Void> syncBranches(
@Parameter(description = "外部系统ID", required = true) @PathVariable Long externalSystemId, @Parameter(description = "外部系统ID", required = true) @PathVariable Long externalSystemId
@Parameter(description = "项目ID", required = true) @PathVariable Long projectId
) { ) {
gitManagerService.syncBranches(externalSystemId, projectId); gitManagerService.syncBranches(externalSystemId);
return Response.success(); return Response.success();
} }

View File

@ -3,13 +3,8 @@ package com.qqchen.deploy.backend.deploy.api;
import com.qqchen.deploy.backend.deploy.entity.RepositoryProject; import com.qqchen.deploy.backend.deploy.entity.RepositoryProject;
import com.qqchen.deploy.backend.deploy.dto.RepositoryProjectDTO; import com.qqchen.deploy.backend.deploy.dto.RepositoryProjectDTO;
import com.qqchen.deploy.backend.deploy.query.RepositoryProjectQuery; import com.qqchen.deploy.backend.deploy.query.RepositoryProjectQuery;
import com.qqchen.deploy.backend.deploy.service.IRepositoryProjectService;
import com.qqchen.deploy.backend.framework.api.Response;
import com.qqchen.deploy.backend.framework.controller.BaseController; import com.qqchen.deploy.backend.framework.controller.BaseController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
@ -25,26 +20,6 @@ import java.util.List;
@Tag(name = "Git仓库项目管理", description = "Git仓库项目管理相关接口") @Tag(name = "Git仓库项目管理", description = "Git仓库项目管理相关接口")
public class RepositoryProjectApiController extends BaseController<RepositoryProject, RepositoryProjectDTO, Long, RepositoryProjectQuery> { public class RepositoryProjectApiController extends BaseController<RepositoryProject, RepositoryProjectDTO, Long, RepositoryProjectQuery> {
@Resource
private IRepositoryProjectService repositoryProjectService;
@Operation(summary = "同步指定外部系统下指定仓库组的项目")
@PostMapping("/{externalSystemId}/groups/{groupId}/sync")
public Response<Integer> syncProjects(
@Parameter(description = "外部系统ID", required = true) @PathVariable Long externalSystemId,
@Parameter(description = "仓库组ID", required = true) @PathVariable Long groupId
) {
return Response.success(repositoryProjectService.syncProjects(externalSystemId, groupId));
}
@Operation(summary = "统计指定外部系统下的项目数量")
@GetMapping("/{externalSystemId}/count")
public Response<Long> countByExternalSystemId(
@Parameter(description = "外部系统ID", required = true) @PathVariable Long externalSystemId
) {
return Response.success(repositoryProjectService.countByExternalSystemId(externalSystemId));
}
@Override @Override
protected void exportData(HttpServletResponse response, List<RepositoryProjectDTO> data) { protected void exportData(HttpServletResponse response, List<RepositoryProjectDTO> data) {

View File

@ -23,13 +23,12 @@ public interface IGitServiceIntegration extends IExternalSystemIntegration {
List<GitGroupResponse> groups(ExternalSystem system); List<GitGroupResponse> groups(ExternalSystem system);
/** /**
* 获取指定组下的所有项目 * 获取所有项目
* *
* @param system 外部系统配置 * @param system 外部系统配置
* @param groupId 组ID
* @return 项目列表 * @return 项目列表
*/ */
List<GitProjectResponse> projects(ExternalSystem system, Long groupId); List<GitProjectResponse> projects(ExternalSystem system);
/** /**
* 获取指定项目的所有分支 * 获取指定项目的所有分支

View File

@ -69,9 +69,9 @@ public class GitServiceIntegrationImpl implements IGitServiceIntegration {
} }
@Override @Override
public List<GitProjectResponse> projects(ExternalSystem system, Long groupId) { public List<GitProjectResponse> projects(ExternalSystem system) {
try { try {
String url = String.format("%s/api/v4/groups/%d/projects?per_page=100", system.getUrl(), groupId); String url = String.format("%s/api/v4/projects?per_page=100", system.getUrl());
HttpHeaders headers = createHeaders(system); HttpHeaders headers = createHeaders(system);
HttpEntity<String> entity = new HttpEntity<>(headers); HttpEntity<String> entity = new HttpEntity<>(headers);
@ -84,7 +84,7 @@ public class GitServiceIntegrationImpl implements IGitServiceIntegration {
return response.getBody() != null ? response.getBody() : Collections.emptyList(); return response.getBody() != null ? response.getBody() : Collections.emptyList();
} catch (Exception e) { } catch (Exception e) {
log.error("Failed to fetch git projects for system: {} and group: {}", system.getName(), groupId, e); log.error("Failed to fetch git projects for system: {}", system.getName(), e);
return Collections.emptyList(); return Collections.emptyList();
} }
} }

View File

@ -28,4 +28,6 @@ public interface IRepositoryProjectRepository extends IBaseRepository<Repository
Long countByExternalSystemId(Long externalSystemId); Long countByExternalSystemId(Long externalSystemId);
List<RepositoryProject> findByExternalSystemId(Long externalSystemId); List<RepositoryProject> findByExternalSystemId(Long externalSystemId);
void deleteByExternalSystemId(Long externalSystemId);
} }

View File

@ -7,13 +7,6 @@ import com.qqchen.deploy.backend.deploy.dto.GitInstanceDTO;
*/ */
public interface IGitManagerService { public interface IGitManagerService {
/**
* 同步Git所有数据项目分支
*
* @param externalSystemId 外部系统ID
*/
void syncAll(Long externalSystemId);
/** /**
* 同步Git组 * 同步Git组
* *
@ -25,17 +18,15 @@ public interface IGitManagerService {
* 同步Git项目 * 同步Git项目
* *
* @param externalSystemId 外部系统ID * @param externalSystemId 外部系统ID
* @param groupId 组ID
*/ */
void syncProjects(Long externalSystemId, Long groupId); void syncProjects(Long externalSystemId);
/** /**
* 同步Git分支 * 同步Git分支
* *
* @param externalSystemId 外部系统ID * @param externalSystemId 外部系统ID
* @param projectId 项目ID
*/ */
void syncBranches(Long externalSystemId, Long projectId); void syncBranches(Long externalSystemId);
/** /**
* 获取Git实例信息 * 获取Git实例信息

View File

@ -14,10 +14,9 @@ public interface IRepositoryBranchService extends IBaseService<RepositoryBranch,
* 同步指定外部系统下指定项目的分支 * 同步指定外部系统下指定项目的分支
* *
* @param externalSystemId 外部系统ID * @param externalSystemId 外部系统ID
* @param projectId 项目ID
* @return 同步的分支数量 * @return 同步的分支数量
*/ */
Integer syncBranches(Long externalSystemId, Long projectId); Integer syncBranches(Long externalSystemId);
/** /**
* 统计指定外部系统下的分支数量 * 统计指定外部系统下的分支数量

View File

@ -16,10 +16,9 @@ public interface IRepositoryProjectService extends IBaseService<RepositoryProjec
* 同步指定外部系统下指定仓库组的项目 * 同步指定外部系统下指定仓库组的项目
* *
* @param externalSystemId 外部系统ID * @param externalSystemId 外部系统ID
* @param groupId 仓库组ID
* @return 同步的项目数量 * @return 同步的项目数量
*/ */
Integer syncProjects(Long externalSystemId, Long groupId); Integer syncProjects(Long externalSystemId);
/** /**
* 统计指定外部系统下的项目数量 * 统计指定外部系统下的项目数量

View File

@ -44,44 +44,6 @@ public class GitManagerServiceImpl implements IGitManagerService {
@Resource @Resource
private IRepositorySyncHistoryRepository repositorySyncHistoryRepository; private IRepositorySyncHistoryRepository repositorySyncHistoryRepository;
@Override
@Transactional(rollbackFor = Exception.class)
public void syncAll(Long externalSystemId) {
try {
// 1. 创建同步历史记录
RepositorySyncHistoryDTO allHistory = repositorySyncHistoryService.createSyncHistory(externalSystemId, RepositorySyncType.GROUP);
try {
// 2. 同步组
syncGroups(externalSystemId);
// 3. 获取所有组同步每个组的项目
List<RepositoryGroupDTO> groups = repositoryGroupService.findByExternalSystemId(externalSystemId);
for (RepositoryGroupDTO group : groups) {
syncProjects(externalSystemId, group.getId());
}
// 4. 获取所有项目同步每个项目的分支
List<RepositoryProjectDTO> projects = repositoryProjectService.findByExternalSystemId(externalSystemId);
for (RepositoryProjectDTO project : projects) {
syncBranches(externalSystemId, project.getId());
}
// 5. 更新同步历史记录为成功
repositorySyncHistoryService.updateSyncHistory(allHistory.getId(), ExternalSystemSyncStatus.SUCCESS, null);
log.info("Successfully synchronized all Git data for external system: {}", externalSystemId);
} catch (Exception e) {
// 6. 更新同步历史记录为失败
repositorySyncHistoryService.updateSyncHistory(allHistory.getId(), ExternalSystemSyncStatus.FAILED, e.getMessage());
log.error("Failed to synchronize Git data for external system: {}", externalSystemId, e);
throw e;
}
} catch (Exception e) {
log.error("Failed to create sync history for external system: {}", externalSystemId, e);
throw new BusinessException(REPOSITORY_SYNC_FAILED);
}
}
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void syncGroups(Long externalSystemId) { public void syncGroups(Long externalSystemId) {
@ -110,22 +72,22 @@ public class GitManagerServiceImpl implements IGitManagerService {
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void syncProjects(Long externalSystemId, Long groupId) { public void syncProjects(Long externalSystemId) {
try { try {
// 1. 创建同步历史记录 // 1. 创建同步历史记录
RepositorySyncHistoryDTO projectHistory = repositorySyncHistoryService.createSyncHistory(externalSystemId, RepositorySyncType.PROJECT); RepositorySyncHistoryDTO projectHistory = repositorySyncHistoryService.createSyncHistory(externalSystemId, RepositorySyncType.PROJECT);
try { try {
// 2. 同步项目 // 2. 同步项目
Integer projectCount = repositoryProjectService.syncProjects(externalSystemId, groupId); Integer projectCount = repositoryProjectService.syncProjects(externalSystemId);
// 3. 更新同步历史记录为成功 // 3. 更新同步历史记录为成功
repositorySyncHistoryService.updateSyncHistory(projectHistory.getId(), ExternalSystemSyncStatus.SUCCESS, null); repositorySyncHistoryService.updateSyncHistory(projectHistory.getId(), ExternalSystemSyncStatus.SUCCESS, null);
log.info("Successfully synchronized {} projects for group {} in external system: {}", projectCount, groupId, externalSystemId); log.info("Successfully synchronized {} projects in external system: {}", projectCount, externalSystemId);
} catch (Exception e) { } catch (Exception e) {
// 4. 更新同步历史记录为失败 // 4. 更新同步历史记录为失败
repositorySyncHistoryService.updateSyncHistory(projectHistory.getId(), ExternalSystemSyncStatus.FAILED, e.getMessage()); repositorySyncHistoryService.updateSyncHistory(projectHistory.getId(), ExternalSystemSyncStatus.FAILED, e.getMessage());
log.error("Failed to synchronize projects for group {} in external system: {}", groupId, externalSystemId, e); log.error("Failed to synchronize projects in external system: {}", externalSystemId, e);
throw e; throw e;
} }
} catch (Exception e) { } catch (Exception e) {
@ -136,22 +98,22 @@ public class GitManagerServiceImpl implements IGitManagerService {
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void syncBranches(Long externalSystemId, Long projectId) { public void syncBranches(Long externalSystemId) {
try { try {
// 1. 创建同步历史记录 // 1. 创建同步历史记录
RepositorySyncHistoryDTO branchHistory = repositorySyncHistoryService.createSyncHistory(externalSystemId, RepositorySyncType.BRANCH); RepositorySyncHistoryDTO branchHistory = repositorySyncHistoryService.createSyncHistory(externalSystemId, RepositorySyncType.BRANCH);
try { try {
// 2. 同步分支 // 2. 同步分支
Integer branchCount = repositoryBranchService.syncBranches(externalSystemId, projectId); Integer branchCount = repositoryBranchService.syncBranches(externalSystemId);
// 3. 更新同步历史记录为成功 // 3. 更新同步历史记录为成功
repositorySyncHistoryService.updateSyncHistory(branchHistory.getId(), ExternalSystemSyncStatus.SUCCESS, null); repositorySyncHistoryService.updateSyncHistory(branchHistory.getId(), ExternalSystemSyncStatus.SUCCESS, null);
log.info("Successfully synchronized {} branches for project {} in external system: {}", branchCount, projectId, externalSystemId); log.info("Successfully synchronized {} branches for external system: {}", branchCount, externalSystemId);
} catch (Exception e) { } catch (Exception e) {
// 4. 更新同步历史记录为失败 // 4. 更新同步历史记录为失败
repositorySyncHistoryService.updateSyncHistory(branchHistory.getId(), ExternalSystemSyncStatus.FAILED, e.getMessage()); repositorySyncHistoryService.updateSyncHistory(branchHistory.getId(), ExternalSystemSyncStatus.FAILED, e.getMessage());
log.error("Failed to synchronize branches for project {} in external system: {}", projectId, externalSystemId, e); log.error("Failed to synchronize branches for external system: {}", externalSystemId, e);
throw e; throw e;
} }
} catch (Exception e) { } catch (Exception e) {

View File

@ -46,43 +46,44 @@ public class RepositoryBranchServiceImpl extends BaseServiceImpl<RepositoryBranc
@Override @Override
@Transactional @Transactional
public Integer syncBranches(Long externalSystemId, Long projectId) { public Integer syncBranches(Long externalSystemId) {
try { // try {
// 1. 获取外部系统信息 // // 1. 获取外部系统信息
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. 获取项目信息 // // 2. 获取项目信息
RepositoryProject project = repositoryProjectRepository.findById(projectId) // RepositoryProject project = repositoryProjectRepository.findById(projectId)
.orElseThrow(() -> new BusinessException(ResponseCode.REPOSITORY_PROJECT_NOT_FOUND)); // .orElseThrow(() -> new BusinessException(ResponseCode.REPOSITORY_PROJECT_NOT_FOUND));
//
// 3. 获取远程仓库分支信息 // // 3. 获取远程仓库分支信息
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 in remote git system: {}, project: {}", externalSystem.getName(), project.getName()); // log.info("No branches found in remote git system: {}, project: {}", externalSystem.getName(), project.getName());
return 0; // return 0;
} // }
//
// 4. 获取本地已存在的仓库分支 // // 4. 获取本地已存在的仓库分支
List<RepositoryBranch> existingBranches = repositoryBranchRepository.findByExternalSystemIdAndProjectId(externalSystemId, projectId); // List<RepositoryBranch> existingBranches = repositoryBranchRepository.findByExternalSystemIdAndProjectId(externalSystemId, projectId);
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()));
//
// 5. 更新或创建仓库分支 // // 5. 更新或创建仓库分支
List<RepositoryBranch> branchesToSave = remoteBranches.stream() // List<RepositoryBranch> branchesToSave = remoteBranches.stream()
.map(remoteBranch -> updateOrCreateBranch(externalSystemId, projectId, remoteBranch, existingBranchMap)) // .map(remoteBranch -> updateOrCreateBranch(externalSystemId, projectId, remoteBranch, existingBranchMap))
.collect(Collectors.toList()); // .collect(Collectors.toList());
//
// 6. 保存仓库分支 // // 6. 保存仓库分支
repositoryBranchRepository.saveAll(branchesToSave); // repositoryBranchRepository.saveAll(branchesToSave);
//
log.info("Successfully synchronized {} branches for external system: {}, project: {}", // log.info("Successfully synchronized {} branches for external system: {}, project: {}",
branchesToSave.size(), externalSystem.getName(), project.getName()); // branchesToSave.size(), externalSystem.getName(), project.getName());
return branchesToSave.size(); // return branchesToSave.size();
} catch (Exception e) { // } catch (Exception e) {
log.error("Failed to sync repository branches for external system: {}, project: {}", externalSystemId, projectId, e); // log.error("Failed to sync repository branches for external system: {}, project: {}", externalSystemId, projectId, e);
throw new BusinessException(ResponseCode.REPOSITORY_SYNC_FAILED); // throw new BusinessException(ResponseCode.REPOSITORY_SYNC_FAILED);
} // }
return null;
} }
private RepositoryBranch updateOrCreateBranch( private RepositoryBranch updateOrCreateBranch(

View File

@ -49,70 +49,54 @@ public class RepositoryProjectServiceImpl extends BaseServiceImpl<RepositoryProj
@Override @Override
@Transactional @Transactional
public Integer syncProjects(Long externalSystemId, Long groupId) { public Integer syncProjects(Long externalSystemId) {
try { try {
// 1. 获取外部系统信息 // 1. 获取外部系统信息
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. 获取仓库组信息 // 2. 获取远程仓库项目信息
RepositoryGroup group = repositoryGroupRepository.findById(groupId) List<GitProjectResponse> remoteProjects = gitServiceIntegration.projects(externalSystem);
.orElseThrow(() -> new BusinessException(ResponseCode.REPOSITORY_GROUP_NOT_FOUND));
// 3. 获取远程仓库项目信息
List<GitProjectResponse> remoteProjects = gitServiceIntegration.projects(externalSystem, group.getGroupId());
if (remoteProjects.isEmpty()) { if (remoteProjects.isEmpty()) {
log.info("No projects found in remote git system: {}, group: {}", externalSystem.getName(), group.getName()); log.info("No projects found in remote git system: {}", externalSystem.getName());
return 0; return 0;
} }
// 4. 获取本地已存在的仓库项目 // 3. 清空当前系统中该外部系统的所有项目
List<RepositoryProject> existingProjects = repositoryProjectRepository.findByExternalSystemIdAndGroupId(externalSystemId, groupId); repositoryProjectRepository.deleteByExternalSystemId(externalSystemId);
Map<Long, RepositoryProject> existingProjectMap = existingProjects.stream() repositoryProjectRepository.flush();
.collect(Collectors.toMap(RepositoryProject::getProjectId, Function.identity()));
// 5. 更新或创建仓库项目 // 4. 批量保存从Git API获取的项目到数据库
List<RepositoryProject> projectsToSave = remoteProjects.stream() List<RepositoryProject> projectsToSave = remoteProjects.stream()
.map(remoteProject -> updateOrCreateProject(externalSystemId, groupId, remoteProject, existingProjectMap)) .map(remoteProject -> {
RepositoryProject project = new RepositoryProject();
project.setExternalSystemId(externalSystemId);
project.setProjectId(remoteProject.getId());
project.setName(remoteProject.getName());
project.setPath(remoteProject.getPath());
project.setDescription(remoteProject.getDescription());
project.setVisibility(remoteProject.getVisibility());
project.setIsDefaultBranch(remoteProject.getDefaultBranch());
project.setWebUrl(remoteProject.getWebUrl());
project.setSshUrl(remoteProject.getSshUrl());
project.setHttpUrl(remoteProject.getHttpUrl());
project.setLastActivityAt(remoteProject.getLastActivityAt());
return project;
})
.collect(Collectors.toList()); .collect(Collectors.toList());
// 6. 保存仓库项目 List<RepositoryProject> savedProjects = repositoryProjectRepository.saveAll(projectsToSave);
repositoryProjectRepository.saveAll(projectsToSave);
log.info("Successfully synchronized {} projects for external system: {}",
savedProjects.size(), externalSystem.getName());
return savedProjects.size();
log.info("Successfully synchronized {} projects for external system: {}, group: {}",
projectsToSave.size(), externalSystem.getName(), group.getName());
return projectsToSave.size();
} catch (Exception e) { } catch (Exception e) {
log.error("Failed to sync repository projects for external system: {}, group: {}", externalSystemId, groupId, e); log.error("Failed to sync repository projects for external system: {}", externalSystemId, e);
throw new BusinessException(ResponseCode.REPOSITORY_SYNC_FAILED); throw new BusinessException(ResponseCode.REPOSITORY_SYNC_FAILED);
} }
} }
private RepositoryProject updateOrCreateProject(
Long externalSystemId,
Long groupId,
GitProjectResponse remoteProject,
Map<Long, RepositoryProject> existingProjectMap) {
RepositoryProject project = existingProjectMap.getOrDefault(remoteProject.getId(), new RepositoryProject());
// 更新基本信息
project.setExternalSystemId(externalSystemId);
project.setGroupId(groupId);
project.setProjectId(remoteProject.getId());
project.setName(remoteProject.getName());
project.setPath(remoteProject.getPath());
project.setDescription(remoteProject.getDescription());
project.setVisibility(remoteProject.getVisibility());
project.setIsDefaultBranch(remoteProject.getDefaultBranch());
project.setWebUrl(remoteProject.getWebUrl());
project.setSshUrl(remoteProject.getSshUrl());
project.setHttpUrl(remoteProject.getHttpUrl());
project.setLastActivityAt(remoteProject.getLastActivityAt());
return project;
}
@Override @Override
public Long countByExternalSystemId(Long externalSystemId) { public Long countByExternalSystemId(Long externalSystemId) {
return repositoryProjectRepository.countByExternalSystemId(externalSystemId); return repositoryProjectRepository.countByExternalSystemId(externalSystemId);

View File

@ -298,15 +298,8 @@ CREATE TABLE deploy_repo_group
avatar_url VARCHAR(255) NULL COMMENT '头像URL', avatar_url VARCHAR(255) NULL COMMENT '头像URL',
web_url VARCHAR(255) NULL COMMENT '网页URL', web_url VARCHAR(255) NULL COMMENT '网页URL',
visibility ENUM('private', 'internal', 'public') NOT NULL DEFAULT 'private' COMMENT '可见性private-私有internal-内部public-公开', visibility ENUM('private', 'internal', 'public') NOT NULL DEFAULT 'private' COMMENT '可见性private-私有internal-内部public-公开',
sort INT DEFAULT 0 COMMENT '排序号', sort INT DEFAULT 0 COMMENT '排序号'
-- 索引
INDEX idx_external_system_group_id (external_system_id, group_id) COMMENT '外部系统组ID索引',
INDEX idx_parent_id (parent_id) COMMENT '父级ID索引',
INDEX idx_path ( PATH) COMMENT '路径索引',
-- 外键约束
CONSTRAINT fk_group_external_system FOREIGN KEY (external_system_id)REFERENCES sys_external_system (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='代码仓库组表'; ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='代码仓库组表';
-- 代码仓库项目表 -- 代码仓库项目表