From 6987076622f5de594f1dba7f7dac4b08ece8ddb2 Mon Sep 17 00:00:00 2001 From: pooneyy <85266337+pooneyy@users.noreply.github.com> Date: Tue, 7 Oct 2025 15:17:57 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=94=A7=20chore(renovate):=20update=20?= =?UTF-8?q?github=20actions=20bot=20configuration?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - update git config email and name to use official github-actions[bot] credentials - update gitIgnoredAuthors to use official bot email address for proper commit attribution --- .github/workflows/renovate-app-version.yml | 4 ++-- renovate.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/renovate-app-version.yml b/.github/workflows/renovate-app-version.yml index bbb507cb9..f527b14df 100644 --- a/.github/workflows/renovate-app-version.yml +++ b/.github/workflows/renovate-app-version.yml @@ -55,8 +55,8 @@ jobs: - name: Configure repo run: | - git config --local user.email "githubaction@githubaction.com" - git config --local user.name "github-action" + git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com" + git config --local user.name "github-actions[bot]" - name: Get list of updated files by the last commit in this branch separated by space id: updated-files diff --git a/renovate.json b/renovate.json index d0388a883..47af01a8b 100644 --- a/renovate.json +++ b/renovate.json @@ -4,7 +4,7 @@ "config:recommended" ], "gitIgnoredAuthors": [ - "githubaction@githubaction.com", + "41898282+github-actions[bot]@users.noreply.github.com", "85266337+pooneyy@users.noreply.github.com" ], "prBodyColumns": [ From a67be051730a66aa8c008c79d23006939abc1209 Mon Sep 17 00:00:00 2001 From: pooneyy <85266337+pooneyy@users.noreply.github.com> Date: Tue, 7 Oct 2025 15:41:29 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20refactor(renovate):=20?= =?UTF-8?q?simplify=20version=20extraction=20and=20add=20safety=20function?= =?UTF-8?q?s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - consolidate multiple regex patterns into single comprehensive pattern for version extraction - add type hints for version variables to improve code clarity - implement safe_rename_directory function with comprehensive error handling and validation - create write_version_file function with proper directory creation and error handling - replace direct file operations with safer function calls - add permission and system error handling for directory operations - implement existence checks before directory operations - add operation success validation after renaming - improve error messages with clear success/failure indicators --- .github/workflows/renovate-app-version.py | 114 ++++++++++++++-------- 1 file changed, 72 insertions(+), 42 deletions(-) diff --git a/.github/workflows/renovate-app-version.py b/.github/workflows/renovate-app-version.py index 0fd651dff..804c6b9d5 100644 --- a/.github/workflows/renovate-app-version.py +++ b/.github/workflows/renovate-app-version.py @@ -24,40 +24,9 @@ def extract_version_from_string(input_string) -> dict: # 保存原始候选字符串用于失败时返回 original_candidate = candidate - # 标准的 major.minor.patch 格式(点号分隔) - pattern1 = r'(\d+\.\d+\.\d+)' - match1 = re.search(pattern1, candidate) - if match1: - return {"success": True, "version": match1.group(1)} - - # 连字符分隔的版本号 major-minor-patch - pattern2 = r'(\d+\-\d+\-\d+)' - match2 = re.search(pattern2, candidate) - if match2: - # 将连字符转换为点号 - version_with_dots = match2.group(1).replace('-', '.') - return {"success": True, "version": version_with_dots} - - # 混合分隔符(如 major.minor-patch) - pattern3 = r'(\d+[\.\-]\d+[\.\-]\d+)' - match3 = re.search(pattern3, candidate) - if match3: - # 将所有分隔符统一为点号 - version_mixed = match3.group(1) - version_normalized = re.sub(r'[\.\-]', '.', version_mixed) - return {"success": True, "version": version_normalized} - - # 两个部分的版本号 (major.minor 或 major-minor) - pattern4 = r'(\d+[\.\-]\d+)(?![\.\-]\d)' # 确保后面没有第三个数字部分 - match4 = re.search(pattern4, candidate) - if match4: - version_two_part = match4.group(1) - version_normalized = re.sub(r'[\.\-]', '.', version_two_part) - return {"success": True, "version": version_normalized} - # 从复杂标签中提取包含数字和分隔符的版本号部分 - pattern5 = r'(\d+(?:[\.\-]\d+)+)' - matches = re.findall(pattern5, candidate) + pattern = r'(\d+(?:[\.\-]\d+)+)' + matches = re.findall(pattern, candidate) if matches: # 选择最长的匹配项,并统一分隔符为点号 best_match = max(matches, key=len) @@ -86,6 +55,69 @@ def replace_version_in_dirname(old_ver_dir : str, new_version : str) -> str: else: return new_version + +def safe_rename_directory(old_path: str, new_path: str) -> bool: + """ + 安全地重命名目录,包含错误处理 + + Args: + old_path (str): 原目录路径 + new_path (str): 新目录路径 + + Returns: + bool: 重命名是否成功 + """ + try: + if not os.path.exists(old_path): + print(f"错误: 原目录不存在: {old_path}") + return False + + if os.path.exists(new_path): + print(f"错误: 目标目录已存在: {new_path}") + return False + + print(f"将 {old_path} 重命名为 {new_path}") + shutil.move(old_path, new_path) + + # 验证重命名是否成功 + if os.path.exists(new_path) and not os.path.exists(old_path): + print(f"✓ 重命名成功") + return True + else: + print(f"✗ 重命名后验证失败") + return False + + except PermissionError as e: + print(f"✗ 权限错误: {e}") + return False + except OSError as e: + print(f"✗ 系统错误: {e}") + return False + except Exception as e: + print(f"✗ 未知错误: {e}") + return False + +def write_version_file(file_path: str, version: str) -> bool: + """ + 写入版本文件 + + Args: + file_path (str): 版本文件路径 + version (str): 版本号 + + Returns: + bool: 写入是否成功 + """ + try: + os.makedirs(os.path.dirname(file_path), exist_ok=True) + with open(file_path, 'w') as f: + f.write(version) + print(f"✓ 版本文件已更新: {file_path}") + return True + except Exception as e: + print(f"✗ 写入版本文件失败: {e}") + return False + def main(): app_name = sys.argv[1] old_ver_dir = sys.argv[2] @@ -107,8 +139,8 @@ def main(): image = services[first_service].get('image', '') print(f"该服务的镜像: {image}") - new_version = extract_version_from_string(image).get("version", "latest") - old_version = extract_version_from_string(old_ver_dir).get("version", "latest") + new_version : str = extract_version_from_string(image).get("version", "latest") + old_version : str = extract_version_from_string(old_ver_dir).get("version", "latest") print(f"旧版本号: {old_version}") print(f"新版本号: {new_version}") new_ver_dir = replace_version_in_dirname(old_ver_dir, new_version) @@ -116,15 +148,13 @@ def main(): old_path = f"apps/{app_name}/{old_ver_dir}" new_path = f"apps/{app_name}/{new_ver_dir}" - if not os.path.exists(new_path): - print(f"将 {old_path} 重命名为 {new_path}") - shutil.move(old_path, new_path) - + if safe_rename_directory(old_path, new_path): + # 更新版本文件 version_file = f"apps/{app_name}/{old_version}.version" - with open(version_file, 'w') as f: - f.write(new_version) + if not write_version_file(version_file, new_version): + print("版本文件更新失败,但目录重命名成功") else: - print(f"错误: {new_path} 文件夹已存在") + print("错误: 目录重命名失败") sys.exit(1) if __name__ == "__main__":