11name : Translate Docusaurus Documentation
22
33on :
4- pull_request :
4+ pull_request_target :
55 types : [labeled]
66
77permissions :
8- checks : write
98 contents : write
109 pull-requests : write
1110
@@ -15,10 +14,10 @@ jobs:
1514 timeout-minutes : 1440
1615 runs-on : self-hosted
1716 steps :
18- - name : Checkout PR branch
17+ - name : Checkout PR code
1918 uses : actions/checkout@v4
2019 with :
21- ref : ${{ github.head_ref }}
20+ ref : ${{ github.event.pull_request.head.sha }}
2221 fetch-depth : 0
2322
2423 - name : Setup Node.js
@@ -42,25 +41,80 @@ jobs:
4241 run : node translate.js
4342
4443 - name : Commit translations
44+ id : commit
4545 run : |
4646 git config --local user.name "github-actions"
4747 git config --local user.email "github-actions@github.com"
4848 git add -A i18n/
4949 if ! git diff --cached --quiet; then
5050 git commit -m "Auto-translate docs"
51- git push origin HEAD:${{ github.head_ref }}
51+ echo "has_changes=true" >> "$GITHUB_OUTPUT"
5252 else
5353 echo "No translation changes to commit"
54+ echo "has_changes=false" >> "$GITHUB_OUTPUT"
5455 fi
5556
56- - name : Remove label
57- if : always()
57+ - name : Push to PR branch (same-repo)
58+ if : steps.commit.outputs.has_changes == 'true' && github.event.pull_request.head.repo.full_name == github.repository
59+ run : git push origin HEAD:refs/heads/${{ github.head_ref }}
60+
61+ - name : Push translation branch (fork PR)
62+ if : steps.commit.outputs.has_changes == 'true' && github.event.pull_request.head.repo.full_name != github.repository
63+ run : git push origin HEAD:refs/heads/translations/pr-${{ github.event.pull_request.number }} --force
64+
65+ - name : Create or update translation PR (fork PR)
66+ if : steps.commit.outputs.has_changes == 'true' && github.event.pull_request.head.repo.full_name != github.repository
5867 uses : actions/github-script@v7
5968 with :
6069 script : |
61- github.rest.issues.removeLabel({
70+ const prNumber = context.payload.pull_request.number;
71+ const branch = `translations/pr-${prNumber}`;
72+ const baseBranch = context.payload.pull_request.base.ref;
73+
74+ const { data: existingPRs } = await github.rest.pulls.list({
6275 owner: context.repo.owner,
6376 repo: context.repo.repo,
64- issue_number: context.issue.number,
65- name: 'TRANSLATE DOCUSAURUS'
66- })
77+ head: `${context.repo.owner}:${branch}`,
78+ state: 'open'
79+ });
80+
81+ if (existingPRs.length === 0) {
82+ const { data: newPR } = await github.rest.pulls.create({
83+ owner: context.repo.owner,
84+ repo: context.repo.repo,
85+ title: `Auto-translate: PR #${prNumber}`,
86+ body: `Automated translations for #${prNumber}.\n\nMerge this PR **after** #${prNumber} has been merged.`,
87+ head: branch,
88+ base: baseBranch
89+ });
90+
91+ await github.rest.issues.createComment({
92+ owner: context.repo.owner,
93+ repo: context.repo.repo,
94+ issue_number: prNumber,
95+ body: `Translations have been generated and are available in #${newPR.number}.\nPlease merge the translation PR after this PR is merged.`
96+ });
97+ } else {
98+ await github.rest.issues.createComment({
99+ owner: context.repo.owner,
100+ repo: context.repo.repo,
101+ issue_number: prNumber,
102+ body: `Translations have been updated in #${existingPRs[0].number}.`
103+ });
104+ }
105+
106+ - name : Remove label
107+ if : always()
108+ uses : actions/github-script@v7
109+ with :
110+ script : |
111+ try {
112+ await github.rest.issues.removeLabel({
113+ owner: context.repo.owner,
114+ repo: context.repo.repo,
115+ issue_number: context.payload.pull_request.number,
116+ name: 'TRANSLATE DOCUSAURUS'
117+ });
118+ } catch (error) {
119+ core.warning(`Could not remove label: ${error.message}`);
120+ }
0 commit comments