update #141
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: generate list | |
on: | |
push: | |
branches: | |
- main # 或者指定您仓库的默认分支 | |
jobs: | |
update-list: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v3 | |
- name: Set up Python | |
uses: actions/setup-python@v4 | |
with: | |
python-version: '3.x' | |
- name: Generate list.txt | |
run: | | |
python - <<EOF | |
import json | |
import os | |
from pathlib import Path | |
problems_dir = Path('./problems') | |
if not problems_dir.exists(): | |
with open(problems_dir / 'list.txt', 'w') as f: | |
f.write("No problems directory found") | |
exit(0) | |
# 存储 id 和 title 的列表 | |
problem_list = [] | |
# 遍历 problems 目录 | |
for problem_dir in problems_dir.iterdir(): | |
if problem_dir.is_dir(): | |
json_file = problem_dir / 'problem.json' | |
if json_file.exists(): | |
try: | |
with open(json_file, 'r', encoding='utf-8') as f: | |
data = json.load(f) | |
problem_id = problem_dir.name | |
title = data.get('title', '') | |
if title: # 只有当 title 存在时才添加 | |
problem_list.append((int(problem_id), title)) | |
except (json.JSONDecodeError, ValueError) as e: | |
print(f"Error processing {json_file}: {e}") | |
# 按 id 排序 | |
problem_list.sort(key=lambda x: x[0]) | |
# 写入文件 | |
with open(problems_dir / 'list.txt', 'w', encoding='utf-8') as f: | |
for _, title in problem_list: | |
f.write(f"{title}\n") | |
EOF | |
- name: Commit and push changes | |
run: | | |
git config --global user.name "github-actions[bot]" | |
git config --global user.email "github-actions[bot]@users.noreply.github.com" | |
git add ./problems/list.txt | |
git commit -m "Update list.txt" | |
git push | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |