Skip to content

Fix Chinese translation workflow #88

Fix Chinese translation workflow

Fix Chinese translation workflow #88

name: Translate Notebooks to Chinese
on:
push:
branches:
- main
paths:
- "**/*.ipynb"
- "translate_notebooks.py"
- ".github/workflows/translate-notebooks.yml"
pull_request:
branches:
- main
paths:
- "**/*.ipynb"
- "translate_notebooks.py"
- ".github/workflows/translate-notebooks.yml"
workflow_dispatch:
permissions:
contents: write
concurrency:
group: translate-notebooks-${{ github.ref }}
cancel-in-progress: true
jobs:
translate:
if: github.actor != 'github-actions[bot]' && (github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository)
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Install dependencies
run: |
pip install nbformat requests
- name: Determine notebooks to translate
id: notebooks
shell: bash
run: |
set -euo pipefail
NOTEBOOK_LIST=.changed-notebooks.txt
: > "$NOTEBOOK_LIST"
if [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then
find . -type f -name '*.ipynb' -not -path './translated-notebooks/*' | sed 's|^\./||' | sort > "$NOTEBOOK_LIST"
elif [[ "${{ github.event_name }}" == "pull_request" ]]; then
git diff --name-only "${{ github.event.pull_request.base.sha }}" "${{ github.event.pull_request.head.sha }}" -- '*.ipynb' ':(exclude)translated-notebooks/**' | sort > "$NOTEBOOK_LIST"
elif [[ "${{ github.event.before }}" == "0000000000000000000000000000000000000000" ]]; then
find . -type f -name '*.ipynb' -not -path './translated-notebooks/*' | sed 's|^\./||' | sort > "$NOTEBOOK_LIST"
else
git diff --name-only "${{ github.event.before }}" "${{ github.sha }}" -- '*.ipynb' ':(exclude)translated-notebooks/**' | sort > "$NOTEBOOK_LIST"
fi
NOTEBOOK_COUNT="$(grep -c . "$NOTEBOOK_LIST" || true)"
echo "count=$NOTEBOOK_COUNT" >> "$GITHUB_OUTPUT"
echo "file_list=$NOTEBOOK_LIST" >> "$GITHUB_OUTPUT"
echo "Selected notebooks:"
cat "$NOTEBOOK_LIST" || true
- name: Translate Notebooks
if: steps.notebooks.outputs.count != '0'
env:
DASHSCOPE_API_KEY: ${{ secrets.DASHSCOPE_API_KEY }}
QWEN_API_KEY: ${{ secrets.QWEN_API_KEY }}
DASHSCOPE_API_ENDPOINT: ${{ vars.DASHSCOPE_API_ENDPOINT }}
QWEN_API_ENDPOINT: ${{ vars.QWEN_API_ENDPOINT }}
QWEN_MODEL: ${{ vars.QWEN_MODEL }}
run: |
python translate_notebooks.py --file-list "${{ steps.notebooks.outputs.file_list }}"
- name: Commit translated notebooks
if: github.event_name != 'pull_request' && steps.notebooks.outputs.count != '0'
run: |
if [ -z "$(git status --porcelain -- translated-notebooks)" ]; then
echo "No translated notebook changes to commit."
exit 0
fi
git config user.email "github-actions[bot]@users.noreply.github.com"
git config user.name "GitHub Actions Bot"
git add translated-notebooks
git commit -m "chore: update Chinese notebook translations"
git push origin HEAD:main