增加构建通知
This commit is contained in:
parent
3645d8d062
commit
566425869d
@ -18,7 +18,9 @@ import java.time.format.DateTimeFormatter;
|
||||
import java.util.Base64;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
@ -30,12 +32,58 @@ import java.util.stream.Collectors;
|
||||
public class GitServiceIntegrationImpl extends BaseExternalSystemIntegration implements IGitServiceIntegration {
|
||||
|
||||
private final RestTemplate restTemplate = new RestTemplate();
|
||||
|
||||
// Git系统解密缓存 - 线程安全
|
||||
private static final Map<Long, GitSystemCache> SYSTEM_CACHE = new ConcurrentHashMap<>();
|
||||
private static final long CACHE_EXPIRE_TIME = 30 * 60 * 1000; // 30分钟过期
|
||||
|
||||
/**
|
||||
* Git系统缓存内部类
|
||||
*/
|
||||
private static class GitSystemCache {
|
||||
final ExternalSystem decryptedSystem;
|
||||
final long expireTime;
|
||||
|
||||
GitSystemCache(ExternalSystem system) {
|
||||
this.decryptedSystem = system;
|
||||
this.expireTime = System.currentTimeMillis() + CACHE_EXPIRE_TIME;
|
||||
}
|
||||
|
||||
boolean isExpired() {
|
||||
return System.currentTimeMillis() > expireTime;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 线程安全地获取Git系统缓存
|
||||
* 如果缓存不存在或已过期,会重新解密
|
||||
*/
|
||||
private synchronized GitSystemCache getSystemCache(ExternalSystem system) {
|
||||
Long systemId = system.getId();
|
||||
GitSystemCache cache = SYSTEM_CACHE.get(systemId);
|
||||
|
||||
if (cache == null || cache.isExpired()) {
|
||||
log.debug("Git系统缓存失效,重新解密: systemId={}", systemId);
|
||||
|
||||
// 解密系统信息(只在这里解密一次)
|
||||
ExternalSystem decryptedSystem = decryptSystem(system);
|
||||
|
||||
// 创建新缓存
|
||||
cache = new GitSystemCache(decryptedSystem);
|
||||
SYSTEM_CACHE.put(systemId, cache);
|
||||
|
||||
log.debug("Git系统缓存已更新: systemId={}, expireTime={}", systemId, cache.expireTime);
|
||||
}
|
||||
|
||||
return cache;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean testConnection(ExternalSystem system) {
|
||||
system = decryptSystem(system);
|
||||
try {
|
||||
// 直接使用原始系统信息构建URL(URL不需要解密)
|
||||
String url = system.getUrl() + "/api/v4/version";
|
||||
// 创建请求头(内部自动处理解密)
|
||||
HttpHeaders headers = createHeaders(system);
|
||||
HttpEntity<String> entity = new HttpEntity<>(headers);
|
||||
|
||||
@ -55,9 +103,10 @@ public class GitServiceIntegrationImpl extends BaseExternalSystemIntegration imp
|
||||
|
||||
@Override
|
||||
public List<GitGroupResponse> groups(ExternalSystem system) {
|
||||
system = decryptSystem(system);
|
||||
try {
|
||||
// 直接使用原始系统信息构建URL(URL不需要解密)
|
||||
String url = String.format("%s/api/v4/groups?per_page=100", system.getUrl());
|
||||
// 创建请求头(内部自动处理解密)
|
||||
HttpHeaders headers = createHeaders(system);
|
||||
HttpEntity<String> entity = new HttpEntity<>(headers);
|
||||
|
||||
@ -78,9 +127,10 @@ public class GitServiceIntegrationImpl extends BaseExternalSystemIntegration imp
|
||||
|
||||
@Override
|
||||
public List<GitProjectResponse> projects(ExternalSystem system) {
|
||||
system = decryptSystem(system);
|
||||
try {
|
||||
// 直接使用原始系统信息构建URL(URL不需要解密)
|
||||
String url = String.format("%s/api/v4/projects", system.getUrl());
|
||||
// 创建请求头(内部自动处理解密)
|
||||
HttpHeaders headers = createHeaders(system);
|
||||
HttpEntity<String> entity = new HttpEntity<>(headers);
|
||||
|
||||
@ -101,9 +151,10 @@ public class GitServiceIntegrationImpl extends BaseExternalSystemIntegration imp
|
||||
|
||||
@Override
|
||||
public List<GitProjectResponse> projectsByGroup(ExternalSystem system, Long groupId) {
|
||||
system = decryptSystem(system);
|
||||
try {
|
||||
// 直接使用原始系统信息构建URL(URL不需要解密)
|
||||
String url = String.format("%s/api/v4/groups/%d/projects?per_page=100", system.getUrl(), groupId);
|
||||
// 创建请求头(内部自动处理解密)
|
||||
HttpHeaders headers = createHeaders(system);
|
||||
HttpEntity<String> entity = new HttpEntity<>(headers);
|
||||
|
||||
@ -124,9 +175,10 @@ public class GitServiceIntegrationImpl extends BaseExternalSystemIntegration imp
|
||||
|
||||
@Override
|
||||
public List<GitBranchResponse> branches(ExternalSystem system, Long projectId) {
|
||||
system = decryptSystem(system);
|
||||
try {
|
||||
// 直接使用原始系统信息构建URL(URL不需要解密)
|
||||
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);
|
||||
// 然后解析为对象
|
||||
@ -153,30 +205,35 @@ public class GitServiceIntegrationImpl extends BaseExternalSystemIntegration imp
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建HTTP请求头
|
||||
* 创建包含认证信息的完整请求头
|
||||
* 内部自动处理系统解密和缓存
|
||||
*/
|
||||
private HttpHeaders createHeaders(ExternalSystem system) {
|
||||
// 获取缓存的解密系统信息
|
||||
GitSystemCache cache = getSystemCache(system);
|
||||
ExternalSystem decryptedSystem = cache.decryptedSystem;
|
||||
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||
|
||||
// 根据认证类型设置认证信息
|
||||
switch (system.getAuthType()) {
|
||||
// 根据认证类型设置认证信息(使用解密后的系统信息)
|
||||
switch (decryptedSystem.getAuthType()) {
|
||||
case BASIC -> {
|
||||
// Basic认证
|
||||
String auth = system.getUsername() + ":" + system.getPassword();
|
||||
String auth = decryptedSystem.getUsername() + ":" + decryptedSystem.getPassword();
|
||||
byte[] encodedAuth = Base64.getEncoder().encode(auth.getBytes());
|
||||
String authHeader = "Basic " + new String(encodedAuth);
|
||||
headers.set("Authorization", authHeader);
|
||||
}
|
||||
case TOKEN -> {
|
||||
// Token认证 - GitLab使用Private Token
|
||||
headers.set("PRIVATE-TOKEN", system.getToken());
|
||||
headers.set("PRIVATE-TOKEN", decryptedSystem.getToken());
|
||||
}
|
||||
case OAUTH -> {
|
||||
// OAuth认证
|
||||
headers.set("Authorization", "Bearer " + system.getToken());
|
||||
headers.set("Authorization", "Bearer " + decryptedSystem.getToken());
|
||||
}
|
||||
default -> throw new RuntimeException("Unsupported authentication type: " + system.getAuthType());
|
||||
default -> throw new RuntimeException("Unsupported authentication type: " + decryptedSystem.getAuthType());
|
||||
}
|
||||
|
||||
return headers;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user