|
1 | | -name: GPT Translate per Commit |
| 1 | +name: GPT Translate per PR |
2 | 2 |
|
3 | 3 | on: |
4 | | - push: |
| 4 | + pull_request: |
| 5 | + types: [closed] |
5 | 6 | branches: |
6 | 7 | - main |
7 | | - paths-ignore: |
8 | | - - ".github**/*" |
9 | | - - "docs/cn/**/*" |
10 | | - - "docs/release-notes/**/*" |
11 | | - - "docs/fragment/**/*" |
12 | | - |
13 | | - - "docs/release-stable/**/*" |
14 | | - - "api/**/*" |
15 | | - - "i18n/**/*" |
16 | | - - "src/**/*" |
17 | | - - "static/**/*" |
18 | | - - "types/**/*" |
19 | | - - "README.md" |
20 | 8 |
|
21 | 9 | jobs: |
22 | 10 | gpt_translate: |
| 11 | + if: > |
| 12 | + github.event.pull_request.merged == true && |
| 13 | + !startsWith(github.event.pull_request.head.ref, 'translation-') |
23 | 14 | runs-on: ubuntu-latest |
24 | 15 |
|
25 | 16 | steps: |
26 | | - - name: Checkout repository with two latest commits |
27 | | - uses: actions/checkout@v3 |
| 17 | + - name: Checkout repository |
| 18 | + uses: actions/checkout@v4 |
28 | 19 | with: |
29 | | - fetch-depth: 2 |
| 20 | + fetch-depth: 0 |
30 | 21 |
|
31 | | - - name: get changed files name |
32 | | - id: changed_files |
| 22 | + - name: Collect changed documentation files |
| 23 | + id: collect |
| 24 | + uses: actions/github-script@v7 |
| 25 | + with: |
| 26 | + script: | |
| 27 | + const isDoc = (path) => |
| 28 | + path.startsWith('docs/en/') && |
| 29 | + (path.endsWith('.md') || path.endsWith('.json')); |
| 30 | +
|
| 31 | + const toCnPath = (path) => |
| 32 | + `docs/cn/${path.slice('docs/en/'.length)}`; |
| 33 | +
|
| 34 | + const prNumber = context.payload.pull_request.number; |
| 35 | + const files = await github.paginate( |
| 36 | + github.rest.pulls.listFiles, |
| 37 | + { |
| 38 | + owner: context.repo.owner, |
| 39 | + repo: context.repo.repo, |
| 40 | + pull_number: prNumber, |
| 41 | + per_page: 100, |
| 42 | + } |
| 43 | + ); |
| 44 | +
|
| 45 | + const inputSet = new Set(); |
| 46 | + const removedSet = new Set(); |
| 47 | +
|
| 48 | + for (const file of files) { |
| 49 | + const { filename, status, previous_filename: prev } = file; |
| 50 | +
|
| 51 | + if (status === 'removed' && isDoc(filename)) { |
| 52 | + removedSet.add(toCnPath(filename)); |
| 53 | + continue; |
| 54 | + } |
| 55 | +
|
| 56 | + if (status === 'renamed' && prev && isDoc(prev)) { |
| 57 | + removedSet.add(toCnPath(prev)); |
| 58 | + } |
| 59 | +
|
| 60 | + if (isDoc(filename) && status !== 'removed') { |
| 61 | + inputSet.add(`./${filename}`); |
| 62 | + } |
| 63 | + } |
| 64 | +
|
| 65 | + const inputs = Array.from(inputSet).join(' '); |
| 66 | + const removals = Array.from(removedSet).join(' '); |
| 67 | +
|
| 68 | + core.setOutput('input_files', inputs); |
| 69 | + core.setOutput('removed_cn', removals); |
| 70 | + core.setOutput('has_inputs', inputSet.size > 0 ? 'true' : 'false'); |
| 71 | + core.setOutput('has_removals', removedSet.size > 0 ? 'true' : 'false'); |
| 72 | +
|
| 73 | + - name: Exit if no documentation changes |
| 74 | + if: steps.collect.outputs.has_inputs != 'true' && steps.collect.outputs.has_removals != 'true' |
| 75 | + run: | |
| 76 | + echo "No English documentation additions, updates, or deletions detected in PR #${{ github.event.pull_request.number }}." |
| 77 | + exit 0 |
| 78 | +
|
| 79 | + - name: Snapshot existing translation branches |
| 80 | + if: steps.collect.outputs.has_inputs == 'true' |
| 81 | + id: snapshot |
33 | 82 | run: | |
34 | | - echo "files=$(git diff --diff-filter=d --name-only HEAD^ HEAD | grep '\.md$' | grep -v 'cn' | sed -e 's/^/.\//' | tr '\n' ' ')" >> $GITHUB_OUTPUT |
| 83 | + git ls-remote --heads origin 'translation-*' | awk '{print $2}' | sed 's#refs/heads/##' | sort > /tmp/translation-branches-before.txt |
| 84 | + echo "before=/tmp/translation-branches-before.txt" >> "$GITHUB_OUTPUT" |
35 | 85 |
|
36 | 86 | - name: Run GPT Translate |
| 87 | + if: steps.collect.outputs.has_inputs == 'true' |
37 | 88 | |
38 | 89 | with: |
39 | 90 | github_token: ${{ secrets.GITHUB_TOKEN }} |
|
44 | 95 | target_lang: "Simplified-Chinese" |
45 | 96 | system_prompt: ".github/workflows/prompt.txt" |
46 | 97 | refine_system_prompt: ".github/workflows/refine_prompt.txt" |
47 | | - temperature: ${{ secrets.TEMPERATURE }} |
48 | | - refine_temperature: ${{ secrets.REFINE_TEMPERATURE }} |
49 | | - input_files: "${{ steps.changed_files.outputs.files }}" |
| 98 | + temperature: ${{ secrets.TEMPERATURE }} |
| 99 | + refine_temperature: ${{ secrets.REFINE_TEMPERATURE }} |
| 100 | + input_files: "${{ steps.collect.outputs.input_files }}" |
50 | 101 | output_files: "docs/cn/**/*.{md,json}" |
51 | 102 | pr_title: "Add LLM Translations V2" |
| 103 | + |
| 104 | + - name: Identify translation branch |
| 105 | + if: steps.collect.outputs.has_inputs == 'true' |
| 106 | + id: branch |
| 107 | + env: |
| 108 | + SNAPSHOT_FILE: ${{ steps.snapshot.outputs.before }} |
| 109 | + run: | |
| 110 | + git ls-remote --heads origin 'translation-*' | awk '{print $2}' | sed 's#refs/heads/##' | sort > /tmp/translation-branches-after.txt |
| 111 | + comm -13 "$SNAPSHOT_FILE" /tmp/translation-branches-after.txt > /tmp/new-translation-branches.txt |
| 112 | + branch=$(tail -n 1 /tmp/new-translation-branches.txt) |
| 113 | + if [ -n "$branch" ]; then |
| 114 | + echo "Discovered translation branch: $branch" |
| 115 | + echo "branch=$branch" >> "$GITHUB_OUTPUT" |
| 116 | + else |
| 117 | + echo "Unable to determine translation branch created by GPT workflow." |
| 118 | + echo "branch=" >> "$GITHUB_OUTPUT" |
| 119 | + fi |
| 120 | +
|
| 121 | + - name: Apply deletions to translation branch |
| 122 | + if: > |
| 123 | + steps.collect.outputs.has_inputs == 'true' && |
| 124 | + steps.collect.outputs.has_removals == 'true' && |
| 125 | + steps.branch.outputs.branch != '' |
| 126 | + env: |
| 127 | + REMOVED_FILES: ${{ steps.collect.outputs.removed_cn }} |
| 128 | + TRANSLATION_BRANCH: ${{ steps.branch.outputs.branch }} |
| 129 | + run: | |
| 130 | + set -euo pipefail |
| 131 | + git fetch origin "$TRANSLATION_BRANCH" |
| 132 | + git checkout "$TRANSLATION_BRANCH" |
| 133 | +
|
| 134 | + for file in $REMOVED_FILES; do |
| 135 | + if [ -f "$file" ]; then |
| 136 | + rm -f "$file" |
| 137 | + echo "Removed $file" |
| 138 | + fi |
| 139 | + done |
| 140 | +
|
| 141 | + # Clean up empty directories under docs/cn |
| 142 | + find docs/cn -mindepth 1 -type d -empty -print -delete |
| 143 | +
|
| 144 | + if git status --porcelain | grep .; then |
| 145 | + git config user.name "github-actions[bot]" |
| 146 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 147 | + git add -A |
| 148 | + git commit -m "chore: sync deletions for PR #${{ github.event.pull_request.number }}" |
| 149 | + git push origin "$TRANSLATION_BRANCH" |
| 150 | + else |
| 151 | + echo "No deletions to commit." |
| 152 | + fi |
| 153 | +
|
| 154 | + - name: Prepare deletion-only changes |
| 155 | + if: steps.collect.outputs.has_inputs != 'true' && steps.collect.outputs.has_removals == 'true' |
| 156 | + env: |
| 157 | + REMOVED_FILES: ${{ steps.collect.outputs.removed_cn }} |
| 158 | + run: | |
| 159 | + set -euo pipefail |
| 160 | + for file in $REMOVED_FILES; do |
| 161 | + if [ -f "$file" ]; then |
| 162 | + rm -f "$file" |
| 163 | + echo "Removed $file" |
| 164 | + fi |
| 165 | + done |
| 166 | + find docs/cn -mindepth 1 -type d -empty -print -delete |
| 167 | +
|
| 168 | + - name: Open deletion-only translation PR |
| 169 | + if: steps.collect.outputs.has_inputs != 'true' && steps.collect.outputs.has_removals == 'true' |
| 170 | + uses: peter-evans/create-pull-request@v6 |
| 171 | + with: |
| 172 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 173 | + branch: translation-pr-${{ github.event.pull_request.number }} |
| 174 | + base: main |
| 175 | + commit-message: "chore: sync deletions for PR #${{ github.event.pull_request.number }}" |
| 176 | + title: "AI Translate cleanup for PR #${{ github.event.pull_request.number }}" |
| 177 | + body: | |
| 178 | + This automated PR removes translated files that no longer have an English source from PR #${{ github.event.pull_request.number }}. |
0 commit comments