Files
market/.github/workflows/i18n-translate.yml
Yige b8101406fb ci: 引入分支保护配套工作流(path 过滤 → detect-changes,AI review 自动化) (#5)
## Summary / 概要

为了让 `main` 分支保护规则在所有 PR 上稳定生效,调整两个现有 i18n workflow 并新增两个 AI review 配套
workflow。

### 已配置的 main ruleset(ID 16298003)

- 只允许 squash merge,禁止 force push / 删除
- 必需 status check:`validate`、`translate`,require
`strict_required_status_checks_policy=true`(必须 rebase 最新)
- PR
rule:`required_approving_review_count=0`、`required_review_thread_resolution=true`、`dismiss_stale_reviews_on_push=true`
- `bypass_actors=[]`:管理员也不能绕过

### 本 PR 改动

1. **i18n-validate / i18n-translate**:移除 `on.pull_request.paths`,把过滤下移到
job 内 `detect-changes` step。路径不相关时跳过实际逻辑但 job 仍 success,避免 required
check 因 path 过滤缺失而卡死非 i18n PR。
2. **auto-request-copilot-review** (新增):PR
opened/reopened/ready_for_review 时自动 `POST
/pulls/{n}/requested_reviewers` 加 Copilot 为 reviewer。
3. **wait-for-copilot-review** (新增):PR
opened/reopened/synchronize/ready_for_review 时启动,轮询 reviews API 等待
`copilot-pull-request-reviewer[bot]` 在当前 head SHA 提交 review。超时 8 分钟则
fail。
- 选择 polling 而不是 `pull_request_review` 事件触发是因为:Copilot bot 触发的
`pull_request_review` 事件被 GitHub 视为 first-time contributor,workflow run
会卡在 `action_required` 状态需 maintainer 在 web UI 手动批准,无法自动化。

合并后会再 PATCH ruleset 16298003,把 `wait-for-copilot-review` 加入 required
status checks 作为强制门槛。

## Test plan

- [x] `i18n-validate` 在本 PR 上跑出 success(PR 仅触及
`.github/workflows/`,relevant=false,走 skip 分支)
- [x] `i18n-translate` 同上
- [x] `auto-request-copilot-review` 跑出 success,Copilot 被自动加为 reviewer
- [x] `wait-for-copilot-review` 在 polling 窗口内捕获到 Copilot review 并 pass
- [ ] 解决 Copilot 提的 conversations 后能 squash merge
- [ ] 合并后用 PATCH ruleset 把 `wait-for-copilot-review` 加入 required checks
2026-05-13 00:21:51 +08:00

177 lines
6.9 KiB
YAML

name: i18n Auto-Translate
# Uses GitHub Models inference API (https://models.github.ai/inference) with the
# repository's GITHUB_TOKEN. Requires `models: read` permission. Default model is
# openai/gpt-5-mini (a fast/cheap GPT-5 in the catalog); override via repository variable
# TRANSLATE_MODEL (e.g. openai/gpt-5-nano for cheaper, openai/gpt-5 for flagship).
#
# Optional: to use Anthropic Claude directly, add a repo secret ANTHROPIC_API_KEY,
# then set repository variable TRANSLATE_BACKEND=anthropic and TRANSLATE_MODEL to
# a Claude model id (e.g. claude-sonnet-4-6). Claude is NOT in the GitHub Models
# catalog as of 2026-05.
on:
pull_request:
workflow_dispatch:
inputs:
target_locale:
description: "Target locale (default: all from manifest.supportedLocales)"
required: false
skill:
description: "Specific skill path, e.g. skills/web-access (default: all)"
required: false
permissions:
contents: write
pull-requests: write
models: read
concurrency:
group: i18n-translate-${{ github.ref }}
cancel-in-progress: true
jobs:
translate:
# Don't run on bot's own commits to avoid loops
if: github.actor != 'desirecore-bot[bot]' && !contains(github.event.head_commit.message, '[skip ci]')
runs-on: ubuntu-latest
timeout-minutes: 30
steps:
- name: Checkout PR branch
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.ref || github.ref }}
token: ${{ secrets.DESIRECORE_BOT_TOKEN || secrets.GITHUB_TOKEN }}
fetch-depth: 0
- name: Detect relevant changes
id: changes
env:
BASE_SHA: ${{ github.event.pull_request.base.sha }}
HEAD_SHA: ${{ github.event.pull_request.head.sha || github.sha }}
run: |
set -e
if [ "${{ github.event_name }}" != "pull_request" ]; then
echo "relevant=true" >> "$GITHUB_OUTPUT"
exit 0
fi
git fetch --no-tags --depth=1 origin "$BASE_SHA" 2>/dev/null || true
changed=$(git diff --name-only "${BASE_SHA}...${HEAD_SHA}" || true)
if echo "$changed" | grep -qE '^(skills/.+/SKILL(\.zh-CN)?\.md$|manifest\.json$|categories\.json$)'; then
echo "relevant=true" >> "$GITHUB_OUTPUT"
else
echo "relevant=false" >> "$GITHUB_OUTPUT"
fi
- name: Install uv
if: steps.changes.outputs.relevant == 'true'
uses: astral-sh/setup-uv@v3
- name: Check for stale translations
id: precheck
if: steps.changes.outputs.relevant == 'true'
run: |
set +e
uv run --quiet scripts/i18n/translate.py --check
rc=$?
if [ $rc -eq 0 ]; then
echo "stale=false" >> "$GITHUB_OUTPUT"
else
echo "stale=true" >> "$GITHUB_OUTPUT"
fi
exit 0
- name: Translate stale locales
if: steps.changes.outputs.relevant == 'true' && steps.precheck.outputs.stale == 'true'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
TRANSLATE_BACKEND: ${{ vars.TRANSLATE_BACKEND || 'github' }}
TRANSLATE_MODEL: ${{ vars.TRANSLATE_MODEL || 'openai/gpt-5-mini' }}
run: |
set -e
ARGS=()
if [ -n "${{ github.event.inputs.skill }}" ]; then
ARGS+=("${{ github.event.inputs.skill }}")
fi
if [ -n "${{ github.event.inputs.target_locale }}" ]; then
ARGS+=(--target "${{ github.event.inputs.target_locale }}")
fi
uv run --quiet scripts/i18n/translate.py "${ARGS[@]}"
- name: Validate after translation
if: steps.changes.outputs.relevant == 'true' && steps.precheck.outputs.stale == 'true'
run: uv run --quiet scripts/i18n/validate-i18n.py
- name: Detect changes
id: diff
if: steps.changes.outputs.relevant == 'true' && steps.precheck.outputs.stale == 'true'
run: |
if git diff --quiet; then
echo "changed=false" >> "$GITHUB_OUTPUT"
else
echo "changed=true" >> "$GITHUB_OUTPUT"
git diff --stat
fi
- name: Commit & push translations
if: steps.diff.outputs.changed == 'true'
run: |
git config user.name "desirecore-bot"
git config user.email "bot@desirecore.net"
git add -A
git commit -m "chore(i18n): auto-translate skills [skip ci]" \
-m "Generated by scripts/i18n/translate.py via i18n-translate workflow." \
-m "Backend: ${TRANSLATE_BACKEND:-github} Model: ${TRANSLATE_MODEL:-openai/gpt-5-mini}"
git push
env:
TRANSLATE_BACKEND: ${{ vars.TRANSLATE_BACKEND || 'github' }}
TRANSLATE_MODEL: ${{ vars.TRANSLATE_MODEL || 'openai/gpt-5-mini' }}
- name: Comment on PR
if: steps.diff.outputs.changed == 'true' && github.event_name == 'pull_request'
uses: actions/github-script@v7
with:
github-token: ${{ secrets.DESIRECORE_BOT_TOKEN || secrets.GITHUB_TOKEN }}
script: |
const backend = process.env.TRANSLATE_BACKEND || 'github';
const model = process.env.TRANSLATE_MODEL || 'openai/gpt-5-mini';
const body = [
'🌐 **i18n auto-translation pushed**',
'',
'New machine-translated content was added to this PR by `scripts/i18n/translate.py`.',
'',
'**Please review the translated strings** before merging:',
'- Check terminology against `scripts/i18n/glossary.json`',
'- Verify Markdown structure (headings/tables/code fences) is preserved',
'- If you edit a translation, set `metadata.i18n.<locale>.translated_by: human` to lock it.',
'',
`Backend: \`${backend}\` Model: \`${model}\``,
].join('\n');
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body,
});
env:
TRANSLATE_BACKEND: ${{ vars.TRANSLATE_BACKEND || 'github' }}
TRANSLATE_MODEL: ${{ vars.TRANSLATE_MODEL || 'openai/gpt-5-mini' }}
- name: Label on translation failure
if: failure() && github.event_name == 'pull_request'
uses: actions/github-script@v7
with:
github-token: ${{ secrets.DESIRECORE_BOT_TOKEN || secrets.GITHUB_TOKEN }}
script: |
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
labels: ['i18n-translation-failed'],
});
- name: Skip notice
if: steps.changes.outputs.relevant != 'true'
run: echo "No i18n-relevant changes; translation skipped."