|
| 1 | +name: i18n-sync |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + inputs: |
| 6 | + reviewer: |
| 7 | + description: "自动请求评审的 GitHub 用户名(可留空)" |
| 8 | + required: false |
| 9 | + schedule: |
| 10 | + # 每天 UTC 03:00 自动运行(可按需调整) 测试 |
| 11 | + - cron: "0 3 * * *" |
| 12 | + |
| 13 | +permissions: |
| 14 | + contents: write |
| 15 | + pull-requests: write |
| 16 | + |
| 17 | +env: |
| 18 | + I18N_SYNC_BRANCH: "chore/i18n-auto-sync" |
| 19 | + PR_TITLE: "chore: auto sync OCR i18n expected" |
| 20 | + DEFAULT_PR_REVIEWER: "EeeMaoY" |
| 21 | + |
| 22 | +jobs: |
| 23 | + sync-ocr-expected: |
| 24 | + if: github.event_name == 'workflow_dispatch' || github.repository == '1bananachicken/MaaNTE' |
| 25 | + runs-on: ubuntu-latest |
| 26 | + steps: |
| 27 | + - name: Checkout repository |
| 28 | + uses: actions/checkout@v4 |
| 29 | + with: |
| 30 | + token: ${{ secrets.MAANTE_BOT_TOKEN }} |
| 31 | + fetch-depth: 0 |
| 32 | + |
| 33 | + - name: Setup Python |
| 34 | + uses: actions/setup-python@v5 |
| 35 | + with: |
| 36 | + python-version: "3.12" |
| 37 | + |
| 38 | + - name: Prepare fixed sync branch |
| 39 | + env: |
| 40 | + BASE_BRANCH: ${{ github.event.repository.default_branch }} |
| 41 | + run: | |
| 42 | + git checkout -B "$I18N_SYNC_BRANCH" "origin/$BASE_BRANCH" |
| 43 | +
|
| 44 | + - name: Clone translation repository |
| 45 | + run: | |
| 46 | + git clone --depth 1 https://github.com/Abino01/ExtractForNTE.git "$RUNNER_TEMP/ExtractForNTE" |
| 47 | +
|
| 48 | + - name: Show translation repository commit |
| 49 | + run: | |
| 50 | + git -C "$RUNNER_TEMP/ExtractForNTE" rev-parse HEAD |
| 51 | +
|
| 52 | + - name: Run OCR expected sync (write mode) |
| 53 | + run: | |
| 54 | + python tools/i18n/sync_ocr_expected.py --write --i18n-dir "$RUNNER_TEMP/ExtractForNTE" |
| 55 | +
|
| 56 | + - name: Check changes |
| 57 | + id: changes |
| 58 | + run: | |
| 59 | + if git diff --quiet; then |
| 60 | + echo "has_changes=false" >> "$GITHUB_OUTPUT" |
| 61 | + else |
| 62 | + echo "has_changes=true" >> "$GITHUB_OUTPUT" |
| 63 | + fi |
| 64 | +
|
| 65 | + - name: Commit and push changes |
| 66 | + if: steps.changes.outputs.has_changes == 'true' |
| 67 | + uses: actions-js/push@master |
| 68 | + with: |
| 69 | + force: true |
| 70 | + branch: ${{ env.I18N_SYNC_BRANCH }} |
| 71 | + message: "chore: auto sync OCR i18n expected [skip changelog]" |
| 72 | + github_token: ${{ secrets.MAANTE_BOT_TOKEN }} |
| 73 | + |
| 74 | + - name: Create PR when missing |
| 75 | + if: steps.changes.outputs.has_changes == 'true' |
| 76 | + env: |
| 77 | + GH_TOKEN: ${{ secrets.MAANTE_BOT_TOKEN }} |
| 78 | + BASE_BRANCH: ${{ github.event.repository.default_branch }} |
| 79 | + PR_REVIEWER: ${{ github.event.inputs.reviewer || env.DEFAULT_PR_REVIEWER }} |
| 80 | + run: | |
| 81 | + pr_number="$(gh pr list --base "$BASE_BRANCH" --head "$I18N_SYNC_BRANCH" --state open --json number --jq '.[0].number')" |
| 82 | + if [ -n "$pr_number" ]; then |
| 83 | + echo "PR already exists: #$pr_number" |
| 84 | + if [ -n "$PR_REVIEWER" ]; then |
| 85 | + if ! output="$(gh pr edit "$pr_number" --add-reviewer "$PR_REVIEWER" 2>&1)"; then |
| 86 | + if echo "$output" | grep -qi "already requested"; then |
| 87 | + echo "Skip add reviewer (already requested)" |
| 88 | + else |
| 89 | + echo "$output" >&2 |
| 90 | + exit 1 |
| 91 | + fi |
| 92 | + fi |
| 93 | + fi |
| 94 | + exit 0 |
| 95 | + fi |
| 96 | + pr_body="$(cat <<'EOF' |
| 97 | + ## Summary |
| 98 | + - Auto run i18n OCR expected sync. |
| 99 | + - Update expected values based on latest ExtractForNTE translation repository. |
| 100 | +
|
| 101 | + ## Trigger |
| 102 | + - workflow_dispatch |
| 103 | + - schedule (daily UTC) |
| 104 | + EOF |
| 105 | + )" |
| 106 | + reviewer_args=() |
| 107 | + if [ -n "$PR_REVIEWER" ]; then |
| 108 | + reviewer_args=(--reviewer "$PR_REVIEWER") |
| 109 | + fi |
| 110 | + gh pr create \ |
| 111 | + --base "$BASE_BRANCH" \ |
| 112 | + --head "$I18N_SYNC_BRANCH" \ |
| 113 | + --title "$PR_TITLE" \ |
| 114 | + --body "$pr_body" \ |
| 115 | + "${reviewer_args[@]}" |
| 116 | +
|
| 117 | + - name: Show changed files |
| 118 | + run: | |
| 119 | + git status --short |
0 commit comments