QLScriptPublic/.github/workflows/checkScriptStatus.yml
2026-04-07 15:07:20 +08:00

149 lines
5.1 KiB
YAML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

name: Update README Table
on:
push:
branches:
- main
paths:
- 'daily/**' # 仅监控 daily 目录下的变更
- '!daily/notify.py' # 排除 notify.py
- '!daily/**/*.md' # 排除 md 文件
jobs:
update-readme:
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: 检出代码
uses: actions/checkout@v4
with:
fetch-depth: 2
- name: 配置 Python 环境
uses: actions/setup-python@v5
with:
python-version: '3.x'
- name: 运行更新脚本
env:
TZ: Asia/Shanghai
run: |
cat << 'EOF' > update_readme.py
import os
import re
import datetime
import subprocess
# 1. 获取更改的文件列表
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:
diff_cmd = ['git', 'diff', '--name-only', '--cached']
result = subprocess.run(diff_cmd, capture_output=True, text=True, check=True)
changed_files = result.stdout.splitlines()
except Exception:
changed_files = []
# 2. 筛选逻辑:必须在 daily 目录下,且不是 notify.py 和 .md 文件
target_files = []
for f in changed_files:
# 检查文件是否在 daily 目录下且文件存在
if f.startswith('daily/') and os.path.exists(f):
filename = os.path.basename(f)
# 排除 notify.py 和所有 .md 后缀文件
if filename != 'notify.py' and not f.endswith('.md'):
target_files.append(f)
if not target_files:
print("没有检测到符合条件的脚本文件变更(排除 notify.py 和 md退出。")
exit(0)
# 3. 提取 Env 名称
now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
updates = {}
for f in target_files:
with open(f, 'r', encoding='utf-8') as file:
content = file.read()
match = re.search(r'Env\s*\(\s*["\']([^"\']+)["\']\s*\)', content)
if match:
updates[match.group(1)] = now
if not updates:
print("变更的脚本中未找到 Env 标签,退出。")
exit(0)
# 4. 修改 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 f:
full_text = f.read()
start_tag = "<!-- README_TABLE_START -->"
end_tag = "<!-- README_TABLE_END -->"
if start_tag not in full_text or end_tag not in full_text:
print("错误:未找到锚点标记!")
exit(1)
pre_part, temp = full_text.split(start_tag)
table_section, post_part = temp.split(end_tag)
lines = table_section.strip().split('\n')
header, separator, body_lines = [], [], []
mode = 'header'
for line in lines:
l_s = line.strip()
if not l_s.startswith('|'): continue
if '脚本名称' in l_s:
header.append(line)
mode = 'separator'
elif '---' in l_s and mode == 'separator':
separator.append(line)
mode = 'body'
elif mode == 'body':
body_lines.append(line)
existing_names = set()
new_body_lines = []
for line in body_lines:
parts = [p.strip() for p in line.split('|')]
if len(parts) >= 4:
name = parts[1]
existing_names.add(name)
if name in updates:
line = f"| {name} | {updates[name]} | ✅ |"
new_body_lines.append(line)
for name, tm in updates.items():
if name not in existing_names:
new_body_lines.append(f"| {name} | {tm} | ✅ |")
final_table = "\n".join(header + separator + new_body_lines)
final_content = f"{pre_part}{start_tag}\n{final_table}\n{end_tag}{post_part}"
with open(readme_path, 'w', encoding='utf-8') as f:
f.write(final_content)
print("README.md 更新成功。")
EOF
python update_readme.py
- 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
if ! git diff --staged --quiet; then
git commit -m "docs: 自动更新 daily 脚本状态 [skip ci]"
git push
else
echo "无变更需推送。"
fi