SSH密钥测试连接 :param repo_host: 仓库主机地址 :return: 连接成功返回True,失败返回False
(self,repo_host)
| 348 | return False, "不支持的仓库地址格式,请使用SSH或HTTP(S)格式" |
| 349 | |
| 350 | def test_ssh(self,repo_host): |
| 351 | """ |
| 352 | SSH密钥测试连接 |
| 353 | |
| 354 | :param repo_host: 仓库主机地址 |
| 355 | :return: 连接成功返回True,失败返回False |
| 356 | """ |
| 357 | # 首先预处理仓库(添加SSH主机到known_hosts) |
| 358 | success, error_msg = self._prepare_git_repo(repo_host) |
| 359 | if not success: |
| 360 | return False |
| 361 | |
| 362 | PLATFORM_DOMAINS = ["github.com", "gitlab.com", "gitee.com", "bitbucket.org", "coding.net"] |
| 363 | repo_url = repo_host |
| 364 | |
| 365 | if repo_url.startswith("ssh://"): |
| 366 | core_part = repo_url.lstrip("ssh://") |
| 367 | elif repo_url.startswith("git@"): |
| 368 | core_part = repo_url |
| 369 | else: |
| 370 | return False |
| 371 | |
| 372 | try: |
| 373 | # 区分仓库类型 |
| 374 | is_platform_repo = False # 默认为自建仓库 |
| 375 | for repo in PLATFORM_DOMAINS: |
| 376 | if repo in repo_url: |
| 377 | repo_url = repo_url.split(':')[0] |
| 378 | is_platform_repo = True |
| 379 | break |
| 380 | |
| 381 | # 处理自建仓库 |
| 382 | port = None |
| 383 | if not is_platform_repo: |
| 384 | host_port_part = core_part.split("git@")[1].split("/")[0] |
| 385 | base_host = host_port_part.split(":")[0] |
| 386 | |
| 387 | # 分离端口和地址,保留 git@ 前缀 |
| 388 | repo_url = f"git@{base_host}" |
| 389 | if ":" in host_port_part: |
| 390 | _, port = host_port_part.split(":", 1) |
| 391 | if not port.isdigit(): |
| 392 | return False |
| 393 | except Exception: |
| 394 | return False |
| 395 | |
| 396 | cmd_parts = ["ssh -T", "-o BatchMode=yes","-o ConnectTimeout=5","-o PasswordAuthentication=no"] |
| 397 | if port: |
| 398 | cmd_parts.append(f"-p {port}") |
| 399 | cmd_parts.append(repo_url) |
| 400 | test_cmd = " ".join(cmd_parts) |
| 401 | |
| 402 | stdout, stderr = public.ExecShell(test_cmd) |
| 403 | combined = (stdout + stderr).lower() |
| 404 | |
| 405 | return any(s in combined for s in [ |
| 406 | "successfully authenticated", |
| 407 | "welcome to gitlab", |
nothing calls this directly
no test coverage detected