单独同步GIT

This commit is contained in:
dengqichen 2025-10-29 16:42:27 +08:00
parent e89f7a90e8
commit 58491de2f0
10 changed files with 61 additions and 68 deletions

View File

@ -28,25 +28,24 @@ public class RepositoryBranchApiController extends BaseController<RepositoryBran
@Resource @Resource
private IRepositoryBranchService repositoryBranchService; private IRepositoryBranchService repositoryBranchService;
@Operation(summary = "同步Git分支", description = "支持三种同步模式1)只传externalSystemId-全量同步 2)传externalSystemId+repoGroupId-同步仓库组 3)传externalSystemId+repoGroupId+repoProjectId-同步单个项目") @Operation(summary = "同步Git分支", description = "异步同步,支持三种模式1)只传externalSystemId-全量同步 2)传externalSystemId+repoGroupId-同步仓库组 3)传externalSystemId+repoGroupId+repoProjectId-同步单个项目")
@PostMapping("/sync") @PostMapping("/sync")
public Response<Integer> sync( public Response<Void> sync(
@Parameter(description = "外部系统ID", required = true) @RequestParam Long externalSystemId, @Parameter(description = "外部系统ID", required = true) @RequestParam Long externalSystemId,
@Parameter(description = "仓库组ID可选", required = false) @RequestParam(required = false) Long repoGroupId, @Parameter(description = "仓库组ID可选", required = false) @RequestParam(required = false) Long repoGroupId,
@Parameter(description = "仓库项目ID可选传此参数时repoGroupId必传", required = false) @RequestParam(required = false) Long repoProjectId @Parameter(description = "仓库项目ID可选传此参数时repoGroupId必传", required = false) @RequestParam(required = false) Long repoProjectId
) { ) {
Integer count;
if (repoProjectId != null) { if (repoProjectId != null) {
// 同步单个项目 // 同步单个项目
count = repositoryBranchService.syncBranches(externalSystemId, repoGroupId, repoProjectId); repositoryBranchService.syncBranches(externalSystemId, repoGroupId, repoProjectId);
} else if (repoGroupId != null) { } else if (repoGroupId != null) {
// 同步仓库组 // 同步仓库组
count = repositoryBranchService.syncBranches(externalSystemId, repoGroupId); repositoryBranchService.syncBranches(externalSystemId, repoGroupId);
} else { } else {
// 全量同步 // 全量同步
count = repositoryBranchService.syncBranches(externalSystemId); repositoryBranchService.syncBranches(externalSystemId);
} }
return Response.success(count); return Response.success();
} }
@Override @Override

View File

@ -28,13 +28,13 @@ public class RepositoryGroupApiController extends BaseController<RepositoryGroup
@Resource @Resource
private IRepositoryGroupService repositoryGroupService; private IRepositoryGroupService repositoryGroupService;
@Operation(summary = "同步Git仓库组", description = "同步指定外部系统的所有仓库组") @Operation(summary = "同步Git仓库组", description = "异步同步指定外部系统的所有仓库组,立即返回")
@PostMapping("/sync") @PostMapping("/sync")
public Response<Integer> sync( public Response<Void> sync(
@Parameter(description = "外部系统ID", required = true) @RequestParam Long externalSystemId @Parameter(description = "外部系统ID", required = true) @RequestParam Long externalSystemId
) { ) {
Integer count = repositoryGroupService.syncGroups(externalSystemId); repositoryGroupService.syncGroups(externalSystemId);
return Response.success(count); return Response.success();
} }
@Override @Override

View File

@ -28,21 +28,20 @@ public class RepositoryProjectApiController extends BaseController<RepositoryPro
@Resource @Resource
private IRepositoryProjectService repositoryProjectService; private IRepositoryProjectService repositoryProjectService;
@Operation(summary = "同步Git项目", description = "支持两种同步模式1)只传externalSystemId-全量同步 2)传externalSystemId+repoGroupId-同步单个仓库组") @Operation(summary = "同步Git项目", description = "异步同步,支持两种模式1)只传externalSystemId-全量同步 2)传externalSystemId+repoGroupId-同步单个仓库组")
@PostMapping("/sync") @PostMapping("/sync")
public Response<Integer> sync( public Response<Void> sync(
@Parameter(description = "外部系统ID", required = true) @RequestParam Long externalSystemId, @Parameter(description = "外部系统ID", required = true) @RequestParam Long externalSystemId,
@Parameter(description = "仓库组ID可选", required = false) @RequestParam(required = false) Long repoGroupId @Parameter(description = "仓库组ID可选", required = false) @RequestParam(required = false) Long repoGroupId
) { ) {
Integer count;
if (repoGroupId != null) { if (repoGroupId != null) {
// 同步单个仓库组 // 同步单个仓库组
count = repositoryProjectService.syncProjects(externalSystemId, repoGroupId); repositoryProjectService.syncProjects(externalSystemId, repoGroupId);
} else { } else {
// 全量同步 // 全量同步
count = repositoryProjectService.syncProjects(externalSystemId); repositoryProjectService.syncProjects(externalSystemId);
} }
return Response.success(count); return Response.success();
} }
@Override @Override

View File

@ -11,31 +11,28 @@ import com.qqchen.deploy.backend.framework.service.IBaseService;
public interface IRepositoryBranchService extends IBaseService<RepositoryBranch, RepositoryBranchDTO, RepositoryBranchQuery, Long> { public interface IRepositoryBranchService extends IBaseService<RepositoryBranch, RepositoryBranchDTO, RepositoryBranchQuery, Long> {
/** /**
* 同步指定外部系统下的所有分支全量同步 * 同步指定外部系统下的所有分支异步执行全量同步
* *
* @param externalSystemId 外部系统ID * @param externalSystemId 外部系统ID
* @return 同步的分支数量
*/ */
Integer syncBranches(Long externalSystemId); void syncBranches(Long externalSystemId);
/** /**
* 同步指定仓库组下所有项目的分支 * 同步指定仓库组下所有项目的分支异步执行
* *
* @param externalSystemId 外部系统ID * @param externalSystemId 外部系统ID
* @param repoGroupId 仓库组ID * @param repoGroupId 仓库组ID
* @return 同步的分支数量
*/ */
Integer syncBranches(Long externalSystemId, Long repoGroupId); void syncBranches(Long externalSystemId, Long repoGroupId);
/** /**
* 同步指定项目的分支 * 同步指定项目的分支异步执行
* *
* @param externalSystemId 外部系统ID * @param externalSystemId 外部系统ID
* @param repoGroupId 仓库组ID * @param repoGroupId 仓库组ID
* @param repoProjectId 仓库项目ID * @param repoProjectId 仓库项目ID
* @return 同步的分支数量
*/ */
Integer syncBranches(Long externalSystemId, Long repoGroupId, Long repoProjectId); void syncBranches(Long externalSystemId, Long repoGroupId, Long repoProjectId);
/** /**
* 统计指定外部系统下的分支数量 * 统计指定外部系统下的分支数量

View File

@ -13,12 +13,11 @@ import java.util.List;
public interface IRepositoryGroupService extends IBaseService<RepositoryGroup, RepositoryGroupDTO, RepositoryGroupQuery, Long> { public interface IRepositoryGroupService extends IBaseService<RepositoryGroup, RepositoryGroupDTO, RepositoryGroupQuery, Long> {
/** /**
* 同步指定外部系统的所有仓库组包含同步历史记录 * 同步指定外部系统的所有仓库组异步执行包含同步历史记录
* *
* @param externalSystemId 外部系统ID * @param externalSystemId 外部系统ID
* @return 同步的仓库组数量
*/ */
Integer syncGroups(Long externalSystemId); void syncGroups(Long externalSystemId);
/** /**
* 统计外部系统的仓库组数量 * 统计外部系统的仓库组数量

View File

@ -13,21 +13,19 @@ import java.util.List;
public interface IRepositoryProjectService extends IBaseService<RepositoryProject, RepositoryProjectDTO, RepositoryProjectQuery, Long> { public interface IRepositoryProjectService extends IBaseService<RepositoryProject, RepositoryProjectDTO, RepositoryProjectQuery, Long> {
/** /**
* 同步指定外部系统下的所有项目全量同步 * 同步指定外部系统下的所有项目异步执行全量同步
* *
* @param externalSystemId 外部系统ID * @param externalSystemId 外部系统ID
* @return 同步的项目数量
*/ */
Integer syncProjects(Long externalSystemId); void syncProjects(Long externalSystemId);
/** /**
* 同步指定仓库组下的所有项目 * 同步指定仓库组下的所有项目异步执行
* *
* @param externalSystemId 外部系统ID * @param externalSystemId 外部系统ID
* @param repoGroupId 仓库组ID * @param repoGroupId 仓库组ID
* @return 同步的项目数量
*/ */
Integer syncProjects(Long externalSystemId, Long repoGroupId); void syncProjects(Long externalSystemId, Long repoGroupId);
/** /**
* 统计指定外部系统下的项目数量 * 统计指定外部系统下的项目数量

View File

@ -21,6 +21,7 @@ import com.qqchen.deploy.backend.deploy.service.IRepositorySyncHistoryService;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.orm.ObjectOptimisticLockingFailureException; import org.springframework.orm.ObjectOptimisticLockingFailureException;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@ -189,21 +190,24 @@ public class RepositoryBranchServiceImpl extends BaseServiceImpl<RepositoryBranc
} }
@Override @Override
@Async
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public Integer syncBranches(Long externalSystemId) { public void syncBranches(Long externalSystemId) {
return doSyncBranches(externalSystemId, null, null); doSyncBranches(externalSystemId, null, null);
} }
@Override @Override
@Async
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public Integer syncBranches(Long externalSystemId, Long repoGroupId) { public void syncBranches(Long externalSystemId, Long repoGroupId) {
return doSyncBranches(externalSystemId, repoGroupId, null); doSyncBranches(externalSystemId, repoGroupId, null);
} }
@Override @Override
@Async
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public Integer syncBranches(Long externalSystemId, Long repoGroupId, Long repoProjectId) { public void syncBranches(Long externalSystemId, Long repoGroupId, Long repoProjectId) {
return doSyncBranches(externalSystemId, repoGroupId, repoProjectId); doSyncBranches(externalSystemId, repoGroupId, repoProjectId);
} }
/** /**
@ -212,9 +216,8 @@ public class RepositoryBranchServiceImpl extends BaseServiceImpl<RepositoryBranc
* @param externalSystemId 外部系统ID * @param externalSystemId 外部系统ID
* @param repoGroupId 仓库组ID可选 * @param repoGroupId 仓库组ID可选
* @param repoProjectId 仓库项目ID可选 * @param repoProjectId 仓库项目ID可选
* @return 同步的分支数量
*/ */
private Integer doSyncBranches(Long externalSystemId, Long repoGroupId, Long repoProjectId) { private void doSyncBranches(Long externalSystemId, Long repoGroupId, Long repoProjectId) {
// 1. 创建同步历史记录 // 1. 创建同步历史记录
RepositorySyncHistoryDTO syncHistory = repositorySyncHistoryService.createSyncHistory(externalSystemId, RepositorySyncType.BRANCH); RepositorySyncHistoryDTO syncHistory = repositorySyncHistoryService.createSyncHistory(externalSystemId, RepositorySyncType.BRANCH);
@ -240,7 +243,7 @@ public class RepositoryBranchServiceImpl extends BaseServiceImpl<RepositoryBranc
if (projects.isEmpty()) { if (projects.isEmpty()) {
log.info("No projects found to sync"); log.info("No projects found to sync");
repositorySyncHistoryService.updateSyncHistory(syncHistory.getId(), ExternalSystemSyncStatus.SUCCESS, null); repositorySyncHistoryService.updateSyncHistory(syncHistory.getId(), ExternalSystemSyncStatus.SUCCESS, null);
return 0; return;
} }
log.info("Found {} projects to sync", projects.size()); log.info("Found {} projects to sync", projects.size());
@ -339,7 +342,7 @@ public class RepositoryBranchServiceImpl extends BaseServiceImpl<RepositoryBranc
log.info("Successfully synchronized {} branches for external system: {}", log.info("Successfully synchronized {} branches for external system: {}",
totalSyncedBranches.get(), externalSystem.getName()); totalSyncedBranches.get(), externalSystem.getName());
return totalSyncedBranches.get();
} catch (Exception e) { } catch (Exception e) {
// 9. 更新同步历史为失败 // 9. 更新同步历史为失败
log.error("Failed to sync branches", e); log.error("Failed to sync branches", e);

View File

@ -19,6 +19,7 @@ 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 jakarta.annotation.Resource; import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@ -52,8 +53,9 @@ public class RepositoryGroupServiceImpl extends BaseServiceImpl<RepositoryGroup,
private IRepositorySyncHistoryService repositorySyncHistoryService; private IRepositorySyncHistoryService repositorySyncHistoryService;
@Override @Override
@Async
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public Integer syncGroups(Long externalSystemId) { public void syncGroups(Long externalSystemId) {
// 1. 创建同步历史记录 // 1. 创建同步历史记录
RepositorySyncHistoryDTO groupHistory = repositorySyncHistoryService.createSyncHistory(externalSystemId, RepositorySyncType.GROUP); RepositorySyncHistoryDTO groupHistory = repositorySyncHistoryService.createSyncHistory(externalSystemId, RepositorySyncType.GROUP);
@ -67,7 +69,7 @@ public class RepositoryGroupServiceImpl extends BaseServiceImpl<RepositoryGroup,
if (remoteGroups.isEmpty()) { if (remoteGroups.isEmpty()) {
log.info("No groups found in remote git system: {}", externalSystem.getName()); log.info("No groups found in remote git system: {}", externalSystem.getName());
repositorySyncHistoryService.updateSyncHistory(groupHistory.getId(), ExternalSystemSyncStatus.SUCCESS, null); repositorySyncHistoryService.updateSyncHistory(groupHistory.getId(), ExternalSystemSyncStatus.SUCCESS, null);
return 0; return;
} }
// 4. 获取本地已存在的仓库组 // 4. 获取本地已存在的仓库组
@ -106,7 +108,6 @@ public class RepositoryGroupServiceImpl extends BaseServiceImpl<RepositoryGroup,
log.info("Successfully synchronized {} groups for external system: {}", log.info("Successfully synchronized {} groups for external system: {}",
savedGroups.size(), externalSystem.getName()); savedGroups.size(), externalSystem.getName());
return savedGroups.size();
} catch (Exception e) { } catch (Exception e) {
// 8. 更新同步历史记录为失败 // 8. 更新同步历史记录为失败

View File

@ -22,6 +22,7 @@ import com.qqchen.deploy.backend.framework.service.impl.BaseServiceImpl;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.orm.ObjectOptimisticLockingFailureException; import org.springframework.orm.ObjectOptimisticLockingFailureException;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@ -98,15 +99,17 @@ public class RepositoryProjectServiceImpl extends BaseServiceImpl<RepositoryProj
} }
@Override @Override
@Async
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public Integer syncProjects(Long externalSystemId) { public void syncProjects(Long externalSystemId) {
return doSyncProjects(externalSystemId, null); doSyncProjects(externalSystemId, null);
} }
@Override @Override
@Async
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public Integer syncProjects(Long externalSystemId, Long repoGroupId) { public void syncProjects(Long externalSystemId, Long repoGroupId) {
return doSyncProjects(externalSystemId, repoGroupId); doSyncProjects(externalSystemId, repoGroupId);
} }
/** /**
@ -114,9 +117,8 @@ public class RepositoryProjectServiceImpl extends BaseServiceImpl<RepositoryProj
* *
* @param externalSystemId 外部系统ID * @param externalSystemId 外部系统ID
* @param repoGroupId 仓库组ID可选 * @param repoGroupId 仓库组ID可选
* @return 同步的项目数量
*/ */
private Integer doSyncProjects(Long externalSystemId, Long repoGroupId) { private void doSyncProjects(Long externalSystemId, Long repoGroupId) {
// 1. 创建同步历史记录 // 1. 创建同步历史记录
RepositorySyncHistoryDTO projectHistory = repositorySyncHistoryService.createSyncHistory(externalSystemId, RepositorySyncType.PROJECT); RepositorySyncHistoryDTO projectHistory = repositorySyncHistoryService.createSyncHistory(externalSystemId, RepositorySyncType.PROJECT);
@ -139,7 +141,7 @@ public class RepositoryProjectServiceImpl extends BaseServiceImpl<RepositoryProj
if (groups.isEmpty()) { if (groups.isEmpty()) {
log.info("No groups found for external system: {}", externalSystem.getName()); log.info("No groups found for external system: {}", externalSystem.getName());
repositorySyncHistoryService.updateSyncHistory(projectHistory.getId(), ExternalSystemSyncStatus.SUCCESS, null); repositorySyncHistoryService.updateSyncHistory(projectHistory.getId(), ExternalSystemSyncStatus.SUCCESS, null);
return 0; return;
} }
log.info("Found {} groups to sync", groups.size()); log.info("Found {} groups to sync", groups.size());
@ -251,7 +253,7 @@ public class RepositoryProjectServiceImpl extends BaseServiceImpl<RepositoryProj
log.info("Successfully synced projects. Added {} new projects, processed {} total projects", log.info("Successfully synced projects. Added {} new projects, processed {} total projects",
totalCount.get(), processedProjectIds.size()); totalCount.get(), processedProjectIds.size());
return totalCount.get();
} catch (Exception e) { } catch (Exception e) {
// 8. 更新同步历史记录为失败 // 8. 更新同步历史记录为失败
repositorySyncHistoryService.updateSyncHistory(projectHistory.getId(), ExternalSystemSyncStatus.FAILED, e.getMessage()); repositorySyncHistoryService.updateSyncHistory(projectHistory.getId(), ExternalSystemSyncStatus.FAILED, e.getMessage());

View File

@ -61,18 +61,13 @@ spring:
drop-first: false drop-first: false
default-schema: deploy-ease-platform default-schema: deploy-ease-platform
contexts: default contexts: default
data:
redis: redis:
host: 172.22.222.111 host: 172.22.222.111
port: 6379
password: Qichen5210523... password: Qichen5210523...
port: 6379
database: 7 database: 7
timeout: 6000ms timeout: 6000ms
lettuce:
pool:
max-active: 8 # 连接池最大连接数
max-idle: 8 # 连接池最大空闲连接数
min-idle: 0 # 连接池最小空闲连接数
max-wait: -1ms # 连接池最大阻塞等待时间
flowable: flowable:
database-schema-update: true database-schema-update: true
# id-generator: org.flowable.common.engine.impl.db.DbIdGenerator # id-generator: org.flowable.common.engine.impl.db.DbIdGenerator