大声道撒旦

This commit is contained in:
dengqichen 2025-01-09 17:24:04 +08:00
parent d7fb6c8eaa
commit ec369012f3
2 changed files with 60 additions and 116 deletions

View File

@ -30,7 +30,6 @@ import java.util.stream.Collectors;
public class GitServiceIntegrationImpl implements IGitServiceIntegration {
private final RestTemplate restTemplate = new RestTemplate();
private static final DateTimeFormatter GITLAB_DATE_FORMATTER = DateTimeFormatter.ISO_DATE_TIME;
@Override
public boolean testConnection(ExternalSystem system) {
@ -64,7 +63,8 @@ public class GitServiceIntegrationImpl implements IGitServiceIntegration {
url,
HttpMethod.GET,
entity,
new ParameterizedTypeReference<>() {}
new ParameterizedTypeReference<>() {
}
);
return response.getBody() != null ? response.getBody() : Collections.emptyList();
@ -85,7 +85,8 @@ public class GitServiceIntegrationImpl implements IGitServiceIntegration {
url,
HttpMethod.GET,
entity,
new ParameterizedTypeReference<>() {}
new ParameterizedTypeReference<>() {
}
);
return response.getBody() != null ? response.getBody() : Collections.emptyList();
@ -106,7 +107,8 @@ public class GitServiceIntegrationImpl implements IGitServiceIntegration {
url,
HttpMethod.GET,
entity,
new ParameterizedTypeReference<>() {}
new ParameterizedTypeReference<>() {
}
);
return response.getBody() != null ? response.getBody() : Collections.emptyList();
@ -122,27 +124,18 @@ public class GitServiceIntegrationImpl implements IGitServiceIntegration {
String url = String.format("%s/api/v4/projects/%d/repository/branches?per_page=100", system.getUrl(), projectId);
HttpHeaders headers = createHeaders(system);
HttpEntity<String> entity = new HttpEntity<>(headers);
// 先获取原始响应以便调试
ResponseEntity<String> rawResponse = restTemplate.exchange(
url,
HttpMethod.GET,
entity,
String.class
);
log.info("GitLab API response for branches: {}", rawResponse.getBody());
// 然后解析为对象
ResponseEntity<List<GitBranchResponse>> response = restTemplate.exchange(
url,
HttpMethod.GET,
entity,
new ParameterizedTypeReference<>() {}
new ParameterizedTypeReference<>() {
}
);
List<GitBranchResponse> branches = response.getBody();
if (branches != null && !branches.isEmpty()) {
log.info("Found {} branches, first branch: {}", branches.size(), branches.get(0).getName());
log.info("Found {} branches, first branch: {}", branches.size(), branches.getFirst().getName());
} else {
log.warn("No branches found for project: {}", projectId);
}
@ -184,89 +177,3 @@ public class GitServiceIntegrationImpl implements IGitServiceIntegration {
return headers;
}
}
/**
* GitLab事件响应对象
*/
class GitlabEvent {
private String action;
private String targetType;
private String targetBranch;
private LocalDateTime createdAt;
// Getters and setters
public String getAction() {
return action;
}
public void setAction(String action) {
this.action = action;
}
public String getTargetType() {
return targetType;
}
public void setTargetType(String targetType) {
this.targetType = targetType;
}
public String getTargetBranch() {
return targetBranch;
}
public void setTargetBranch(String targetBranch) {
this.targetBranch = targetBranch;
}
public LocalDateTime getCreatedAt() {
return createdAt;
}
public void setCreatedAt(LocalDateTime createdAt) {
this.createdAt = createdAt;
}
}
/**
* GitLab仓库事件响应对象
*/
class GitlabRepositoryEvent {
private String actionName;
private String refType;
private String ref;
private LocalDateTime createdAt;
// Getters and setters
public String getActionName() {
return actionName;
}
public void setActionName(String actionName) {
this.actionName = actionName;
}
public String getRefType() {
return refType;
}
public void setRefType(String refType) {
this.refType = refType;
}
public String getRef() {
return ref;
}
public void setRef(String ref) {
this.ref = ref;
}
public LocalDateTime getCreatedAt() {
return createdAt;
}
public void setCreatedAt(LocalDateTime createdAt) {
this.createdAt = createdAt;
}
}

View File

@ -17,7 +17,6 @@ import com.qqchen.deploy.backend.framework.enums.ResponseCode;
import com.qqchen.deploy.backend.framework.exception.BusinessException;
import com.qqchen.deploy.backend.framework.service.impl.BaseServiceImpl;
import com.qqchen.deploy.backend.deploy.dto.RepositorySyncHistoryDTO;
import com.qqchen.deploy.backend.deploy.entity.RepositorySyncHistory;
import com.qqchen.deploy.backend.deploy.service.IRepositorySyncHistoryService;
import com.qqchen.deploy.backend.deploy.repository.IRepositorySyncHistoryRepository;
import jakarta.annotation.Resource;
@ -32,15 +31,14 @@ import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.time.LocalDateTime;
import java.util.Objects;
/**
* Git仓库分支服务实现
@ -65,9 +63,6 @@ public class RepositoryBranchServiceImpl extends BaseServiceImpl<RepositoryBranc
@Resource
private IRepositorySyncHistoryService repositorySyncHistoryService;
@Resource
private IRepositorySyncHistoryRepository repositorySyncHistoryRepository;
@Resource(name = "repositoryBranchExecutor")
private ThreadPoolTaskExecutor executor;
@ -159,9 +154,16 @@ public class RepositoryBranchServiceImpl extends BaseServiceImpl<RepositoryBranc
Map<Long, RepositoryBranch> projectBranches = existingBranchMap.getOrDefault(remoteBranch.getName(), new HashMap<>());
RepositoryBranch branch = projectBranches.getOrDefault(project.getId(), new RepositoryBranch());
// 更新分支信息
updateBranchInfo(branch, externalSystemId, project.getId(), project.getProjectId(), remoteBranch);
branchesToUpdate.add(branch);
// 检查分支是否有更新
if (isBranchChanged(branch, remoteBranch)) {
// 更新分支信息
updateBranchInfo(branch, externalSystemId, project.getId(), project.getProjectId(), remoteBranch);
branchesToUpdate.add(branch);
log.debug("Branch {} has changes, will be updated", remoteBranch.getName());
} else {
log.debug("Branch {} has no changes, skipping update", remoteBranch.getName());
}
processedBranches.add(remoteBranch.getName());
totalSyncedBranches.incrementAndGet();
}
@ -223,6 +225,41 @@ public class RepositoryBranchServiceImpl extends BaseServiceImpl<RepositoryBranc
}
}
/**
* 检查分支信息是否有更新
*/
private boolean isBranchChanged(RepositoryBranch branch, GitBranchResponse remoteBranch) {
if (branch.getId() == null) {
return true; // 新分支
}
return !Objects.equals(generateCompareKey(branch), generateCompareKey(remoteBranch));
}
/**
* 生成用于比较的字符串
* 使用StringBuilder拼接所有需要比较的字段提高性能
*/
private String generateCompareKey(Object obj) {
StringBuilder key = new StringBuilder();
if (obj instanceof RepositoryBranch branch) {
key.append(branch.getCommitId())
.append(branch.getIsDefaultBranch())
.append(branch.getCanPush())
.append(branch.getDevelopersCanPush())
.append(branch.getDevelopersCanMerge())
.append(branch.getWebUrl());
} else if (obj instanceof GitBranchResponse branch) {
key.append(branch.getCommitId())
.append(branch.getIsDefaultBranch())
.append(branch.getCanPush())
.append(branch.getDevelopersCanPush())
.append(branch.getDevelopersCanMerge())
.append(branch.getWebUrl());
}
return key.toString();
}
/**
* 更新分支信息
*/