大声道撒旦
This commit is contained in:
parent
39e40969b6
commit
2d90609edf
@ -25,34 +25,6 @@ import java.util.List;
|
||||
@Tag(name = "Git仓库分支管理", description = "Git仓库分支管理相关接口")
|
||||
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
|
||||
protected void exportData(HttpServletResponse response, List<RepositoryBranchDTO> data) {
|
||||
|
||||
|
||||
@ -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.dto.RepositoryGroupDTO;
|
||||
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 io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.annotation.Resource;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
@ -25,24 +20,6 @@ import java.util.List;
|
||||
@Tag(name = "Git仓库组管理", description = "Git仓库组管理相关接口")
|
||||
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
|
||||
protected void exportData(HttpServletResponse response, List<RepositoryGroupDTO> data) {
|
||||
|
||||
@ -22,15 +22,6 @@ public class RepositoryManagerApiController {
|
||||
@Resource
|
||||
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 = "同步指定外部系统的所有仓库组")
|
||||
@PostMapping("/{externalSystemId}/sync-groups")
|
||||
public Response<Void> syncGroups(
|
||||
@ -43,20 +34,18 @@ public class RepositoryManagerApiController {
|
||||
@Operation(summary = "同步Git项目", description = "同步指定外部系统下指定仓库组的所有项目")
|
||||
@PostMapping("/{externalSystemId}/groups/{groupId}/sync-projects")
|
||||
public Response<Void> syncProjects(
|
||||
@Parameter(description = "外部系统ID", required = true) @PathVariable Long externalSystemId,
|
||||
@Parameter(description = "仓库组ID", required = true) @PathVariable Long groupId
|
||||
@Parameter(description = "外部系统ID", required = true) @PathVariable Long externalSystemId
|
||||
) {
|
||||
gitManagerService.syncProjects(externalSystemId, groupId);
|
||||
gitManagerService.syncProjects(externalSystemId);
|
||||
return Response.success();
|
||||
}
|
||||
|
||||
@Operation(summary = "同步Git分支", description = "同步指定外部系统下指定项目的所有分支")
|
||||
@PostMapping("/{externalSystemId}/projects/{projectId}/sync-branches")
|
||||
public Response<Void> syncBranches(
|
||||
@Parameter(description = "外部系统ID", required = true) @PathVariable Long externalSystemId,
|
||||
@Parameter(description = "项目ID", required = true) @PathVariable Long projectId
|
||||
@Parameter(description = "外部系统ID", required = true) @PathVariable Long externalSystemId
|
||||
) {
|
||||
gitManagerService.syncBranches(externalSystemId, projectId);
|
||||
gitManagerService.syncBranches(externalSystemId);
|
||||
return Response.success();
|
||||
}
|
||||
|
||||
|
||||
@ -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.dto.RepositoryProjectDTO;
|
||||
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 io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.annotation.Resource;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
@ -25,26 +20,6 @@ import java.util.List;
|
||||
@Tag(name = "Git仓库项目管理", description = "Git仓库项目管理相关接口")
|
||||
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
|
||||
protected void exportData(HttpServletResponse response, List<RepositoryProjectDTO> data) {
|
||||
|
||||
|
||||
@ -23,13 +23,12 @@ public interface IGitServiceIntegration extends IExternalSystemIntegration {
|
||||
List<GitGroupResponse> groups(ExternalSystem system);
|
||||
|
||||
/**
|
||||
* 获取指定组下的所有项目
|
||||
* 获取所有项目
|
||||
*
|
||||
* @param system 外部系统配置
|
||||
* @param groupId 组ID
|
||||
* @return 项目列表
|
||||
*/
|
||||
List<GitProjectResponse> projects(ExternalSystem system, Long groupId);
|
||||
List<GitProjectResponse> projects(ExternalSystem system);
|
||||
|
||||
/**
|
||||
* 获取指定项目的所有分支
|
||||
|
||||
@ -69,9 +69,9 @@ public class GitServiceIntegrationImpl implements IGitServiceIntegration {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<GitProjectResponse> projects(ExternalSystem system, Long groupId) {
|
||||
public List<GitProjectResponse> projects(ExternalSystem system) {
|
||||
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);
|
||||
HttpEntity<String> entity = new HttpEntity<>(headers);
|
||||
|
||||
@ -84,7 +84,7 @@ public class GitServiceIntegrationImpl implements IGitServiceIntegration {
|
||||
|
||||
return response.getBody() != null ? response.getBody() : Collections.emptyList();
|
||||
} 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();
|
||||
}
|
||||
}
|
||||
|
||||
@ -28,4 +28,6 @@ public interface IRepositoryProjectRepository extends IBaseRepository<Repository
|
||||
Long countByExternalSystemId(Long externalSystemId);
|
||||
|
||||
List<RepositoryProject> findByExternalSystemId(Long externalSystemId);
|
||||
|
||||
void deleteByExternalSystemId(Long externalSystemId);
|
||||
}
|
||||
@ -7,13 +7,6 @@ import com.qqchen.deploy.backend.deploy.dto.GitInstanceDTO;
|
||||
*/
|
||||
public interface IGitManagerService {
|
||||
|
||||
/**
|
||||
* 同步Git所有数据(组、项目、分支)
|
||||
*
|
||||
* @param externalSystemId 外部系统ID
|
||||
*/
|
||||
void syncAll(Long externalSystemId);
|
||||
|
||||
/**
|
||||
* 同步Git组
|
||||
*
|
||||
@ -25,17 +18,15 @@ public interface IGitManagerService {
|
||||
* 同步Git项目
|
||||
*
|
||||
* @param externalSystemId 外部系统ID
|
||||
* @param groupId 组ID
|
||||
*/
|
||||
void syncProjects(Long externalSystemId, Long groupId);
|
||||
void syncProjects(Long externalSystemId);
|
||||
|
||||
/**
|
||||
* 同步Git分支
|
||||
*
|
||||
* @param externalSystemId 外部系统ID
|
||||
* @param projectId 项目ID
|
||||
*/
|
||||
void syncBranches(Long externalSystemId, Long projectId);
|
||||
void syncBranches(Long externalSystemId);
|
||||
|
||||
/**
|
||||
* 获取Git实例信息
|
||||
|
||||
@ -14,10 +14,9 @@ public interface IRepositoryBranchService extends IBaseService<RepositoryBranch,
|
||||
* 同步指定外部系统下指定项目的分支
|
||||
*
|
||||
* @param externalSystemId 外部系统ID
|
||||
* @param projectId 项目ID
|
||||
* @return 同步的分支数量
|
||||
*/
|
||||
Integer syncBranches(Long externalSystemId, Long projectId);
|
||||
Integer syncBranches(Long externalSystemId);
|
||||
|
||||
/**
|
||||
* 统计指定外部系统下的分支数量
|
||||
|
||||
@ -16,10 +16,9 @@ public interface IRepositoryProjectService extends IBaseService<RepositoryProjec
|
||||
* 同步指定外部系统下指定仓库组的项目
|
||||
*
|
||||
* @param externalSystemId 外部系统ID
|
||||
* @param groupId 仓库组ID
|
||||
* @return 同步的项目数量
|
||||
*/
|
||||
Integer syncProjects(Long externalSystemId, Long groupId);
|
||||
Integer syncProjects(Long externalSystemId);
|
||||
|
||||
/**
|
||||
* 统计指定外部系统下的项目数量
|
||||
|
||||
@ -44,44 +44,6 @@ public class GitManagerServiceImpl implements IGitManagerService {
|
||||
@Resource
|
||||
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
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void syncGroups(Long externalSystemId) {
|
||||
@ -110,22 +72,22 @@ public class GitManagerServiceImpl implements IGitManagerService {
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void syncProjects(Long externalSystemId, Long groupId) {
|
||||
public void syncProjects(Long externalSystemId) {
|
||||
try {
|
||||
// 1. 创建同步历史记录
|
||||
RepositorySyncHistoryDTO projectHistory = repositorySyncHistoryService.createSyncHistory(externalSystemId, RepositorySyncType.PROJECT);
|
||||
|
||||
try {
|
||||
// 2. 同步项目
|
||||
Integer projectCount = repositoryProjectService.syncProjects(externalSystemId, groupId);
|
||||
Integer projectCount = repositoryProjectService.syncProjects(externalSystemId);
|
||||
|
||||
// 3. 更新同步历史记录为成功
|
||||
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) {
|
||||
// 4. 更新同步历史记录为失败
|
||||
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;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
@ -136,22 +98,22 @@ public class GitManagerServiceImpl implements IGitManagerService {
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void syncBranches(Long externalSystemId, Long projectId) {
|
||||
public void syncBranches(Long externalSystemId) {
|
||||
try {
|
||||
// 1. 创建同步历史记录
|
||||
RepositorySyncHistoryDTO branchHistory = repositorySyncHistoryService.createSyncHistory(externalSystemId, RepositorySyncType.BRANCH);
|
||||
|
||||
try {
|
||||
// 2. 同步分支
|
||||
Integer branchCount = repositoryBranchService.syncBranches(externalSystemId, projectId);
|
||||
Integer branchCount = repositoryBranchService.syncBranches(externalSystemId);
|
||||
|
||||
// 3. 更新同步历史记录为成功
|
||||
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) {
|
||||
// 4. 更新同步历史记录为失败
|
||||
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;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
|
||||
@ -46,43 +46,44 @@ public class RepositoryBranchServiceImpl extends BaseServiceImpl<RepositoryBranc
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public Integer syncBranches(Long externalSystemId, Long projectId) {
|
||||
try {
|
||||
// 1. 获取外部系统信息
|
||||
ExternalSystem externalSystem = externalSystemRepository.findById(externalSystemId)
|
||||
.orElseThrow(() -> new BusinessException(ResponseCode.EXTERNAL_SYSTEM_NOT_FOUND));
|
||||
|
||||
// 2. 获取项目信息
|
||||
RepositoryProject project = repositoryProjectRepository.findById(projectId)
|
||||
.orElseThrow(() -> new BusinessException(ResponseCode.REPOSITORY_PROJECT_NOT_FOUND));
|
||||
|
||||
// 3. 获取远程仓库分支信息
|
||||
List<GitBranchResponse> remoteBranches = gitServiceIntegration.branches(externalSystem, project.getProjectId());
|
||||
if (remoteBranches.isEmpty()) {
|
||||
log.info("No branches found in remote git system: {}, project: {}", externalSystem.getName(), project.getName());
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 4. 获取本地已存在的仓库分支
|
||||
List<RepositoryBranch> existingBranches = repositoryBranchRepository.findByExternalSystemIdAndProjectId(externalSystemId, projectId);
|
||||
Map<String, RepositoryBranch> existingBranchMap = existingBranches.stream()
|
||||
.collect(Collectors.toMap(RepositoryBranch::getName, Function.identity()));
|
||||
|
||||
// 5. 更新或创建仓库分支
|
||||
List<RepositoryBranch> branchesToSave = remoteBranches.stream()
|
||||
.map(remoteBranch -> updateOrCreateBranch(externalSystemId, projectId, remoteBranch, existingBranchMap))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// 6. 保存仓库分支
|
||||
repositoryBranchRepository.saveAll(branchesToSave);
|
||||
|
||||
log.info("Successfully synchronized {} branches for external system: {}, project: {}",
|
||||
branchesToSave.size(), externalSystem.getName(), project.getName());
|
||||
return branchesToSave.size();
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to sync repository branches for external system: {}, project: {}", externalSystemId, projectId, e);
|
||||
throw new BusinessException(ResponseCode.REPOSITORY_SYNC_FAILED);
|
||||
}
|
||||
public Integer syncBranches(Long externalSystemId) {
|
||||
// try {
|
||||
// // 1. 获取外部系统信息
|
||||
// ExternalSystem externalSystem = externalSystemRepository.findById(externalSystemId)
|
||||
// .orElseThrow(() -> new BusinessException(ResponseCode.EXTERNAL_SYSTEM_NOT_FOUND));
|
||||
//
|
||||
// // 2. 获取项目信息
|
||||
// RepositoryProject project = repositoryProjectRepository.findById(projectId)
|
||||
// .orElseThrow(() -> new BusinessException(ResponseCode.REPOSITORY_PROJECT_NOT_FOUND));
|
||||
//
|
||||
// // 3. 获取远程仓库分支信息
|
||||
// List<GitBranchResponse> remoteBranches = gitServiceIntegration.branches(externalSystem, project.getProjectId());
|
||||
// if (remoteBranches.isEmpty()) {
|
||||
// log.info("No branches found in remote git system: {}, project: {}", externalSystem.getName(), project.getName());
|
||||
// return 0;
|
||||
// }
|
||||
//
|
||||
// // 4. 获取本地已存在的仓库分支
|
||||
// List<RepositoryBranch> existingBranches = repositoryBranchRepository.findByExternalSystemIdAndProjectId(externalSystemId, projectId);
|
||||
// Map<String, RepositoryBranch> existingBranchMap = existingBranches.stream()
|
||||
// .collect(Collectors.toMap(RepositoryBranch::getName, Function.identity()));
|
||||
//
|
||||
// // 5. 更新或创建仓库分支
|
||||
// List<RepositoryBranch> branchesToSave = remoteBranches.stream()
|
||||
// .map(remoteBranch -> updateOrCreateBranch(externalSystemId, projectId, remoteBranch, existingBranchMap))
|
||||
// .collect(Collectors.toList());
|
||||
//
|
||||
// // 6. 保存仓库分支
|
||||
// repositoryBranchRepository.saveAll(branchesToSave);
|
||||
//
|
||||
// log.info("Successfully synchronized {} branches for external system: {}, project: {}",
|
||||
// branchesToSave.size(), externalSystem.getName(), project.getName());
|
||||
// return branchesToSave.size();
|
||||
// } catch (Exception e) {
|
||||
// log.error("Failed to sync repository branches for external system: {}, project: {}", externalSystemId, projectId, e);
|
||||
// throw new BusinessException(ResponseCode.REPOSITORY_SYNC_FAILED);
|
||||
// }
|
||||
return null;
|
||||
}
|
||||
|
||||
private RepositoryBranch updateOrCreateBranch(
|
||||
|
||||
@ -49,56 +49,28 @@ public class RepositoryProjectServiceImpl extends BaseServiceImpl<RepositoryProj
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public Integer syncProjects(Long externalSystemId, Long groupId) {
|
||||
public Integer syncProjects(Long externalSystemId) {
|
||||
try {
|
||||
// 1. 获取外部系统信息
|
||||
ExternalSystem externalSystem = externalSystemRepository.findById(externalSystemId)
|
||||
.orElseThrow(() -> new BusinessException(ResponseCode.EXTERNAL_SYSTEM_NOT_FOUND));
|
||||
|
||||
// 2. 获取仓库组信息
|
||||
RepositoryGroup group = repositoryGroupRepository.findById(groupId)
|
||||
.orElseThrow(() -> new BusinessException(ResponseCode.REPOSITORY_GROUP_NOT_FOUND));
|
||||
|
||||
// 3. 获取远程仓库项目信息
|
||||
List<GitProjectResponse> remoteProjects = gitServiceIntegration.projects(externalSystem, group.getGroupId());
|
||||
// 2. 获取远程仓库项目信息
|
||||
List<GitProjectResponse> remoteProjects = gitServiceIntegration.projects(externalSystem);
|
||||
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;
|
||||
}
|
||||
|
||||
// 4. 获取本地已存在的仓库项目
|
||||
List<RepositoryProject> existingProjects = repositoryProjectRepository.findByExternalSystemIdAndGroupId(externalSystemId, groupId);
|
||||
Map<Long, RepositoryProject> existingProjectMap = existingProjects.stream()
|
||||
.collect(Collectors.toMap(RepositoryProject::getProjectId, Function.identity()));
|
||||
// 3. 清空当前系统中该外部系统的所有项目
|
||||
repositoryProjectRepository.deleteByExternalSystemId(externalSystemId);
|
||||
repositoryProjectRepository.flush();
|
||||
|
||||
// 5. 更新或创建仓库项目
|
||||
// 4. 批量保存从Git API获取的项目到数据库
|
||||
List<RepositoryProject> projectsToSave = remoteProjects.stream()
|
||||
.map(remoteProject -> updateOrCreateProject(externalSystemId, groupId, remoteProject, existingProjectMap))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// 6. 保存仓库项目
|
||||
repositoryProjectRepository.saveAll(projectsToSave);
|
||||
|
||||
log.info("Successfully synchronized {} projects for external system: {}, group: {}",
|
||||
projectsToSave.size(), externalSystem.getName(), group.getName());
|
||||
return projectsToSave.size();
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to sync repository projects for external system: {}, group: {}", externalSystemId, groupId, e);
|
||||
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());
|
||||
|
||||
// 更新基本信息
|
||||
.map(remoteProject -> {
|
||||
RepositoryProject project = new RepositoryProject();
|
||||
project.setExternalSystemId(externalSystemId);
|
||||
project.setGroupId(groupId);
|
||||
project.setProjectId(remoteProject.getId());
|
||||
project.setName(remoteProject.getName());
|
||||
project.setPath(remoteProject.getPath());
|
||||
@ -109,8 +81,20 @@ public class RepositoryProjectServiceImpl extends BaseServiceImpl<RepositoryProj
|
||||
project.setSshUrl(remoteProject.getSshUrl());
|
||||
project.setHttpUrl(remoteProject.getHttpUrl());
|
||||
project.setLastActivityAt(remoteProject.getLastActivityAt());
|
||||
|
||||
return project;
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
|
||||
List<RepositoryProject> savedProjects = repositoryProjectRepository.saveAll(projectsToSave);
|
||||
|
||||
log.info("Successfully synchronized {} projects for external system: {}",
|
||||
savedProjects.size(), externalSystem.getName());
|
||||
return savedProjects.size();
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to sync repository projects for external system: {}", externalSystemId, e);
|
||||
throw new BusinessException(ResponseCode.REPOSITORY_SYNC_FAILED);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -298,15 +298,8 @@ CREATE TABLE deploy_repo_group
|
||||
avatar_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-公开',
|
||||
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='代码仓库组表';
|
||||
|
||||
-- 代码仓库项目表
|
||||
|
||||
Loading…
Reference in New Issue
Block a user