-
Notifications
You must be signed in to change notification settings - Fork 42
193 lines (168 loc) · 7.27 KB
/
docs-commit.translate.yaml
File metadata and controls
193 lines (168 loc) · 7.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
name: GPT Translate per PR
on:
workflow_dispatch:
inputs:
pr_number:
description: "Optional: PR number or link to sync"
required: false
jobs:
gpt_translate:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Collect changed documentation files
id: collect
uses: actions/github-script@v7
env:
PR_INPUT: ${{ inputs.pr_number }}
with:
script: |
const isDoc = (path) =>
path.startsWith('docs/en/') &&
(path.endsWith('.md') || path.endsWith('.json'));
const toCnPath = (path) =>
`docs/cn/${path.slice('docs/en/'.length)}`;
const rawInput = (process.env.PR_INPUT || '').trim();
let prNumber = null;
if (rawInput) {
const match = rawInput.match(/\d+$/);
if (match) {
prNumber = Number.parseInt(match[0], 10);
}
}
if (!prNumber) {
core.setFailed('Please provide the PR number (or a link ending with the number).');
return;
}
core.setOutput('pr_number', String(prNumber));
const files = await github.paginate(
github.rest.pulls.listFiles,
{
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber,
per_page: 100,
}
);
const inputSet = new Set();
const removedSet = new Set();
for (const file of files) {
const { filename, status, previous_filename: prev } = file;
if (status === 'removed' && isDoc(filename)) {
removedSet.add(toCnPath(filename));
continue;
}
if (status === 'renamed' && prev && isDoc(prev)) {
removedSet.add(toCnPath(prev));
}
if (isDoc(filename) && status !== 'removed') {
inputSet.add(`./${filename}`);
}
}
const inputs = Array.from(inputSet).join(' ');
const removals = Array.from(removedSet).join(' ');
core.setOutput('input_files', inputs);
core.setOutput('removed_cn', removals);
core.setOutput('has_inputs', inputSet.size > 0 ? 'true' : 'false');
core.setOutput('has_removals', removedSet.size > 0 ? 'true' : 'false');
core.setOutput('pr_number', String(prNumber));
- name: Exit if no documentation changes
if: steps.collect.outputs.has_inputs != 'true' && steps.collect.outputs.has_removals != 'true'
run: |
echo "No English documentation additions, updates, or deletions detected in PR #${{ steps.collect.outputs.pr_number }}."
exit 0
- name: Snapshot existing translation branches
if: steps.collect.outputs.has_inputs == 'true'
id: snapshot
run: |
git ls-remote --heads origin 'translation-*' | awk '{print $2}' | sed 's#refs/heads/##' | sort > /tmp/translation-branches-before.txt
echo "before=/tmp/translation-branches-before.txt" >> "$GITHUB_OUTPUT"
- name: Run GPT Translate
if: steps.collect.outputs.has_inputs == 'true'
uses: BohuTANG/gpt-translate-refine@v1.4.4
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
api_key: ${{ secrets.API_KEY }}
base_url: ${{ secrets.BASE_URL }}
ai_model: ${{ secrets.LLM_MODEL }}
refine_ai_model: ${{ secrets.REFINE_LLM_MODEL }}
target_lang: "Simplified-Chinese"
system_prompt: ".github/workflows/prompt.txt"
refine_system_prompt: ".github/workflows/refine_prompt.txt"
temperature: ${{ secrets.TEMPERATURE }}
refine_temperature: ${{ secrets.REFINE_TEMPERATURE }}
input_files: "${{ steps.collect.outputs.input_files }}"
output_files: "docs/cn/**/*.{md,json}"
pr_title: "Add LLM Translations V2"
- name: Identify translation branch
if: steps.collect.outputs.has_inputs == 'true'
id: branch
env:
SNAPSHOT_FILE: ${{ steps.snapshot.outputs.before }}
run: |
git ls-remote --heads origin 'translation-*' | awk '{print $2}' | sed 's#refs/heads/##' | sort > /tmp/translation-branches-after.txt
comm -13 "$SNAPSHOT_FILE" /tmp/translation-branches-after.txt > /tmp/new-translation-branches.txt
branch=$(tail -n 1 /tmp/new-translation-branches.txt)
if [ -n "$branch" ]; then
echo "Discovered translation branch: $branch"
echo "branch=$branch" >> "$GITHUB_OUTPUT"
else
echo "Unable to determine translation branch created by GPT workflow."
echo "branch=" >> "$GITHUB_OUTPUT"
fi
- name: Apply deletions to translation branch
if: >
steps.collect.outputs.has_inputs == 'true' &&
steps.collect.outputs.has_removals == 'true' &&
steps.branch.outputs.branch != ''
env:
REMOVED_FILES: ${{ steps.collect.outputs.removed_cn }}
TRANSLATION_BRANCH: ${{ steps.branch.outputs.branch }}
run: |
set -euo pipefail
git fetch origin "$TRANSLATION_BRANCH"
git checkout "$TRANSLATION_BRANCH"
for file in $REMOVED_FILES; do
if [ -f "$file" ]; then
rm -f "$file"
echo "Removed $file"
fi
done
# Clean up empty directories under docs/cn
find docs/cn -mindepth 1 -type d -empty -print -delete
if git status --porcelain | grep .; then
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add -A
git commit -m "chore: sync deletions for PR #${{ steps.collect.outputs.pr_number }}"
git push origin "$TRANSLATION_BRANCH"
else
echo "No deletions to commit."
fi
- name: Prepare deletion-only changes
if: steps.collect.outputs.has_inputs != 'true' && steps.collect.outputs.has_removals == 'true'
env:
REMOVED_FILES: ${{ steps.collect.outputs.removed_cn }}
run: |
set -euo pipefail
for file in $REMOVED_FILES; do
if [ -f "$file" ]; then
rm -f "$file"
echo "Removed $file"
fi
done
find docs/cn -mindepth 1 -type d -empty -print -delete
- name: Open deletion-only translation PR
if: steps.collect.outputs.has_inputs != 'true' && steps.collect.outputs.has_removals == 'true'
uses: peter-evans/create-pull-request@v6
with:
token: ${{ secrets.GITHUB_TOKEN }}
branch: translation-pr-${{ steps.collect.outputs.pr_number }}
base: main
commit-message: "chore: sync deletions for PR #${{ steps.collect.outputs.pr_number }}"
title: "AI Translate cleanup for PR #${{ steps.collect.outputs.pr_number }}"
body: |
This automated PR removes translated files that no longer have an English source from PR #${{ steps.collect.outputs.pr_number }}.