File Coverage

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

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