diff --git a/backend/src/main/java/com/qqchen/deploy/backend/deploy/api/RepositoryGroupApiController.java b/backend/src/main/java/com/qqchen/deploy/backend/deploy/api/RepositoryGroupApiController.java index e69651a2..73ab4026 100644 --- a/backend/src/main/java/com/qqchen/deploy/backend/deploy/api/RepositoryGroupApiController.java +++ b/backend/src/main/java/com/qqchen/deploy/backend/deploy/api/RepositoryGroupApiController.java @@ -21,7 +21,7 @@ import java.util.List; */ @Slf4j @RestController -@RequestMapping("/api/v1/repo-group") +@RequestMapping("/api/v1/repository-group") @Tag(name = "Git仓库组管理", description = "Git仓库组管理相关接口") public class RepositoryGroupApiController extends BaseController { diff --git a/backend/src/main/java/com/qqchen/deploy/backend/deploy/api/GitManagerApiController.java b/backend/src/main/java/com/qqchen/deploy/backend/deploy/api/RepositoryManagerApiController.java similarity index 97% rename from backend/src/main/java/com/qqchen/deploy/backend/deploy/api/GitManagerApiController.java rename to backend/src/main/java/com/qqchen/deploy/backend/deploy/api/RepositoryManagerApiController.java index 960f2473..34672ebd 100644 --- a/backend/src/main/java/com/qqchen/deploy/backend/deploy/api/GitManagerApiController.java +++ b/backend/src/main/java/com/qqchen/deploy/backend/deploy/api/RepositoryManagerApiController.java @@ -15,9 +15,9 @@ import org.springframework.web.bind.annotation.*; */ @Slf4j @RestController -@RequestMapping("/api/v1/git-manager") +@RequestMapping("/api/v1/repository-manager") @Tag(name = "Git仓库管理", description = "Git仓库管理相关接口") -public class GitManagerApiController { +public class RepositoryManagerApiController { @Resource private IGitManagerService gitManagerService; diff --git a/backend/src/main/java/com/qqchen/deploy/backend/deploy/integration/response/GitGroupResponse.java b/backend/src/main/java/com/qqchen/deploy/backend/deploy/integration/response/GitGroupResponse.java index dabcb02a..f5e89dd2 100644 --- a/backend/src/main/java/com/qqchen/deploy/backend/deploy/integration/response/GitGroupResponse.java +++ b/backend/src/main/java/com/qqchen/deploy/backend/deploy/integration/response/GitGroupResponse.java @@ -1,5 +1,6 @@ package com.qqchen.deploy.backend.deploy.integration.response; +import com.fasterxml.jackson.annotation.JsonProperty; import lombok.Data; /** @@ -18,9 +19,54 @@ public class GitGroupResponse { private String visibility; + @JsonProperty("parent_id") private Long parentId; + @JsonProperty("web_url") private String webUrl; + @JsonProperty("avatar_url") private String avatarUrl; + + @JsonProperty("full_name") + private String fullName; + + @JsonProperty("full_path") + private String fullPath; + + @JsonProperty("project_creation_level") + private String projectCreationLevel; + + @JsonProperty("subgroup_creation_level") + private String subgroupCreationLevel; + + @JsonProperty("request_access_enabled") + private Boolean requestAccessEnabled; + + @JsonProperty("share_with_group_lock") + private Boolean shareWithGroupLock; + + @JsonProperty("require_two_factor_authentication") + private Boolean requireTwoFactorAuthentication; + + @JsonProperty("two_factor_grace_period") + private Integer twoFactorGracePeriod; + + @JsonProperty("auto_devops_enabled") + private Boolean autoDevopsEnabled; + + @JsonProperty("emails_disabled") + private Boolean emailsDisabled; + + @JsonProperty("mentions_disabled") + private Boolean mentionsDisabled; + + @JsonProperty("lfs_enabled") + private Boolean lfsEnabled; + + @JsonProperty("default_branch_protection") + private Integer defaultBranchProtection; + + @JsonProperty("created_at") + private String createdAt; } \ No newline at end of file diff --git a/backend/src/main/java/com/qqchen/deploy/backend/deploy/repository/IRepositoryGroupRepository.java b/backend/src/main/java/com/qqchen/deploy/backend/deploy/repository/IRepositoryGroupRepository.java index 0982a536..a466ce14 100644 --- a/backend/src/main/java/com/qqchen/deploy/backend/deploy/repository/IRepositoryGroupRepository.java +++ b/backend/src/main/java/com/qqchen/deploy/backend/deploy/repository/IRepositoryGroupRepository.java @@ -36,4 +36,11 @@ public interface IRepositoryGroupRepository extends IBaseRepository new BusinessException(ResponseCode.EXTERNAL_SYSTEM_NOT_FOUND)); + + // 2. 从Git API获取所有仓库组信息 + List remoteGroups = gitServiceIntegration.groups(externalSystem); + if (remoteGroups.isEmpty()) { + log.info("No groups found in remote git system: {}", externalSystem.getName()); + return 0; + } + try { - // 1. 获取外部系统信息 - ExternalSystem externalSystem = externalSystemRepository.findById(externalSystemId) - .orElseThrow(() -> new BusinessException(ResponseCode.EXTERNAL_SYSTEM_NOT_FOUND)); + // 3. 清空当前系统中该外部系统的所有仓库组 + repositoryGroupRepository.deleteByExternalSystemId(externalSystemId); + repositoryGroupRepository.flush(); - // 2. 获取远程仓库组信息 - List remoteGroups = gitServiceIntegration.groups(externalSystem); - if (remoteGroups.isEmpty()) { - log.info("No groups found in remote git system: {}", externalSystem.getName()); - return 0; - } - - // 3. 获取本地已存在的仓库组 - List existingGroups = repositoryGroupRepository.findByExternalSystemId(externalSystemId); - Map existingGroupMap = existingGroups.stream() - .collect(Collectors.toMap(RepositoryGroup::getGroupId, Function.identity())); - - // 4. 更新或创建仓库组 + // 4. 批量保存从Git API获取的仓库组到数据库 List groupsToSave = remoteGroups.stream() - .map(remoteGroup -> updateOrCreateGroup(externalSystemId, remoteGroup, existingGroupMap)) + .map(remoteGroup -> { + RepositoryGroup group = new RepositoryGroup(); + group.setExternalSystemId(externalSystemId); + group.setGroupId(remoteGroup.getId()); + group.setName(remoteGroup.getName()); + group.setPath(remoteGroup.getPath()); + group.setDescription(remoteGroup.getDescription()); + group.setVisibility(remoteGroup.getVisibility()); + group.setWebUrl(remoteGroup.getWebUrl()); + group.setAvatarUrl(remoteGroup.getAvatarUrl()); + group.setParentId(remoteGroup.getParentId()); + return group; + }) .collect(Collectors.toList()); - // 5. 保存仓库组 - repositoryGroupRepository.saveAll(groupsToSave); - - log.info("Successfully synchronized {} groups for external system: {}", - groupsToSave.size(), externalSystem.getName()); - return groupsToSave.size(); + List savedGroups = repositoryGroupRepository.saveAll(groupsToSave); + + log.info("Successfully synchronized {} groups for external system: {}", + savedGroups.size(), externalSystem.getName()); + return savedGroups.size(); + } catch (Exception e) { log.error("Failed to sync repository groups for external system: {}", externalSystemId, e); throw new BusinessException(ResponseCode.REPOSITORY_SYNC_FAILED); } } - private RepositoryGroup updateOrCreateGroup( - Long externalSystemId, - GitGroupResponse remoteGroup, - Map existingGroupMap) { - - RepositoryGroup group = existingGroupMap.getOrDefault(remoteGroup.getId(), new RepositoryGroup()); - - // 更新基本信息 - group.setExternalSystemId(externalSystemId); - group.setGroupId(remoteGroup.getId()); - group.setName(remoteGroup.getName()); - group.setPath(remoteGroup.getPath()); - group.setDescription(remoteGroup.getDescription()); - group.setVisibility(remoteGroup.getVisibility()); - group.setParentId(remoteGroup.getParentId()); - group.setWebUrl(remoteGroup.getWebUrl()); - group.setAvatarUrl(remoteGroup.getAvatarUrl()); - - return group; - } - @Override public Long countByExternalSystemId(Long externalSystemId) { return repositoryGroupRepository.countByExternalSystemId(externalSystemId); diff --git a/backend/src/main/resources/db/migration/V1.0.0__init_schema.sql b/backend/src/main/resources/db/migration/V1.0.0__init_schema.sql index bcf3e9a2..753b84f4 100644 --- a/backend/src/main/resources/db/migration/V1.0.0__init_schema.sql +++ b/backend/src/main/resources/db/migration/V1.0.0__init_schema.sql @@ -304,13 +304,9 @@ CREATE TABLE deploy_repo_group 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 '路径索引', - UNIQUE INDEX uk_external_system_group_path (external_system_id, PATH) COMMENT '外部系统下路径唯一', -- 外键约束 - CONSTRAINT fk_group_parent FOREIGN KEY (parent_id) - REFERENCES deploy_repo_group (id), - CONSTRAINT fk_group_external_system FOREIGN KEY (external_system_id) - REFERENCES sys_external_system (id) + 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='代码仓库组表'; -- 代码仓库项目表