File Coverage

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

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