| File: | lib/CheckSpelling/Apply.pm |
| Coverage: | 70.7% |
| line | stmt | bran | cond | sub | time | code |
|---|---|---|---|---|---|---|
| 1 | 1 1 1 | 237605 1 19 | package CheckSpelling::Apply; use CheckSpelling::Util; #!/usr/bin/env perl | |||
| 2 | ":" || q@<<"=END_OF_PERL"@; | |||||
| 3 | ||||||
| 4 | 1 1 1 | 2 0 21 | use Symbol 'gensym'; | |||
| 5 | 1 1 1 | 156 1171 23 | use IPC::Open3; | |||
| 6 | 1 1 1 | 2 1 18 | use File::Basename qw(dirname); | |||
| 7 | 1 1 1 | 1 1 13 | use File::Path qw(make_path); | |||
| 8 | 1 1 1 | 152 295 32 | use File::Spec::Functions qw(catfile path); | |||
| 9 | 1 1 1 | 2 2 14 | use File::Temp qw/ tempfile tempdir /; | |||
| 10 | 1 1 1 | 1 0 19 | use JSON::PP; | |||
| 11 | 1 1 1 | 1 1 2470 | 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 | 12 | 9 | my ($command) = @_; | |||
| 32 | ||||||
| 33 | 12 | 13 | my @path = path; | |||
| 34 | 12 | 71 | my @pathext = (''); | |||
| 35 | ||||||
| 36 | 12 | 13 | if ($^O eq 'MSWin32') { | |||
| 37 | 1 4 | 5 4 | push @pathext, map { lc } split /;/, $ENV{PATHEXT}; | |||
| 38 | } | |||||
| 39 | ||||||
| 40 | 12 | 7 | for my $dir (@path) { | |||
| 41 | 51 | 16 | for my $suffix (@pathext) { | |||
| 42 | 63 | 70 | my $f = catfile $dir, "$command$suffix"; | |||
| 43 | 63 | 230 | return $f if -x $f; | |||
| 44 | } | |||||
| 45 | } | |||||
| 46 | } | |||||
| 47 | ||||||
| 48 | sub needs_command_because { | |||||
| 49 | 11 | 1817 | my ($command, $reason) = @_; | |||
| 50 | 11 | 11 | 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 | ||||||
| 54 | sub check_basic_tools { | |||||
| 55 | 3 | 1341 | 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 | 9 | $ENV{GH_NO_EXTENSION_UPDATE_NOTIFIER}=1; | |||
| 59 | 3 | 3 | needs_command_because('gh', 'interact with github'); | |||
| 60 | } | |||||
| 61 | ||||||
| 62 | sub get_token { | |||||
| 63 | 7 | 1243 | our $token; | |||
| 64 | 7 | 55 | return $token if defined $token && $token ne ''; | |||
| 65 | 4 | 7 | $token = $ENV{'GH_TOKEN'} || $ENV{'GITHUB_TOKEN'}; | |||
| 66 | 4 | 11 | return $token if defined $token && $token ne ''; | |||
| 67 | 1 | 0 | my ($err, $exit); | |||
| 68 | 1 | 5 | ($token, $err, $exit) = capture_system('gh', 'auth', 'token'); | |||
| 69 | 1 | 4 | chomp $token; | |||
| 70 | 1 | 3 | chomp $err; | |||
| 71 | 1 | 3 | return ($token, $err, $exit); | |||
| 72 | }; | |||||
| 73 | ||||||
| 74 | sub 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 | ||||||
| 86 | sub tempfile_name { | |||||
| 87 | 5 | 15 | my ($fh, $filename) = tempfile(); | |||
| 88 | 5 | 847 | close $fh; | |||
| 89 | 5 | 8 | return $filename; | |||
| 90 | } | |||||
| 91 | ||||||
| 92 | sub strip_comments { | |||||
| 93 | 2 | 3 | my ($file) = @_; | |||
| 94 | 2 | 4 | my ($fh, $filename) = tempfile(); | |||
| 95 | 2 | 266 | open INPUT, '<', $file; | |||
| 96 | 2 | 22 | while (<INPUT>) { | |||
| 97 | 408 | 193 | next if /^\s*(?:#.*)/; | |||
| 98 | 404 | 246 | print $fh $_; | |||
| 99 | } | |||||
| 100 | 2 | 5 | close INPUT; | |||
| 101 | 2 | 26 | close $fh; | |||
| 102 | 2 | 4 | return $filename; | |||
| 103 | } | |||||
| 104 | ||||||
| 105 | sub capture_system { | |||||
| 106 | 25 | 59 | my @args = @_; | |||
| 107 | 25 | 88 | my $pid = open3(my $child_in, my $child_out, my $child_err = gensym, @args); | |||
| 108 | 25 | 42636 | my (@err, @out); | |||
| 109 | 25 | 1885769 | while (my $output = <$child_out>) { | |||
| 110 | 19 | 2820 | push @out, $output; | |||
| 111 | } | |||||
| 112 | 25 | 350 | while (my $error = <$child_err>) { | |||
| 113 | 21 | 64 | push @err, $error; | |||
| 114 | } | |||||
| 115 | 25 | 246 | waitpid( $pid, 0 ); | |||
| 116 | 25 | 92 | my $child_exit_status = $?; | |||
| 117 | 25 | 63 | my $output_joined = join '', @out; | |||
| 118 | 25 | 40 | my $error_joined = join '', @err; | |||
| 119 | 25 | 611 | return ($output_joined, $error_joined, $child_exit_status); | |||
| 120 | } | |||||
| 121 | ||||||
| 122 | sub capture_merged_system { | |||||
| 123 | 10 | 15 | my ($output_joined, $error_joined, $child_exit_status) = capture_system(@_); | |||
| 124 | 10 | 42 | my $joiner = ($output_joined ne '') ? "\n" : ''; | |||
| 125 | 10 | 35 | return ($output_joined.$joiner.$error_joined, $child_exit_status); | |||
| 126 | } | |||||
| 127 | ||||||
| 128 | sub compare_files { | |||||
| 129 | 1 | 1022 | my ($one, $two) = @_; | |||
| 130 | 1 | 3 | my $one_stripped = strip_comments($one); | |||
| 131 | 1 | 1 | my $two_stripped = strip_comments($two); | |||
| 132 | 1 | 1 | my $exit_code; | |||
| 133 | 1 | 1 | (undef, undef, $exit_code) = capture_system( | |||
| 134 | 'diff', | |||||
| 135 | '-qwB', | |||||
| 136 | $one_stripped, $two_stripped | |||||
| 137 | ); | |||||
| 138 | 1 | 5 | 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 | 6 | return 0 if $? == 0; | |||
| 148 | 1 | 7 | return 1; | |||
| 149 | } | |||||
| 150 | ||||||
| 151 | my $bash_script=q{ | |||||
| 152 | =END_OF_PERL@ | |||||
| 153 | # bash | |||||
| 154 | set -e | |||||
| 155 | if [ "$OUTPUT" = "$ERROR" ]; then | |||||
| 156 | ("$@" 2>&1) > "$OUTPUT" | |||||
| 157 | else | |||||
| 158 | "$@" > "$OUTPUT" 2> "$ERROR" | |||||
| 159 | fi | |||||
| 160 | exit | |||||
| 161 | }; | |||||
| 162 | ||||||
| 163 | my $repo = $ENV{GITHUB_REPOSITORY} || 'check-spelling/check-spelling'; | |||||
| 164 | my $ref = $ENV{GITHUB_REF_NAME} || 'prerelease'; | |||||
| 165 | ||||||
| 166 | sub check_current_script { | |||||
| 167 | 4 | 13207 | 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 | ||||||
| 187 | sub die_with_message { | |||||
| 188 | 5 | 6 | our $program; | |||
| 189 | 5 | 8 | my ($gh_err_text) = @_; | |||
| 190 | 5 | 24 | 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 | 18 | if ($gh_err_text =~ /proxyconnect tcp:.*connect: connection refused/) { | |||
| 195 | 1 | 25 | 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 | 10 | if (defined $ENV{$proxy}) { | |||
| 198 | 1 | 4 | print " $proxy: '$ENV{$proxy}'\n"; | |||
| 199 | } | |||||
| 200 | } | |||||
| 201 | 1 | 3 | print "\n$gh_err_text"; | |||
| 202 | 1 1 1 | 1 4 13 | { CheckSpelling::Util::tear_here(6); die "exiting"; } | |||
| 203 | } | |||||
| 204 | 4 | 21 | if ($gh_err_text =~ /dial unix .*: connect: .*/) { | |||
| 205 | 1 | 26 | print "$program: Unix http socket is not working.\n"; | |||
| 206 | 1 | 26444 | my $gh_http_unix_socket = `gh config get http_unix_socket`; | |||
| 207 | 1 | 20 | print " http_unix_socket: $gh_http_unix_socket\n"; | |||
| 208 | 1 | 4 | print "\n$gh_err_text"; | |||
| 209 | 1 1 1 | 4 27 18 | { CheckSpelling::Util::tear_here(7); die "exiting"; } | |||
| 210 | } | |||||
| 211 | } | |||||
| 212 | ||||||
| 213 | sub gh_is_happy_internal { | |||||
| 214 | 5 | 14 | my ($output, $exit_code) = capture_merged_system(qw(gh api /installation/repositories)); | |||
| 215 | 5 | 9 | return ($exit_code, $output) if $exit_code == 0; | |||
| 216 | 5 | 14 | ($output, $exit_code) = capture_merged_system(qw(gh api /user)); | |||
| 217 | 5 | 19 | return ($exit_code, $output); | |||
| 218 | } | |||||
| 219 | ||||||
| 220 | sub gh_is_happy { | |||||
| 221 | 3 | 3 | my ($program) = @_; | |||
| 222 | 3 | 7 | my ($gh_auth_status, $gh_status_lines) = gh_is_happy_internal(); | |||
| 223 | 3 | 8 | return 1 if $gh_auth_status == 0; | |||
| 224 | 3 | 10 | die_with_message($gh_status_lines); | |||
| 225 | ||||||
| 226 | 1 | 3 | my @problematic_env_variables; | |||
| 227 | 1 | 3 | for my $variable (qw(GH_TOKEN GITHUB_TOKEN GITHUB_ACTIONS CI)) { | |||
| 228 | 4 | 9 | if (defined $ENV{$variable}) { | |||
| 229 | 2 | 21 | delete $ENV{$variable}; | |||
| 230 | 2 | 4 | push @problematic_env_variables, $variable; | |||
| 231 | 2 | 4 | ($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 | 35 | print $gh_status_lines; | |||
| 240 | 1 | 9 | return 0; | |||
| 241 | } | |||||
| 242 | ||||||
| 243 | sub tools_are_ready { | |||||
| 244 | 3 | 89895 | my ($program) = @_; | |||
| 245 | 3 | 8 | unless (gh_is_happy($program)) { | |||
| 246 | 1 | 5 | $! = 1; | |||
| 247 | 1 | 5 | my $or_gh_token = (defined $ENV{CI} && $ENV{CI}) ? ' or set the GH_TOKEN environment variable' : ''; | |||
| 248 | 1 | 7 | CheckSpelling::Util::die_custom $program, 248, "$program requires a happy gh, please try 'gh auth login'$or_gh_token\n"; | |||
| 249 | } | |||||
| 250 | } | |||||
| 251 | ||||||
| 252 | sub run_pipe { | |||||
| 253 | 8 | 24 | my @args = @_; | |||
| 254 | 8 | 36 | my ($out, undef, $exit_code) = capture_system(@args); | |||
| 255 | 8 | 51 | return $out; | |||
| 256 | } | |||||
| 257 | ||||||
| 258 | sub unzip_pipe { | |||||
| 259 | 6 | 14 | my ($artifact, $file) = @_; | |||
| 260 | 6 | 9 | return run_pipe( | |||
| 261 | 'unzip', | |||||
| 262 | '-p', $artifact, | |||||
| 263 | $file | |||||
| 264 | ); | |||||
| 265 | } | |||||
| 266 | ||||||
| 267 | sub retrieve_spell_check_this { | |||||
| 268 | 1 | 2 | my ($artifact, $config_ref) = @_; | |||
| 269 | 1 | 2 | my $spell_check_this_config = unzip_pipe($artifact, 'spell_check_this.json'); | |||
| 270 | 1 | 8 | return unless $spell_check_this_config =~ /\{.*\}/s; | |||
| 271 | 1 | 1 | my %config; | |||
| 272 | 1 1 1 | 1 2 6 | 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 | 354 | my ($repo, $branch, $destination, $path) = ($config{url}, $config{branch}, $config{config}, $config{path}); | |||
| 274 | 1 | 4 | my $spell_check_this_dir = tempdir(); | |||
| 275 | 1 | 169 | 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 | 4 | if ($?) { | |||
| 285 | 0 | 0 | CheckSpelling::Util::die_custom $program, 285, "git clone $repo#$branch failed"; | |||
| 286 | } | |||||
| 287 | ||||||
| 288 | 1 | 150 | make_path($destination); | |||
| 289 | 1 | 2197 | system('cp', '-i', '-R', glob("$spell_check_this_dir/$path/*"), $destination); | |||
| 290 | 1 | 2606 | system('git', 'add', '-f', $destination); | |||
| 291 | } | |||||
| 292 | ||||||
| 293 | sub case_biased { | |||||
| 294 | 1 | 5 | lc($a)."-".$a cmp lc($b)."-".$b; | |||
| 295 | } | |||||
| 296 | ||||||
| 297 | sub add_to_excludes { | |||||
| 298 | 1 | 2 | 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 | 3 | unless ($should_exclude_patterns =~ /\w/) { | |||
| 302 | 1 | 2 | $should_exclude_patterns = unzip_pipe($artifact, 'should_exclude.txt'); | |||
| 303 | 1 | 7 | return unless $should_exclude_patterns =~ /\w/; | |||
| 304 | 1 | 8 | $should_exclude_patterns =~ s{^(.*)}{^\\Q$1\\E\$}gm; | |||
| 305 | } | |||||
| 306 | 1 | 2 | my $need_to_add_excludes; | |||
| 307 | my %excludes; | |||||
| 308 | 1 | 6 | if (-f $excludes) { | |||
| 309 | 1 | 9 | open EXCLUDES, '<', $excludes; | |||
| 310 | 1 | 8 | while (<EXCLUDES>) { | |||
| 311 | 1 | 1 | chomp; | |||
| 312 | 1 | 2 | next unless /./; | |||
| 313 | 1 | 4 | $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 | 2 | next unless $pattern =~ /./; | |||
| 321 | 1 | 3 | $excludes{$pattern."\n"} = 1; | |||
| 322 | } | |||||
| 323 | 1 | 26 | open EXCLUDES, '>', $excludes; | |||
| 324 | 1 | 7 | print EXCLUDES join "", sort case_biased keys %excludes; | |||
| 325 | 1 | 29 | close EXCLUDES; | |||
| 326 | 1 | 5 | system('git', 'add', '--', $excludes) if $need_to_add_excludes; | |||
| 327 | } | |||||
| 328 | ||||||
| 329 | sub remove_stale { | |||||
| 330 | 1 | 3 | my ($artifact, $config_ref) = @_; | |||
| 331 | 1 | 6 | my @stale = split /\s+/s, unzip_pipe($artifact, 'remove_words.txt'); | |||
| 332 | 1 | 14 | return unless @stale; | |||
| 333 | 1 1 | 2 3 | my @expect_files = @{$config_ref->{"expect_files"}}; | |||
| 334 | @expect_files = grep { | |||||
| 335 | 1 1 | 18 7 | print STDERR "Could not find $_\n" unless -f $_; | |||
| 336 | 1 | 3 | -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 | 0 | my $re = join "|", @stale; | |||
| 343 | 1 | 1 | for my $file (@expect_files) { | |||
| 344 | 1 | 8 | open INPUT, '<', $file; | |||
| 345 | 1 | 1 | my @keep; | |||
| 346 | 1 | 9 | while (<INPUT>) { | |||
| 347 | 2 | 34 | next if /^(?:$re)(?:(?:\r|\n)*$|[# ].*)/; | |||
| 348 | 1 | 1 | push @keep, $_; | |||
| 349 | } | |||||
| 350 | 1 | 2 | close INPUT; | |||
| 351 | ||||||
| 352 | 1 | 27 | open OUTPUT, '>', $file; | |||
| 353 | 1 | 4 | print OUTPUT join '', @keep; | |||
| 354 | 1 | 30 | close OUTPUT; | |||
| 355 | }; | |||||
| 356 | } | |||||
| 357 | ||||||
| 358 | sub add_expect { | |||||
| 359 | 1 | 1 | my ($artifact, $config_ref) = @_; | |||
| 360 | 1 | 1 | my @add = split /\s+/s, (unzip_pipe($artifact, 'tokens.txt')); | |||
| 361 | 1 | 4 | return unless @add; | |||
| 362 | 1 | 1 | my $new_expect_file = $config_ref->{"new_expect_file"}; | |||
| 363 | 1 | 1 | my @words; | |||
| 364 | 1 | 85 | make_path (dirname($new_expect_file)); | |||
| 365 | 1 | 7 | 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 | 2 | @words = sort case_biased keys %items; | |||
| 375 | 1 | 23 | open FILE, q{>}, $new_expect_file; | |||
| 376 | 1 | 2 | for my $word (@words) { | |||
| 377 | 1 | 5 | print FILE "$word\n" if $word =~ /\S/; | |||
| 378 | }; | |||||
| 379 | 1 | 11 | close FILE; | |||
| 380 | 1 | 2610 | system("git", "add", $new_expect_file); | |||
| 381 | } | |||||
| 382 | ||||||
| 383 | sub get_artifact_metadata { | |||||
| 384 | 3 | 3 | my ($url) = @_; | |||
| 385 | 3 | 4 | my $json_file = tempfile_name(); | |||
| 386 | 3 | 3 | my ($curl_stdout, $curl_stderr, $curl_result); | |||
| 387 | 3 | 13 | my @curl_args = ( | |||
| 388 | 'curl', | |||||
| 389 | $url, | |||||
| 390 | '-A', | |||||
| 391 | $ua, | |||||
| 392 | '-s', | |||||
| 393 | '--fail-with-body', | |||||
| 394 | ); | |||||
| 395 | 3 | 3 | my ($gh_token) = get_token(); | |||
| 396 | 3 | 7 | push @curl_args, '-u', "token:$gh_token" if defined $gh_token; | |||
| 397 | 3 | 6 | push @curl_args, ( | |||
| 398 | '-o', | |||||
| 399 | $json_file | |||||
| 400 | ); | |||||
| 401 | 3 | 3 | ($curl_stdout, $curl_stderr, $curl_result) = capture_system( | |||
| 402 | @curl_args | |||||
| 403 | ); | |||||
| 404 | 3 | 11 | unless ($curl_result == 0) { | |||
| 405 | 1 | 7 | if ($curl_stdout eq '') { | |||
| 406 | 1 | 5 | local $/; | |||
| 407 | 1 | 22 | open my $error_fh, '<', $json_file; | |||
| 408 | 1 | 10 | $curl_stdout = <$error_fh>; | |||
| 409 | 1 | 5 | close $error_fh; | |||
| 410 | } | |||||
| 411 | return ( | |||||
| 412 | 1 | 11 | out => $curl_stdout, | |||
| 413 | err => $curl_stderr, | |||||
| 414 | result => $curl_result, | |||||
| 415 | ); | |||||
| 416 | } | |||||
| 417 | 2 | 2 | my $link; | |||
| 418 | 2 | 38 | open my $json_file_fh, '<', $json_file; | |||
| 419 | 2 | 3 | my ($id, $download_url, $count); | |||
| 420 | { | |||||
| 421 | 2 2 | 1 12 | local $/; | |||
| 422 | 2 | 20 | my $content = <$json_file_fh>; | |||
| 423 | 2 | 13 | my $json = decode_json $content; | |||
| 424 | 2 | 5097 | my $artifact = $json->{'artifacts'}->[0]; | |||
| 425 | 2 | 3 | $id = $artifact->{'id'}; | |||
| 426 | 2 | 27 | $download_url = $artifact->{'archive_download_url'}; | |||
| 427 | 2 | 7 | $count = $json->{'total_count'}; | |||
| 428 | } | |||||
| 429 | 2 | 7 | 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 | 16 | id => $id, | |||
| 439 | download => $download_url, | |||||
| 440 | count => $count, | |||||
| 441 | ); | |||||
| 442 | } | |||||
| 443 | ||||||
| 444 | sub 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 | 6 | my %first = get_artifact_metadata($url.$page); | |||
| 449 | 2 | 5 | $page = $first{'count'}; | |||
| 450 | 2 | 2 | if (defined $page) { | |||
| 451 | 1 | 3 | my %second = get_artifact_metadata($url.$page); | |||
| 452 | 1 | 3 | my ($id_1, $id_2) = ($first{'id'}, $second{'id'}); | |||
| 453 | 1 | 7 | if (defined $id_1 && defined $id_2) { | |||
| 454 | 1 | 3 | if ($id_2 > $id_1) { | |||
| 455 | return ( | |||||
| 456 | 0 | 0 | download => $second{'download'}, | |||
| 457 | ); | |||||
| 458 | } | |||||
| 459 | } | |||||
| 460 | } | |||||
| 461 | 2 | 5 | my $download = $first{'download'}; | |||
| 462 | 2 | 2 | if (defined $download) { | |||
| 463 | return ( | |||||
| 464 | 1 | 4 | download => $download, | |||
| 465 | ); | |||||
| 466 | } | |||||
| 467 | 1 | 5 | return %first; | |||
| 468 | } | |||||
| 469 | ||||||
| 470 | sub download_latest_artifact { | |||||
| 471 | 2 | 6 | my %maybe_download = get_latest_artifact_metadata(@_); | |||
| 472 | 2 | 3 | my $download = $maybe_download{'download'}; | |||
| 473 | 2 | 4 | my $zip_file = tempfile_name(); | |||
| 474 | 2 | 7 | if (defined $download) { | |||
| 475 | 1 | 3 | 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 | 2 | push @curl_args, '-u', "token:$gh_token" if defined $gh_token; | |||
| 486 | 1 | 2 | push @curl_args, ( | |||
| 487 | '-o', | |||||
| 488 | $zip_file | |||||
| 489 | ); | |||||
| 490 | 1 | 4 | ($curl_stdout, $curl_stderr, $curl_result) = capture_system( | |||
| 491 | @curl_args | |||||
| 492 | ); | |||||
| 493 | 1 | 7 | if ($curl_result != 0) { | |||
| 494 | 1 | 4 | if ($curl_stdout eq '') { | |||
| 495 | 1 | 7 | local $/; | |||
| 496 | 1 | 20 | open my $error_fh, '<', $zip_file; | |||
| 497 | 1 | 10 | $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 | 2 | my ($out, $err, $result) = ($maybe_download{'out'}, $maybe_download{'err'}, $maybe_download{'result'}); | |||
| 513 | 1 | 3 | return ("$out\n$err", $result); | |||
| 514 | } | |||||
| 515 | ||||||
| 516 | sub get_artifacts { | |||||
| 517 | 2 | 3205 | my ($repo, $run, $suffix) = @_; | |||
| 518 | 2 | 2 | our $program; | |||
| 519 | 2 | 5 | my $artifact_dir = tempdir(CLEANUP => 1); | |||
| 520 | 2 | 332 | my $gh_err_text; | |||
| 521 | 2 | 2 | my $artifact_name = 'check-spelling-comment'; | |||
| 522 | 2 | 2 | if ($suffix) { | |||
| 523 | 0 | 0 | $artifact_name .= "-$suffix"; | |||
| 524 | } | |||||
| 525 | 2 | 2 | my $retries_remaining = 3; | |||
| 526 | 2 | 29 | 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 | 5 | return glob("$artifact_dir/artifact*.zip") unless ($ret >> 8); | |||
| 534 | ||||||
| 535 | 2 | 5 | die_with_message($gh_err_text); | |||
| 536 | 2 | 16 | if ($gh_err_text =~ /no valid artifacts found to download|"Artifact has expired"/) { | |||
| 537 | 1 | 5 | 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 | 7 | if ($expired_json ne '') { | |||
| 544 | 1 | 3 | chomp $expired_json; | |||
| 545 | 1 | 0 | my $expired; | |||
| 546 | 1 1 | 3 9 | eval { $expired = decode_json $expired_json } || CheckSpelling::Util::die_custom $program, 546, "decode_json failed in update_repository with '$expired_json'"; | |||
| 547 | 1 | 135 | if ($expired) { | |||
| 548 | 1 | 34 | print "$program: GitHub Run Artifact expired. You will need to trigger a new run.\n"; | |||
| 549 | 1 1 1 | 0 5 15 | { 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 | 5 | if ($gh_err_text =~ /HTTP 404: Not Found|"status":"404"/) { | |||
| 568 | 1 | 14 | 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 4 7 | { 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 | ||||||
| 622 | sub update_repository { | |||||
| 623 | 1 | 3 | my ($artifact) = @_; | |||
| 624 | 1 | 2 | our $program; | |||
| 625 | 1 | 3 | CheckSpelling::Util::die_custom $program, 625, "$program: artifact argument contains quote characters" if $artifact =~ /'/; | |||
| 626 | 1 | 5 | my $apply = unzip_pipe($artifact, 'apply.json'); | |||
| 627 | 1 | 8 | 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 | 0 | my $config_ref; | |||
| 637 | 1 1 | 1 8 | 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 | 463 | my $git_repo_root = run_pipe('git', 'rev-parse', '--show-toplevel'); | |||
| 641 | 1 | 4 | chomp $git_repo_root; | |||
| 642 | 1 | 5 | 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 | 3 | retrieve_spell_check_this($artifact, $config_ref); | |||
| 646 | 1 | 13 | remove_stale($artifact, $config_ref); | |||
| 647 | 1 | 5 | add_expect($artifact, $config_ref); | |||
| 648 | 1 | 10 | add_to_excludes($artifact, $config_ref); | |||
| 649 | 1 | 2475 | system('git', 'add', '-u', '--', $config_ref->{'spelling_config'}); | |||
| 650 | } | |||||
| 651 | ||||||
| 652 | sub extract_artifacts_from_file { | |||||
| 653 | 1 | 2 | my ($artifact) = @_; | |||
| 654 | 1 | 1248 | open my $artifact_reader, '-|', 'unzip', '-l', $artifact; | |||
| 655 | 1 | 8 | my ($has_artifact, $only_file) = (0, 0); | |||
| 656 | 1 | 778 | while (my $line = <$artifact_reader>) { | |||
| 657 | 6 | 4 | chomp $line; | |||
| 658 | 6 | 12 | if ($line =~ /\s+artifact\.zip$/) { | |||
| 659 | 1 | 2 | $has_artifact = 1; | |||
| 660 | 1 | 1 | next; | |||
| 661 | } | |||||
| 662 | 5 | 9 | if ($line =~ /\s+1 file$/) { | |||
| 663 | 1 | 0 | $only_file = 1; | |||
| 664 | 1 | 156 | next; | |||
| 665 | } | |||||
| 666 | 4 | 9 | $only_file = 0 if $only_file; | |||
| 667 | } | |||||
| 668 | 1 | 11 | close $artifact_reader; | |||
| 669 | 1 | 0 | my @artifacts; | |||
| 670 | 1 | 9 | if ($has_artifact && $only_file) { | |||
| 671 | 1 | 7 | my $artifact_dir = tempdir(CLEANUP => 1); | |||
| 672 | 1 | 221 | my ($fh, $gh_err) = tempfile(); | |||
| 673 | 1 | 125 | close $fh; | |||
| 674 | 1 | 2309 | system('unzip', '-q', '-d', $artifact_dir, $artifact, 'artifact.zip'); | |||
| 675 | 1 | 21 | @artifacts = ("$artifact_dir/artifact.zip"); | |||
| 676 | } else { | |||||
| 677 | 0 | 0 | @artifacts = ($artifact); | |||
| 678 | } | |||||
| 679 | 1 | 11 | return @artifacts; | |||
| 680 | } | |||||
| 681 | ||||||
| 682 | sub main { | |||||
| 683 | 2 | 177128 | our $program; | |||
| 684 | 2 | 3 | my ($bash_script, $first, $run); | |||
| 685 | 2 | 5 | ($program, $bash_script, $first, $run) = @_; | |||
| 686 | 2 | 2 | my $syntax = "$program <RUN_URL | OWNER/REPO RUN | ARTIFACT.zip>"; | |||
| 687 | # Stages | |||||
| 688 | # - 1 check for tools basic | |||||
| 689 | 2 | 5 | 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 | 4 | CheckSpelling::Util::die_custom $program, 696, $syntax unless defined $first; | |||
| 697 | 2 | 1 | $ENV{'GITHUB_API_URL'} ||= 'https://api.github.com'; | |||
| 698 | 2 | 2 | my $repo; | |||
| 699 | my @artifacts; | |||||
| 700 | 2 | 8 | if (-s $first) { | |||
| 701 | 1 | 2 | @artifacts = extract_artifacts_from_file($first); | |||
| 702 | } else { | |||||
| 703 | 1 | 1 | my $suffix; | |||
| 704 | 1 | 7 | 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 | 8 | 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 | 2 | for my $artifact (@artifacts) { | |||
| 717 | 1 | 2 | update_repository($artifact); | |||
| 718 | } | |||||
| 719 | } | |||||
| 720 | ||||||
| 721 | # main($0 ne '-' ? $0 : 'apply.pl', $bash_script, @ARGV); | |||||