Skip to content

Commit 5f197d5

Browse files
authored
Add manual PR translation workflow (#2886)
1 parent 67fb287 commit 5f197d5

File tree

1 file changed

+94
-7
lines changed

1 file changed

+94
-7
lines changed

.github/workflows/docs-pr.translate.yaml

Lines changed: 94 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ jobs:
2222
- name: Parse PR input
2323
id: pr
2424
uses: actions/github-script@v7
25+
env:
26+
PR_INPUT: ${{ inputs.pr }}
2527
with:
2628
script: |
27-
const raw = core.getInput('pr');
29+
const raw = process.env.PR_INPUT || '';
2830
if (!raw || raw.trim() === '') {
2931
throw new Error('PR input is required.');
3032
}
@@ -61,11 +63,14 @@ jobs:
6163
- name: Collect changed documentation files
6264
id: collect
6365
uses: actions/github-script@v7
66+
env:
67+
PR_NUMBER: ${{ steps.pr.outputs.pr_number }}
6468
with:
6569
script: |
66-
const prNumber = parseInt('${{ steps.pr.outputs.pr_number }}', 10);
70+
const rawPrNumber = process.env.PR_NUMBER || '';
71+
const prNumber = Number.parseInt(rawPrNumber, 10);
6772
if (!prNumber) {
68-
throw new Error('PR number is missing.');
73+
throw new Error(`PR number is missing or invalid: "${rawPrNumber}"`);
6974
}
7075
7176
const isDoc = (path) =>
@@ -120,14 +125,21 @@ jobs:
120125
uses: actions/checkout@v4
121126
with:
122127
fetch-depth: 0
128+
ref: main
123129

124-
- name: Checkout PR revision
130+
- name: Populate PR content
125131
if: steps.collect.outputs.has_inputs == 'true'
132+
env:
133+
PR: ${{ steps.pr.outputs.pr_number }}
134+
INPUT_FILES: ${{ steps.collect.outputs.input_files }}
126135
run: |
127136
set -euo pipefail
128-
PR=${{ steps.pr.outputs.pr_number }}
129-
git fetch origin pull/${PR}/head
130-
git checkout -B translation-source-${PR} FETCH_HEAD
137+
git fetch --no-tags --depth=1 origin pull/${PR}/head:refs/remotes/origin/pr-${PR}
138+
for file in $INPUT_FILES; do
139+
clean_path=${file#./}
140+
mkdir -p "$(dirname "$clean_path")"
141+
git show origin/pr-${PR}:"$clean_path" > "$clean_path"
142+
done
131143
132144
- name: Snapshot existing translation branches
133145
if: steps.collect.outputs.has_inputs == 'true'
@@ -171,6 +183,77 @@ jobs:
171183
echo "branch=" >> "$GITHUB_OUTPUT"
172184
fi
173185
186+
- name: Annotate translation PR
187+
if: steps.branch.outputs.branch != ''
188+
uses: actions/github-script@v7
189+
env:
190+
SOURCE_PR: ${{ steps.pr.outputs.pr_number }}
191+
SOURCE_PR_URL: ${{ steps.pr.outputs.html_url }}
192+
REMOVED_FILES: ${{ steps.collect.outputs.removed_cn }}
193+
with:
194+
script: |
195+
const branch = '${{ steps.branch.outputs.branch }}';
196+
if (!branch) {
197+
core.info('No translation branch detected, skipping annotation.');
198+
return;
199+
}
200+
201+
const owner = context.repo.owner;
202+
const repo = context.repo.repo;
203+
204+
const prs = await github.paginate(github.rest.pulls.list, {
205+
owner,
206+
repo,
207+
head: `${owner}:${branch}`,
208+
state: 'open',
209+
per_page: 100,
210+
});
211+
212+
if (prs.length === 0) {
213+
core.warning(`Unable to find translation PR for branch ${branch}`);
214+
return;
215+
}
216+
217+
const translationPr = prs[0];
218+
const sourcePrNumber = process.env.SOURCE_PR;
219+
const sourcePrUrl = process.env.SOURCE_PR_URL;
220+
221+
const removedFilesRaw = (process.env.REMOVED_FILES || '').trim();
222+
const removedFilesList = removedFilesRaw
223+
? removedFilesRaw.split(/\s+/).map((file) => `- \`${file}\``).join('\n')
224+
: '- *(none)*';
225+
226+
const annotationBlock = [
227+
'---',
228+
'### 🔗 Source Pull Request',
229+
`- PR #${sourcePrNumber}`,
230+
`- ${sourcePrUrl}`,
231+
'',
232+
'### 🗑️ Synchronized CN Deletions',
233+
removedFilesList,
234+
'---',
235+
].join('\n');
236+
237+
let updatedBody = translationPr.body || '';
238+
239+
const marker = '\n---\n### 🔗 Source Pull Request';
240+
if (updatedBody.includes(marker)) {
241+
updatedBody = updatedBody.replace(/---\n### 🔗 Source Pull Request[\s\S]*?---/m, annotationBlock);
242+
} else {
243+
updatedBody = `${updatedBody.trim()}\n\n${annotationBlock}`.trim();
244+
}
245+
246+
const updatedTitle = translationPr.title.includes(`#${sourcePrNumber}`)
247+
? translationPr.title
248+
: `${translationPr.title} (from #${sourcePrNumber})`;
249+
250+
await github.rest.pulls.update({
251+
owner,
252+
repo,
253+
pull_number: translationPr.number,
254+
title: updatedTitle,
255+
body: updatedBody,
256+
});
174257
- name: Apply deletions to translation branch
175258
if: >
176259
steps.collect.outputs.has_inputs == 'true' &&
@@ -184,6 +267,10 @@ jobs:
184267
git fetch origin "$TRANSLATION_BRANCH"
185268
git checkout "$TRANSLATION_BRANCH"
186269
270+
if command -v sudo >/dev/null 2>&1; then
271+
sudo chown -R "$(id -u)":"$(id -g)" .git docs || true
272+
fi
273+
187274
for file in $REMOVED_FILES; do
188275
if [ -f "$file" ]; then
189276
rm -f "$file"

0 commit comments

Comments
 (0)