generated from mintlify/starter
-
Notifications
You must be signed in to change notification settings - Fork 165
180 lines (154 loc) · 7.8 KB
/
zh-cn-sync-check.yml
File metadata and controls
180 lines (154 loc) · 7.8 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
name: Chinese Translation Sync Check
on:
pull_request:
branches:
- main
paths:
- '**/*.mdx'
- '!zh-CN/**/*.mdx'
- '!snippets/zh/**/*.mdx'
jobs:
check-zh-cn-translations:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
- name: Install dependencies
run: npm ci
- name: Check changed MDX files have zh-CN equivalents and check for moved files
id: check-translations
run: |
# Get list of changed MDX files
CHANGED_FILES=$(git diff --name-only --diff-filter=ACMRT ${{ github.event.pull_request.base.sha }} ${{ github.event.pull_request.head.sha }} | grep "\.mdx$" | grep -v "^zh-CN/" | grep -v "^snippets/zh/" || true)
# Get list of deleted MDX files (potentially moved)
DELETED_FILES=$(git diff --name-only --diff-filter=D ${{ github.event.pull_request.base.sha }} ${{ github.event.pull_request.head.sha }} | grep "\.mdx$" | grep -v "^zh-CN/" | grep -v "^snippets/zh/" || true)
# Get list of added MDX files (potentially destinations of moves)
ADDED_FILES=$(git diff --name-only --diff-filter=A ${{ github.event.pull_request.base.sha }} ${{ github.event.pull_request.head.sha }} | grep "\.mdx$" | grep -v "^zh-CN/" | grep -v "^snippets/zh/" || true)
# Exit if no MDX files were changed, deleted or added
if [ -z "$CHANGED_FILES" ] && [ -z "$DELETED_FILES" ] && [ -z "$ADDED_FILES" ]; then
echo "No MDX files changed outside of zh-CN and snippets/zh directories. Skipping check."
exit 0
fi
# Check for moved files - these would be deleted and added in the same PR
MOVED_FILES=""
MOVE_DESTINATIONS=""
if [ ! -z "$DELETED_FILES" ] && [ ! -z "$ADDED_FILES" ]; then
echo "Potential file moves detected:"
echo "------------------------"
echo "Deleted files:"
echo "$DELETED_FILES"
echo "------------------------"
echo "Added files:"
echo "$ADDED_FILES"
echo "------------------------"
# Check if redirects are added in docs.json for deleted files
DOCS_JSON_CHANGED=$(git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ github.event.pull_request.head.sha }} | grep -q "docs.json" && echo "true" || echo "false")
if [ "$DOCS_JSON_CHANGED" = "false" ]; then
echo "⚠️ docs.json was not modified - if files were moved, redirects should be added."
fi
# Check docs.json content for redirects if it was changed
if [ "$DOCS_JSON_CHANGED" = "true" ]; then
echo "Checking docs.json for redirects..."
# Get the current content of docs.json
DOCS_JSON_CONTENT=$(cat docs.json)
for deleted_file in $DELETED_FILES; do
# Remove .mdx extension
SOURCE_PATH="${deleted_file%.mdx}"
# Check if a redirect exists for this path
if echo "$DOCS_JSON_CONTENT" | grep -q "\"source\": \"/${SOURCE_PATH}\"" || echo "$DOCS_JSON_CONTENT" | grep -q "\"source\": \"${SOURCE_PATH}\""; then
echo "✅ Found redirect for moved file: ${deleted_file}"
else
echo "❌ Missing redirect in docs.json for: ${deleted_file}"
MOVED_FILES="$MOVED_FILES ${deleted_file}"
fi
done
else
# If docs.json wasn't changed, all deleted files are suspect
MOVED_FILES="$DELETED_FILES"
fi
fi
echo "Changed MDX files outside zh-CN directory:"
echo "$CHANGED_FILES"
echo "------------------------"
# Check each file for corresponding zh-CN updates
MISSING_TRANSLATIONS=""
for file in $CHANGED_FILES; do
# Determine the corresponding zh-CN path based on whether it's a snippet or not
if [[ $file == snippets/* ]]; then
# For snippets, the Chinese version should be in snippets/zh/
zh_file="${file/snippets\//snippets\/zh\/}"
else
# For regular docs, the Chinese version should be in zh-CN/
zh_file="zh-CN/${file}"
fi
# Check if the corresponding zh-CN file was also modified
if git diff --name-only --diff-filter=ACMRT ${{ github.event.pull_request.base.sha }} ${{ github.event.pull_request.head.sha }} | grep -q "^${zh_file}$"; then
echo "✅ Found corresponding change: ${zh_file}"
else
# Check if the zh-CN file exists at all
if [ -f "${zh_file}" ]; then
echo "❌ Missing update to existing file: ${zh_file}"
MISSING_TRANSLATIONS="$MISSING_TRANSLATIONS ${zh_file}"
else
echo "❌ Missing equivalent file: ${zh_file}"
MISSING_TRANSLATIONS="$MISSING_TRANSLATIONS ${zh_file}"
fi
fi
done
# For added files, check corresponding zh-CN files
for file in $ADDED_FILES; do
# Determine the corresponding zh-CN path based on whether it's a snippet or not
if [[ $file == snippets/* ]]; then
# For snippets, the Chinese version should be in snippets/zh/
zh_file="${file/snippets\//snippets\/zh\/}"
else
# For regular docs, the Chinese version should be in zh-CN/
zh_file="zh-CN/${file}"
fi
# Check if the corresponding zh-CN file was also added
if git diff --name-only --diff-filter=A ${{ github.event.pull_request.base.sha }} ${{ github.event.pull_request.head.sha }} | grep -q "^${zh_file}$"; then
echo "✅ Found corresponding added file: ${zh_file}"
else
echo "❌ Missing equivalent for added file: ${zh_file}"
MISSING_TRANSLATIONS="$MISSING_TRANSLATIONS ${zh_file}"
fi
done
# Determine if we need to fail the check
SHOULD_FAIL="false"
# Check for missing translations
if [ "$MISSING_TRANSLATIONS" != "" ]; then
SHOULD_FAIL="true"
echo "------------------------"
echo "❌ The following translations need to be updated:"
for missing in $MISSING_TRANSLATIONS; do
echo "- $missing"
done
fi
# Check for moved files without redirects
if [ "$MOVED_FILES" != "" ]; then
SHOULD_FAIL="true"
echo "------------------------"
echo "❌ The following files were moved but missing redirects in docs.json:"
for moved in $MOVED_FILES; do
echo "- $moved"
done
echo "------------------------"
echo "Please add redirects in docs.json for moved files using the format:"
echo '{"redirects":[{"source":"/path/to/old-file","destination":"/path/to/new-file"}]}'
echo "------------------------"
fi
# Exit with error if we found issues
if [ "$SHOULD_FAIL" = "true" ]; then
echo "------------------------"
echo "Please fix the issues above before merging this PR."
exit 1
else
echo "------------------------"
echo "✅ All checks passed!"
fi