forked from langgenius/dify-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
397 lines (337 loc) · 15.7 KB
/
sync_docs_analyze.yml
File metadata and controls
397 lines (337 loc) · 15.7 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
name: Analyze Documentation Changes
on:
pull_request:
branches: [main, revamp]
types: [opened, synchronize, reopened]
paths:
# IMPORTANT: These paths should match the language directories defined in tools/translate/config.json
# Currently configured for: en (source), cn, jp (targets)
# If you add/remove languages in config.json, update these paths accordingly
- 'docs.json'
- 'en/**/*.md'
- 'en/**/*.mdx'
- 'en/**/openapi*.json'
- 'zh/**/*.md'
- 'zh/**/*.mdx'
- 'zh/**/openapi*.json'
- 'ja/**/*.md'
- 'ja/**/*.mdx'
- 'ja/**/openapi*.json'
- 'versions/**/*.md'
- 'versions/**/*.mdx'
permissions:
contents: read
pull-requests: read
jobs:
analyze:
runs-on: ubuntu-latest
steps:
- name: Checkout PR
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.9'
- name: Determine comparison range
id: determine-range
env:
GH_TOKEN: ${{ github.token }}
run: |
echo "Determining comparison range..."
PR_NUMBER="${{ github.event.pull_request.number }}"
EVENT_ACTION="${{ github.event.action }}"
PR_BASE="${{ github.event.pull_request.base.sha }}"
PR_HEAD="${{ github.event.pull_request.head.sha }}"
if [ "$EVENT_ACTION" = "synchronize" ]; then
echo "🔄 Synchronize event - detecting incremental changes"
# Try to get last processed commit from translation PR
TRANSLATION_PR=$(gh pr list \
--search "head:docs-sync-pr-${PR_NUMBER} state:open" \
--json number \
--jq '.[0].number // empty' 2>/dev/null || echo "")
LAST_PROCESSED=""
if [ -n "$TRANSLATION_PR" ]; then
echo "Found translation PR #${TRANSLATION_PR}"
# Extract last processed commit from comments (reverse order to get latest)
LAST_PROCESSED=$(gh pr view "$TRANSLATION_PR" \
--json comments \
--jq '.comments | reverse | .[] | .body' 2>/dev/null \
| grep -oP 'Last-Processed-Commit: \K[a-f0-9]+' \
| head -1 || echo "")
if [ -n "$LAST_PROCESSED" ]; then
echo "✅ Found tracked commit in translation PR: $LAST_PROCESSED"
fi
fi
# Use tracked commit if available, otherwise fall back to github.event.before
if [ -n "$LAST_PROCESSED" ]; then
COMPARE_BASE="$LAST_PROCESSED"
echo "Using last processed commit: $COMPARE_BASE"
elif [ -n "${{ github.event.before }}" ] && [ "${{ github.event.before }}" != "0000000000000000000000000000000000000000" ]; then
COMPARE_BASE="${{ github.event.before }}"
echo "Using github.event.before: $COMPARE_BASE"
else
# Fallback to PR base (first push after PR creation)
COMPARE_BASE="$PR_BASE"
echo "⚠️ No previous commit found, using PR base: $COMPARE_BASE"
fi
COMPARE_HEAD="$PR_HEAD"
IS_INCREMENTAL="true"
else
echo "🆕 New PR event - analyzing full changes"
# Use merge-base to find where branch diverged from main
# This allows stale branches to trigger automation without false "mixed content" errors
MERGE_BASE=$(git merge-base "$PR_BASE" "$PR_HEAD")
echo "Branch diverged from main at: $MERGE_BASE"
COMPARE_BASE="$MERGE_BASE"
COMPARE_HEAD="$PR_HEAD"
IS_INCREMENTAL="false"
fi
echo "compare_base=$COMPARE_BASE" >> $GITHUB_OUTPUT
echo "compare_head=$COMPARE_HEAD" >> $GITHUB_OUTPUT
echo "is_incremental=$IS_INCREMENTAL" >> $GITHUB_OUTPUT
echo "📊 Comparison range: $COMPARE_BASE...$COMPARE_HEAD"
- name: Categorize and validate PR changes
id: categorize
run: |
echo "Categorizing PR changes..."
# Get comparison range from previous step
BASE_SHA="${{ steps.determine-range.outputs.compare_base }}"
HEAD_SHA="${{ steps.determine-range.outputs.compare_head }}"
echo "Base SHA: $BASE_SHA"
echo "Head SHA: $HEAD_SHA"
# Run PR analyzer
cd tools/translate
python pr_analyzer.py "$BASE_SHA" "$HEAD_SHA" > /tmp/pr_analysis_output.txt 2>&1
# Parse analyzer output
if [ $? -eq 0 ]; then
# Successful analysis
source /tmp/pr_analysis_output.txt
echo "PR categorization successful"
echo "PR Type: $pr_type"
echo "Should Skip: $should_skip"
# Set GitHub outputs
echo "pr_type=$pr_type" >> $GITHUB_OUTPUT
echo "should_skip=$should_skip" >> $GITHUB_OUTPUT
if [ "$should_skip" = "true" ]; then
if [ "$pr_type" = "translation" ]; then
echo "✅ Translation-only PR detected. Skipping automation (direct review process)."
elif [ "$pr_type" = "none" ]; then
echo "✅ No relevant documentation changes detected. Skipping workflow."
fi
exit 0
fi
else
# Analysis failed - likely mixed PR
echo "PR categorization failed - likely mixed content PR"
ERROR_MESSAGE=$(cat /tmp/pr_analysis_output.txt | grep "error_message=" | cut -d'=' -f2- || echo "Mixed content PR detected")
echo "error=mixed_pr" >> $GITHUB_OUTPUT
echo "error_message<<EOF" >> $GITHUB_OUTPUT
echo "$ERROR_MESSAGE" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
exit 1
fi
- name: Analyze source language changes for translation
if: steps.categorize.outputs.pr_type == 'source'
id: analyze
run: |
echo "Analyzing source language changes for automatic translation..."
# Use comparison range from determine-range step
BASE_SHA="${{ steps.determine-range.outputs.compare_base }}"
HEAD_SHA="${{ steps.determine-range.outputs.compare_head }}"
IS_INCREMENTAL="${{ steps.determine-range.outputs.is_incremental }}"
echo "Comparison: $BASE_SHA...$HEAD_SHA"
echo "Incremental: $IS_INCREMENTAL"
# Get all changed files (not just English ones for file analysis)
CHANGED_FILES=$(git diff --name-only $BASE_SHA $HEAD_SHA)
# Count changes for security limits
FILE_COUNT=$(echo "$CHANGED_FILES" | wc -l)
echo "Changed files count: $FILE_COUNT"
# Security check: Limit number of files
MAX_FILES=50
if [ "$FILE_COUNT" -gt "$MAX_FILES" ]; then
echo "Error: Too many files changed ($FILE_COUNT > $MAX_FILES)"
echo "error=too_many_files" >> $GITHUB_OUTPUT
exit 1
fi
# Create analysis report
cat > /tmp/analysis.json <<EOF
{
"pr_number": ${{ github.event.pull_request.number }},
"pr_title": "${{ github.event.pull_request.title }}",
"pr_author": "${{ github.event.pull_request.user.login }}",
"base_sha": "$BASE_SHA",
"head_sha": "$HEAD_SHA",
"is_incremental": $IS_INCREMENTAL,
"event_action": "${{ github.event.action }}",
"file_count": $FILE_COUNT,
"timestamp": "$(date -u +%Y-%m-%dT%H:%M:%SZ)",
"repository": "${{ github.repository }}",
"ref": "${{ github.ref }}",
"pr_type": "source"
}
EOF
# Save changed files list
echo "$CHANGED_FILES" > /tmp/changed_files.txt
# Analyze file types and sizes for source language files that need translation
> /tmp/file_analysis.txt
> /tmp/openapi_analysis.txt
while IFS= read -r file; do
if [[ "$file" =~ ^en/.*\.(md|mdx)$ ]] && [ -f "$file" ]; then
SIZE=$(stat -f%z "$file" 2>/dev/null || stat -c%s "$file" 2>/dev/null || echo "0")
echo "$file|$SIZE|markdown" >> /tmp/file_analysis.txt
# Security check: File size limit (10MB)
MAX_SIZE=$((10 * 1024 * 1024))
if [ "$SIZE" -gt "$MAX_SIZE" ]; then
echo "Error: File $file exceeds size limit ($SIZE > $MAX_SIZE)"
echo "error=file_too_large" >> $GITHUB_OUTPUT
exit 1
fi
elif [[ "$file" =~ ^en/.*/openapi.*\.json$ ]] && [ -f "$file" ]; then
SIZE=$(stat -f%z "$file" 2>/dev/null || stat -c%s "$file" 2>/dev/null || echo "0")
echo "$file|$SIZE|openapi_json" >> /tmp/openapi_analysis.txt
# Security check: File size limit for OpenAPI JSON (10MB)
MAX_SIZE=$((10 * 1024 * 1024))
if [ "$SIZE" -gt "$MAX_SIZE" ]; then
echo "Error: OpenAPI file $file exceeds size limit ($SIZE > $MAX_SIZE)"
echo "error=file_too_large" >> $GITHUB_OUTPUT
exit 1
fi
fi
done <<< "$CHANGED_FILES"
# Check for docs.json changes
if echo "$CHANGED_FILES" | grep -q '^docs\.json$'; then
echo "true" > /tmp/docs_json_changed.txt
# Use PR analyzer's docs.json analysis
cd tools/translate
python3 - <<EOF
import sys
sys.path.append('.')
from pr_analyzer import PRAnalyzer
analyzer = PRAnalyzer("$BASE_SHA", "$HEAD_SHA")
docs_changes = analyzer.analyze_docs_json_changes()
structure_changes = {
"structure_changed": docs_changes["any_docs_json_changes"],
"navigation_modified": docs_changes["source_section"],
"languages_affected": analyzer.config["target_languages"] if docs_changes["source_section"] else []
}
import json
with open("/tmp/structure_changes.json", "w") as f:
json.dump(structure_changes, f, indent=2)
EOF
else
echo "false" > /tmp/docs_json_changed.txt
echo '{"structure_changed": false, "navigation_modified": false, "languages_affected": []}' > /tmp/structure_changes.json
fi
echo "has_changes=true" >> $GITHUB_OUTPUT
echo "Analysis complete"
- name: Validate file paths
if: steps.analyze.outputs.has_changes == 'true'
run: |
echo "Validating source language file paths for translation..."
# Security: Validate source language files that will be translated
while IFS='|' read -r file size; do
if [ -n "$file" ]; then
# Check for directory traversal attempts
if echo "$file" | grep -q '\.\./'; then
echo "Error: Invalid file path detected: $file"
exit 1
fi
# Check file extension for source language files
if ! echo "$file" | grep -qE '\.(md|mdx)$'; then
echo "Error: Invalid file type for translation: $file"
exit 1
fi
# Check path starts with en/ (only source language files need translation)
if ! echo "$file" | grep -qE '^en/'; then
echo "Error: Non-source-language file in translation list: $file"
exit 1
fi
fi
done < /tmp/file_analysis.txt
# Validate OpenAPI JSON files
if [ -f "/tmp/openapi_analysis.txt" ] && [ -s "/tmp/openapi_analysis.txt" ]; then
while IFS='|' read -r file size file_type; do
if [ -n "$file" ]; then
# Check for directory traversal
if echo "$file" | grep -q '\.\./'; then
echo "Error: Invalid file path: $file"
exit 1
fi
# Check file extension
if ! echo "$file" | grep -qE '\.json$'; then
echo "Error: Invalid OpenAPI file type: $file"
exit 1
fi
# Check path starts with en/
if ! echo "$file" | grep -qE '^en/'; then
echo "Error: Non-source-language OpenAPI file in translation list: $file"
exit 1
fi
# Check pattern match (configurable via openapi*.json)
if ! echo "$file" | grep -qE 'openapi.*\.json$'; then
echo "Error: File doesn't match OpenAPI pattern: $file"
exit 1
fi
fi
done < /tmp/openapi_analysis.txt
fi
echo "All source language file paths validated for translation"
- name: Create analysis summary
if: steps.analyze.outputs.has_changes == 'true'
run: |
echo "Creating analysis summary for source language changes..."
BASE_SHA="${{ steps.determine-range.outputs.compare_base }}"
HEAD_SHA="${{ steps.determine-range.outputs.compare_head }}"
PR_NUMBER=${{ github.event.pull_request.number }}
IS_INCREMENTAL="${{ steps.determine-range.outputs.is_incremental }}"
# Use SyncPlanGenerator for consistent logic across workflows
cd tools/translate
python3 - <<EOF
import json
import sys
sys.path.append('.')
from pr_analyzer import SyncPlanGenerator
# Generate sync plan using centralized logic
generator = SyncPlanGenerator("$BASE_SHA", "$HEAD_SHA")
sync_plan = generator.generate_sync_plan()
# Add PR metadata to sync plan
sync_plan["metadata"].update({
"pr_number": $PR_NUMBER,
"pr_title": "${{ github.event.pull_request.title }}",
"pr_author": "${{ github.event.pull_request.user.login }}",
"event_action": "${{ github.event.action }}",
"is_incremental": "$IS_INCREMENTAL" == "true",
"file_count": len(sync_plan["files_to_sync"]) + len(sync_plan["openapi_files_to_sync"]),
"timestamp": "$(date -u +%Y-%m-%dT%H:%M:%SZ)",
"repository": "${{ github.repository }}",
"ref": "${{ github.ref }}",
"pr_type": "source"
})
# Save analysis.json (for backward compatibility with execute workflow)
with open("/tmp/analysis.json", "w") as f:
json.dump(sync_plan["metadata"], f, indent=2)
# Save sync plan
with open("/tmp/sync_plan.json", "w") as f:
json.dump(sync_plan, f, indent=2)
print(f"Source language sync plan created:")
print(f" - {len(sync_plan['files_to_sync'])} markdown files to translate")
print(f" - {len(sync_plan['openapi_files_to_sync'])} OpenAPI JSON files to translate")
if sync_plan['structure_changes'].get('structure_changed'):
print(" - Documentation structure changes detected")
EOF
- name: Upload analysis artifacts
if: steps.analyze.outputs.has_changes == 'true'
uses: actions/upload-artifact@v4
with:
name: docs-sync-analysis-${{ github.run_id }}
path: |
/tmp/analysis.json
/tmp/changed_files.txt
/tmp/file_analysis.txt
/tmp/openapi_analysis.txt
/tmp/sync_plan.json
/tmp/docs_json_changed.txt
/tmp/structure_changes.json
retention-days: 1