-
Notifications
You must be signed in to change notification settings - Fork 1.3k
508 lines (452 loc) · 17.2 KB
/
Copy pathupdate-xlf-on-comment.yml
File metadata and controls
508 lines (452 loc) · 17.2 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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
name: Update XLF files on command
on:
issue_comment:
types: [created]
permissions:
contents: read
env:
REPO_NAME: ${{ github.event.repository.name }}
RUN_URL: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}
jobs:
authorize:
name: Authorize Request
if: github.event.issue.pull_request
runs-on: ubuntu-latest
permissions:
contents: read
issues: write
pull-requests: write
outputs:
should_run: ${{ steps.command-filter.outputs.should_run }}
head_ref: ${{ steps.metadata.outputs.head_ref }}
steps:
- name: Evaluate comment command
id: command-filter
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7
env:
COMMENT_BODY: ${{ github.event.comment.body }} # GH Has a Character Limit which prevents overflow issues. https://github.com/dead-claudia/github-limits
with:
script: |
const body = (process.env.COMMENT_BODY || '').trim().toLowerCase();
const shouldRun = body.startsWith('/updatexlf') || body.startsWith('/xlf');
core.setOutput('should_run', String(shouldRun));
if (!shouldRun) {
core.info('Comment does not invoke /updatexlf or /xlf. Skipping workflow.');
}
- name: Ensure commenter is trusted
if: steps.command-filter.outputs.should_run == 'true'
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7
with:
github-token: ${{ github.token }}
script: |
const { owner, repo } = context.repo;
const username = context.payload.comment.user.login;
if (!username) {
throw new Error('Unable to resolve commenter username from event payload.');
}
try {
const { data: permission } = await github.rest.repos.getCollaboratorPermissionLevel({
owner,
repo,
username,
});
const allowed = ['admin', 'write'];
if (!allowed.includes(permission.permission)) {
throw new Error(`@${username} has "${permission.permission}" access.`);
}
core.info(`Verified ${username} has ${permission.permission} access.`);
} catch (error) {
throw new Error(`Only collaborators with write access may trigger this workflow. ${error.message || error}`);
}
- name: React to comment
if: steps.command-filter.outputs.should_run == 'true'
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7
with:
github-token: ${{ github.token }}
script: |
const { owner, repo } = context.repo;
const commentId = context.payload.comment?.id;
if (!commentId) {
core.warning('No comment ID found on payload; skipping reaction.');
return;
}
await github.rest.reactions.createForIssueComment({
owner,
repo,
comment_id: Number(commentId),
content: 'eyes',
});
- name: Comment on PR - Started
if: steps.command-filter.outputs.should_run == 'true'
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7
env:
COMMENT_BODY: ${{ format('▶️ XLF update workflow started. Track progress in [this workflow run]({0}).', env.RUN_URL) }}
with:
github-token: ${{ github.token }}
script: |
const { owner, repo } = context.repo;
await github.rest.issues.createComment({
owner,
repo,
issue_number: Number(context.payload.issue.number),
body: process.env.COMMENT_BODY,
});
- name: Capture PR metadata
id: metadata
if: steps.command-filter.outputs.should_run == 'true'
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7
with:
script: |
const pr = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.payload.issue.number,
});
if (pr.data.state !== 'open') {
core.setFailed(`Pull request #${pr.data.number} is not open.`);
return;
}
core.setOutput('head_ref', pr.data.head.ref);
generate:
name: Generate XLF Updates
needs: authorize
if: needs.authorize.outputs.should_run == 'true'
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: read
outputs:
changes: ${{ steps.check.outputs.changes }}
diff_summary: ${{ steps.package.outputs.diff_summary }}
defaults:
run:
working-directory: pr
steps:
- name: Checkout PR head
uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6
with:
token: ${{ github.token }}
fetch-depth: 0
ref: refs/pull/${{ github.event.issue.number }}/head
path: pr
persist-credentials: false
- name: Run UpdateXlf target
id: update
run: |
chmod +x ./build.sh
if ./build.sh /t:UpdateXlf; then
echo "mode=incremental" >> "$GITHUB_OUTPUT"
exit 0
fi
if ./build.sh; then
echo "mode=full" >> "$GITHUB_OUTPUT"
exit 0
fi
exit 1
continue-on-error: true
timeout-minutes: 20
env:
GITHUB_TOKEN: ""
GH_TOKEN: ""
- name: Check for XLF changes
id: check
if: steps.update.outcome == 'success'
run: |
set -euo pipefail
shopt -s globstar
mapfile -t changed_files < <(git diff --name-only)
if [ ${#changed_files[@]} -eq 0 ]; then
echo "changes=false" >> "$GITHUB_OUTPUT"
exit 0
fi
for file in "${changed_files[@]}"; do
if [[ "$file" != *.xlf ]]; then
echo "Unexpected file changed: $file" >&2
exit 1
fi
done
printf '%s\n' "${changed_files[@]}" > __changed-files.txt
echo "changes=true" >> "$GITHUB_OUTPUT"
- name: Evaluate generation result
id: evaluate
if: always()
run: |
status="success"
reason=""
if [ "${UPDATE_OUTCOME}" != "success" ]; then
status="failed"
reason="update"
fi
echo "status=$status" >> "$GITHUB_OUTPUT"
if [ -n "$reason" ]; then
echo "failure_reason=$reason" >> "$GITHUB_OUTPUT"
fi
env:
UPDATE_OUTCOME: ${{ steps.update.outcome }}
- name: Package XLF artifacts
id: package
if: steps.evaluate.outputs.status == 'success' && steps.check.outputs.changes == 'true'
run: |
mkdir -p __artifacts
mv __changed-files.txt __artifacts/changed-files.txt
git diff --stat -- '**/*.xlf' > __artifacts/diff-summary.txt
tar -czf __artifacts/xlf-files.tar.gz -T __artifacts/changed-files.txt
printf 'diff_summary<<EOF\n%s\nEOF\n' "$(cat __artifacts/diff-summary.txt)" >> "$GITHUB_OUTPUT"
- name: Upload XLF artifacts
if: steps.evaluate.outputs.status == 'success' && steps.check.outputs.changes == 'true'
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
with:
name: xlf-updates
path: pr/__artifacts
if-no-files-found: error
- name: Create failure diagnostics
if: steps.evaluate.outputs.status == 'failed'
env:
FAILURE_REASON: ${{ steps.evaluate.outputs.failure_reason }}
run: |
mkdir -p __failure
if [ -n "${FAILURE_REASON}" ]; then
echo "${FAILURE_REASON}" > __failure/failure_reason.txt
else
echo "unknown" > __failure/failure_reason.txt
fi
- name: Upload failure diagnostics
if: steps.evaluate.outputs.status == 'failed'
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
with:
name: xlf-updates-failure
path: pr/__failure
if-no-files-found: ignore
- name: Fail job when update unsuccessful
if: steps.evaluate.outputs.status == 'failed'
run: |
echo "XLF update failed."
exit 1
apply:
name: Apply XLF Updates
needs: [authorize, generate]
if: needs.authorize.outputs.should_run == 'true' && needs.generate.result == 'success'
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
issues: write
steps:
- name: Checkout PR branch
uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6
with:
token: ${{ github.token }}
fetch-depth: 0
ref: ${{ needs.authorize.outputs.head_ref }}
persist-credentials: false
- name: Download XLF artifacts
if: needs.generate.outputs.changes == 'true'
uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4
with:
name: xlf-updates
path: artifact
- name: Apply XLF updates
if: needs.generate.outputs.changes == 'true'
run: |
tar -xzf artifact/xlf-files.tar.gz
- name: Validate and stage XLF files
if: needs.generate.outputs.changes == 'true'
run: |
set -euo pipefail
shopt -s globstar
if [ ! -f artifact/changed-files.txt ]; then
echo "Expected changed-files.txt is missing" >&2
exit 1
fi
while IFS= read -r file; do
[ -z "$file" ] && continue
if [[ "$file" != *.xlf ]]; then
echo "Disallowed file in artifact: $file" >&2
exit 1
fi
if [ ! -f "$file" ]; then
echo "File listed in artifact is missing: $file" >&2
exit 1
fi
git add "$file"
done < artifact/changed-files.txt
- name: Prepare commit message
id: commit-message
if: needs.generate.outputs.changes == 'true'
shell: bash
run: |
COMMIT_DATE=$(date -u +"%Y-%m-%d")
printf 'message=Update XLF translation files - %s\n' "$COMMIT_DATE" >> "$GITHUB_OUTPUT"
- name: Commit XLF changes
id: commit
if: needs.generate.outputs.changes == 'true'
shell: bash
env:
COMMIT_MESSAGE: ${{ steps.commit-message.outputs.message }}
ALLOWED_PATTERNS: |
**/*.xlf
GIT_USER_NAME: github-actions[bot]
GIT_USER_EMAIL: github-actions[bot]@users.noreply.github.com
run: |
set -euo pipefail
shopt -s globstar
git config user.name "${GIT_USER_NAME}"
git config user.email "${GIT_USER_EMAIL}"
mapfile -t staged_files < <(git diff --cached --name-only)
if [ ${#staged_files[@]} -eq 0 ]; then
echo "No staged files were found. Nothing to commit." >&2
exit 1
fi
if [ -z "${ALLOWED_PATTERNS//,/ }" ]; then
echo "No allowed patterns were provided." >&2
exit 1
fi
patterns_normalised=$(printf '%s' "${ALLOWED_PATTERNS}" | tr ',' '\n')
mapfile -t allowed_patterns < <(printf '%s\n' "${patterns_normalised}" | sed -e 's/^\s*//' -e 's/\s*$//' | sed -e '/^$/d')
if [ ${#allowed_patterns[@]} -eq 0 ]; then
echo "Allowed pattern list is empty after normalisation." >&2
exit 1
fi
for file in "${staged_files[@]}"; do
match=false
for pattern in "${allowed_patterns[@]}"; do
if [[ "$file" == $pattern ]]; then
match=true
break
fi
done
if [ "$match" = false ]; then
echo "File '$file' does not match the allowed patterns." >&2
exit 1
fi
done
git commit -m "${COMMIT_MESSAGE}"
- name: Push XLF changes
if: steps.commit.outcome == 'success'
env:
HEAD_REF: ${{ needs.authorize.outputs.head_ref }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
git push "https://x-access-token:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}" HEAD:"${HEAD_REF}"
- name: Prepare success comment
id: success-comment
if: steps.commit.outcome == 'success'
shell: bash
env:
DIFF_SUMMARY: ${{ needs.generate.outputs.diff_summary }}
run: |
body="✅ XLF translation files have been updated and committed to this PR. See [workflow details](${RUN_URL})."
if [ -n "${DIFF_SUMMARY}" ]; then
body="${body}"$'\n\n```\n'"${DIFF_SUMMARY}"$'\n```'
fi
printf 'body<<EOF\n%s\nEOF\n' "$body" >> "$GITHUB_OUTPUT"
- name: Comment on PR - Success
if: steps.commit.outcome == 'success'
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7
env:
COMMENT_BODY: ${{ steps.success-comment.outputs.body }}
with:
github-token: ${{ github.token }}
script: |
const { owner, repo } = context.repo;
const reactionCommentId = context.payload.comment.id;
await github.rest.issues.createComment({
owner,
repo,
issue_number: Number(context.payload.issue.number),
body: process.env.COMMENT_BODY,
});
if (reactionCommentId) {
await github.rest.reactions.createForIssueComment({
owner,
repo,
comment_id: Number(reactionCommentId),
content: '+1',
});
}
- name: Comment on PR - No changes
if: needs.generate.outputs.changes != 'true'
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7
env:
COMMENT_BODY: ${{ format('ℹ️ No XLF files needed to be updated. Review [the workflow run]({0}).', env.RUN_URL) }}
with:
github-token: ${{ github.token }}
script: |
const { owner, repo } = context.repo;
const reactionCommentId = context.payload.comment.id;
await github.rest.issues.createComment({
owner,
repo,
issue_number: Number(context.payload.issue.number),
body: process.env.COMMENT_BODY,
});
if (reactionCommentId) {
await github.rest.reactions.createForIssueComment({
owner,
repo,
comment_id: Number(reactionCommentId),
content: '+1',
});
}
report-failure:
name: Report Failure
needs: [authorize, generate, apply]
if: needs.authorize.outputs.should_run == 'true' && needs.authorize.result == 'success' && failure()
runs-on: ubuntu-latest
permissions:
issues: write
pull-requests: write
steps:
- name: Download failure diagnostics
uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4
with:
name: xlf-updates-failure
path: diagnostics
continue-on-error: true
- name: Prepare failure comment
id: failure-comment
shell: bash
env:
GENERATE_RESULT: ${{ needs.generate.result }}
APPLY_RESULT: ${{ needs.apply.result }}
run: |
run_url="${RUN_URL}"
reason=""
if [ -f diagnostics/failure_reason.txt ]; then
reason=$(tr -d '\r' < diagnostics/failure_reason.txt | tr -d '\000')
fi
message="❌ Failed to update XLF files."
if [ "${GENERATE_RESULT}" = "failure" ]; then
case "$reason" in
update)
message="$message The XLF update script failed."
;;
esac
elif [ "${APPLY_RESULT}" = "failure" ]; then
message="$message Applying or pushing the XLF changes failed."
fi
message="$message Please check [the workflow run](${run_url}) for details."
printf 'body<<EOF\n%s\nEOF\n' "$message" >> "$GITHUB_OUTPUT"
- name: Comment on PR - Failure
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7
env:
COMMENT_BODY: ${{ steps.failure-comment.outputs.body }}
with:
github-token: ${{ github.token }}
script: |
const { owner, repo } = context.repo;
const reactionCommentId = context.payload.comment.id;
await github.rest.issues.createComment({
owner,
repo,
issue_number: Number(context.payload.issue.number),
body: process.env.COMMENT_BODY,
});
if (reactionCommentId) {
await github.rest.reactions.createForIssueComment({
owner,
repo,
comment_id: Number(reactionCommentId),
content: 'confused',
});
}