-
Notifications
You must be signed in to change notification settings - Fork 377
157 lines (139 loc) · 6.09 KB
/
translate.yml
File metadata and controls
157 lines (139 loc) · 6.09 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
name: Auto Translate Docs
on:
workflow_run:
workflows: ["Process Documentation"]
types:
- completed
branches-ignore:
- 'main'
push:
branches-ignore:
- 'main'
paths-ignore:
- '.github/workflows/**'
jobs:
translate:
runs-on: ubuntu-latest
# Only run if the workflow_run event was successful, or if it's a direct push
if: github.event_name == 'push' || github.event.workflow_run.conclusion == 'success'
permissions:
contents: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0 # Fetches all history for git diff
token: ${{ secrets.GITHUB_TOKEN }}
# For workflow_run events, checkout the head of the triggering workflow
ref: ${{ github.event_name == 'workflow_run' && github.event.workflow_run.head_sha || github.sha }}
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.9'
- name: Install dependencies
run: pip install httpx aiofiles python-dotenv
- name: Get changed markdown files
id: changed-files
run: |
# Get the list of newly added files between the current and previous commit
# We filter for .md and .mdx files that are inside the language directories
# Only include added (A) files, skip modified (M) and deleted (D) files
# Determine the commit SHA to use based on event type
if [[ "${{ github.event_name }}" == "workflow_run" ]]; then
current_sha="${{ github.event.workflow_run.head_sha }}"
echo "Using workflow_run head_sha: $current_sha"
else
current_sha="${{ github.sha }}"
echo "Using github.sha: $current_sha"
fi
# Try different approaches to get the diff
if [[ -n "${{ github.event.before }}" && "${{ github.event_name }}" == "push" ]]; then
echo "Using github.event.before: ${{ github.event.before }}"
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)
else
echo "Using HEAD~1 for comparison"
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)
fi
echo "Detected files (Added only):"
echo "$files"
# Filter out files that don't actually exist
existing_files=""
if [[ -n "$files" ]]; then
while IFS= read -r file; do
if [[ -n "$file" && -f "$file" ]]; then
if [[ -z "$existing_files" ]]; then
existing_files="$file"
else
existing_files="$existing_files"$'\n'"$file"
fi
else
echo "Skipping non-existent file: $file"
fi
done <<< "$files"
fi
echo "Final files to translate:"
echo "$existing_files"
if [[ -z "$existing_files" ]]; then
echo "No new markdown files to translate."
echo "files=" >> $GITHUB_OUTPUT
else
# The script expects absolute paths, but we run it from the root, so relative is fine.
echo "files<<EOF" >> $GITHUB_OUTPUT
echo "$existing_files" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
fi
- name: Run translation script
if: steps.changed-files.outputs.files
env:
DIFY_API_KEY: ${{ secrets.DIFY_API_KEY }}
run: |
echo "Files to translate:"
echo "${{ steps.changed-files.outputs.files }}"
# Create temporary file list
echo "${{ steps.changed-files.outputs.files }}" > /tmp/files_to_translate.txt
# Start all translation processes in parallel
pids=()
while IFS= read -r file; do
if [[ -n "$file" ]]; then
echo "Starting translation for $file..."
python tools/translate/main.py "$file" "$DIFY_API_KEY" &
pids+=($!)
fi
done < /tmp/files_to_translate.txt
# Wait for all background processes to complete
echo "Waiting for ${#pids[@]} translation processes to complete..."
failed=0
for pid in "${pids[@]}"; do
if ! wait "$pid"; then
echo "Translation process $pid failed"
failed=1
fi
done
if [ $failed -eq 1 ]; then
echo "Some translations failed"
exit 1
fi
echo "All translations completed successfully"
- name: Commit and push changes
run: |
git config --global user.name 'github-actions[bot]'
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
# Check if there are any changes to commit
if [[ -n $(git status --porcelain) ]]; then
git add .
git commit -m "docs: auto-translate documentation"
# Push to the appropriate branch based on event type
if [[ "${{ github.event_name }}" == "workflow_run" ]]; then
# For workflow_run events, push to the head branch of the triggering workflow
branch_ref="${{ github.event.workflow_run.head_branch }}"
echo "Pushing to workflow_run head branch: $branch_ref"
git push origin HEAD:$branch_ref
else
# For push events, push to the same branch the workflow was triggered from
echo "Pushing to current branch: ${{ github.ref_name }}"
git push origin HEAD:${{ github.ref_name }}
fi
echo "Translated files have been pushed to the branch."
else
echo "No new translations to commit."
fi