Skip to content

Auto Doc Translate

Auto Doc Translate #65

#
# Copyright (c) 2025 Huawei Technologies Co., Ltd. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# This file is a part of the vllm-ascend project.
#
name: Auto Doc Translate
on:
schedule:
- cron: '0 0 */3 * *'
workflow_dispatch:
inputs:
target_branch:
description: 'Target branch for the upstream PR'
required: false
default: 'main'
type: string
concurrency:
group: translation-${{ github.ref }}
cancel-in-progress: true
jobs:
translate:
runs-on: linux-amd64-cpu-8-hk
if: github.event_name == 'workflow_dispatch' || github.event_name == 'schedule'
permissions:
contents: read
env:
UPSTREAM_REPO: vllm-project/vllm-ascend
steps:
- name: Checkout repository
uses: actions/checkout@v6
with:
repository: vllm-ascend-ci/vllm-ascend
token: ${{ secrets.PAT_TOKEN }}
ref: main
- name: Setup git and branch
run: |
TARGET_BRANCH="${{ inputs.target_branch || 'main' }}"
echo "TARGET_BRANCH=${TARGET_BRANCH}" >> $GITHUB_ENV
BRANCH_NAME="auto-pr/doc-translate-$(date +%Y%m%d%H%M%S)"
echo "BRANCH_NAME=${BRANCH_NAME}" >> $GITHUB_ENV
git remote add upstream "https://github.com/${{ env.UPSTREAM_REPO }}.git"
git fetch upstream
git config user.name "${{ github.actor }}"
git config user.email "${{ github.actor }}@users.noreply.github.com"
git checkout -B "${BRANCH_NAME}" "upstream/${TARGET_BRANCH}"
# Use latest translation script from upstream/main
git checkout upstream/main -- .github/workflows/scripts/po_translate.py
git checkout upstream/main -- docs/requirements-docs.txt
git restore --staged .github/workflows/scripts/po_translate.py docs/requirements-docs.txt
- name: Setup Python
uses: actions/setup-python@v6
with:
python-version: '3.11'
- name: Build docs and generate PO files
working-directory: ./docs
run: |
sudo apt-get update && sudo apt-get install -y make
pip install -r requirements-docs.txt
make clean && make html
sphinx-build -b gettext source _build/gettext
sphinx-intl update -p _build/gettext -l zh_CN
- name: Detect PO files to translate
id: detect
run: |
git config --global --add safe.directory "$GITHUB_WORKSPACE"
tracked=$(git diff --name-only --diff-filter=ACMUX || true)
untracked=$(git ls-files --others --exclude-standard || true)
po_files=$(printf "%s\n%s" "$tracked" "$untracked" | grep '\.po$' | tr '\n' ',' | sed 's/,$//' || true)
if [ -z "$po_files" ]; then
echo "No PO files to translate"
echo "has_changes=false" >> $GITHUB_OUTPUT
else
echo "PO files: $po_files"
echo "has_changes=true" >> $GITHUB_OUTPUT
echo "files=$po_files" >> $GITHUB_OUTPUT
fi
- name: Translate PO files
if: steps.detect.outputs.has_changes == 'true'
env:
DEEPSEEK_API_KEY: ${{ secrets.DEEPSEEK_API_KEY }}
run: |
python .github/workflows/scripts/po_translate.py \
--files "${{ steps.detect.outputs.files }}" \
--output-json /tmp/translation_results.json
- name: Process translated files
if: steps.detect.outputs.has_changes == 'true'
id: results
run: |
[ ! -f /tmp/translation_results.json ] && echo "No results" && exit 1
success_files=$(python3 -c "
import json, os
with open('/tmp/translation_results.json') as f:
for p in json.load(f).get('success_files', []):
if os.path.exists(p): print(p)
")
[ -z "$success_files" ] && echo "No files translated" && exit 1
count=0
file_list=""
mkdir -p /tmp/translated_po_files
while IFS= read -r file; do
[ -n "$file" ] && [ -f "$file" ] && {
count=$((count + 1))
file_list="${file_list}- <code>${file}</code>"$'\n'
git add "$file"
}
done <<< "$success_files"
[ $count -eq 0 ] && echo "No files processed" && exit 1
echo "Processed $count file(s)"
{
echo "file_count=$count"
echo "file_list<<EOF"
echo "$file_list"
echo "EOF"
} >> $GITHUB_OUTPUT
- name: Commit and push
if: steps.detect.outputs.has_changes == 'true'
env:
GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }}
run: |
git diff --cached --quiet && echo "Nothing to commit" && exit 1
count=$(git diff --cached --name-only | wc -l)
git commit -s -m "[Doc] Auto-translate ${count} file(s)"
git push -f origin "${{ env.BRANCH_NAME }}"
- name: Create PR in upstream
if: steps.detect.outputs.has_changes == 'true'
uses: actions/github-script@v8
env:
FILE_LIST: ${{ steps.results.outputs.file_list }}
FILE_COUNT: ${{ steps.results.outputs.file_count }}
RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
PR_HEAD: vllm-ascend-ci:${{ env.BRANCH_NAME }}
PR_BASE: ${{ env.TARGET_BRANCH }}
with:
github-token: ${{ secrets.PAT_TOKEN }}
script: |
try {
const branchLabel = process.env.PR_BASE.split('/').pop();
const date = new Date().toISOString().slice(0, 10);
const prTitle = branchLabel === 'main'
? '[Doc] Translated Doc files ' + date
: '[' + branchLabel + '][Doc] Translated Doc files ' + date;
const prBody = '## Auto-Translation Summary\n\n' +
'Translated **' + process.env.FILE_COUNT + '** file(s):\n\n' +
process.env.FILE_LIST + '\n---\n\n' +
'[Workflow run](' + process.env.RUN_URL + ')';
const pr = await github.rest.pulls.create({
owner: 'vllm-project', repo: 'vllm-ascend',
head: process.env.PR_HEAD, base: process.env.PR_BASE,
title: prTitle, body: prBody
});
core.info('Created PR #' + pr.data.number);
} catch (error) {
if (error.message.includes('A pull request already exists')) {
core.warning('PR already exists');
} else { throw error; }
}