Skip to content

Commit 5b4f881

Browse files
committed
chore: update
1 parent b4bca61 commit 5b4f881

2 files changed

Lines changed: 124 additions & 13 deletions

File tree

.github/workflows/translate.yml

Lines changed: 95 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
name: Auto Translate Docs
22

33
on:
4+
workflow_run:
5+
workflows: ["Process Documentation"]
6+
types:
7+
- completed
8+
branches-ignore:
9+
- 'main'
410
push:
511
branches-ignore:
612
- 'main'
13+
paths-ignore:
14+
- '.github/workflows/**'
715

816
jobs:
917
translate:
1018
runs-on: ubuntu-latest
19+
# Only run if the workflow_run event was successful, or if it's a direct push
20+
if: github.event_name == 'push' || github.event.workflow_run.conclusion == 'success'
1121
permissions:
1222
contents: write
1323
steps:
@@ -16,6 +26,8 @@ jobs:
1626
with:
1727
fetch-depth: 0 # Fetches all history for git diff
1828
token: ${{ secrets.GITHUB_TOKEN }}
29+
# For workflow_run events, checkout the head of the triggering workflow
30+
ref: ${{ github.event_name == 'workflow_run' && github.event.workflow_run.head_sha || github.sha }}
1931

2032
- name: Set up Python
2133
uses: actions/setup-python@v4
@@ -28,16 +40,57 @@ jobs:
2840
- name: Get changed markdown files
2941
id: changed-files
3042
run: |
31-
# Get the list of changed files between the current and previous commit
43+
# Get the list of newly added files between the current and previous commit
3244
# We filter for .md and .mdx files that are inside the language directories
33-
files=$(git diff --name-only ${{ github.event.before }} ${{ github.sha }} | grep -E '^(en|zh-hans|ja-jp|plugin-dev-en|plugin-dev-zh|plugin-dev-ja)/.*(\.md|\.mdx)$' || true)
34-
if [[ -z "$files" ]]; then
35-
echo "No markdown files to translate."
45+
# Only include added (A) files, skip modified (M) and deleted (D) files
46+
47+
# Determine the commit SHA to use based on event type
48+
if [[ "${{ github.event_name }}" == "workflow_run" ]]; then
49+
current_sha="${{ github.event.workflow_run.head_sha }}"
50+
echo "Using workflow_run head_sha: $current_sha"
51+
else
52+
current_sha="${{ github.sha }}"
53+
echo "Using github.sha: $current_sha"
54+
fi
55+
56+
# Try different approaches to get the diff
57+
if [[ -n "${{ github.event.before }}" && "${{ github.event_name }}" == "push" ]]; then
58+
echo "Using github.event.before: ${{ github.event.before }}"
59+
files=$(git diff --name-status ${{ github.event.before }} $current_sha | grep -E '^A\s+' | cut -f2 | grep -E '^(en|en-us|zh-hans|ja-jp|plugin-dev-en|plugin-dev-zh|plugin-dev-ja|versions)/.*(\.md|\.mdx)$' || true)
60+
else
61+
echo "Using HEAD~1 for comparison"
62+
files=$(git diff --name-status HEAD~1 $current_sha | grep -E '^A\s+' | cut -f2 | grep -E '^(en|en-us|zh-hans|ja-jp|plugin-dev-en|plugin-dev-zh|plugin-dev-ja|versions)/.*(\.md|\.mdx)$' || true)
63+
fi
64+
65+
echo "Detected files (Added only):"
66+
echo "$files"
67+
68+
# Filter out files that don't actually exist
69+
existing_files=""
70+
if [[ -n "$files" ]]; then
71+
while IFS= read -r file; do
72+
if [[ -n "$file" && -f "$file" ]]; then
73+
if [[ -z "$existing_files" ]]; then
74+
existing_files="$file"
75+
else
76+
existing_files="$existing_files"$'\n'"$file"
77+
fi
78+
else
79+
echo "Skipping non-existent file: $file"
80+
fi
81+
done <<< "$files"
82+
fi
83+
84+
echo "Final files to translate:"
85+
echo "$existing_files"
86+
87+
if [[ -z "$existing_files" ]]; then
88+
echo "No new markdown files to translate."
3689
echo "files=" >> $GITHUB_OUTPUT
3790
else
3891
# The script expects absolute paths, but we run it from the root, so relative is fine.
3992
echo "files<<EOF" >> $GITHUB_OUTPUT
40-
echo "$files" >> $GITHUB_OUTPUT
93+
echo "$existing_files" >> $GITHUB_OUTPUT
4194
echo "EOF" >> $GITHUB_OUTPUT
4295
fi
4396
@@ -49,12 +102,35 @@ jobs:
49102
echo "Files to translate:"
50103
echo "${{ steps.changed-files.outputs.files }}"
51104
52-
echo "${{ steps.changed-files.outputs.files }}" | while IFS= read -r file; do
105+
# Create temporary file list
106+
echo "${{ steps.changed-files.outputs.files }}" > /tmp/files_to_translate.txt
107+
108+
# Start all translation processes in parallel
109+
pids=()
110+
while IFS= read -r file; do
53111
if [[ -n "$file" ]]; then
54-
echo "Translating $file..."
55-
python tools/translate/main.py "$file" "$DIFY_API_KEY"
112+
echo "Starting translation for $file..."
113+
python tools/translate/main.py "$file" "$DIFY_API_KEY" &
114+
pids+=($!)
115+
fi
116+
done < /tmp/files_to_translate.txt
117+
118+
# Wait for all background processes to complete
119+
echo "Waiting for ${#pids[@]} translation processes to complete..."
120+
failed=0
121+
for pid in "${pids[@]}"; do
122+
if ! wait "$pid"; then
123+
echo "Translation process $pid failed"
124+
failed=1
56125
fi
57126
done
127+
128+
if [ $failed -eq 1 ]; then
129+
echo "Some translations failed"
130+
exit 1
131+
fi
132+
133+
echo "All translations completed successfully"
58134
59135
- name: Commit and push changes
60136
run: |
@@ -64,8 +140,17 @@ jobs:
64140
if [[ -n $(git status --porcelain) ]]; then
65141
git add .
66142
git commit -m "docs: auto-translate documentation"
67-
# Push to the same branch the workflow was triggered from
68-
git push origin HEAD:${{ github.ref_name }}
143+
# Push to the appropriate branch based on event type
144+
if [[ "${{ github.event_name }}" == "workflow_run" ]]; then
145+
# For workflow_run events, push to the head branch of the triggering workflow
146+
branch_ref="${{ github.event.workflow_run.head_branch }}"
147+
echo "Pushing to workflow_run head branch: $branch_ref"
148+
git push origin HEAD:$branch_ref
149+
else
150+
# For push events, push to the same branch the workflow was triggered from
151+
echo "Pushing to current branch: ${{ github.ref_name }}"
152+
git push origin HEAD:${{ github.ref_name }}
153+
fi
69154
echo "Translated files have been pushed to the branch."
70155
else
71156
echo "No new translations to commit."

tools/translate/main.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,21 @@
1414
"English": "plugin-dev-en",
1515
"Chinese": "plugin-dev-zh",
1616
"Japanese": "plugin-dev-ja"
17+
},
18+
"version_28x": {
19+
"English": "versions/2-8-x/en-us",
20+
"Chinese": "versions/2-8-x/zh-cn",
21+
"Japanese": "versions/2-8-x/ja-jp"
22+
},
23+
"version_30x": {
24+
"English": "versions/3-0-x/en-us",
25+
"Chinese": "versions/3-0-x/zh-cn",
26+
"Japanese": "versions/3-0-x/ja-jp"
27+
},
28+
"version_31x": {
29+
"English": "versions/3-1-x/en-us",
30+
"Chinese": "versions/3-1-x/zh-cn",
31+
"Japanese": "versions/3-1-x/ja-jp"
1732
}
1833
}
1934

@@ -109,12 +124,23 @@ def determine_doc_type_and_language(file_path):
109124
Determine document type and current language based on file path
110125
Returns (doc_type, current_language, language_name)
111126
"""
112-
path_parts = file_path.split(os.sep)
127+
# Normalize path separators
128+
normalized_path = file_path.replace(os.sep, '/')
113129

130+
# Collect all possible matches and find the longest one
131+
matches = []
114132
for doc_type, languages in docs_structure.items():
115133
for lang_name, lang_code in languages.items():
116-
if lang_code in path_parts:
117-
return doc_type, lang_code, lang_name
134+
# Normalize lang_code path separators too
135+
normalized_lang_code = lang_code.replace(os.sep, '/')
136+
if normalized_lang_code in normalized_path:
137+
matches.append((len(normalized_lang_code), doc_type, lang_code, lang_name))
138+
139+
# Return the match with the longest lang_code (most specific)
140+
if matches:
141+
matches.sort(reverse=True) # Sort by length descending
142+
_, doc_type, lang_code, lang_name = matches[0]
143+
return doc_type, lang_code, lang_name
118144

119145
return None, None, None
120146

0 commit comments

Comments
 (0)