-
Notifications
You must be signed in to change notification settings - Fork 8
303 lines (302 loc) · 13.1 KB
/
translation.yml
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
name: spellcheck
on: [pull_request_target]
jobs:
prepare:
outputs:
status: ${{ steps.translation-check.outputs.status }}
translation: ${{ steps.translation-check.outputs.translation }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
ref: ${{ github.head_ref }}
repository: ${{ github.event.pull_request.head.repo.full_name }}
- id: changed-files
run: git diff --diff-filter=d --name-only origin/${{ github.base_ref }}...${{ github.event.pull_request.head.sha }} > ./changed_files.txt
- name: Check translation
# Check there is only one translation change
# If there are more than one, the workflow will fail
id: translation-changes
run: |
changed_files=$(cat ./changed_files.txt)
if [ -z "$changed_files" ]; then
echo "No files changed"
else
translation_files=$(echo "$changed_files" | grep -E ".*\.md" || ./pass.sh)
# Filter out files not in "files/uk" folder
translation_files=$(echo "$translation_files" | grep -E "^files/uk/.*\.md" || ./pass.sh)
echo "$translation_files"
echo "$translation_files" > ./translation_files.txt
fi
- id: translation-check
run: |
translation_files=$(cat ./translation_files.txt)
if [ -z "$translation_files" ]; then
echo "No translation files changed"
else
if [ $(echo "$translation_files" | wc -l) -gt 1 ]; then
echo "More than one translation file changed"
exit 1
else
echo "translation=$translation_files" >> $GITHUB_OUTPUT
# Does this file exist in the commit ${{ github.event.pull_request.base.sha }}?
if $(git show ${{ github.event.pull_request.base.sha }}:$translation_files > /dev/null 2>&1); then
echo "status=update" >> $GITHUB_OUTPUT
else
echo "status=translation" >> $GITHUB_OUTPUT
fi
fi
fi
languagetool:
if: ${{ needs.prepare.outputs.translation != '' }}
name: runner / languagetool
needs: [prepare]
outputs:
has_mistakes: ${{ steps.check-spelling.outputs.has_mistakes }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.head_ref }}
repository: ${{ github.event.pull_request.head.repo.full_name }}
- name: Remove code blocks
run: |
file=${{ needs.prepare.outputs.translation }}
sed -i '/```/,/```/d' $file
# Strip all macros
- id: strip-macros
run: |
file=${{ needs.prepare.outputs.translation }}
# Remove all macros with no arguments
sed -i 's/{{[a-zA-Z_-]*}}//' $file
# Replace macros with one argument
sed -i 's/{{[[:alnum:]_-]*(\("[[:alnum:]_-]*"\))}}/\1/g' $file
# Replace macros with two arguments
sed -i 's/{{[[:alnum:]_-]*(\("[[:alnum:]_-]*"\), \("[[:alnum:]_-]*"\))}}/\2/g' $file
# Replace macros with more than two arguments
sed -i 's/{{[[:alnum:]_-]*(\("[[:alnum:]_-]*"\), \("[[:alnum:]_-]*"\), \("[[:alnum:]_-]*"\))}}/\2/g' $file
# Reduce markdown to plain text
- run: sudo apt install pandoc -y
- id: md2txt
name: Convert markdown to plain text
run: |
file=${{ needs.prepare.outputs.translation }}
tmpHtmlFile=$(echo $file | sed 's/\.md/\.html/')
pandoc -f markdown -t html -o $tmpHtmlFile $file
newFileName=$(echo $file | sed 's/\.md/\.txt/')
pandoc -f html -t plain -o $newFileName $tmpHtmlFile
echo "translation=$newFileName" >> $GITHUB_OUTPUT
echo $newFileName
- if: steps.md2txt.outputs.translation == ''
name: Check translation is found
run: echo "No translation file found" && exit 1
- uses: actions/setup-java@v3
with:
distribution: "temurin"
java-version: "8"
- name: Download LanguageTool
run: wget -q https://languagetool.org/download/LanguageTool-6.3.zip
- name: Unzip LanguageTool
run: unzip -qq LanguageTool-6.3.zip
- name: Add spelling additions
run: |
cat uk_spelling_additions.txt >> ./LanguageTool-6.3/org/languagetool/resource/uk/hunspell/spelling.txt
cat uk_ignore_additions.txt >> ./LanguageTool-6.3/org/languagetool/resource/uk/hunspell/ignore.txt
- id: disabled-rules
name: Determine disabled rules
run: echo "disabled_rules=$(cat disabled_rules.txt | tr '\n' ',')" >> $GITHUB_OUTPUT
- id: check-spelling
name: Check spelling
run: |
cd LanguageTool-6.3
java -jar languagetool-commandline.jar -d ${{steps.disabled-rules.outputs.disabled_rules}} -l uk --json ../${{ steps.md2txt.outputs.translation }} > ../result.json
cat ../result.json
matches=$(cat ../result.json | jq '.matches')
# Check if matches equal []
echo "has_mistakes=$(if [ "$matches" == "[]" ]; then echo "false"; else echo "true"; fi)" >> $GITHUB_OUTPUT
- name: Upload result.json
uses: actions/upload-artifact@v3
with:
name: result
path: result.json
- name: Upload text file
uses: actions/upload-artifact@v3
with:
name: text
path: ${{ steps.md2txt.outputs.translation }}
report-spelling:
if: ${{ needs.languagetool.outputs.has_mistakes != 'false' }}
name: Report spelling
needs: [prepare, languagetool]
permissions:
contents: read
pull-requests: write
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.head_ref }}
repository: ${{ github.event.pull_request.head.repo.full_name }}
- uses: actions/download-artifact@v3
with:
name: result
path: .
- uses: actions/setup-node@v4
with:
node-version: "20"
- uses: actions/download-artifact@v3
with:
name: text
path: .
- name: Create mapping
run: node scripts/create-file-mapping.js ./index.txt ${{ needs.prepare.outputs.translation }} > ./mapping.json && cat ./mapping.json
- env:
COMMIT_ID: ${{ github.event.pull_request.head.sha }}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ github.event.pull_request.number }}
name: Send results
run: node scripts/send-comments.js ${{ needs.prepare.outputs.translation }}
timeout-minutes: 5
- name: Debug results
run: echo "Spelling errors found" && cat result.json
set-title:
if: ${{ needs.prepare.outputs.translation != '' }}
name: Set title
needs: [prepare]
permissions:
contents: read
pull-requests: write
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.head_ref }}
repository: ${{ github.event.pull_request.head.repo.full_name }}
- id: determine
run: |
translation=${{ needs.prepare.outputs.translation }}
# Trim files/uk/ from the beginning of the path
translation=${translation#files/uk/}
# Trim index.md from the end of the path
translation=${translation%/index.md}
echo "slug=$translation" >> $GITHUB_OUTPUT
# Set the topmost folder as a section
section=$(echo $translation | cut -d'/' -f1)
# if section is "web", switch to the second folder
section=$(if [ "$section" == "web" ]; then echo $(echo $translation | cut -d'/' -f2); else echo "$section"; fi)
# if section is "javascript", set it to "JS"
section=$(if [ "$section" == "javascript" ]; then echo "JS"; else echo "$section"; fi)
# if section is "css", set it to "CSS"
section=$(if [ "$section" == "css" ]; then echo "CSS"; else echo "$section"; fi)
# if section is "html", set it to "HTML"
section=$(if [ "$section" == "html" ]; then echo "HTML"; else echo "$section"; fi)
# if section is "svg", set it to "SVG"
section=$(if [ "$section" == "svg" ]; then echo "SVG"; else echo "$section"; fi)
# otherwise, just capitalize the first letter
section=$(echo $section | sed 's/\b\(.\)/\u\1/g')
echo "section=$section" >> $GITHUB_OUTPUT
- env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh pr edit ${{ github.event.pull_request.number }} --repo webdoky/content --title "${{ needs.prepare.outputs.status }}(${{ steps.determine.outputs.section }}): ${{ steps.determine.outputs.slug }}"
set-translation-body:
if: ${{ needs.prepare.outputs.translation != '' && needs.prepare.outputs.status == 'translation' }}
name: Set translation body
needs: [prepare]
permissions:
contents: read
pull-requests: write
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.head_ref }}
repository: ${{ github.event.pull_request.head.repo.full_name }}
# set the last section of slug as name
- id: create-body
run: |
translation=${{ needs.prepare.outputs.translation }}
echo $translation
# get "slug" frontmatter value
slug=$(cat $translation | grep -E "^slug: " | sed 's/slug: //')
echo $slug
# get "title" frontmatter value
title=$(cat $translation | grep -E "^title: " | sed 's/title: //')
# escape HTML entities from title
title=$(echo $title | sed 's/</\</g')
title=$(echo $title | sed 's/>/\>/g')
# replace "/uk/" with "/en-us/" in translation
original=$(echo $translation | sed 's/\/uk\//\/en-us\//')
body="Оригінальний вміст: [$title@MDN](https://developer.mozilla.org/en-us/docs/$slug), [сирці $title@GitHub](https://github.com/mdn/content/blob/main/$original)"
echo $body
echo $body > ./body.md
- env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh pr edit ${{ github.event.pull_request.number }} --body "$(cat ./body.md)" --repo webdoky/content
set-update-body:
if: ${{ needs.prepare.outputs.translation != '' && needs.prepare.outputs.status == 'update' }}
name: Set update body
needs: [prepare]
permissions:
contents: read
pull-requests: write
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
# We need latest commit in master, not PR commit
ref: master
- id: get-latest-commit-time
name: Get latest commit time
run: |
latest_commit_time=$(git log -1 --format=%ct -- ${{ needs.prepare.outputs.translation }})
echo "latest_commit_time=$latest_commit_time" >> $GITHUB_OUTPUT
- name: Checkout original content
uses: actions/checkout@v4
with:
repository: mdn/content
path: original
fetch-depth: 0
- id: get-new-commits
name: Get new commits
# Get new commits sha for $translation file
run: |
original=${{ needs.prepare.outputs.translation }}
# replace /uk/ with /en-us/ in original
original=$(echo $original | sed 's/\/uk\//\/en-us\//')
cd ./original
git log --pretty=format:"%H" --since=${{steps.get-latest-commit-time.outputs.latest_commit_time}} -- $original > ../new_commits.txt
cat ../new_commits.txt
- id: create-body
run: |
translation=${{ needs.prepare.outputs.translation }}
# get "slug" frontmatter value
slug=$(cat $translation | grep -E "^slug: " | sed 's/slug: //')
# get "title" frontmatter value
title=$(cat $translation | grep -E "^title: " | sed 's/title: //')
# escape HTML entities from title
title=$(echo $title | sed 's/</\</g')
title=$(echo $title | sed 's/>/\>/g')
# replace "/uk/" with "/en-us/" in translation
original=$(echo $translation | sed 's/\/uk\//\/en-us\//')
echo "Оригінальний вміст: [$title@MDN](https://developer.mozilla.org/en-us/docs/$slug), [сирці $title@GitHub](https://github.com/mdn/content/blob/main/$original)" > ./body.md
echo "" >> ./body.md
echo "Нові зміни:" >> ./body.md
# Add commits from ./new_commits.txt to body as links
commits=$(cat ./new_commits.txt)
for commit in $commits
do
changes=$(cd ./original && git log -1 --format=%s $commit)
# escape HTML entities
changes=$(echo $changes | sed 's/</\</g')
changes=$(echo $changes | sed 's/>/\>/g')
echo "- [$changes](https://github.com/mdn/content/commit/$commit)" >> ./body.md
done
cat ./body.md
- env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh pr edit ${{ github.event.pull_request.number }} --body "$(cat ./body.md)" --repo webdoky/content