增加GIT同步时的最大数量,分页

This commit is contained in:
dengqichen 2025-12-04 20:03:12 +08:00
parent 909337044f
commit 612717654b
2 changed files with 105 additions and 47 deletions

View File

@ -3,6 +3,7 @@ package com.qqchen.deploy.backend.deploy.entity;
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.Table; import jakarta.persistence.Table;
import jakarta.persistence.UniqueConstraint;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;

View File

@ -33,6 +33,10 @@ public class GitServiceIntegrationImpl extends BaseExternalSystemIntegration imp
private static final Map<Long, GitSystemCache> SYSTEM_CACHE = new ConcurrentHashMap<>(); private static final Map<Long, GitSystemCache> SYSTEM_CACHE = new ConcurrentHashMap<>();
private static final long CACHE_EXPIRE_TIME = 30 * 60 * 1000; // 30分钟过期 private static final long CACHE_EXPIRE_TIME = 30 * 60 * 1000; // 30分钟过期
// GitLab API 分页配置
private static final int PER_PAGE = 100; // GitLab API 最大值
private static final int MAX_PAGES = 100; // 防止无限循环最多获取 10000
/** /**
* Git系统缓存内部类 * Git系统缓存内部类
*/ */
@ -99,25 +103,43 @@ public class GitServiceIntegrationImpl extends BaseExternalSystemIntegration imp
@Override @Override
public List<GitGroupResponse> groups(ExternalSystem system) { public List<GitGroupResponse> groups(ExternalSystem system) {
List<GitGroupResponse> allGroups = new ArrayList<>();
HttpHeaders headers = createHeaders(system);
HttpEntity<String> entity = new HttpEntity<>(headers);
try { try {
// 直接使用原始系统信息构建URLURL不需要解密 int page = 1;
String url = String.format("%s/api/v4/groups?per_page=100", system.getUrl()); while (page <= MAX_PAGES) {
// 创建请求头内部自动处理解密 String url = String.format("%s/api/v4/groups?per_page=%d&page=%d",
HttpHeaders headers = createHeaders(system); system.getUrl(), PER_PAGE, page);
HttpEntity<String> entity = new HttpEntity<>(headers);
ResponseEntity<List<GitGroupResponse>> response = restTemplate.exchange(
url,
HttpMethod.GET,
entity,
new ParameterizedTypeReference<>() {}
);
ResponseEntity<List<GitGroupResponse>> response = restTemplate.exchange( List<GitGroupResponse> groups = response.getBody();
url, if (groups == null || groups.isEmpty()) {
HttpMethod.GET, break;
entity,
new ParameterizedTypeReference<>() {
} }
);
allGroups.addAll(groups);
return response.getBody() != null ? response.getBody() : Collections.emptyList(); log.debug("Fetched {} groups from page {}", groups.size(), page);
// 如果返回数量小于 per_page说明已经是最后一页
if (groups.size() < PER_PAGE) {
break;
}
page++;
}
log.info("Total fetched {} groups for system: {}", allGroups.size(), system.getName());
return allGroups;
} catch (Exception e) { } catch (Exception e) {
log.error("Failed to fetch git groups for system: {}", system.getName(), e); log.error("Failed to fetch git groups for system: {}", system.getName(), e);
return Collections.emptyList(); return allGroups.isEmpty() ? Collections.emptyList() : allGroups;
} }
} }
@ -147,56 +169,91 @@ public class GitServiceIntegrationImpl extends BaseExternalSystemIntegration imp
@Override @Override
public List<GitProjectResponse> projectsByGroup(ExternalSystem system, Long groupId) { public List<GitProjectResponse> projectsByGroup(ExternalSystem system, Long groupId) {
List<GitProjectResponse> allProjects = new ArrayList<>();
HttpHeaders headers = createHeaders(system);
HttpEntity<String> entity = new HttpEntity<>(headers);
try { try {
// 直接使用原始系统信息构建URLURL不需要解密 int page = 1;
String url = String.format("%s/api/v4/groups/%d/projects?per_page=100", system.getUrl(), groupId); while (page <= MAX_PAGES) {
// 创建请求头内部自动处理解密 String url = String.format("%s/api/v4/groups/%d/projects?per_page=%d&page=%d",
HttpHeaders headers = createHeaders(system); system.getUrl(), groupId, PER_PAGE, page);
HttpEntity<String> entity = new HttpEntity<>(headers);
ResponseEntity<List<GitProjectResponse>> response = restTemplate.exchange(
url,
HttpMethod.GET,
entity,
new ParameterizedTypeReference<>() {}
);
ResponseEntity<List<GitProjectResponse>> response = restTemplate.exchange( List<GitProjectResponse> projects = response.getBody();
url, if (projects == null || projects.isEmpty()) {
HttpMethod.GET, break;
entity,
new ParameterizedTypeReference<>() {
} }
);
allProjects.addAll(projects);
return response.getBody() != null ? response.getBody() : Collections.emptyList(); log.debug("Fetched {} projects from page {} for group {}", projects.size(), page, groupId);
// 如果返回数量小于 per_page说明已经是最后一页
if (projects.size() < PER_PAGE) {
break;
}
page++;
}
log.info("Total fetched {} projects for system: {} and group: {}", allProjects.size(), system.getName(), groupId);
return allProjects;
} 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: {} and group: {}", system.getName(), groupId, e);
return Collections.emptyList(); return allProjects.isEmpty() ? Collections.emptyList() : allProjects;
} }
} }
@Override @Override
public List<GitBranchResponse> branches(ExternalSystem system, Long projectId) { public List<GitBranchResponse> branches(ExternalSystem system, Long projectId) {
List<GitBranchResponse> allBranches = new ArrayList<>();
HttpHeaders headers = createHeaders(system);
HttpEntity<String> entity = new HttpEntity<>(headers);
try { try {
// 直接使用原始系统信息构建URLURL不需要解密 int page = 1;
String url = String.format("%s/api/v4/projects/%d/repository/branches?per_page=100", system.getUrl(), projectId); while (page <= MAX_PAGES) {
// 创建请求头内部自动处理解密 String url = String.format("%s/api/v4/projects/%d/repository/branches?per_page=%d&page=%d",
HttpHeaders headers = createHeaders(system); system.getUrl(), projectId, PER_PAGE, page);
HttpEntity<String> entity = new HttpEntity<>(headers);
// 然后解析为对象 ResponseEntity<List<GitBranchResponse>> response = restTemplate.exchange(
ResponseEntity<List<GitBranchResponse>> response = restTemplate.exchange( url,
url, HttpMethod.GET,
HttpMethod.GET, entity,
entity, new ParameterizedTypeReference<>() {}
new ParameterizedTypeReference<>() { );
}
);
List<GitBranchResponse> branches = response.getBody(); List<GitBranchResponse> branches = response.getBody();
if (branches != null && !branches.isEmpty()) { if (branches == null || branches.isEmpty()) {
log.info("Found {} branches, first branch: {}", branches.size(), branches.getFirst().getName()); break;
}
allBranches.addAll(branches);
log.debug("Fetched {} branches from page {} for project {}", branches.size(), page, projectId);
// 如果返回数量小于 per_page说明已经是最后一页
if (branches.size() < PER_PAGE) {
break;
}
page++;
}
if (!allBranches.isEmpty()) {
log.info("Total fetched {} branches for project: {}, first branch: {}",
allBranches.size(), projectId, allBranches.getFirst().getName());
} else { } else {
log.warn("No branches found for project: {}", projectId); log.warn("No branches found for project: {}", projectId);
} }
return branches != null ? branches : Collections.emptyList(); return allBranches;
} catch (Exception e) { } catch (Exception e) {
log.error("Failed to fetch git branches for system: {} and project: {}", system.getName(), projectId, e); log.error("Failed to fetch git branches for system: {} and project: {}", system.getName(), projectId, e);
return Collections.emptyList(); return allBranches.isEmpty() ? Collections.emptyList() : allBranches;
} }
} }