File Coverage

File:lib/CheckSpelling/Apply.pm
Coverage:70.7%

linestmtbrancondsubtimecode
1
1
1
1
252896
1
22
package CheckSpelling::Apply; use CheckSpelling::Util; #!/usr/bin/env perl
2":" || q@<<"=END_OF_PERL"@;
3
4
1
1
1
2
1
31
use Symbol 'gensym';
5
1
1
1
151
1127
26
use IPC::Open3;
6
1
1
1
3
0
21
use File::Basename qw(dirname);
7
1
1
1
1
1
16
use File::Path qw(make_path);
8
1
1
1
127
286
30
use File::Spec::Functions qw(catfile path);
9
1
1
1
2
1
15
use File::Temp qw/ tempfile tempdir /;
10
1
1
1
2
2
21
use JSON::PP;
11
1
1
1
1
1
2286
use warnings;
12
13my @safe_path = qw(
14    /opt/homebrew/bin
15    /opt/homebrew/sbin
16    /usr/local/bin
17    /usr/bin
18    /bin
19    /usr/sbin
20    /sbin
21);
22
23my $bin = glob("~/bin");
24push @safe_path, $bin if -d $bin;
25
26my $ua = 'check-spelling-agent/0.0.4';
27
28$ENV{'PATH'} = join ':', @safe_path unless defined $ENV{SYSTEMROOT};
29
30sub check_exists_command {
31
12
11
    my ($command) = @_;
32
33
12
16
    my @path = path;
34
12
72
    my @pathext = ('');
35
36
12
13
    if ($^O eq 'MSWin32') {
37
1
4
4
4
        push @pathext, map { lc } split /;/, $ENV{PATHEXT};
38    }
39
40
12
9
    for my $dir (@path) {
41
51
26
        for my $suffix (@pathext) {
42
63
64
            my $f = catfile $dir, "$command$suffix";
43
63
193
            return $f if -x $f;
44        }
45    }
46}
47
48sub needs_command_because {
49
11
1696
    my ($command, $reason) = @_;
50
11
9
    return if check_exists_command($command);
51
1
7
    CheckSpelling::Util::die_custom $program, 51, 'Please install `'.$command.'` - it is needed to '.$reason;
52}
53
54sub check_basic_tools {
55
3
1142
    needs_command_because('git', 'interact with git repositories');
56
3
4
    needs_command_because('curl', 'download other tools');
57
3
16
    $ENV{GH_NO_UPDATE_NOTIFIER}=1;
58
3
8
    $ENV{GH_NO_EXTENSION_UPDATE_NOTIFIER}=1;
59
3
4
    needs_command_because('gh', 'interact with github');
60}
61
62sub get_token {
63
7
1167
    our $token;
64
7
59
    return $token if defined $token && $token ne '';
65
4
9
    $token = $ENV{'GH_TOKEN'} || $ENV{'GITHUB_TOKEN'};
66
4
12
    return $token if defined $token && $token ne '';
67
1
1
    my ($err, $exit);
68
1
2
    ($token, $err, $exit) = capture_system('gh', 'auth', 'token');
69
1
4
    chomp $token;
70
1
3
    chomp $err;
71
1
4
    return ($token, $err, $exit);
72};
73
74sub download_with_curl {
75
0
0
    my ($url, $dest, $flags) = @_;
76
0
0
    $flags = '-fsL' unless defined $flags;
77
0
0
    system('curl',
78        '--connect-timeout', 3,
79        '-A', $ua,
80        $flags,
81        '-o', $dest,
82        $url
83    );
84}
85
86sub tempfile_name {
87
5
21
    my ($fh, $filename) = tempfile();
88
5
974
    close $fh;
89
5
8
    return $filename;
90}
91
92sub strip_comments {
93
2
3
    my ($file) = @_;
94
2
3
    my ($fh, $filename) = tempfile();
95
2
217
    open INPUT, '<', $file;
96
2
21
    while (<INPUT>) {
97
408
177
        next if /^\s*(?:#.*)/;
98
404
260
        print $fh $_;
99    }
100
2
4
    close INPUT;
101
2
28
    close $fh;
102
2
6
    return $filename;
103}
104
105sub capture_system {
106
25
85
    my @args = @_;
107
25
113
    my $pid = open3(my $child_in, my $child_out, my $child_err = gensym, @args);
108
25
44585
    my (@err, @out);
109
25
1914020
    while (my $output = <$child_out>) {
110
19
1702
        push @out, $output;
111    }
112
25
469
    while (my $error = <$child_err>) {
113
21
101
        push @err, $error;
114    }
115
25
302
    waitpid( $pid, 0 );
116
25
98
    my $child_exit_status = $?;
117
25
78
    my $output_joined = join '', @out;
118
25
34
    my $error_joined = join '', @err;
119
25
678
    return ($output_joined, $error_joined, $child_exit_status);
120}
121
122sub capture_merged_system {
123
10
23
    my ($output_joined, $error_joined, $child_exit_status) = capture_system(@_);
124
10
48
    my $joiner = ($output_joined ne '') ? "\n" : '';
125
10
44
    return ($output_joined.$joiner.$error_joined, $child_exit_status);
126}
127
128sub compare_files {
129
1
823
    my ($one, $two) = @_;
130
1
3
    my $one_stripped = strip_comments($one);
131
1
2
    my $two_stripped = strip_comments($two);
132
1
1
    my $exit_code;
133
1
2
    (undef, undef, $exit_code) = capture_system(
134            'diff',
135            '-qwB',
136            $one_stripped, $two_stripped
137        );
138
1
12
    if ($? == -1) {
139
0
0
        print "could not compare '$one' and '$two': $!\n";
140
0
0
        return 0;
141    }
142
1
3
    if ($? & 127) {
143
0
0
        printf "child died with signal %d, %s core dump\n",
144        ($? & 127),  ($? & 128) ? 'with' : 'without';
145
0
0
        return 0;
146    }
147
1
1
    return 0 if $? == 0;
148
1
8
    return 1;
149}
150
151my $bash_script=q{
152=END_OF_PERL@
153# bash
154set -e
155if [ "$OUTPUT" = "$ERROR" ]; then
156    ("$@" 2>&1) > "$OUTPUT"
157else
158    "$@" > "$OUTPUT" 2> "$ERROR"
159fi
160exit
161};
162
163my $repo = $ENV{GITHUB_REPOSITORY} || 'check-spelling/check-spelling';
164my $ref = $ENV{GITHUB_REF_NAME} || 'prerelease';
165
166sub check_current_script {
167
4
12263
    return if defined $ENV{'APPLY_SKIP_UPDATE_CHECK'};
168
0
0
    if ("$0" eq '-') {
169
0
0
        my ($bash_script) = @_;
170
0
0
        my $fh;
171
0
0
        ($fh, $0) = tempfile();
172
0
0
        $bash_script =~ s/^=.*\@$//m;
173
0
0
        print $fh $bash_script;
174
0
0
        close $fh;
175
0
0
        return;
176    }
177
0
0
    my $filename = tempfile_name();
178
0
0
    my $source = "https://raw.githubusercontent.com/$repo/$ref/apply.pl";
179
0
0
    download_with_curl($source, $filename);
180
0
0
    if ($? == 0) {
181
0
0
        if (compare_files($filename, $0)) {
182
0
0
            print "Current apply script differs from '$source' (locally downloaded to `$filename`). You may wish to upgrade.\n";
183        }
184    }
185}
186
187sub die_with_message {
188
5
8
    our $program;
189
5
9
    my ($gh_err_text) = @_;
190
5
28
    if ($gh_err_text =~ /error connecting to / && $gh_err_text =~ /check your internet connection/) {
191
0
0
        print "$program: Internet access may be limited. Check your connection (this often happens with lousy cable internet service providers where their CG-NAT or whatever strands the modem).\n\n$gh_err_text";
192
0
0
0
0
0
0
        { CheckSpelling::Util::tear_here(5); die "exiting"; }
193    }
194
5
20
    if ($gh_err_text =~ /proxyconnect tcp:.*connect: connection refused/) {
195
1
49
        print "$program: Proxy is not accepting connections.\n";
196
1
2
        for my $proxy (qw(http_proxy HTTP_PROXY https_proxy HTTPS_PROXY)) {
197
4
9
            if (defined $ENV{$proxy}) {
198
1
6
                print "  $proxy: '$ENV{$proxy}'\n";
199            }
200        }
201
1
3
        print "\n$gh_err_text";
202
1
1
1
1
7
13
        { CheckSpelling::Util::tear_here(6); die "exiting"; }
203    }
204
4
15
    if ($gh_err_text =~ /dial unix .*: connect: .*/) {
205
1
30
        print "$program: Unix http socket is not working.\n";
206
1
29995
        my $gh_http_unix_socket = `gh config get http_unix_socket`;
207
1
26
        print "  http_unix_socket: $gh_http_unix_socket\n";
208
1
8
        print "\n$gh_err_text";
209
1
1
1
4
11
20
        { CheckSpelling::Util::tear_here(7); die "exiting"; }
210    }
211}
212
213sub gh_is_happy_internal {
214
5
14
    my ($output, $exit_code) = capture_merged_system(qw(gh api /installation/repositories));
215
5
16
    return ($exit_code, $output) if $exit_code == 0;
216
5
10
    ($output, $exit_code) = capture_merged_system(qw(gh api /user));
217
5
18
    return ($exit_code, $output);
218}
219
220sub gh_is_happy {
221
3
5
    my ($program) = @_;
222
3
9
    my ($gh_auth_status, $gh_status_lines) = gh_is_happy_internal();
223
3
10
    return 1 if $gh_auth_status == 0;
224
3
25
    die_with_message($gh_status_lines);
225
226
1
2
    my @problematic_env_variables;
227
1
2
    for my $variable (qw(GH_TOKEN GITHUB_TOKEN GITHUB_ACTIONS CI)) {
228
4
11
        if (defined $ENV{$variable}) {
229
2
28
            delete $ENV{$variable};
230
2
4
            push @problematic_env_variables, $variable;
231
2
5
            ($gh_auth_status, $gh_status_lines) = gh_is_happy_internal();
232
2
11
            if ($gh_auth_status == 0) {
233
0
0
                print STDERR "$0: gh program did not like these environment variables: ".join(', ', @problematic_env_variables)." -- consider unsetting them.\n";
234
0
0
                return 1;
235            }
236        }
237    }
238
239
1
30
    print $gh_status_lines;
240
1
9
    return 0;
241}
242
243sub tools_are_ready {
244
3
102038
    my ($program) = @_;
245
3
9
    unless (gh_is_happy($program)) {
246
1
3
        $! = 1;
247
1
6
        my $or_gh_token = (defined $ENV{CI} && $ENV{CI}) ? ' or set the GH_TOKEN environment variable' : '';
248
1
21
        CheckSpelling::Util::die_custom $program, 248, "$program requires a happy gh, please try 'gh auth login'$or_gh_token\n";
249    }
250}
251
252sub run_pipe {
253
8
28
    my @args = @_;
254
8
18
    my ($out, undef, $exit_code) = capture_system(@args);
255
8
43
    return $out;
256}
257
258sub unzip_pipe {
259
6
16
    my ($artifact, $file) = @_;
260
6
15
    return run_pipe(
261        'unzip',
262        '-p', $artifact,
263        $file
264    );
265}
266
267sub retrieve_spell_check_this {
268
1
2
    my ($artifact, $config_ref) = @_;
269
1
1
    my $spell_check_this_config = unzip_pipe($artifact, 'spell_check_this.json');
270
1
13
    return unless $spell_check_this_config =~ /\{.*\}/s;
271
1
2
    my %config;
272
1
1
1
1
2
7
    eval { %config = %{decode_json $spell_check_this_config}; } || CheckSpelling::Util::die_custom $program, 272, "decode_json failed in retrieve_spell_check_this with '$spell_check_this_config'";
273
1
416
    my ($repo, $branch, $destination, $path) = ($config{url}, $config{branch}, $config{config}, $config{path});
274
1
5
    my $spell_check_this_dir = tempdir();
275
1
221
    my $exit_code;
276
1
3
    (undef, undef, $exit_code) = capture_system(
277            'git', 'clone',
278            '--depth', '1',
279            '--no-tags',
280            $repo,
281            '--branch', $branch,
282            $spell_check_this_dir
283        );
284
1
5
    if ($?) {
285
0
0
        CheckSpelling::Util::die_custom $program, 285, "git clone $repo#$branch failed";
286    }
287
288
1
151
    make_path($destination);
289
1
2139
    system('cp', '-i', '-R', glob("$spell_check_this_dir/$path/*"), $destination);
290
1
2310
    system('git', 'add', '-f', $destination);
291}
292
293sub case_biased {
294
1
5
    lc($a)."-".$a cmp lc($b)."-".$b;
295}
296
297sub add_to_excludes {
298
1
5
    my ($artifact, $config_ref) = @_;
299
1
2
    my $excludes = $config_ref->{"excludes_file"};
300
1
3
    my $should_exclude_patterns = unzip_pipe($artifact, 'should_exclude.patterns');
301
1
4
    unless ($should_exclude_patterns =~ /\w/) {
302
1
3
        $should_exclude_patterns = unzip_pipe($artifact, 'should_exclude.txt');
303
1
7
        return unless $should_exclude_patterns =~ /\w/;
304
1
11
        $should_exclude_patterns =~ s{^(.*)}{^\\Q$1\\E\$}gm;
305    }
306
1
2
    my $need_to_add_excludes;
307    my %excludes;
308
1
7
    if (-f $excludes) {
309
1
8
        open EXCLUDES, '<', $excludes;
310
1
9
        while (<EXCLUDES>) {
311
1
1
            chomp;
312
1
2
            next unless /./;
313
1
3
            $excludes{$_."\n"} = 1;
314        }
315
1
2
        close EXCLUDES;
316    } else {
317
0
0
        $need_to_add_excludes = 1;
318    }
319
1
3
    for $pattern (split /\n/, $should_exclude_patterns) {
320
1
4
        next unless $pattern =~ /./;
321
1
3
        $excludes{$pattern."\n"} = 1;
322    }
323
1
23
    open EXCLUDES, '>', $excludes;
324
1
9
    print EXCLUDES join "", sort case_biased keys %excludes;
325
1
37
    close EXCLUDES;
326
1
6
    system('git', 'add', '--', $excludes) if $need_to_add_excludes;
327}
328
329sub remove_stale {
330
1
14
    my ($artifact, $config_ref) = @_;
331
1
4
    my @stale = split /\s+/s, unzip_pipe($artifact, 'remove_words.txt');
332
1
17
    return unless @stale;
333
1
1
1
3
    my @expect_files = @{$config_ref->{"expect_files"}};
334    @expect_files = grep {
335
1
1
3
7
        print STDERR "Could not find $_\n" unless -f $_;
336
1
4
        -f $_;
337    } @expect_files;
338
1
1
    unless (@expect_files) {
339
0
0
        CheckSpelling::Util::die_custom $program, 339, "Could not find any of the processed expect files, are you on the wrong branch?";
340    }
341
342
1
1
    my $re = join "|", @stale;
343
1
3
    for my $file (@expect_files) {
344
1
9
        open INPUT, '<', $file;
345
1
1
        my @keep;
346
1
9
        while (<INPUT>) {
347
2
46
            next if /^(?:$re)(?:(?:\r|\n)*$|[# ].*)/;
348
1
2
            push @keep, $_;
349        }
350
1
2
        close INPUT;
351
352
1
25
        open OUTPUT, '>', $file;
353
1
5
        print OUTPUT join '', @keep;
354
1
40
        close OUTPUT;
355    };
356}
357
358sub add_expect {
359
1
1
    my ($artifact, $config_ref) = @_;
360
1
5
    my @add = split /\s+/s, (unzip_pipe($artifact, 'tokens.txt'));
361
1
5
    return unless @add;
362
1
1
    my $new_expect_file = $config_ref->{"new_expect_file"};
363
1
1
    my @words;
364
1
97
    make_path (dirname($new_expect_file));
365
1
8
    if (-s $new_expect_file) {
366
0
0
        open FILE, q{<}, $new_expect_file;
367
0
0
        local $/ = undef;
368
0
0
        @words = split /\s+/, <FILE>;
369
0
0
        close FILE;
370    }
371
1
1
    my %items;
372
1
2
    @items{@words} = @words x (1);
373
1
4
    @items{@add} = @add x (1);
374
1
4
    @words = sort case_biased keys %items;
375
1
24
    open FILE, q{>}, $new_expect_file;
376
1
2
    for my $word (@words) {
377
1
6
        print FILE "$word\n" if $word =~ /\S/;
378    };
379
1
11
    close FILE;
380
1
2279
    system("git", "add", $new_expect_file);
381}
382
383sub get_artifact_metadata {
384
3
3
    my ($url) = @_;
385
3
6
    my $json_file = tempfile_name();
386
3
6
    my ($curl_stdout, $curl_stderr, $curl_result);
387
3
12
    my @curl_args = (
388        'curl',
389        $url,
390        '-A',
391        $ua,
392        '-s',
393        '--fail-with-body',
394    );
395
3
5
    my ($gh_token) = get_token();
396
3
8
    push @curl_args, '-u', "token:$gh_token" if defined $gh_token;
397
3
3
    push @curl_args, (
398        '-o',
399        $json_file
400    );
401
3
5
    ($curl_stdout, $curl_stderr, $curl_result) = capture_system(
402        @curl_args
403    );
404
3
13
    unless ($curl_result == 0) {
405
1
5
        if ($curl_stdout eq '') {
406
1
6
            local $/;
407
1
29
            open my $error_fh, '<', $json_file;
408
1
10
            $curl_stdout = <$error_fh>;
409
1
3
            close $error_fh;
410        }
411        return (
412
1
12
            out    => $curl_stdout,
413            err    => $curl_stderr,
414            result => $curl_result,
415        );
416    }
417
2
3
    my $link;
418
2
76
    open my $json_file_fh, '<', $json_file;
419
2
3
    my ($id, $download_url, $count);
420    {
421
2
2
2
11
        local $/;
422
2
25
        my $content = <$json_file_fh>;
423
2
15
        my $json = decode_json $content;
424
2
5007
        my $artifact = $json->{'artifacts'}->[0];
425
2
2
        $id = $artifact->{'id'};
426
2
2
        $download_url = $artifact->{'archive_download_url'};
427
2
10
        $count = $json->{'total_count'};
428    }
429
2
10
    close $json_file_fh;
430
2
3
    if ($count == 0) {
431        return (
432
0
0
            out => '',
433            err => 'no artifact matches any of the names or patterns provided',
434            result => (3 << 8),
435        );
436    }
437    return (
438
2
18
        id       => $id,
439        download => $download_url,
440        count    => $count,
441    );
442}
443
444sub get_latest_artifact_metadata {
445
2
4
    my ($artifact_dir, $repo, $run, $artifact_name) = @_;
446
2
2
    my $page = 1;
447
2
4
    my $url = "$ENV{GITHUB_API_URL}/repos/$repo/actions/runs/$run/artifacts?name=$artifact_name&per_page=1&page=";
448
2
4
    my %first = get_artifact_metadata($url.$page);
449
2
4
    $page = $first{'count'};
450
2
3
    if (defined $page) {
451
1
4
        my %second = get_artifact_metadata($url.$page);
452
1
4
        my ($id_1, $id_2) = ($first{'id'}, $second{'id'});
453
1
10
        if (defined $id_1 && defined $id_2) {
454
1
2
            if ($id_2 > $id_1) {
455                return (
456
0
0
                    download => $second{'download'},
457                );
458            }
459        }
460    }
461
2
3
    my $download = $first{'download'};
462
2
7
    if (defined $download) {
463        return (
464
1
5
            download => $download,
465        );
466    }
467
1
7
    return %first;
468}
469
470sub download_latest_artifact {
471
2
4
    my %maybe_download = get_latest_artifact_metadata(@_);
472
2
3
    my $download = $maybe_download{'download'};
473
2
3
    my $zip_file = tempfile_name();
474
2
6
    if (defined $download) {
475
1
4
        my @curl_args = (
476            'curl',
477            $download,
478            '-L',
479            '-A',
480            $ua,
481            '-s',
482            '--fail-with-body',
483        );
484
1
2
        my ($gh_token) = get_token();
485
1
3
        push @curl_args, '-u', "token:$gh_token" if defined $gh_token;
486
1
1
        push @curl_args, (
487            '-o',
488            $zip_file
489        );
490
1
1
        ($curl_stdout, $curl_stderr, $curl_result) = capture_system(
491            @curl_args
492        );
493
1
6
        if ($curl_result != 0) {
494
1
2
            if ($curl_stdout eq '') {
495
1
6
                local $/;
496
1
28
                open my $error_fh, '<', $zip_file;
497
1
11
                $curl_stdout = <$error_fh>;
498
1
6
                close $error_fh;
499            }
500
1
12
            return ("$curl_stdout\n$curl_stderr", $curl_result);
501        }
502
0
0
        my ($artifact_dir, $repo, $run, $artifact_name) = @_;
503
0
0
        ($out, $err, $result) = capture_system(
504            'unzip',
505            '-q',
506            $zip_file,
507            '-d',
508            $artifact_dir,
509            );
510
0
0
        return ("$out\n$err", $result);
511    }
512
1
3
    my ($out, $err, $result) = ($maybe_download{'out'}, $maybe_download{'err'}, $maybe_download{'result'});
513
1
4
    return ("$out\n$err", $result);
514}
515
516sub get_artifacts {
517
2
3172
    my ($repo, $run, $suffix) = @_;
518
2
0
    our $program;
519
2
5
    my $artifact_dir = tempdir(CLEANUP => 1);
520
2
305
    my $gh_err_text;
521
2
4
    my $artifact_name = 'check-spelling-comment';
522
2
5
    if ($suffix) {
523
0
0
        $artifact_name .= "-$suffix";
524    }
525
2
2
    my $retries_remaining = 3;
526
2
8
    while ($retries_remaining-- > 0) {
527
2
6
        ($gh_err_text, $ret) = download_latest_artifact(
528            $artifact_dir,
529            $repo,
530            $run,
531            $artifact_name
532        );
533
2
4
        return glob("$artifact_dir/artifact*.zip") unless ($ret >> 8);
534
535
2
7
        die_with_message($gh_err_text);
536
2
19
        if ($gh_err_text =~ /no valid artifacts found to download|"Artifact has expired"/) {
537
1
21
            my $expired_json = run_pipe(
538                'gh', 'api',
539                "/repos/$repo/actions/runs/$run/artifacts",
540                '-q',
541                '.artifacts.[]|select(.name=="'.$artifact_name.'")|.expired'
542            );
543
1
8
            if ($expired_json ne '') {
544
1
3
                chomp $expired_json;
545
1
1
                my $expired;
546
1
1
4
8
                eval { $expired = decode_json $expired_json } || CheckSpelling::Util::die_custom $program, 546, "decode_json failed in update_repository with '$expired_json'";
547
1
124
                if ($expired) {
548
1
43
                    print "$program: GitHub Run Artifact expired. You will need to trigger a new run.\n";
549
1
1
1
1
5
16
                    { CheckSpelling::Util::tear_here(1); die "exiting"; }
550                }
551            }
552
0
0
            print "$program: GitHub Run may not have completed. If so, please wait for it to finish and try again.\n";
553
0
0
0
0
0
0
            { CheckSpelling::Util::tear_here(2); die "exiting"; }
554        }
555
1
2
        if ($gh_err_text =~ /no artifact matches any of the names or patterns provided/) {
556
0
0
            $github_server_url = $ENV{GITHUB_SERVER_URL} || '';
557
0
0
            my $run_link;
558
0
0
            if ($github_server_url) {
559
0
0
                $run_link = "[$run]($github_server_url/$repo/actions/runs/$run)";
560            } else {
561
0
0
                $run_link = "$run";
562            }
563
0
0
            print "$program: The referenced repository ($repo) run ($run_link) does not have a corresponding artifact ($artifact_name). If it was deleted, that's unfortunate. Consider pushing a change to the branch to trigger a new run?\n";
564
0
0
            print "If you don't think anyone deleted the artifact, please file a bug to https://github.com/check-spelling/check-spelling/issues/new including as much information about how you triggered this error as possible.\n";
565
0
0
0
0
0
0
            { CheckSpelling::Util::tear_here(3); die "exiting"; }
566        }
567
1
6
        if ($gh_err_text =~ /HTTP 404: Not Found|"status":"404"/) {
568
1
16
            print "$program: The referenced repository ($repo) may not exist, perhaps you do not have permission to see it. If the repository is hosted by GitHub Enterprise, check-spelling does not know how to integrate with it.\n";
569
1
1
1
1
5
10
            { CheckSpelling::Util::tear_here(8); die "exiting"; }
570        }
571
0
0
        if ($gh_err_text =~ /HTTP 403: API rate limit exceeded for .*?./) {
572        } elsif ($gh_err_text =~ m{dial tcp \S+:\d+: i/o timeout$}) {
573
0
0
            if ($retries_remaining <= 0) {
574
0
0
                print "$program: Timeout connecting to GitHub. This is probably caused by an outage of sorts.\nCheck https://www.githubstatus.com/history\nTry again later.";
575
0
0
0
0
0
0
                { CheckSpelling::Util::tear_here(9); die "exiting"; }
576            }
577        } else {
578
0
0
            print "$program: Unknown error, please check the list of known issues https://github.com/check-spelling/check-spelling/issues?q=is%3Aissue%20apply.pl and file a bug to https://github.com/check-spelling/check-spelling/issues/new?title=%60apply.pl%60%20scenario&body=Please%20provide%20details+preferably%20including%20a%20link%20to%20a%20workflow%20run,%20the%20configuration%20of%20the%20repository,%20and%20anything%20else%20you%20may%20know%20about%20the%20problem%2e\n";
579
0
0
            print $gh_err_text;
580
0
0
0
0
0
0
            { CheckSpelling::Util::tear_here(4); die "exiting"; }
581        }
582
0
0
        my $request_id = $1 if ($gh_err_text =~ /\brequest ID\s+(\S+)/);
583
0
0
        my $timestamp = $1 if ($gh_err_text =~ /\btimestamp\s+(.*? UTC)/);
584
0
0
        my $has_gh_token = defined $ENV{GH_TOKEN} || defined $ENV{GITHUB_TOKEN};
585
0
0
        my $meta_url = 'https://api.github.com/meta';
586
0
0
        while (1) {
587
0
0
            my @curl_args = qw(curl);
588
0
0
            unless ($has_gh_token) {
589
0
0
                my ($gh_token) = get_token();
590
0
0
                push @curl_args, '-u', "token:$gh_token" if defined $gh_token;
591            }
592
0
0
            push @curl_args, '-I', $meta_url;
593
0
0
            my ($curl_stdout, $curl_stderr, $curl_result);
594
0
0
            ($curl_stdout, $curl_stderr, $curl_result) = capture_system(@curl_args);
595
0
0
            my $delay = 1;
596
0
0
            if ($curl_stdout =~ m{^HTTP/\S+\s+200}) {
597
0
0
                if ($curl_stdout =~ m{^x-ratelimit-remaining:\s+(\d+)$}m) {
598
0
0
                    my $ratelimit_remaining = $1;
599
0
0
                    last if ($ratelimit_remaining > 10);
600
601
0
0
                    $delay = 5;
602
0
0
                    print STDERR "Sleeping for $delay seconds because $ratelimit_remaining is close to 0\n";
603                } else {
604
0
0
                    print STDERR "Couldn't find x-ratelimit-remaining, will sleep for $delay\n";
605                }
606            } elsif ($curl_stdout =~ m{^HTTP/\S+\s+403}) {
607
0
0
                if ($curl_stdout =~ /^retry-after:\s+(\d+)/m) {
608
0
0
                    $delay = $1;
609
0
0
                    print STDERR "Sleeping for $delay seconds (presumably due to API rate limit)\n";
610                } else {
611
0
0
                    print STDERR "Couldn't find retry-after, will sleep for $delay\n";
612                }
613            } else {
614
0
0
                my $response = $1 if $curl_stdout =~ m{^(HTTP/\S+)};
615
0
0
                print STDERR "Unexpected response ($response) from $meta_url; sleeping for $delay\n";
616            }
617
0
0
            sleep $delay;
618        }
619    }
620}
621
622sub update_repository {
623
1
2
    my ($artifact) = @_;
624
1
2
    our $program;
625
1
6
    CheckSpelling::Util::die_custom $program, 625, "$program: artifact argument contains quote characters" if $artifact =~ /'/;
626
1
4
    my $apply = unzip_pipe($artifact, 'apply.json');
627
1
7
    unless ($apply =~ /\{.*\}/s) {
628
0
0
        print STDERR "$program: Could not retrieve valid apply.json from artifact\n";
629
0
0
        $apply = '{
630            "expect_files": [".github/actions/spelling/expect.txt"],
631            "new_expect_file": ".github/actions/spelling/expect.txt",
632            "excludes_file": ".github/actions/spelling/excludes.txt",
633            "spelling_config": ".github/actions/spelling"
634        }';
635    }
636
1
1
    my $config_ref;
637
1
1
1
11
    eval { $config_ref = decode_json($apply); } ||
638        CheckSpelling::Util::die_custom $program, 638, "$program: decode_json failed in update_repository with '$apply'";
639
640
1
466
    my $git_repo_root = run_pipe('git', 'rev-parse', '--show-toplevel');
641
1
5
    chomp $git_repo_root;
642
1
7
    CheckSpelling::Util::die_custom $program, 642, "$program: Could not find git repo root..." unless $git_repo_root =~ /\w/;
643
1
5
    chdir $git_repo_root;
644
645
1
4
    retrieve_spell_check_this($artifact, $config_ref);
646
1
13
    remove_stale($artifact, $config_ref);
647
1
4
    add_expect($artifact, $config_ref);
648
1
14
    add_to_excludes($artifact, $config_ref);
649
1
2164
    system('git', 'add', '-u', '--', $config_ref->{'spelling_config'});
650}
651
652sub extract_artifacts_from_file {
653
1
1
    my ($artifact) = @_;
654
1
1294
    open my $artifact_reader, '-|', 'unzip', '-l', $artifact;
655
1
8
    my ($has_artifact, $only_file) = (0, 0);
656
1
790
    while (my $line = <$artifact_reader>) {
657
6
5
        chomp $line;
658
6
18
        if ($line =~ /\s+artifact\.zip$/) {
659
1
1
            $has_artifact = 1;
660
1
2
            next;
661        }
662
5
11
        if ($line =~ /\s+1 file$/) {
663
1
1
            $only_file = 1;
664
1
114
            next;
665        }
666
4
8
        $only_file = 0 if $only_file;
667    }
668
1
13
    close $artifact_reader;
669
1
1
    my @artifacts;
670
1
9
    if ($has_artifact && $only_file) {
671
1
8
        my $artifact_dir = tempdir(CLEANUP => 1);
672
1
277
        my ($fh, $gh_err) = tempfile();
673
1
166
        close $fh;
674
1
2191
        system('unzip', '-q', '-d', $artifact_dir, $artifact, 'artifact.zip');
675
1
26
        @artifacts = ("$artifact_dir/artifact.zip");
676    } else {
677
0
0
        @artifacts = ($artifact);
678    }
679
1
11
    return @artifacts;
680}
681
682sub main {
683
2
171662
    our $program;
684
2
3
    my ($bash_script, $first, $run);
685
2
4
    ($program, $bash_script, $first, $run) = @_;
686
2
3
    my $syntax = "$program <RUN_URL | OWNER/REPO RUN | ARTIFACT.zip>";
687    # Stages
688    # - 1 check for tools basic
689
2
6
    check_basic_tools();
690    # - 2 check for current
691    # -> 1. download the latest version to a temp file
692    # -> 2. parse current and latest (stripping comments) and compare (whitespace insensitively)
693    # -> 3. offer to update if the latest version is different
694
2
3
    check_current_script($bash_script);
695    # - 4 parse arguments
696
2
1
    CheckSpelling::Util::die_custom $program, 696, $syntax unless defined $first;
697
2
3
    $ENV{'GITHUB_API_URL'} ||= 'https://api.github.com';
698
2
2
    my $repo;
699    my @artifacts;
700
2
8
    if (-s $first) {
701
1
3
        @artifacts = extract_artifacts_from_file($first);
702    } else {
703
1
9
        my $suffix;
704
1
1
        if ($first =~ m{^\s*https://.*/([^/]+/[^/]+)/actions/runs/(\d+)(?:/attempts/\d+|)(?:#(\S+)|)\s*$}) {
705
0
0
            ($repo, $run, $suffix) = ($1, $2, $3);
706        } else {
707
1
1
            $repo = $first;
708        }
709
1
10
        CheckSpelling::Util::die_custom $program, 709, $syntax unless defined $repo && defined $run;
710        # - 3 check for tool readiness (is `gh` working)
711
0
0
        tools_are_ready($program);
712
0
0
        @artifacts = get_artifacts($repo, $run, $suffix);
713    }
714
715    # - 5 do work
716
1
4
    for my $artifact (@artifacts) {
717
1
3
        update_repository($artifact);
718    }
719}
720
721# main($0 ne '-' ? $0 : 'apply.pl', $bash_script, @ARGV);