feat(aliyun): 更新阿里云URL构建逻辑,支持组织名称并优化令牌解码处理

This commit is contained in:
HibiKier 2025-08-01 09:28:46 +08:00
parent 0e19de102a
commit 6c136b904d
3 changed files with 42 additions and 25 deletions

View File

@ -450,16 +450,27 @@ class AliyunCodeupManager(BaseRepoManager):
""" """
# 定义预处理函数构建阿里云CodeUp的URL # 定义预处理函数构建阿里云CodeUp的URL
def prepare_aliyun_url(repo_name: str) -> str: def prepare_aliyun_url(repo_url: str) -> str:
import base64
repo_name = repo_url.split("/")[-1].replace(".git", "")
# 构建仓库URL # 构建仓库URL
# 阿里云CodeUp的仓库URL格式通常为 # 阿里云CodeUp的仓库URL格式通常为
# https://codeup.aliyun.com/{organization_id}/{repo_name}.git # https://codeup.aliyun.com/{organization_id}/{organization_name}/{repo_name}.git
url = f"https://codeup.aliyun.com/{self.config.aliyun_codeup.organization_id}/{repo_name}.git" url = f"https://codeup.aliyun.com/{self.config.aliyun_codeup.organization_id}/{self.config.aliyun_codeup.organization_name}/{repo_name}.git"
# 添加访问令牌 # 添加访问令牌 - 使用base64解码后的令牌
if self.config.aliyun_codeup.rdc_access_token_encrypted: if self.config.aliyun_codeup.rdc_access_token_encrypted:
token = self.config.aliyun_codeup.rdc_access_token_encrypted try:
url = url.replace("https://", f"https://oauth2:{token}@") # 解码RDC访问令牌
token = base64.b64decode(
self.config.aliyun_codeup.rdc_access_token_encrypted.encode()
).decode()
# 阿里云CodeUp使用oauth2:token的格式进行身份验证
url = url.replace("https://", f"https://oauth2:{token}@")
logger.debug(f"使用RDC令牌构建阿里云URL: {url.split('@')[0]}@***")
except Exception as e:
logger.error(f"解码RDC令牌失败: {e}")
return url return url
@ -541,7 +552,7 @@ class AliyunCodeupManager(BaseRepoManager):
raise e raise e
except Exception as e: except Exception as e:
if retry < self.config.aliyun_codeup.download_retry: if retry < self.config.aliyun_codeup.download_retry:
logger.warning(f"下载文件失败,将重试: {e}") logger.warning("下载文件失败,将重试", LOG_COMMAND, e=e)
await asyncio.sleep(1) await asyncio.sleep(1)
continue continue
raise RepoDownloadError(f"下载文件失败: {e}") raise RepoDownloadError(f"下载文件失败: {e}")

View File

@ -9,7 +9,7 @@ import aiofiles
from zhenxun.services.log import logger from zhenxun.services.log import logger
from .config import RepoConfig from .config import LOG_COMMAND, RepoConfig
from .models import ( from .models import (
FileDownloadResult, FileDownloadResult,
RepoCommitInfo, RepoCommitInfo,
@ -230,11 +230,13 @@ class BaseRepoManager(ABC):
""" """
from .models import RepoType from .models import RepoType
repo_name = repo_url.split("/")[-1].replace(".git", "")
try: try:
# 创建结果对象 # 创建结果对象
result = RepoUpdateResult( result = RepoUpdateResult(
repo_type=repo_type or RepoType.GITHUB, # 默认使用GitHub类型 repo_type=repo_type or RepoType.GITHUB, # 默认使用GitHub类型
repo_name=repo_url.split("/")[-1].replace(".git", ""), repo_name=repo_name,
owner=owner or "", owner=owner or "",
old_version="", old_version="",
new_version="", new_version="",
@ -244,7 +246,7 @@ class BaseRepoManager(ABC):
if not await check_git(): if not await check_git():
return RepoUpdateResult( return RepoUpdateResult(
repo_type=repo_type or RepoType.GITHUB, repo_type=repo_type or RepoType.GITHUB,
repo_name=repo_url.split("/")[-1].replace(".git", ""), repo_name=repo_name,
owner=owner or "", owner=owner or "",
old_version="", old_version="",
new_version="", new_version="",
@ -258,14 +260,14 @@ class BaseRepoManager(ABC):
# 检查本地目录是否存在 # 检查本地目录是否存在
if not local_path.exists(): if not local_path.exists():
# 如果不存在,则克隆仓库 # 如果不存在,则克隆仓库
logger.info(f"克隆仓库 {repo_url}{local_path}") logger.info(f"克隆仓库 {repo_url}{local_path}", LOG_COMMAND)
success, stdout, stderr = await run_git_command( success, stdout, stderr = await run_git_command(
f"clone -b {branch} {repo_url} {local_path}" f"clone -b {branch} {repo_url} {local_path}"
) )
if not success: if not success:
return RepoUpdateResult( return RepoUpdateResult(
repo_type=repo_type or RepoType.GITHUB, repo_type=repo_type or RepoType.GITHUB,
repo_name=repo_url.split("/")[-1].replace(".git", ""), repo_name=repo_name,
owner=owner or "", owner=owner or "",
old_version="", old_version="",
new_version="", new_version="",
@ -287,7 +289,7 @@ class BaseRepoManager(ABC):
if not success: if not success:
return RepoUpdateResult( return RepoUpdateResult(
repo_type=repo_type or RepoType.GITHUB, repo_type=repo_type or RepoType.GITHUB,
repo_name=repo_url.split("/")[-1].replace(".git", ""), repo_name=repo_name,
owner=owner or "", owner=owner or "",
old_version="", old_version="",
new_version="", new_version="",
@ -308,18 +310,18 @@ class BaseRepoManager(ABC):
# 如果远程URL不匹配则更新它 # 如果远程URL不匹配则更新它
remote_url = remote_url.strip() remote_url = remote_url.strip()
if success and repo_url not in remote_url and remote_url not in repo_url: if success and repo_url not in remote_url and remote_url not in repo_url:
logger.info(f"更新远程URL: {remote_url} -> {repo_url}") logger.info(f"更新远程URL: {remote_url} -> {repo_url}", LOG_COMMAND)
await run_git_command( await run_git_command(
f"remote set-url origin {repo_url}", cwd=local_path f"remote set-url origin {repo_url}", cwd=local_path
) )
# 获取远程更新 # 获取远程更新
logger.info("获取远程更新") logger.info("获取远程更新", LOG_COMMAND)
success, _, stderr = await run_git_command("fetch origin", cwd=local_path) success, _, stderr = await run_git_command("fetch origin", cwd=local_path)
if not success: if not success:
return RepoUpdateResult( return RepoUpdateResult(
repo_type=repo_type or RepoType.GITHUB, repo_type=repo_type or RepoType.GITHUB,
repo_name=repo_url.split("/")[-1].replace(".git", ""), repo_name=repo_name,
owner=owner or "", owner=owner or "",
old_version=old_version.strip(), old_version=old_version.strip(),
new_version="", new_version="",
@ -334,14 +336,14 @@ class BaseRepoManager(ABC):
# 如果当前分支不是目标分支,则切换分支 # 如果当前分支不是目标分支,则切换分支
if success and current_branch != branch: if success and current_branch != branch:
logger.info(f"切换分支: {current_branch} -> {branch}") logger.info(f"切换分支: {current_branch} -> {branch}", LOG_COMMAND)
success, _, stderr = await run_git_command( success, _, stderr = await run_git_command(
f"checkout {branch}", cwd=local_path f"checkout {branch}", cwd=local_path
) )
if not success: if not success:
return RepoUpdateResult( return RepoUpdateResult(
repo_type=repo_type or RepoType.GITHUB, repo_type=repo_type or RepoType.GITHUB,
repo_name=repo_url.split("/")[-1].replace(".git", ""), repo_name=repo_name,
owner=owner or "", owner=owner or "",
old_version=old_version.strip(), old_version=old_version.strip(),
new_version="", new_version="",
@ -349,16 +351,16 @@ class BaseRepoManager(ABC):
) )
# 拉取最新代码 # 拉取最新代码
logger.info("拉取最新代码") logger.info("拉取最新代码", LOG_COMMAND)
pull_cmd = f"pull origin {branch}" pull_cmd = f"pull origin {branch}"
if force: if force:
pull_cmd = f"pull --force origin {branch}" pull_cmd = f"pull --force origin {branch}"
logger.info("使用强制拉取模式") logger.info("使用强制拉取模式", LOG_COMMAND)
success, _, stderr = await run_git_command(pull_cmd, cwd=local_path) success, _, stderr = await run_git_command(pull_cmd, cwd=local_path)
if not success: if not success:
return RepoUpdateResult( return RepoUpdateResult(
repo_type=repo_type or RepoType.GITHUB, repo_type=repo_type or RepoType.GITHUB,
repo_name=repo_url.split("/")[-1].replace(".git", ""), repo_name=repo_name,
owner=owner or "", owner=owner or "",
old_version=old_version.strip(), old_version=old_version.strip(),
new_version="", new_version="",
@ -373,7 +375,9 @@ class BaseRepoManager(ABC):
# 如果版本相同,则无需更新 # 如果版本相同,则无需更新
if old_version.strip() == new_version.strip(): if old_version.strip() == new_version.strip():
logger.info(f"仓库 {repo_url} 已是最新版本: {new_version.strip()}") logger.info(
f"仓库 {repo_url} 已是最新版本: {new_version.strip()}", LOG_COMMAND
)
result.success = True result.success = True
return result return result
@ -389,16 +393,16 @@ class BaseRepoManager(ABC):
if line.strip() if line.strip()
] ]
result.changed_files = changed_files result.changed_files = changed_files
logger.info(f"变更的文件列表: {changed_files}") logger.info(f"变更的文件列表: {changed_files}", LOG_COMMAND)
result.success = True result.success = True
return result return result
except Exception as e: except Exception as e:
logger.error(f"Git更新失败: {e}") logger.error("Git更新失败", LOG_COMMAND, e=e)
return RepoUpdateResult( return RepoUpdateResult(
repo_type=repo_type or RepoType.GITHUB, repo_type=repo_type or RepoType.GITHUB,
repo_name=repo_url.split("/")[-1].replace(".git", ""), repo_name=repo_name,
owner=owner or "", owner=owner or "",
old_version="", old_version="",
new_version="", new_version="",

View File

@ -34,6 +34,8 @@ class AliyunCodeupConfig:
access_key_secret: str = "NmJ3d2VNRU1MREY0T1RtRnBqMlFqdlBxN3pMUk1j" access_key_secret: str = "NmJ3d2VNRU1MREY0T1RtRnBqMlFqdlBxN3pMUk1j"
# 组织ID # 组织ID
organization_id: str = "67a361cf556e6cdab537117a" organization_id: str = "67a361cf556e6cdab537117a"
# 组织名称
organization_name: str = "zhenxun-org"
# RDC Access Token # RDC Access Token
rdc_access_token_encrypted: str = ( rdc_access_token_encrypted: str = (
"cHQtYXp0allnQWpub0FYZWpqZm1RWGtneHk0XzBlMmYzZTZmLWQwOWItNDE4Mi1iZWUx" "cHQtYXp0allnQWpub0FYZWpqZm1RWGtneHk0XzBlMmYzZTZmLWQwOWItNDE4Mi1iZWUx"