feat: update rules #32
Workflow file for this run
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: check and fix | |
on: | |
push: | |
branches: | |
- "main" | |
pull_request: | |
types: [opened, synchronize, reopened] | |
paths: | |
- "**.json" | |
- "scripts/check.py" | |
- "scripts/fix_*.py" | |
- "scripts/create_rules_markdown.py" | |
- ".github/workflows/check-and-fix.yml" | |
defaults: | |
run: | |
shell: bash -e {0} | |
permissions: | |
contents: write | |
jobs: | |
check-forbid: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
- uses: astral-sh/setup-uv@v5 | |
with: | |
version: "0.5.9" | |
enable-cache: true | |
- name: Check for forbidden file changes | |
run: | | |
forbidden_files=( | |
"rules.md" | |
"diff.md" | |
"bilibili-danmu-blocklist-output.json" | |
) | |
base_commit=${{ github.event.pull_request.base.sha }} | |
head_commit=${{ github.event.pull_request.head.sha }} | |
# 检查累计提交是否更改了指定文件 | |
changed_files=$(git diff --name-only $base_commit $head_commit) | |
for file in "${forbidden_files[@]}"; do | |
if echo "$changed_files" | grep -q "$file"; then | |
is_changed_from_base=true | |
break | |
fi | |
done | |
# 如果累计提交修改了指定文件,则进一步排除 github-actions[bot] 的提交,检查其余提交是否更改了指定文件 | |
if [ -n "$is_changed_from_base" ]; then | |
# 获取所有提交 | |
all_commits=$(git log --pretty=format:"%H %an" $base_commit..$head_commit) | |
# 排除 github-actions[bot] 的提交 | |
commits=$(echo "$all_commits" | grep -v "github-actions\[bot\]" | awk '{print $1}') | |
# 创建一个临时目录来模拟最终状态 | |
temp_dir=$(mktemp -d) | |
git worktree add $temp_dir $base_commit | |
# 应用所有非 github-actions[bot] 的提交 | |
for commit in $commits; do | |
git -C $temp_dir cherry-pick $commit | |
done | |
# 进行一次 squash 操作 | |
git -C $temp_dir reset --soft $(git -C $temp_dir merge-base HEAD $base_commit) | |
git -C $temp_dir commit -m "Squashed commit" | |
# 检查最终状态是否更改了指定的文件 | |
for file in "${forbidden_files[@]}"; do | |
if git -C $temp_dir ls-tree -r HEAD --name-only | grep -q "$file"; then | |
echo "Error: Forbidden file $file was modified in the commits." | |
git worktree remove $temp_dir --force | |
exit 1 | |
fi | |
done | |
check-json: | |
needs: check-forbid | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
- uses: astral-sh/setup-uv@v5 | |
with: | |
version: "0.5.9" | |
enable-cache: true | |
- name: Set up Python | |
run: uv python install 3.13 | |
- name: Install the project | |
run: uv sync --all-extras --dev | |
- name: Check the json file | |
run: uv run scripts/check.py | |
fix: | |
needs: check-json | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
- uses: astral-sh/setup-uv@v5 | |
with: | |
version: "0.5.9" | |
enable-cache: true | |
- name: Set up Python | |
run: uv python install 3.13 | |
- name: Initialize the dev-environment | |
run: | | |
pip install ruff | |
uv sync --all-extras --dev | |
- name: Fix - Format the python files | |
run: ruff format | |
- name: Fix - Sort the json file | |
run: uv run scripts/fix_sort.py | |
- name: Fix - Add timestamp to the rules | |
run: uv run scripts/fix_id.py | |
- name: Create rules.md | |
run: uv run scripts/create_rules_markdown.py | |
- name: Configure Git | |
run: | | |
git config --global user.name "github-actions[bot]" | |
git config --global user.email "github-actions[bot]@users.noreply.github.com" | |
- name: Check for changes | |
id: git_diff | |
run: | | |
git diff --exit-code || echo "has_changed=true" >> $GITHUB_OUTPUT | |
- name: Commit changes | |
if: ${{ steps.git_diff.outputs.has_changed == 'true' }} | |
run: | | |
git add . | |
git commit -m "fix: auto fix" | |
git pull --rebase origin ${{ github.head_ref }} | |
git push origin HEAD:${{ github.head_ref }} | |
- name: Push changes | |
if: ${{ steps.git_diff.outputs.has_changed == 'true' }} | |
uses: ad-m/github-push-action@master | |
with: | |
github_token: ${{ secrets.GITHUB_TOKEN }} | |
branch: ${{ github.head_ref }} |