Compare commits

...

2 Commits

Author SHA1 Message Date
HibiKier
c9456f292d feat(config): 修改错误提示信息,更新基础配置文件名称为.env.example
Some checks are pending
Sequential Lint and Type Check / ruff-call (push) Waiting to run
Sequential Lint and Type Check / pyright-call (push) Blocked by required conditions
2025-08-01 10:01:34 +08:00
HibiKier
6c136b904d feat(aliyun): 更新阿里云URL构建逻辑,支持组织名称并优化令牌解码处理 2025-08-01 09:28:46 +08:00
4 changed files with 71 additions and 32 deletions

View File

@ -40,7 +40,7 @@ async def _(setting: Setting) -> Result:
return Result.fail("配置已存在请先删除DB_URL内容和前端密码再进行设置。") return Result.fail("配置已存在请先删除DB_URL内容和前端密码再进行设置。")
env_file = Path() / ".env.example" env_file = Path() / ".env.example"
if not env_file.exists(): if not env_file.exists():
return Result.fail("配置文件.env.dev不存在。") return Result.fail("基础配置文件.env.example不存在。")
env_text = env_file.read_text(encoding="utf-8") env_text = env_file.read_text(encoding="utf-8")
to_env_file = Path() / ".env.dev" to_env_file = Path() / ".env.dev"
if setting.db_url: if setting.db_url:

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:
# 解码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}@") 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="",
@ -285,15 +287,37 @@ class BaseRepoManager(ABC):
"rev-parse --is-inside-work-tree", cwd=local_path "rev-parse --is-inside-work-tree", cwd=local_path
) )
if not success: if not success:
# 如果不是Git仓库尝试初始化它
logger.info(f"目录 {local_path} 不是Git仓库尝试初始化", LOG_COMMAND)
init_success, _, init_stderr = await run_git_command(
"init", cwd=local_path
)
if not init_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="",
error_message=f"{local_path} 不是一个Git仓库", error_message=f"初始化Git仓库失败: {init_stderr}",
) )
# 添加远程仓库
remote_success, _, remote_stderr = await run_git_command(
f"remote add origin {repo_url}", cwd=local_path
)
if not remote_success:
return RepoUpdateResult(
repo_type=repo_type or RepoType.GITHUB,
repo_name=repo_name,
owner=owner or "",
old_version="",
new_version="",
error_message=f"添加远程仓库失败: {remote_stderr}",
)
logger.info(f"成功初始化Git仓库 {local_path}", LOG_COMMAND)
# 获取当前提交ID作为旧版本 # 获取当前提交ID作为旧版本
success, old_version, _ = await run_git_command( success, old_version, _ = await run_git_command(
"rev-parse HEAD", cwd=local_path "rev-parse HEAD", cwd=local_path
@ -308,18 +332,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 +358,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 +373,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 +397,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 +415,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"