diff --git a/backend/src/main/java/com/qqchen/deploy/backend/deploy/integration/IJenkinsServiceIntegration.java b/backend/src/main/java/com/qqchen/deploy/backend/deploy/integration/IJenkinsServiceIntegration.java index 03db9e59..74c04240 100644 --- a/backend/src/main/java/com/qqchen/deploy/backend/deploy/integration/IJenkinsServiceIntegration.java +++ b/backend/src/main/java/com/qqchen/deploy/backend/deploy/integration/IJenkinsServiceIntegration.java @@ -13,15 +13,6 @@ import java.util.Map; */ public interface IJenkinsServiceIntegration extends IExternalSystemIntegration { - /** - * 测试Jenkins连接 - * - * @param system Jenkins系统配置 - * @return 连接是否成功 - */ - boolean testConnection(ExternalSystem system); - - /** * 使用参数触发构建 * diff --git a/backend/src/main/java/com/qqchen/deploy/backend/deploy/integration/IK8sServiceIntegration.java b/backend/src/main/java/com/qqchen/deploy/backend/deploy/integration/IK8sServiceIntegration.java index ac651de8..49f6a948 100644 --- a/backend/src/main/java/com/qqchen/deploy/backend/deploy/integration/IK8sServiceIntegration.java +++ b/backend/src/main/java/com/qqchen/deploy/backend/deploy/integration/IK8sServiceIntegration.java @@ -13,14 +13,6 @@ import java.util.List; */ public interface IK8sServiceIntegration extends IExternalSystemIntegration { - /** - * 测试K8S连接 - * - * @param system K8S系统配置 - * @return 连接是否成功 - */ - boolean testConnection(ExternalSystem system); - /** * 查询所有命名空间 * diff --git a/backend/src/main/java/com/qqchen/deploy/backend/deploy/integration/impl/BaseExternalSystemIntegration.java b/backend/src/main/java/com/qqchen/deploy/backend/deploy/integration/impl/BaseExternalSystemIntegration.java index aa6f3c3e..f5da0e67 100644 --- a/backend/src/main/java/com/qqchen/deploy/backend/deploy/integration/impl/BaseExternalSystemIntegration.java +++ b/backend/src/main/java/com/qqchen/deploy/backend/deploy/integration/impl/BaseExternalSystemIntegration.java @@ -112,6 +112,37 @@ public abstract class BaseExternalSystemIntegration { // 默认空实现,子类按需覆盖 } + // ==================== 连接测试模板方法 ==================== + + /** + * 测试连接(模板方法) + * 统一处理连接失败时清除缓存的逻辑 + * + * @param system 系统配置 + * @return 是否连接成功 + */ + public final boolean testConnection(ExternalSystem system) { + try { + return doTestConnection(system); + } catch (Exception e) { + log.error("连接测试失败: systemId={}, systemName={}, systemType={}, error={}", + system.getId(), system.getName(), getClass().getSimpleName(), e.getMessage(), e); + // 连接失败时清除缓存,下次重新创建 + clearCredentialCache(system.getId()); + return false; + } + } + + /** + * 执行实际的连接测试逻辑 + * 子类实现具体的测试逻辑,无需处理异常和缓存清理 + * + * @param system 系统配置 + * @return 是否连接成功 + * @throws Exception 连接失败时抛出异常 + */ + protected abstract boolean doTestConnection(ExternalSystem system) throws Exception; + // ==================== 通用凭证获取方法 ==================== /** diff --git a/backend/src/main/java/com/qqchen/deploy/backend/deploy/integration/impl/GitServiceIntegrationImpl.java b/backend/src/main/java/com/qqchen/deploy/backend/deploy/integration/impl/GitServiceIntegrationImpl.java index 43107c5c..cc805f90 100644 --- a/backend/src/main/java/com/qqchen/deploy/backend/deploy/integration/impl/GitServiceIntegrationImpl.java +++ b/backend/src/main/java/com/qqchen/deploy/backend/deploy/integration/impl/GitServiceIntegrationImpl.java @@ -85,27 +85,20 @@ public class GitServiceIntegrationImpl extends BaseExternalSystemIntegration entity = new HttpEntity<>(headers); + protected boolean doTestConnection(ExternalSystem system) throws Exception { + String url = system.getUrl() + "/api/v4/version"; + // 使用基类统一的凭证获取方法 + HttpHeaders headers = getCredential(system); + HttpEntity entity = new HttpEntity<>(headers); - ResponseEntity response = restTemplate.exchange( - url, - HttpMethod.GET, - entity, - String.class - ); + ResponseEntity response = restTemplate.exchange( + url, + HttpMethod.GET, + entity, + String.class + ); - return response.getStatusCode() == HttpStatus.OK; - } catch (Exception e) { - log.error("Git connection test failed for system: {}", system.getName(), e); - // 连接失败时清除缓存,下次重新创建 - clearCredentialCache(system.getId()); - return false; - } + return response.getStatusCode() == HttpStatus.OK; } @Override diff --git a/backend/src/main/java/com/qqchen/deploy/backend/deploy/integration/impl/JenkinsServiceIntegrationImpl.java b/backend/src/main/java/com/qqchen/deploy/backend/deploy/integration/impl/JenkinsServiceIntegrationImpl.java index 4760a991..2b461cd4 100644 --- a/backend/src/main/java/com/qqchen/deploy/backend/deploy/integration/impl/JenkinsServiceIntegrationImpl.java +++ b/backend/src/main/java/com/qqchen/deploy/backend/deploy/integration/impl/JenkinsServiceIntegrationImpl.java @@ -106,31 +106,26 @@ public class JenkinsServiceIntegrationImpl extends BaseExternalSystemIntegration } @Override - public boolean testConnection(ExternalSystem system) { - try { - // 直接使用原始系统信息构建URL(URL不需要解密) - String url = system.getUrl() + "/api/json"; + protected boolean doTestConnection(ExternalSystem system) throws Exception { + // 直接使用原始系统信息构建URL(URL不需要解密) + String url = system.getUrl() + "/api/json"; - // 创建请求头(内部自动处理解密和crumb) - HttpHeaders headers = createHeaders(system); + // 创建请求头(内部自动处理解密和crumb) + HttpHeaders headers = createHeaders(system); - // 打印实际发送的请求头 - log.debug("Authorization头: {}", headers.getFirst("Authorization")); + // 打印实际发送的请求头 + log.debug("Authorization头: {}", headers.getFirst("Authorization")); - HttpEntity entity = new HttpEntity<>(headers); + HttpEntity entity = new HttpEntity<>(headers); - ResponseEntity response = restTemplate.exchange( - url, - HttpMethod.GET, - entity, - String.class - ); + ResponseEntity response = restTemplate.exchange( + url, + HttpMethod.GET, + entity, + String.class + ); - return response.getStatusCode() == HttpStatus.OK; - } catch (Exception e) { - log.error("Failed to connect to Jenkins: {}", system.getUrl(), e); - return false; - } + return response.getStatusCode() == HttpStatus.OK; } /** diff --git a/backend/src/main/java/com/qqchen/deploy/backend/deploy/integration/impl/K8sServiceIntegrationImpl.java b/backend/src/main/java/com/qqchen/deploy/backend/deploy/integration/impl/K8sServiceIntegrationImpl.java index a0bf01bb..d6ba83f5 100644 --- a/backend/src/main/java/com/qqchen/deploy/backend/deploy/integration/impl/K8sServiceIntegrationImpl.java +++ b/backend/src/main/java/com/qqchen/deploy/backend/deploy/integration/impl/K8sServiceIntegrationImpl.java @@ -111,31 +111,21 @@ public class K8sServiceIntegrationImpl extends BaseExternalSystemIntegration systemIntegrations; - + private Map integrationMap; @Resource @@ -87,10 +88,10 @@ public class ExternalSystemServiceImpl extends BaseServiceImpl page(ExternalSystemQuery query) { - org.springframework.data.domain.Page page = super.page(query); + public Page page(ExternalSystemQuery query) { + Page page = super.page(query); // 查询后处理:用掩码替换敏感数据 page.getContent().forEach(this::maskSensitiveData); return page; @@ -233,12 +234,12 @@ public class ExternalSystemServiceImpl extends BaseServiceImpl