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