From 46304d6682e060b52b6009aabbeeb9d1fc56cfb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=88=9A=E8=BE=B0=E5=85=88=E7=94=9F?= Date: Tue, 3 Dec 2024 00:28:43 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=8E=A5=E5=8F=A3=E8=BF=9E?= =?UTF-8?q?=E6=8E=A5=E6=88=90=E5=8A=9F=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../deploy/backend/entity/ExternalSystem.java | 10 ++- .../backend/framework/enums/ResponseCode.java | 12 ++- .../integration/impl/GitIntegration.java | 15 ++-- .../backend/model/ExternalSystemDTO.java | 2 + .../impl/ExternalSystemServiceImpl.java | 33 +++++++- .../db/migration/V1.0.0__init_schema.sql | 20 ++--- .../db/migration/V1.0.1__init_data.sql | 21 ++++-- .../src/main/resources/messages.properties | 6 +- .../impl/ExternalSystemServiceImplTest.java | 75 +++++++++++++++++++ 9 files changed, 166 insertions(+), 28 deletions(-) diff --git a/backend/src/main/java/com/qqchen/deploy/backend/entity/ExternalSystem.java b/backend/src/main/java/com/qqchen/deploy/backend/entity/ExternalSystem.java index d6fd92cd..311e2f07 100644 --- a/backend/src/main/java/com/qqchen/deploy/backend/entity/ExternalSystem.java +++ b/backend/src/main/java/com/qqchen/deploy/backend/entity/ExternalSystem.java @@ -51,7 +51,7 @@ public class ExternalSystem extends Entity { private Boolean enabled = true; /** - * 认证方式:BASIC/TOKEN/OAUTH等 + * 认证方式:BASIC/TOKEN/OAUTH�� */ @Column(name = "auth_type", nullable = false) @Enumerated(EnumType.STRING) @@ -63,7 +63,7 @@ public class ExternalSystem extends Entity { private String username; /** - * 密码/密钥 + * 密码 */ private String password; @@ -85,6 +85,12 @@ public class ExternalSystem extends Entity { @Column(name = "last_sync_time") private LocalDateTime lastSyncTime; + /** + * 最近连接成功时间 + */ + @Column(name = "last_connect_time") + private LocalDateTime lastConnectTime; + /** * 系统特有配置,JSON格式 */ diff --git a/backend/src/main/java/com/qqchen/deploy/backend/framework/enums/ResponseCode.java b/backend/src/main/java/com/qqchen/deploy/backend/framework/enums/ResponseCode.java index dee8f1db..992a8a37 100644 --- a/backend/src/main/java/com/qqchen/deploy/backend/framework/enums/ResponseCode.java +++ b/backend/src/main/java/com/qqchen/deploy/backend/framework/enums/ResponseCode.java @@ -70,7 +70,17 @@ public enum ResponseCode { EXTERNAL_SYSTEM_TYPE_URL_EXISTS(2501, "external.system.type.url.exists"), EXTERNAL_SYSTEM_DISABLED(2502, "external.system.disabled"), EXTERNAL_SYSTEM_SYNC_FAILED(2503, "external.system.sync.failed"), - EXTERNAL_SYSTEM_TYPE_NOT_SUPPORTED(2504, "external.system.type.not.supported"); + EXTERNAL_SYSTEM_TYPE_NOT_SUPPORTED(2504, "external.system.type.not.supported"), + + /** + * Git系统认证方式错误 + */ + EXTERNAL_SYSTEM_GIT_AUTH_TYPE_ERROR(2501, "Git系统只支持Token认证"), + + /** + * Git系统Token必填 + */ + EXTERNAL_SYSTEM_GIT_TOKEN_REQUIRED(2502, "Git系统必须提供Token"); private final int code; private final String messageKey; // 国际化消息key diff --git a/backend/src/main/java/com/qqchen/deploy/backend/integration/impl/GitIntegration.java b/backend/src/main/java/com/qqchen/deploy/backend/integration/impl/GitIntegration.java index 4b85f9bb..8943870c 100644 --- a/backend/src/main/java/com/qqchen/deploy/backend/integration/impl/GitIntegration.java +++ b/backend/src/main/java/com/qqchen/deploy/backend/integration/impl/GitIntegration.java @@ -6,6 +6,7 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.http.*; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; +import org.springframework.web.client.HttpClientErrorException; @Slf4j @Service @@ -16,11 +17,9 @@ public class GitIntegration implements IExternalSystemIntegration { @Override public boolean testConnection(ExternalSystem system) { try { - String url = system.getUrl() + "/api/v4/version"; // GitLab API + String url = system.getUrl() + "/api/v4/version"; HttpHeaders headers = new HttpHeaders(); - if (system.getToken() != null) { - headers.set("PRIVATE-TOKEN", system.getToken()); - } + headers.set("PRIVATE-TOKEN", system.getToken()); HttpEntity entity = new HttpEntity<>(headers); ResponseEntity response = restTemplate.exchange( @@ -31,8 +30,14 @@ public class GitIntegration implements IExternalSystemIntegration { ); return response.getStatusCode() == HttpStatus.OK; + } catch (HttpClientErrorException.Unauthorized e) { + log.error("GitLab token authentication failed: {}", system.getUrl()); + return false; + } catch (HttpClientErrorException.NotFound e) { + log.error("GitLab API endpoint not found: {}", system.getUrl()); + return false; } catch (Exception e) { - log.error("Failed to connect to Git: {}", system.getUrl(), e); + log.error("Failed to connect to GitLab: {}", system.getUrl(), e); return false; } } diff --git a/backend/src/main/java/com/qqchen/deploy/backend/model/ExternalSystemDTO.java b/backend/src/main/java/com/qqchen/deploy/backend/model/ExternalSystemDTO.java index 51c223a3..ec0912d9 100644 --- a/backend/src/main/java/com/qqchen/deploy/backend/model/ExternalSystemDTO.java +++ b/backend/src/main/java/com/qqchen/deploy/backend/model/ExternalSystemDTO.java @@ -43,5 +43,7 @@ public class ExternalSystemDTO extends BaseDTO { private LocalDateTime lastSyncTime; + private LocalDateTime lastConnectTime; + private String config; } \ No newline at end of file diff --git a/backend/src/main/java/com/qqchen/deploy/backend/service/impl/ExternalSystemServiceImpl.java b/backend/src/main/java/com/qqchen/deploy/backend/service/impl/ExternalSystemServiceImpl.java index 423e3ab8..a342d505 100644 --- a/backend/src/main/java/com/qqchen/deploy/backend/service/impl/ExternalSystemServiceImpl.java +++ b/backend/src/main/java/com/qqchen/deploy/backend/service/impl/ExternalSystemServiceImpl.java @@ -13,6 +13,7 @@ import com.qqchen.deploy.backend.service.IExternalSystemService; import jakarta.annotation.PostConstruct; import jakarta.annotation.Resource; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -69,6 +70,31 @@ public class ExternalSystemServiceImpl extends BaseServiceImpl externalSystemService.validateUniqueConstraints(systemDTO)) + .isInstanceOf(BusinessException.class) + .hasFieldOrPropertyWithValue("errorCode", ResponseCode.EXTERNAL_SYSTEM_GIT_TOKEN_REQUIRED); + } + + @Test + void validateUniqueConstraints_WhenGitWithWrongAuthType_ShouldThrowException() { + // 准备数据 + systemDTO.setType(ExternalSystem.SystemType.GIT); + systemDTO.setAuthType(ExternalSystem.AuthType.BASIC); + + // Mock + when(externalSystemRepository.existsByNameAndDeletedFalse(systemDTO.getName())).thenReturn(false); + when(externalSystemRepository.existsByTypeAndUrlAndDeletedFalse(systemDTO.getType(), systemDTO.getUrl())) + .thenReturn(false); + + // 验证 + assertThatThrownBy(() -> externalSystemService.validateUniqueConstraints(systemDTO)) + .isInstanceOf(BusinessException.class) + .hasFieldOrPropertyWithValue("errorCode", ResponseCode.EXTERNAL_SYSTEM_GIT_AUTH_TYPE_ERROR); + } + + @Test + void update_WhenGitWithoutToken_ShouldThrowException() { + // 准备数据 + systemDTO.setType(ExternalSystem.SystemType.GIT); + systemDTO.setAuthType(ExternalSystem.AuthType.TOKEN); + systemDTO.setToken(null); + + // 验证 + assertThatThrownBy(() -> externalSystemService.update(1L, systemDTO)) + .isInstanceOf(BusinessException.class) + .hasFieldOrPropertyWithValue("errorCode", ResponseCode.EXTERNAL_SYSTEM_GIT_TOKEN_REQUIRED); + } + + @Test + void update_WhenGitWithWrongAuthType_ShouldThrowException() { + // 准备数据 + systemDTO.setType(ExternalSystem.SystemType.GIT); + systemDTO.setAuthType(ExternalSystem.AuthType.BASIC); + + // 验证 + assertThatThrownBy(() -> externalSystemService.update(1L, systemDTO)) + .isInstanceOf(BusinessException.class) + .hasFieldOrPropertyWithValue("errorCode", ResponseCode.EXTERNAL_SYSTEM_GIT_AUTH_TYPE_ERROR); + } + + @Test + void testConnection_WhenSuccess_ShouldUpdateLastConnectTime() { + // Mock + when(externalSystemRepository.findById(1L)).thenReturn(Optional.of(system)); + when(externalSystemRepository.save(any(ExternalSystem.class))).thenReturn(system); + + // 执行 + boolean result = externalSystemService.testConnection(1L); + + // 验证 + assertThat(result).isTrue(); + assertThat(system.getLastConnectTime()).isNotNull(); + verify(externalSystemRepository).save(system); + } } \ No newline at end of file