« blib/lib/Test/CPAN/Health/Check/CIConfig.pm
↑ Summary
blib/lib/Test/CPAN/Health/Check/Changelog.pm »
| 1 | package Test::CPAN::Health::Check::CPANTesters; | ||
| 2 | |||
| 3 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 2 | ▶ | use autodie qw(:all); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | use Carp qw(croak carp); | ||
| 8 | use HTTP::Tiny (); | ||
| 9 | use JSON::MaybeXS (); | ||
| 10 | use Readonly; | ||
| 11 | use Params::Validate::Strict qw(validate_strict); | ||
| 12 | |||
| 13 | use parent 'Test::CPAN::Health::Check'; | ||
| 14 | |||
| 15 | our $VERSION = '0.1.0'; | ||
| 16 | |||
| 17 | Readonly::Scalar my $METACPAN_API => 'https://fastapi.metacpan.org/v1'; | ||
| 18 | Readonly::Scalar my $HTTP_TIMEOUT => 30; | ||
| 19 | Readonly::Scalar my $SCORE_PASS => 80; | ||
| 20 | Readonly::Scalar my $SCORE_WARN => 60; | ||
| 21 | |||
| 22 | =head1 NAME | ||
| 23 | |||
| 24 | Test::CPAN::Health::Check::CPANTesters - Check CPAN Testers pass/fail ratio | ||
| 25 | |||
| 26 | =head1 SYNOPSIS | ||
| 27 | |||
| 28 | use Test::CPAN::Health::Check::CPANTesters; | ||
| 29 | |||
| 30 | my $check = Test::CPAN::Health::Check::CPANTesters->new; | ||
| 31 | my $result = $check->run($dist); | ||
| 32 | |||
| 33 | =head1 DESCRIPTION | ||
| 34 | |||
| 35 | Fetches test statistics for the distribution's latest CPAN release via the | ||
| 36 | MetaCPAN release search API (which embeds aggregated CPAN Testers data). | ||
| 37 | |||
| 38 | Score = C<int(pass / (pass + fail) * 100)>. C<na> and C<unknown> results are | ||
| 39 | not counted in the denominator so that untested platforms do not artificially | ||
| 40 | lower the score. | ||
| 41 | |||
| 42 | A C<fail> status from this check triggers a hard cap on the overall Report | ||
| 43 | score at 75 (applied by L<Test::CPAN::Health::Report>). | ||
| 44 | |||
| 45 | =head1 LIMITATIONS | ||
| 46 | |||
| 47 | =over 4 | ||
| 48 | |||
| 49 | =item * The check can only run against a distribution that has been released | ||
| 50 | to CPAN and indexed by MetaCPAN. Unreleased local distributions will be | ||
| 51 | skipped. | ||
| 52 | |||
| 53 | =item * CPAN Testers data in MetaCPAN may lag a few days behind the live | ||
| 54 | CPAN Testers database. | ||
| 55 | |||
| 56 | =item * Only pass and fail grades are used; C<na> (platform not applicable) | ||
| 57 | and C<unknown> (incomplete reports) are excluded from the denominator. | ||
| 58 | |||
| 59 | =back | ||
| 60 | |||
| 61 | =cut | ||
| 62 | |||
| 63 | sub id { return 'cpan_testers' } | ||
| 64 | sub name { return 'CPAN Testers' } | ||
| 65 | sub description { return 'Checks the CPAN Testers pass/fail ratio for the distribution' } | ||
| 66 | sub weight { return 8 } | ||
| 67 | sub category { return 'ci' } | ||
| 68 | |||
| 69 | =head2 run | ||
| 70 | |||
| 71 | =head3 PURPOSE | ||
| 72 | |||
| 73 | Fetch the CPAN Testers statistics for the latest release of this distribution | ||
| 74 | and compute a pass-rate score. | ||
| 75 | |||
| 76 | =head3 API SPECIFICATION | ||
| 77 | |||
| 78 | =head4 INPUT | ||
| 79 | |||
| 80 | dist Test::CPAN::Health::Distribution required | ||
| 81 | context Hashref optional | ||
| 82 | |||
| 83 | =head4 OUTPUT | ||
| 84 | |||
| 85 | L<Test::CPAN::Health::Result> with check_id C<'cpan_testers'>. | ||
| 86 | |||
| 87 | =head3 MESSAGES | ||
| 88 | |||
| 89 | Code | Severity | Message | Resolution | ||
| 90 | ------+----------+--------------------------------------------+----------- | ||
| 91 | CT001 | SKIP | Network checks disabled | Remove --no-network | ||
| 92 | CT002 | SKIP | Distribution name not available | Add META.yml | ||
| 93 | CT003 | SKIP | Distribution not found on MetaCPAN | Release to CPAN first | ||
| 94 | CT004 | SKIP | No CPAN Testers data available yet | Wait for smoke reports | ||
| 95 | CT005 | ERROR | MetaCPAN API error: ... | Transient; retry | ||
| 96 | CT006 | PASS | CPAN Testers: N pass, M fail (P%) | | ||
| 97 | CT007 | WARN | CPAN Testers: N pass, M fail (P%) | Investigate failures | ||
| 98 | CT008 | FAIL | CPAN Testers: N pass, M fail (P%) | Fix failing platforms | ||
| 99 | |||
| 100 | =head3 FORMAL SPECIFICATION | ||
| 101 | |||
| 102 | -- Z schema (placeholder) -- | ||
| 103 | CPANTestersOp | ||
| 104 | pass : N | ||
| 105 | fail : N | ||
| 106 | score : 0..100 | ||
| 107 | ------------------------------------------------------- | ||
| 108 | no_network => status = skip | ||
| 109 | dist_name = undef => status = skip | ||
| 110 | not on MetaCPAN => status = skip | ||
| 111 | pass + fail = 0 => status = skip (no data) | ||
| 112 | score >= 80 => status = pass | ||
| 113 | score >= 60 => status = warn | ||
| 114 | score < 60 => status = fail (Report hard-caps at 75) | ||
| 115 | |||
| 116 | =head3 SIDE EFFECTS | ||
| 117 | |||
| 118 | Makes one HTTPS POST request to C<fastapi.metacpan.org>. | ||
| 119 | |||
| 120 | =head3 USAGE EXAMPLE | ||
| 121 | |||
| 122 | my $result = Test::CPAN::Health::Check::CPANTesters->new->run($dist); | ||
| 123 | printf "Pass rate: %d%%\n", $result->score; | ||
| 124 | |||
| 125 | =cut | ||
| 126 | |||
| 127 | sub run { | ||
| 128 | my ($self, $dist, $context) = @_; | ||
| 129 | |||
| 130 | croak 'dist must be a Test::CPAN::Health::Distribution' | ||
| 131 | unless ref($dist) && $dist->isa('Test::CPAN::Health::Distribution'); | ||
| 132 | |||
| 133 | return $self->_skip('Network checks disabled (--no-network)') | ||
| 134 | if $self->no_network; | ||
| 135 | |||
| 136 | my $dist_name = $dist->name; | ||
| 137 | return $self->_skip('Distribution name not available') | ||
| 138 | unless defined $dist_name && length $dist_name; | ||
| 139 | |||
| 140 | (my $dist_slug = $dist_name) =~ s/ :: /-/gx; | ||
| 141 | |||
| 142 | # Search MetaCPAN for the latest indexed release of this distribution. | ||
| 143 | # MetaCPAN embeds aggregated CPAN Testers pass/fail/na/unknown counts in | ||
| 144 | # the 'tests' field of each release document. | ||
| 145 | my ($data, $err) = _http_post( | ||
| 146 | "$METACPAN_API/release/_search", | ||
| 147 | { | ||
| 148 | query => { | ||
| 149 | bool => { | ||
| 150 | must => [ | ||
| 151 | { term => { distribution => $dist_slug } }, | ||
| 152 | { term => { status => 'latest' } }, | ||
| 153 | ], | ||
| 154 | }, | ||
| 155 | }, | ||
| 156 | size => 1, | ||
| 157 | _source => [ 'tests', 'version', 'date' ], | ||
| 158 | }, | ||
| 159 | ); | ||
| 160 | return $self->_error("MetaCPAN API error: $err") if $err; | ||
| 161 | |||
| 162 | my $hits = $data->{hits}{hits} // []; | ||
| 163 | return $self->_skip('Distribution not found on MetaCPAN') unless @$hits; | ||
| 164 | |||
| 165 | my $release = $hits->[0]{_source} // {}; | ||
| 166 | my $ver = $release->{version} // '?'; | ||
| 167 | my ($n_pass, $n_fail, $n_na, $n_unknown) = _parse_testers_counts($release->{tests}); | ||
| 168 | my $total = $n_pass + $n_fail; | ||
| 169 | |||
| 170 | return $self->_skip( | ||
| 171 | "No CPAN Testers data yet for $dist_slug-$ver" | ||
| 172 | ) unless $total; | ||
| 173 | |||
| 174 | my $score = int($n_pass / $total * 100); | ||
| 175 | my $status = $score >= $SCORE_PASS ? 'pass' | ||
| 176 | : $score >= $SCORE_WARN ? 'warn' | ||
| 177 | : 'fail'; | ||
| 178 | my @details = $n_fail > 0 | ||
| 179 | ? ( "CPAN Testers reports $n_fail failure(s) -- see https://www.cpantesters.org/" ) | ||
| 180 | : (); | ||
| 181 | |||
| 182 | return $self->_result( | ||
| 183 | status => $status, | ||
| 184 | score => $score, | ||
| 185 | summary => "CPAN Testers: $n_pass pass, $n_fail fail ($score% pass rate) for v$ver", | ||
| 186 | details => \@details, | ||
| 187 | data => { | ||
| 188 | name => $self->name, | ||
| 189 | pass => $n_pass, | ||
| 190 | fail => $n_fail, | ||
| 191 | na => $n_na, | ||
| 192 | unknown => $n_unknown, | ||
| 193 | pass_rate => $score, | ||
| 194 | version => $release->{version}, | ||
| 195 | }, | ||
| 196 | ); | ||
| 197 | } | ||
| 198 | |||
| 199 | sub _parse_testers_counts { | ||
| 200 | my ($tests) = @_; | ||
| 201 | $tests //= {}; | ||
| 202 | return ( | ||
| 203 | $tests->{pass} // 0, | ||
| 204 | $tests->{fail} // 0, | ||
| 205 | $tests->{na} // 0, | ||
| 206 | $tests->{unknown} // 0, | ||
| 207 | ); | ||
| 208 | } | ||
| 209 | |||
| 210 | # --------------------------------------------------------------------------- | ||
| 211 | # Private helpers | ||
| 212 | # --------------------------------------------------------------------------- | ||
| 213 | |||
| 214 | sub _http_post { | ||
| 215 | my ($url, $body) = @_; | ||
| 216 | |||
| 217 | my $ua = HTTP::Tiny->new(timeout => $HTTP_TIMEOUT); | ||
| 218 | my $res = $ua->post($url, { | ||
| 219 | headers => { | ||
| 220 | 'Content-Type' => 'application/json', | ||
| 221 | 'Accept' => 'application/json', | ||
| 222 | }, | ||
| 223 | content => JSON::MaybeXS::encode_json($body), | ||
| 224 | }); | ||
| 225 | |||
| 226 | return (undef, "HTTP $res->{status} $res->{reason}") unless $res->{success}; | ||
| 227 | |||
| 228 | my $data = eval { JSON::MaybeXS::decode_json($res->{content}) }; | ||
| 229 | return (undef, "JSON parse error: $@") if $@; | ||
| 230 | |||
| 231 | return ($data, undef); | ||
| 232 | } | ||
| 233 | |||
| 234 | =head1 AUTHOR | ||
| 235 | |||
| 236 | Nigel Horne, C<< <njh at nigelhorne.com> >> | ||
| 237 | |||
| 238 | =head1 LICENSE AND COPYRIGHT | ||
| 239 | |||
| 240 | Copyright (C) 2025-2026 Nigel Horne. | ||
| 241 | |||
| 242 | This program is free software; you can redistribute it and/or modify it | ||
| 243 | under the terms of the GNU General Public License as published by the Free | ||
| 244 | Software Foundation; either version 2 of the License, or (at your option) | ||
| 245 | any later version. | ||
| 246 | |||
| 247 | =cut | ||
| 248 | |||
| 249 | 1; | ||