forked from check-spelling/check-spelling
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unknown-words.sh
executable file
·3247 lines (3036 loc) · 116 KB
/
unknown-words.sh
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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# This CI acceptance test is based on:
# https://github.com/jsoref/spelling/tree/04648bdc63723e5cdf5cbeaff2225a462807abc8
# It is conceptually `f` which runs `w` (spelling-unknown-word-splitter)
# plus `fchurn` which uses `dn` mostly rolled together.
set -e
export spellchecker="${spellchecker:-$THIS_ACTION_PATH}"
basic_setup() {
if [ "$(id -u)" != 0 ]; then
SUDO=sudo
fi
$SUDO "$spellchecker/fast-install.pl"
. "$spellchecker/common.sh"
}
dispatcher() {
if [ -n "$INPUT_EVENT_ALIASES" ]; then
GITHUB_EVENT_NAME="$(echo "$INPUT_EVENT_ALIASES" | jq -r ".$GITHUB_EVENT_NAME // $Q$GITHUB_EVENT_NAME$Q")"
fi
INPUT_TASK="${INPUT_TASK:-"$INPUT_CUSTOM_TASK"}"
case "$INPUT_TASK" in
comment|collapse_previous_comment)
comment_task
;;
pr_head_sha)
pr_head_sha_task
;;
esac
case "$GITHUB_EVENT_NAME" in
'')
(
echo 'check-spelling does not know what to do because GITHUB_EVENT_NAME is empty.'
echo
echo 'This could be because of a configuration error with event_aliases.'
echo 'It could be because you are using `act` or a similar GitHub Runner shim,'
echo 'and its configuration is incorrect.'
) >&2
github_step_summary_likely_fatal \
'GITHUB_EVENT_NAME is empty' \
'Please see the log for more information.' \
'Is `event_aliases` misconfigured? Are you using `act` or a similar shim?'
exit 1
;;
push)
if to_boolean "$INPUT_SUPPRESS_PUSH_FOR_OPEN_PULL_REQUEST" && ! echo "$GITHUB_REPOSITORY" | grep -q '^..*/..*$'; then
(
echo '$GITHUB_REPOSITORY '"($GITHUB_REPOSITORY) does not appear to be an OWNER/REPOSITORY"
if [ -n "$ACT" ]; then
echo '[act] `git remote -v origin` is probably misconfigured'
fi
echo 'Cannot determine if there is an open pull request, proceeding as if there is not.'
) >&2
elif to_boolean "$INPUT_SUPPRESS_PUSH_FOR_OPEN_PULL_REQUEST"; then
pull_request_json="$(mktemp_json)"
pull_request_headers="$(mktemp)"
pull_heads_query="$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/pulls?head=${GITHUB_REPOSITORY%/*}:$GITHUB_REF"
keep_headers=1 call_curl \
"$pull_heads_query" > "$pull_request_json"
mv "$response_headers" "$pull_request_headers"
if [ -n "$(jq .documentation_url "$pull_request_json" 2>/dev/null)" ]; then
(
echo "Request for '$pull_heads_query' appears to have yielded an error, it is probably an authentication error."
if [ -n "$ACT" ]; then
echo '[act] If you want to use suppress_push_for_open_pull_request, you need to set GITHUB_TOKEN'
fi
echo "Headers:"
cat "$pull_request_headers"
echo "Response:"
cat "$pull_request_json"
echo 'Cannot determine if there is an open pull request, proceeding as if there is not.'
) >&2
elif [ "$(jq length "$pull_request_json")" -gt 0 ]; then
(
open_pr_number="$(jq -r '.[0].number' "$pull_request_json")"
echo "Found [open PR #$open_pr_number]($GITHUB_SERVER_URL/$GITHUB_REPOSITORY/pull/$open_pr_number) - check-spelling should run there."
echo
echo '::warning title=WARNING: Skipped check-spelling::This workflow is intentionally terminating early with a success code -- it has not checked for misspellings.'
pull_request_event_name=pull_request_target
if [ -n "$workflow_path" ]; then
if ! grep -q pull_request_target "$workflow_path" && grep -q pull_request "$workflow_path"; then
pull_request_event_name=pull_request
fi
workflow="workflow (${b}$workflow_path${b})"
else
workflow='workflow'
fi
echo "::notice title=Workflow skipped::See ${b}check-spelling${b} ${b}$pull_request_event_name${b} $workflow in PR #$open_pr_number. $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/pull/$open_pr_number/checks"
echo "# ⏭️ Workflow skipped$n${n}See ${b}check-spelling${b} ${b}$pull_request_event_name${b} $workflow in PR #$open_pr_number.$n$n$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/pull/$open_pr_number/checks" >> "$GITHUB_STEP_SUMMARY"
if [ -n "$ACT" ]; then
echo
echo 'You appear to be running nektos/act, you should probably comment out:'
echo
echo " suppress_push_for_open_pull_request: $INPUT_SUPPRESS_PUSH_FOR_OPEN_PULL_REQUEST"
fi
) >&2
exit 0
fi
fi
if [ -z "$INPUT_TASK" ]; then
INPUT_TASK=spelling
fi
;;
pull_request|pull_request_target)
if [ -z "$INPUT_TASK" ]; then
INPUT_TASK=spelling
fi
if [ "$INPUT_TASK" = spelling ] && [ "$(are_head_and_base_in_same_repo "$GITHUB_EVENT_PATH" '.pull_request')" != 'true' ]; then
api_output=$(mktemp)
api_error=$(mktemp)
GH_TOKEN="$GITHUB_TOKEN" gh api --method POST -H "Accept: application/vnd.github+json" "$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/branches/${GITHUB_BASE_REF:-$GITHUB_REF_NAME}/rename" > "$api_output" 2> "$api_error" || true
if ! grep -Eq 'not authorized|not accessible' "$api_output"; then
if to_boolean "$INPUT_USE_SARIF"; then
INPUT_USE_SARIF=
set_up_reporter
fi
echo '::error title=Unsafe Permissions: check-spelling::This workflow configuration is unsafe. Please see https://github.com/check-spelling/check-spelling/wiki/Feature:-Restricted-Permissions'
github_step_summary_likely_fatal \
'Unsafe Permissions' \
'This workflow configuration is unsafe.' \
':information_source: Please see https://github.com/check-spelling/check-spelling/wiki/Feature:-Restricted-Permissions'
quit 5
fi
fi
;;
schedule)
export GH_TOKEN="$GITHUB_TOKEN"
exec "$spellchecker/check-pull-requests.sh"
;;
issue_comment)
if to_boolean "$DEBUG"; then
set -x
fi
if [ -z "$INPUT_TASK" ]; then
INPUT_TASK=update
fi
handle_comment
;;
pull_request_review_comment)
body="$(echo '
If you are trying to ask @check-spelling-bot to update a PR,
please quote the comment link as a top level comment instead
of in a comment on a block of code.
Future versions may support this feature.
For the time being, early adopters should remove the
`pull_request_review_comment` event from their workflow.
workflow.
' | strip_lead)"
( echo 'check-spelling does not currently support comments on code.
'"$body" \
| strip_lead
) >&2
github_step_summary_likely_fatal_event 'Handling comments on code is not supported by check-spelling' "$body" 'unsupported-configuration-review-comment'
quit 0
;;
*)
body="$(echo "
check-spelling does not currently support the GitHub $b$GITHUB_EVENT_NAME$b event.
If you think it can, consider using:
with:
event_aliases: {$Q$GITHUB_EVENT_NAME$Q:${Q}supported_event_name${Q}}
Future versions may support this feature." \
| perl -pe 's/^ {10}//'
)"
echo "$body" >&2
github_step_summary_likely_fatal_event 'Unsupported event name' "$body" 'unsupported-configuration-event'
exit 1
;;
esac
}
load_env() {
input_variables="$(mktemp)"
"$spellchecker/load-env.pl" > "$input_variables"
. "$input_variables"
}
wrap_in_json() {
echo '{}' | jq -r --arg arg "$2" ".$1="'$arg'
}
wrap_file_in_json() {
echo '{}' | jq -r --rawfile arg "$2" ".$1="'$arg'
}
who_am_i() {
who_am_i='query { viewer { databaseId } }'
who_am_i_json="$(wrap_in_json 'query' "$who_am_i")"
comment_author_id=$(
call_curl \
-H "Content-Type: application/json" \
--data-binary "$who_am_i_json" \
"$GITHUB_GRAPHQL_URL" |
jq -r '.data.viewer.databaseId // empty'
)
}
get_is_comment_minimized() {
comment_is_collapsed_query="query { node(id:$Q$1$Q) { ... on IssueComment { minimizedReason } } }"
comment_is_collapsed_json="$(wrap_in_json 'query' "$comment_is_collapsed_query")"
call_curl \
-H "Content-Type: application/json" \
--data-binary "$comment_is_collapsed_json" \
"$GITHUB_GRAPHQL_URL" |
jq -r '.data.node.minimizedReason'
}
is_comment_minimized() {
[ "$(get_is_comment_minimized "$1")" != "null" ]
}
get_previous_comment() {
comment_search_re="$(title="$report_header" perl -e 'my $title=quotemeta($ENV{title}); $title=~ s/\\././g; print "(?:^|\n)$title";')"
get_a_comment "$comment_search_re" | head -1
}
get_a_comment() {
comment_search_re="$1"
if [ -z "$comment_author_id" ]; then
who_am_i
fi
# In English
# we're composing a list
# but decomposing our input
# selecting elements
# if the user.login.id is our target user
# and it isn't the comment we just posted...
# and the body has our magic keyword (this requires some tuning for matrices)
# from that element, we only want the node_id
# we want the last element
# if there's no element, we want the empty string (not 'null')
jq_comment_query='[ .[] | select(. | (.user.id=='"${comment_author_id:-0}"') and (.node_id != "'"$posted_comment_node_id"'") and (.body | test ("'"$comment_search_re"'") ) ) | .node_id ] | .[-1] // empty'
get_page() {
url="$1"
dir="$2"
keep_headers=1 call_curl "$url" > "$pr_comments"
# Subset of rfc8288#section-3
link="$(dir="$dir" perl -ne 'next unless s/^link:.*<([^>]*)>[^,]*$ENV{dir}.*/$1/; print' "$response_headers" )"
if [ -n "$link" ] && [ "$dir" = "last" ]; then
get_page "$link" "prev"
return
fi
node_id="$(jq -r "$jq_comment_query" "$pr_comments")"
if [ -n "$node_id" ]; then
(
echo "$node_id"
get_is_comment_minimized "$node_id"
)
return
fi
if [ -n "$link" ]; then
get_page "$link" "prev"
return
fi
}
pr_comments="$(mktemp_json)"
get_page "$COMMENTS_URL" "last"
rm "$pr_comments"
}
get_comment_url_from_id() {
id="$1"
comment_url_from_id_query="query { node(id:$Q$id$Q) { ... on IssueComment { url } } }"
comment_url_from_id_json="$(wrap_in_json 'query' "$comment_url_from_id_query")"
call_curl \
-H "Content-Type: application/json" \
--data-binary "$comment_url_from_id_json" \
"$GITHUB_GRAPHQL_URL" |
jq -r '.data.node.url // empty'
}
comment_task() {
set_up_files
if [ -n "$INPUT_INTERNAL_STATE_DIRECTORY" ]; then
if [ -z "$NEW_TOKENS" ]; then
NEW_TOKENS="$tokens_file"
fi
if [ -z "$STALE_TOKENS" ]; then
STALE_TOKENS="$INPUT_INTERNAL_STATE_DIRECTORY/remove_words.txt"
fi
if [ -s "$INPUT_INTERNAL_STATE_DIRECTORY/followup" ]; then
followup="$(cat "$INPUT_INTERNAL_STATE_DIRECTORY/followup")"
if [ "$followup" = "collapse_previous_comment" ]; then
previous_comment_node_id="$(cat "$data_dir/previous_comment.txt")"
if [ -n "$previous_comment_node_id" ]; then
collapse_comment "$previous_comment_node_id"
quit 0
fi
fi
fi
else
# This behavior was used internally and is not recommended.
# I hope to remove support for it relatively soon, as I don't think anyone
# externally picked up this flavor.
# check-spelling/spell-check-this never suggested it.
handle_mixed_archive() {
if [ -n "$1" ]; then
ls -d "$(dirname "$1")"/*/"$(basename "$1")" 2>/dev/null || echo "$1"
fi
}
NEW_TOKENS="$(handle_mixed_archive "$NEW_TOKENS")"
STALE_TOKENS="$(handle_mixed_archive "$STALE_TOKENS")"
NEW_EXCLUDES="$(handle_mixed_archive "$NEW_EXCLUDES")"
SUGGESTED_DICTIONARIES="$(handle_mixed_archive "$SUGGESTED_DICTIONARIES")"
fi
touch "$diff_output"
if [ -f "$NEW_TOKENS" ]; then
patch_add="$(cat "$NEW_TOKENS")"
fi
if [ -f "$STALE_TOKENS" ]; then
patch_remove="$(cat "$STALE_TOKENS")"
fi
if [ -f "$NEW_EXCLUDES" ]; then
cat "$NEW_EXCLUDES" > "$should_exclude_file"
fi
if [ -f "$SUGGESTED_DICTIONARIES" ]; then
cat "$SUGGESTED_DICTIONARIES" > "$extra_dictionaries_json"
fi
fewer_misspellings_canary="$(mktemp)"
quit_without_error=1
get_has_errors
if [ -z "$has_errors" ] && [ -z "$patch_add" ]; then
quit
fi
check_spelling_report
}
get_pull_request_url() {
jq -r '.pull_request.url // .issue.pull_request.url // empty' "$GITHUB_EVENT_PATH"
}
get_pr_sha_from_url() {
pull_request_head_info="$(mktemp_json)"
pull_request "$1" | jq -r ".head // empty" > "$pull_request_head_info"
jq -r ".sha // empty" "$pull_request_head_info"
}
pr_head_sha_task() {
pull_request_url="$(get_pull_request_url)"
if [ -n "$pull_request_url" ]; then
echo "PR_HEAD_SHA=$(get_pr_sha_from_url "$pull_request_url")" >> "$GITHUB_ENV"
fi
quit
}
get_action_repo_info() {
workflow="$workflow_path" perl -e '
exit if defined $ENV{ACT};
exit unless $ENV{GITHUB_WORKSPACE} =~ m{(.*?)(?:/[^/]+){2}$};
my $base = "$1/_actions/";
exit unless $ENV{GITHUB_ACTION_PATH} =~ m{\Q$base\E(.*)};
my @parts = split m{/}, $1;
exit unless scalar @parts == 3;
my $action = "$parts[0]/$parts[1]\@$parts[2]";
exit unless open WORKFLOW, "<", $ENV{workflow };
while (<WORKFLOW>) {
next unless /^\s*uses:\s*\Q$action\E/;
print $action;
exit;
}
'
}
get_workflow_path() {
if [ -s "$action_workflow_path_file" ]; then
cat "$action_workflow_path_file"
elif [ -e "$GITHUB_WORKFLOW" ]; then
echo "$GITHUB_WORKFLOW" | tee "$action_workflow_path_file"
else
workflow_path_from_env=$(perl -e 'my $workflow = $ENV{GITHUB_WORKFLOW_REF}; $workflow =~ s!(?:[^/]+/){2}!!; $workflow =~ s!\@.*!!; print $workflow')
if [ -e "$workflow_path_from_env" ]; then
echo "$workflow_path_from_env" | tee "$action_workflow_path_file"
return
fi
action_run="$(mktemp_json)"
if call_curl \
"$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID" > "$action_run"; then
workflow_url="$(jq -r '.workflow_url // empty' "$action_run")"
if [ -n "$workflow_url" ]; then
workflow_json="$(mktemp_json)"
if call_curl \
"$workflow_url" > "$workflow_json"; then
jq -r .path "$workflow_json" | tee "$action_workflow_path_file"
fi
else
possible_workflows=$(mktemp)
github_job_pattern="^\s\s*$GITHUB_JOB\s*:\s*$"
if [ -n "$ACT" ]; then
# https://github.com/nektos/act/issues/1473
github_job_pattern="$github_job_pattern\|^\s\s*name\s*:\s*$GITHUB_JOB\s*$"
fi
find .github/workflows \( -name '*.yml' -o -name '*.yaml' \) -type f ! -empty -print0 |
xargs -0 grep -l --null "^\s\s*uses\s*:\s*$GH_ACTION_REPOSITORY@$GH_ACTION_REF" |
xargs -0 grep -l --null "^name\s*:\s*$GITHUB_WORKFLOW\s*$" |
xargs -0 grep -l --null "$github_job_pattern" > "$possible_workflows"
if [ "$(tr -cd '\000' < "$possible_workflows" | char_count)" -eq 1 ]; then
xargs -0 < "$possible_workflows" | tee "$action_workflow_path_file"
fi
fi
fi
fi
}
should_patch_head() {
if [ ! -d "$bucket/$project" ]; then
# if there is no directory in the merged state, then adding files into it
# will not result in a merge conflict
true
else
# if there is a directory in the merged state, then we don't want to
# suggest changes to the directory if it doesn't exist in the branch,
# because that would almost certainly result in merge conflicts.
# If people want to talk to the bot, they should rebase first.
pull_request_url="$(get_pull_request_url)"
if [ -z "$pull_request_url" ]; then
false
else
pull_request_sha="$(get_pr_sha_from_url "$pull_request_url")"
git fetch origin "$pull_request_sha" >&2
if git ls-tree "$pull_request_sha" -- "$bucket/$project" 2> /dev/null | grep -q tree; then
return 0
fi
return 1
fi
fi
}
offer_quote_reply() {
if [ -n "$offer_quote_reply_cached" ]; then
return "$offer_quote_reply_cached"
fi
if to_boolean "$INPUT_EXPERIMENTAL_APPLY_CHANGES_VIA_BOT"; then
case "$GITHUB_EVENT_NAME" in
issue_comment)
issue="$(mktemp_json)"
pull_request_info="$(mktemp_json)"
if [ "$(are_issue_head_and_base_in_same_repo)" != 'true' ] || ! should_patch_head; then
offer_quote_reply_cached=1
else
offer_quote_reply_cached=0
fi
;;
pull_request|pull_request_target)
if [ "$(are_head_and_base_in_same_repo "$GITHUB_EVENT_PATH" '.pull_request')" != 'true' ] || ! should_patch_head; then
offer_quote_reply_cached=1
else
offer_quote_reply_cached=0
fi
;;
*)
offer_quote_reply_cached=1
;;
esac
else
offer_quote_reply_cached=1
fi
return "$offer_quote_reply_cached"
}
command_v() {
command -v "$1" >/dev/null 2>/dev/null
}
react_comment_and_die() {
trigger_comment_url="$1"
message="$2"
react="$3"
echo "::error ::$message"
react "$trigger_comment_url" "$react" > /dev/null
report="@check-spelling-bot: ${react_prefix}$message${N}See [log]($(get_action_log)) for details."
if [ -n "$GITHUB_STEP_SUMMARY" ]; then
echo "$report" >> "$GITHUB_STEP_SUMMARY"
fi
if [ -n "$COMMENTS_URL" ] && [ -z "${COMMENTS_URL##*:*}" ]; then
PAYLOAD="$(mktemp_json)"
wrap_in_json 'body' "$report" > "$PAYLOAD"
res=0
comment "$COMMENTS_URL" "$PAYLOAD" > /dev/null || res=$?
if [ "$res" -gt 0 ]; then
if ! to_boolean "$DEBUG"; then
echo "::error ::Failed posting to $COMMENTS_URL"
cat "$PAYLOAD"
fi
return "$res"
fi
rm "$PAYLOAD"
fi
quit 1
}
confused_comment() {
react_comment_and_die "$1" "$2" "confused"
}
get_github_user_and_email() {
user_json="$(mktemp_json)"
call_curl \
"$GITHUB_API_URL/users/$1" > "$user_json"
github_name="$(jq -r '.name // empty' "$user_json")"
if [ -z "$github_name" ]; then
github_name="$1"
fi
github_email="$(jq -r '.email // empty' "$user_json")"
rm "$user_json"
if [ -z "$github_email" ]; then
github_email="$1@users.noreply.github.com"
fi
COMMIT_AUTHOR="--author=$github_name <$github_email>"
}
git_commit() {
commit_reason="$1"
git add -u
git config user.email "check-spelling-bot@users.noreply.github.com"
git config user.name "check-spelling-bot"
git commit \
"$COMMIT_AUTHOR" \
--date="$created_at" \
-m "$(echo "[check-spelling] Update metadata
$commit_reason
Signed-off-by: check-spelling-bot <check-spelling-bot@users.noreply.github.com>
on-behalf-of: @check-spelling <check-spelling-bot@check-spelling.dev>
" | strip_lead)"
}
mktemp_json() {
file="$(mktemp)"
mv "$file" "$file.json"
echo "$file.json"
}
show_github_actions_push_disclaimer() {
pr_number="$(jq -r '.issue.number' "$GITHUB_EVENT_PATH")"
pr_path_escaped="$(echo "$GITHUB_REPOSITORY/pull/$pr_number" | perl -pe 's{/}{\%2F}g')"
pr_query=$(echo "{
repository(owner:$Q${GITHUB_REPOSITORY%/*}$Q, name:$Q${GITHUB_REPOSITORY#*/}$Q) {
pullRequest(number:$pr_number) {
headRepository {
nameWithOwner
}
headRefName
}
}
}" |
strip_lead_and_blanks
)
pr_query_json="$(wrap_in_json 'query' "$pr_query")"
repository_edit_branch=$(
call_curl \
-H "Content-Type: application/json" \
--data-binary "$pr_query_json" \
"$GITHUB_GRAPHQL_URL" |
jq -r '(.data.repository.pullRequest.headRepository.nameWithOwner + "/edit/" + .data.repository.pullRequest.headRefName)'
)
get_job_info_and_step_info > /dev/null
update_job_name="${job_name:-update}"
action_ref=$(get_action_repo_info)
if to_boolean "$INPUT_CHECKOUT"; then
workflow_ssh_key_hint='`check-spelling`/`with`/`ssh_key`, then add them:
``` diff
name: '"${update_job_name}"'
...
steps:
...
- name: apply spelling updates
uses: ${action_ref:-check-spelling/check-spelling@...}
with:
checkout: '"$INPUT_CHECKOUT"'
+ ssh_key: "${{ secrets.CHECK_SPELLING }}"
```'
else
workflow_ssh_key_hint='`checkout`/`with`/`ssh-key`, then add them:
``` diff
name: '"${update_job_name}"'
...
steps:
...
- name: checkout
uses: actions/checkout@...
+ with:
+ ssh-key: "${{ secrets.CHECK_SPELLING }}"
```'
fi
if [ -n "$workflow_path" ]; then
qualified_workflow_path="$b$workflow_path$b workflow"
else
qualified_workflow_path="workflow"
fi
if [ "$(jq -r '.repository.owner.type // empty' "$GITHUB_EVENT_PATH" )" = 'User' ]; then
owner="$(jq -r '.repository.owner.login // empty' "$GITHUB_EVENT_PATH" )"
OWNER_TEXT="The owner ${owner:+"($owner)"}"
else
OWNER_TEXT='Users with the Admin role'
fi
OUTPUT="### :hourglass: check-spelling changes applied
As [configured](https://github.com/check-spelling/check-spelling/wiki/Feature:-Update-expect-list#github_token), the commit pushed by @check-spelling-bot to GitHub doesn't trigger GitHub workflows due to a limitation of the @github-actions system.
<details><summary>$OWNER_TEXT can address this for future interactions :magic_wand:</summary>
#### Create a deploy key and secret
$B sh
(
set -e
brand=check-spelling; repo=$q$GITHUB_REPOSITORY$q; SECRET_NAME=CHECK_SPELLING"'
cd "$(mktemp -d)"
ssh-keygen -f "./$brand" -q -N "" -C "$brand key for $repo"
gh repo deploy-key add "./$brand.pub" -R "$repo" -w -t "$brand-talk-to-bot"
gh secret -R "$repo" set "$SECRET_NAME" < "./$brand"
)'"
$B
#### Configure update job in workflow to use secret
If the $qualified_workflow_path ${b}${update_job_name}${b} job doesn't already have the $workflow_ssh_key_hint
</details>
<!--$n$report_header$n-->
To trigger another validation round and hopefully a :white_check_mark:, please add a blank line, e.g. to [$expect_file]($GITHUB_SERVER_URL/$repository_edit_branch/$expect_file?pr=$pr_path_escaped) and commit the change."
echo "$OUTPUT" | tee -a "$GITHUB_STEP_SUMMARY" > "$BODY"
body_to_payload
COMMENTS_URL="$(jq -r '.issue.comments_url' "$GITHUB_EVENT_PATH")"
response="$(mktemp)"
res=0
comment "$COMMENTS_URL" "$PAYLOAD" > "$response" || res=$?
if [ $res -eq 0 ]; then
track_comment "$response"
fi
}
are_head_and_base_in_same_repo() {
jq -r '('"$2"'.head.repo.full_name // "head") == ('"$2"'.base.repo.full_name // "base")' "$1"
}
are_issue_head_and_base_in_same_repo() {
jq -r '.issue // empty' "$GITHUB_EVENT_PATH" > "$issue"
pull_request_url="$(jq -r '.pull_request.url // empty' "$issue")"
pull_request "$pull_request_url" > "$pull_request_info"
are_head_and_base_in_same_repo "$pull_request_info" ''
}
report_if_bot_comment_is_minimized() {
minimized_info=$(mktemp_json)
call_curl \
-H "Content-Type: application/json" \
--data-binary "$(echo '{}' | jq --arg query "query { node(id: $Q$bot_comment_node_id$Q) { ... on IssueComment { isMinimized minimizedReason } } }" '.query = $query')" \
"$GITHUB_GRAPHQL_URL" > "$minimized_info"
if [ "$(jq '.data.node.isMinimized' "$minimized_info")" == 'true' ]; then
minimized_reason=$(jq -r '.data.node.minimizedReason | ascii_downcase // empty' "$minimized_info")
decorated_reason=" (_${minimized_reason}_)"
minimized_reason_suffix='.'
previous_comment_node_id=$(get_previous_comment)
if [ -n "$previous_comment_node_id" ]; then
latest_comment_url=$(get_comment_url_from_id "$previous_comment_node_id")
if [ -n "$latest_comment_url" ] && [ "$comment_url" != "$latest_comment_url" ]; then
minimized_reason_suffix=". Did you mean to apply the most recent report ($latest_comment_url)?"
fi
fi
case "$minimized_reason" in
"")
;;
"resolved")
minimized_reason="$decorated_reason. This probably means the changes have already been applied";;
"outdated")
minimized_reason="$decorated_reason. This probably means the referenced report has been obsoleted by a more recent push & review";;
*)
minimized_reason="$decorated_reason";;
esac
confused_comment "$trigger_comment_url" "The referenced report comment $(comment_url_to_html_url "$comment_url") is hidden$minimized_reason$minimized_reason_suffix"
fi
}
char_count() {
wc -c | xargs
}
line_count() {
wc -l | xargs
}
handle_comment() {
action="$(jq -r '.action // empty' "$GITHUB_EVENT_PATH")"
if [ "$action" != "created" ]; then
quit 0
fi
if ! offer_quote_reply; then
quit 0
fi
comment="$(mktemp_json)"
jq -r '.comment // empty' "$GITHUB_EVENT_PATH" > "$comment"
body="$(mktemp)"
jq -r '.body // empty' "$comment" > "$body"
trigger="$(perl -ne 'print if /\@check-spelling-bot(?:\s+|:\s*)apply.*\Q$ENV{INPUT_REPORT_TITLE_SUFFIX}\E/' < "$body")"
rm "$body"
if [ -z "$trigger" ]; then
quit 0
fi
trigger_comment_url="$(jq -r '.url // empty' "$comment")"
sender_login="$(jq -r '.sender.login // empty' "$GITHUB_EVENT_PATH")"
pull_request_head_info="$(mktemp_json)"
jq .head "$pull_request_info" > "$pull_request_head_info"
pull_request_sha="$(jq -r '.sha // empty' "$pull_request_head_info")"
set_comments_url "$GITHUB_EVENT_NAME" "$GITHUB_EVENT_PATH" "$pull_request_sha"
react_prefix_base="Could not perform [request]($(comment_url_to_html_url "$trigger_comment_url")).$N"
react_prefix="$react_prefix_base"
# Ideally we'd be able to consider `.repository.collaborators_url`` and honor
# `Allow edits from maintainers`:
# https://docs.github.com/pull-requests/collaborating-with-pull-requests/working-with-forks/allowing-changes-to-a-pull-request-branch-created-from-a-fork
# However, at this time, that doesn't work.
#
# Note that the PR author doesn't have to have any ownership stake in the head
# repo branch, so we don't special case that account.
collaborators_url="$(jq -r '.head.repo.collaborators_url // empty' "$pull_request_info")"
collaborators_url="$(echo "$collaborators_url" | perl -pe "s<\{/collaborator\}></$sender_login/permission>")"
collaborator_permission="$(collaborator "$collaborators_url" | jq -r '.permission // empty')"
case "$collaborator_permission" in
admin)
;;
write)
;;
*)
collaborator_suffix="$(jq -r '(if .pull_request.head.repo.full_name then " to " + .pull_request.head.repo.full_name else "" end)' "$pull_request_info")"
confused_comment "$trigger_comment_url" "Commenter (@$sender_login) isn't a collaborator$collaborator_suffix."
;;
esac
created_at="$(jq -r '.created_at // empty' "$comment")"
issue_url="$(jq -r '.url // empty' "$issue")"
pull_request_ref="$(jq -r '.ref // empty' "$pull_request_head_info")"
if git remote get-url origin | grep -q ^https://; then
pull_request_repo="$(jq -r '.repo.clone_url // empty' "$pull_request_head_info")"
else
pull_request_repo="$(jq -r '.repo.ssh_url // empty' "$pull_request_head_info")"
fi
git remote add request "$pull_request_repo"
git fetch request "$pull_request_sha"
git config advice.detachedHead false
git reset --hard
git checkout "$pull_request_sha"
set_up_files
git reset --hard
number_filter() {
perl -pe 's<\{.*\}></(\\d+)>'
}
export pull_request_base="$(jq -r '.comment.html_url' "$GITHUB_EVENT_PATH" | perl -pe 's/\d+$/(\\d+)/')"
comments_base="$(jq -r '.repository.comments_url // empty' "$GITHUB_EVENT_PATH" | number_filter)"
export issue_comments_base="$(jq -r '.repository.issue_comment_url // empty' "$GITHUB_EVENT_PATH" | number_filter)"
export comments_url="$pull_request_base|$comments_base|$issue_comments_base"
summary_url=$(echo "$trigger" | perl -ne '
next unless m{($ENV{GITHUB_SERVER_URL}/$ENV{GITHUB_REPOSITORY}/actions/runs/\d+(?:/attempts/\d+|))};
print $1;
')
comment_url=$(echo "$trigger" | perl -ne '
next unless m{((?:$ENV{comments_url}))};
my $capture=$1;
my $old_base=$ENV{pull_request_base};
my $prefix=$ENV{issue_comments_base};
$old_base=~s{\Q(\d+)\E$}{};
$prefix=~s{\Q(\d+)\E$}{};
$capture =~ s{$old_base}{$prefix};
print "$capture\n";
' | sort -u)
if [ -n "$summary_url" ]; then
[ -n "$comment_url" ] &&
confused_comment "$trigger_comment_url" "Found both summary url (/actions/runs) and _/_$b$comments_url${b}_/_ in comment."
else
[ -n "$comment_url" ] ||
confused_comment "$trigger_comment_url" "Did not find match for _/_$b$comments_url${b}_/_ in comment."
[ "$(echo "$comment_url" | line_count)" -gt 1 ] &&
confused_comment "$trigger_comment_url" "Found more than one _/_$b$comments_url${b}_/_ match in comment:$n$B$n$comment_url$n$B"
fi
if [ -n "$comment_url" ]; then
res=0
comment "$comment_url" > "$comment" ||
confused_comment "$trigger_comment_url" "Failed to retrieve $b$comment_url$b."
bot_comment_author=$(jq -r '.user.login // empty' "$comment")
bot_comment_node_id=$(jq -r '.node_id // empty' "$comment")
bot_comment_url=$(jq -r '.issue_url // .comment.url' "$comment")
github_actions_bot="github-actions[bot]"
[ -n "$bot_comment_author" ] ||
confused_comment "$trigger_comment_url" "Could not retrieve author of $(comment_url_to_html_url "$comment_url")."
[ "$bot_comment_author" = "$github_actions_bot" ] ||
confused_comment "$trigger_comment_url" "Expected @$github_actions_bot to be author of $(comment_url_to_html_url "$comment_url") (found @$bot_comment_author)."
[ "$issue_url" = "$bot_comment_url" ] ||
confused_comment "$trigger_comment_url" "Referenced comment was for a different object: $bot_comment_url"
comment_body=$(mktemp)
jq -r '.body // empty' "$comment" > "$comment_body"
rm "$comment"
grep -q '@check-spelling-bot Report' "$comment_body" ||
confused_comment "$trigger_comment_url" "$(comment_url_to_html_url "$comment_url") does not appear to be a @check-spelling-bot report"
report_if_bot_comment_is_minimized
skip_wrapping=1
instructions_head=$(mktemp)
(
patch_add=1
patch_remove=1
should_exclude_patterns=$(mktemp)
patch_variables "$comment_body" > "$instructions_head"
)
git restore -- "$bucket/$project" 2> /dev/null || true
res=0
. "$instructions_head" || res=$?
if [ $res -gt 0 ]; then
echo "instructions_head failed ($res)"
cat "$instructions_head"
confused_comment "$trigger_comment_url" "Failed to set up environment to apply changes for $(comment_url_to_html_url "$comment_url")."
fi
rm "$comment_body" "$instructions_head"
instructions=$(generate_instructions)
react_prefix="${react_prefix}[Instructions]($(comment_url_to_html_url "$comment_url")) "
. "$instructions" || res=$?
if [ $res -gt 0 ]; then
echo "instructions failed ($res)"
cat "$instructions"
res=0
confused_comment "$trigger_comment_url" "Failed to apply changes."
fi
rm "$instructions"
update_note="per $(comment_url_to_html_url "$comment_url")"
elif [ -n "$summary_url" ]; then
if [ -n "$INPUT_REPORT_TITLE_SUFFIX" ]; then
title_suffix_re='.*'"$("$quote_meta" "$INPUT_REPORT_TITLE_SUFFIX")"
fi
comment_search_re='@check-spelling-bot(?:[\t ]+|:[\t ]*)apply.*'"$("$quote_meta" "$summary_url")$title_suffix_re"
COMMENTS_URL=$(jq -r '.issue.comments_url' "$GITHUB_EVENT_PATH")
bot_comment_node_id_and_status=$(get_a_comment "$comment_search_re")
if [ -n "$bot_comment_node_id_and_status" ]; then
bot_comment_node_id="$(echo "$bot_comment_node_id_and_status" | head -1)"
comment_url=$(get_comment_url_from_id "$bot_comment_node_id")
report_if_bot_comment_is_minimized
fi
summary_url_api=$(
echo "$summary_url" | perl -ne '
next unless m{$ENV{GITHUB_SERVER_URL}/($ENV{GITHUB_REPOSITORY}/actions/runs/\d+(?:/attempts/\d+|))};
print "/repos/$1";
')
[ -n "$summary_url_api" ] || confused_comment "$trigger_comment_url" "Failed to retrieve api url -- this should never happen. Please file a bug."
gh_api_fault=$(mktemp)
gh_api_err=$(mktemp)
if ! GH_TOKEN="$GITHUB_TOKEN" gh api "/repos/$GITHUB_REPOSITORY/actions/cache/usage" > "$gh_api_fault" 2> "$gh_api_err"; then
if [ "$(jq -r .message "$gh_api_fault")" = 'Resource not accessible by integration' ]; then
confused_comment "$trigger_comment_url" "In order for this job to handle $summary_url, it needs:$(echo "
$B diff
permissions:
+ actions: read
$B
" | perl -pe 's/^ {8}//')"
fi
confused_comment "$trigger_comment_url" "$(echo "Unexpected error retrieving action metadata for $GITHUB_REPOSITORY. Please file a bug.
Details:
$B json
$(cat "$gh_api_fault")
$B
$B
$(cat "$gh_api_err")
$B
" | strip_lead)"
fi
head_branch=$(GH_TOKEN="$GITHUB_TOKEN" gh api "$summary_url_api" -q '.head_branch')
[ -n "$head_branch" ] ||
confused_comment "$trigger_comment_url" "Failed to retrieve $b.head_branch$b for $summary_url. Please file a bug."
[ "$head_branch" = "$pull_request_ref" ] ||
confused_comment "$trigger_comment_url" "$summary_url ($head_branch) does not match expected branch ($pull_request_ref)"
apply_output=$(mktemp)
apply_err=$(mktemp)
GH_TOKEN="$GITHUB_TOKEN" \
"$spellchecker/apply.pl" "$summary_url" > "$apply_output" 2> "$apply_err" ||
confused_comment "$trigger_comment_url" "Apply failed.${N}$(cat "$apply_output")${N}${N}$(cat "$apply_err")"
update_note="for $summary_url"
else
confused_comment "$trigger_comment_url" "Unexpected state."
fi
git status --u=no --porcelain | grep -q . ||
confused_comment "$trigger_comment_url" "Request did not change repository content.${N}Maybe someone already applied these changes?"
react_prefix="$react_prefix_base"
get_github_user_and_email "$sender_login"
git_commit "$(echo "Update $update_note
Accepted in $(comment_url_to_html_url "$trigger_comment_url")
"|strip_lead)" ||
confused_comment "$trigger_comment_url" "Did not generate a commit.${N}Perhaps there was a merge conflict or an object changed from being a directory to a file or vice versa? (Please file a bug including a link to this comment.)"
git push request "HEAD:$pull_request_ref" ||
{
{
git show HEAD
} || true
confused_comment "$trigger_comment_url" "Generated a commit, but the $pull_request_repo rejected the commit.${N}Maybe this task lost a race with another push?"
}
react "$trigger_comment_url" 'eyes' > /dev/null
react "${comment_url:-$trigger_comment_url}" 'rocket' > /dev/null
trigger_node=$(jq -r '.comment.node_id // empty' "$GITHUB_EVENT_PATH")
collapse_comment "$trigger_node" "$bot_comment_node_id"
if git remote get-url origin | grep -q ^https://; then
show_github_actions_push_disclaimer
else
echo "### :white_check_mark: check-spelling changes applied" >> "$GITHUB_STEP_SUMMARY"
fi
echo "
#### Metadata updates
$B
$(git diff HEAD~..HEAD --stat)
$B
" | strip_lead >> "$GITHUB_STEP_SUMMARY"
echo "# end"
quit 0
}
encode_artifact() {
echo 'You will want to cat the following content to:'
echo
echo "perl -ne 'next if /--- (?:BEGIN|END) BASE64 ---/; next unless s{^[^|]*\|\s*}{};print' | base64 -d -o artifact.zip"
echo '--- BEGIN BASE64 ---'
base64 "$1"
echo '--- END BASE64 ---'
}
build_artifact_suffix() {
artifact_suffix="-$(echo "$INPUT_REPORT_TITLE_SUFFIX" | perl -pe 's/^\s+|\s+$//; s/[^a-z]+/-/gi;')"
}
define_variables() {
if [ -f "$output_variables" ]; then
return
fi
. "$spellchecker/update-state.sh"
load_env
GITHUB_TOKEN="${GITHUB_TOKEN:-"$INPUT_GITHUB_TOKEN"}"
if [ -n "$GITHUB_TOKEN" ]; then
export AUTHORIZATION_HEADER="Authorization: token $GITHUB_TOKEN"
else
export AUTHORIZATION_HEADER='X-No-Authorization: Sorry About That'
fi
if [ -n "$ACT" ] &&
[ -z "$GITHUB_ACTION_REPOSITORY" ] &&
[ -z "$GITHUB_ACTION_REF" ] &&
[ "$GH_ACTION_REF" = "$GITHUB_REF_NAME" ] &&
[ "$GH_ACTION_REPOSITORY" = "$GITHUB_REPOSITORY" ]; then
# https://github.com/nektos/act/issues/1473 github.action_repository / github.action_ref aren't properly filled in
GH_ACTION_REPOSITORY=$(git -C "$GITHUB_ACTION_PATH" config --get remote.origin.url|perl -pe 's{.*[:/]([^:/]+/[^/]+)$}{$1}')
GH_ACTION_REF=${GITHUB_ACTION_PATH##*@}
fi
export early_warnings="$(mktemp)"
if [ -n "$INPUT_INTERNAL_STATE_DIRECTORY" ]; then
data_dir="$INPUT_INTERNAL_STATE_DIRECTORY"
if [ ! -e "$data_dir" ] && [ -n "$INPUT_CALLER_CONTAINER" ]; then
mkdir -p "$data_dir"
docker cp "$INPUT_CALLER_CONTAINER:$data_dir" "$(dirname "$data_dir")"
fi
artifact=artifact
if [ -n "$INPUT_REPORT_TITLE_SUFFIX" ]; then
build_artifact_suffix
if [ -e "$data_dir/$artifact$artifact_suffix.zip" ]; then
artifact="$artifact$artifact_suffix"