From fec492b2aba23846b1766b948f305f964474bd9d Mon Sep 17 00:00:00 2001 From: smallfawn <860562056@QQ.COM> Date: Thu, 2 Apr 2026 08:22:51 +0800 Subject: [PATCH] Improve README updater to support .py files Enhance the GitHub Actions workflow that updates the README table: add .py to watched paths, broaden changed-file detection, and support both .js and .py targets. Make the Env name regex more flexible (matches Env(...)), add robust error handling for git diff, check README.md exists, and simplify table parsing/append logic so new scripts are added and existing rows updated correctly. Also tidy comments/spacing and adjust the automated commit message. --- .github/workflows/checkScriptStatus.yml | 103 +++++++++--------------- 1 file changed, 40 insertions(+), 63 deletions(-) diff --git a/.github/workflows/checkScriptStatus.yml b/.github/workflows/checkScriptStatus.yml index 8228c35..29b8716 100644 --- a/.github/workflows/checkScriptStatus.yml +++ b/.github/workflows/checkScriptStatus.yml @@ -1,25 +1,24 @@ -#目的是给非backup目录下的触发提交的文件,给相应markdown表格的标签打成绿色,记录更新时间等等:待AI实现 name: Update README Table on: push: branches: - - main # 如果你的主分支是 master,请将其改为 master + - main # 确认你的主分支名是 main 而不是 master paths: - - '**/*.js' # 只有在修改或新增 JS 文件时才触发 - - '**/*.py' # 只有在修改或新增 JS 文件时才触发 + - '**/*.js' + - '**/*.py' jobs: update-readme: runs-on: ubuntu-latest permissions: - contents: write # 必须赋予写入权限,以便工作流可以推送更改到仓库 + contents: write steps: - name: 检出代码 uses: actions/checkout@v4 with: - fetch-depth: 2 # 必须拉取历史记录以便使用 git diff 查找更改的文件 + fetch-depth: 2 - name: 配置 Python 环境 uses: actions/setup-python@v5 @@ -28,116 +27,94 @@ jobs: - name: 运行更新脚本 env: - TZ: Asia/Shanghai # 确保时间是北京时间 + TZ: Asia/Shanghai run: | - # 创建并执行 Python 处理脚本 cat << 'EOF' > update_readme.py import os import re import datetime import subprocess - # 1. 获取本次提交更改的 JS 文件列表 + # 1. 获取更改的文件列表(增加对 .py 的支持) try: diff_cmd = ['git', 'diff', '--name-only', 'HEAD~1', 'HEAD'] result = subprocess.run(diff_cmd, capture_output=True, text=True, check=True) changed_files = result.stdout.splitlines() - except subprocess.CalledProcessError: - print("无法获取差异文件,跳过...") + except Exception: changed_files = [] - js_files = [f for f in changed_files if f.endswith('.js') and os.path.exists(f)] + # 同时匹配 js 和 py 文件 + target_files = [f for f in changed_files if (f.endswith('.js') or f.endswith('.py')) and os.path.exists(f)] - if not js_files: - print("没有 JS 文件发生更改。") + if not target_files: + print("没有符合条件的脚本文件发生更改。") exit(0) - # 2. 提取文件中的脚本名称 (匹配 new Env("XXX")) + # 2. 提取文件中的脚本名称 now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') updates = {} - for f in js_files: + for f in target_files: with open(f, 'r', encoding='utf-8') as file: content = file.read() - match = re.search(r'new\s+Env\s*\(\s*["\']([^"\']+)["\']\s*\)', content) + # 匹配 new Env("XXX") 或 Env('XXX') + match = re.search(r'Env\s*\(\s*["\']([^"\']+)["\']\s*\)', content) if match: script_name = match.group(1) updates[script_name] = now if not updates: - print("修改的文件中未找到 new Env 声明。") + print("修改的文件中未找到 Env 声明。") exit(0) - # 3. 读取并修改 README.md 中的表格 + # 3. 修改 README.md readme_path = 'README.md' + if not os.path.exists(readme_path): + print("未找到 README.md") + exit(1) + with open(readme_path, 'r', encoding='utf-8') as file: lines = file.readlines() new_lines = [] - in_table = False updated_scripts_set = set() - + + # 处理表格 for line in lines: - # 定位表格头部 - if '|' in line and '脚本名称' in line and '更新时间' in line: - in_table = True - new_lines.append(line) - continue - - if in_table: - # 如果遇到空行或非表格格式,说明表格结束 - if not line.strip() or not line.strip().startswith('|'): - in_table = False - # 将新增的脚本(表格中之前没有的)追加在表格末尾 - for script, time in updates.items(): - if script not in updated_scripts_set: - new_lines.append(f"| {script} | {time} | ✅ |\n") - updated_scripts_set.add(script) - new_lines.append(line) - continue - - # 忽略 Markdown 表格分割线 (例如 |:---|:---:|:---:|) - if re.match(r'^\s*\|?[\s\:\-]+\|[\s\:\-]+\|[\s\:\-]+\|?\s*$', line): - new_lines.append(line) - continue - - # 处理现有的表格数据行 + processed = False + # 匹配表格行,并提取脚本名 + if line.strip().startswith('|'): parts = [p.strip() for p in line.split('|')] - if len(parts) >= 4: # | 1:脚本名称 | 2:更新时间 | 3:当前状态 | + if len(parts) >= 4: script_name = parts[1] if script_name in updates: - parts[2] = updates[script_name] - parts[3] = "✅" + parts[2] = updates[script_name] # 更新时间 + parts[3] = "✅" # 状态 line = f"| {parts[1]} | {parts[2]} | {parts[3]} |\n" updated_scripts_set.add(script_name) - + processed = True new_lines.append(line) - # 兜底:如果 README 以表格结尾(后面没有空行) - if in_table: - for script, time in updates.items(): - if script not in updated_scripts_set: - new_lines.append(f"| {script} | {time} | ✅ |\n") - updated_scripts_set.add(script) + # 如果是新脚本,追加到末尾 + for script, time in updates.items(): + if script not in updated_scripts_set: + new_lines.append(f"| {script} | {time} | ✅ |\n") with open(readme_path, 'w', encoding='utf-8') as file: file.writelines(new_lines) - - print(f"成功更新了以下脚本的状态: {list(updated_scripts_set)}") + + print(f"成功更新: {list(updates.keys())}") EOF python update_readme.py - - name: 提交并推送到仓库 + - name: 提交并推送 run: | git config --local user.email "github-actions[bot]@users.noreply.github.com" git config --local user.name "github-actions[bot]" git add README.md - - # 检查是否有文件更改,如果有则提交。 - # [skip ci] 非常重要!它能防止 Action 无限循环触发自身。 if ! git diff --staged --quiet; then - git commit -m "docs: 自动更新脚本列表状态 [skip ci]" + git commit -m "docs: 自动更新脚本状态 [skip ci]" git push else - echo "README.md 无变化,不需要提交。" + echo "没有变化需要提交" fi \ No newline at end of file