diff --git a/zhenxun/utils/repo_utils/aliyun_manager.py b/zhenxun/utils/repo_utils/aliyun_manager.py index 5fa2ee80..17a72add 100644 --- a/zhenxun/utils/repo_utils/aliyun_manager.py +++ b/zhenxun/utils/repo_utils/aliyun_manager.py @@ -450,16 +450,27 @@ class AliyunCodeupManager(BaseRepoManager): """ # 定义预处理函数,构建阿里云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 # 阿里云CodeUp的仓库URL格式通常为: - # https://codeup.aliyun.com/{organization_id}/{repo_name}.git - url = f"https://codeup.aliyun.com/{self.config.aliyun_codeup.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}/{self.config.aliyun_codeup.organization_name}/{repo_name}.git" - # 添加访问令牌 + # 添加访问令牌 - 使用base64解码后的令牌 if self.config.aliyun_codeup.rdc_access_token_encrypted: - token = self.config.aliyun_codeup.rdc_access_token_encrypted - url = url.replace("https://", f"https://oauth2:{token}@") + try: + # 解码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 @@ -541,7 +552,7 @@ class AliyunCodeupManager(BaseRepoManager): raise e except Exception as e: if retry < self.config.aliyun_codeup.download_retry: - logger.warning(f"下载文件失败,将重试: {e}") + logger.warning("下载文件失败,将重试", LOG_COMMAND, e=e) await asyncio.sleep(1) continue raise RepoDownloadError(f"下载文件失败: {e}") diff --git a/zhenxun/utils/repo_utils/base_manager.py b/zhenxun/utils/repo_utils/base_manager.py index c7960eee..c25f3ea6 100644 --- a/zhenxun/utils/repo_utils/base_manager.py +++ b/zhenxun/utils/repo_utils/base_manager.py @@ -9,7 +9,7 @@ import aiofiles from zhenxun.services.log import logger -from .config import RepoConfig +from .config import LOG_COMMAND, RepoConfig from .models import ( FileDownloadResult, RepoCommitInfo, @@ -230,11 +230,13 @@ class BaseRepoManager(ABC): """ from .models import RepoType + repo_name = repo_url.split("/")[-1].replace(".git", "") + try: # 创建结果对象 result = RepoUpdateResult( repo_type=repo_type or RepoType.GITHUB, # 默认使用GitHub类型 - repo_name=repo_url.split("/")[-1].replace(".git", ""), + repo_name=repo_name, owner=owner or "", old_version="", new_version="", @@ -244,7 +246,7 @@ class BaseRepoManager(ABC): if not await check_git(): return RepoUpdateResult( repo_type=repo_type or RepoType.GITHUB, - repo_name=repo_url.split("/")[-1].replace(".git", ""), + repo_name=repo_name, owner=owner or "", old_version="", new_version="", @@ -258,14 +260,14 @@ class BaseRepoManager(ABC): # 检查本地目录是否存在 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( f"clone -b {branch} {repo_url} {local_path}" ) if not success: return RepoUpdateResult( repo_type=repo_type or RepoType.GITHUB, - repo_name=repo_url.split("/")[-1].replace(".git", ""), + repo_name=repo_name, owner=owner or "", old_version="", new_version="", @@ -287,7 +289,7 @@ class BaseRepoManager(ABC): if not success: return RepoUpdateResult( repo_type=repo_type or RepoType.GITHUB, - repo_name=repo_url.split("/")[-1].replace(".git", ""), + repo_name=repo_name, owner=owner or "", old_version="", new_version="", @@ -308,18 +310,18 @@ class BaseRepoManager(ABC): # 如果远程URL不匹配,则更新它 remote_url = remote_url.strip() 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( 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) if not success: return RepoUpdateResult( repo_type=repo_type or RepoType.GITHUB, - repo_name=repo_url.split("/")[-1].replace(".git", ""), + repo_name=repo_name, owner=owner or "", old_version=old_version.strip(), new_version="", @@ -334,14 +336,14 @@ class BaseRepoManager(ABC): # 如果当前分支不是目标分支,则切换分支 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( f"checkout {branch}", cwd=local_path ) if not success: return RepoUpdateResult( repo_type=repo_type or RepoType.GITHUB, - repo_name=repo_url.split("/")[-1].replace(".git", ""), + repo_name=repo_name, owner=owner or "", old_version=old_version.strip(), new_version="", @@ -349,16 +351,16 @@ class BaseRepoManager(ABC): ) # 拉取最新代码 - logger.info("拉取最新代码") + logger.info("拉取最新代码", LOG_COMMAND) pull_cmd = f"pull origin {branch}" if force: pull_cmd = f"pull --force origin {branch}" - logger.info("使用强制拉取模式") + logger.info("使用强制拉取模式", LOG_COMMAND) success, _, stderr = await run_git_command(pull_cmd, cwd=local_path) if not success: return RepoUpdateResult( repo_type=repo_type or RepoType.GITHUB, - repo_name=repo_url.split("/")[-1].replace(".git", ""), + repo_name=repo_name, owner=owner or "", old_version=old_version.strip(), new_version="", @@ -373,7 +375,9 @@ class BaseRepoManager(ABC): # 如果版本相同,则无需更新 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 return result @@ -389,16 +393,16 @@ class BaseRepoManager(ABC): if line.strip() ] result.changed_files = changed_files - logger.info(f"变更的文件列表: {changed_files}") + logger.info(f"变更的文件列表: {changed_files}", LOG_COMMAND) result.success = True return result except Exception as e: - logger.error(f"Git更新失败: {e}") + logger.error("Git更新失败", LOG_COMMAND, e=e) return RepoUpdateResult( repo_type=repo_type or RepoType.GITHUB, - repo_name=repo_url.split("/")[-1].replace(".git", ""), + repo_name=repo_name, owner=owner or "", old_version="", new_version="", diff --git a/zhenxun/utils/repo_utils/config.py b/zhenxun/utils/repo_utils/config.py index 8ccd4fc9..befe7555 100644 --- a/zhenxun/utils/repo_utils/config.py +++ b/zhenxun/utils/repo_utils/config.py @@ -34,6 +34,8 @@ class AliyunCodeupConfig: access_key_secret: str = "NmJ3d2VNRU1MREY0T1RtRnBqMlFqdlBxN3pMUk1j" # 组织ID organization_id: str = "67a361cf556e6cdab537117a" + # 组织名称 + organization_name: str = "zhenxun-org" # RDC Access Token rdc_access_token_encrypted: str = ( "cHQtYXp0allnQWpub0FYZWpqZm1RWGtneHk0XzBlMmYzZTZmLWQwOWItNDE4Mi1iZWUx"