blib/lib/Test/CPAN/Health/Check/AbandonedDeps.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 12 / 12 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/Test/CPAN/Health/Check.pm ↑ Summary blib/lib/Test/CPAN/Health/Check/Benchmarks.pm »
1 package Test::CPAN::Health::Check::AbandonedDeps;
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 Readonly::Scalar my $ABANDONED_YEARS => 3;
22  
23 # Seconds in one year (approximate)
24 Readonly::Scalar my $SECS_PER_YEAR => 31_557_600; # 365.25 * 24 * 3600
25  
26 =head1 NAME
27  
28 Test::CPAN::Health::Check::AbandonedDeps - Check for dependencies with no recent releases
29  
30 =head1 SYNOPSIS
31  
32 use Test::CPAN::Health::Check::AbandonedDeps;
33  
34 my $check = Test::CPAN::Health::Check::AbandonedDeps->new;
35 my $result = $check->run($dist);
36  
37 =head1 DESCRIPTION
38  
39 Queries MetaCPAN to find the date of the latest release for each runtime
40 dependency. A dependency is flagged as I<potentially abandoned> when its
41 most recent release is older than C<ABANDONED_YEARS> (currently 3 years).
42  
43 Perl core modules and lowercase pragmas are excluded from analysis.
44  
45 Score = (active_deps / total_checked) * 100.
46 Status thresholds: pass E<ge> 80, warn E<ge> 60, fail otherwise.
47  
48 =head1 LIMITATIONS
49  
50 =over 4
51  
52 =item * An old module that is stable and needs no updates would also be
53 flagged. The signal is I<potentially> abandoned, not definitively so.
54  
55 =item * MetaCPAN is queried serially for each dependency.
56  
57 =item * The date threshold is computed against the current system clock.
58  
59 =back
60  
61 =cut
62  
63 sub id { return 'abandoned_deps' }
64 sub name { return 'Abandoned Dependencies' }
65 sub description { return 'Checks for dependencies that appear to be unmaintained' }
66 sub weight { return 5 }
67 sub category { return 'security' }
68  
69 =head2 new
70  
71 Construct an AbandonedDeps check. Accepts all base-class parameters plus
72 an optional C<ignore> arrayref of module names to exclude from the check.
73  
74 =cut
75  
76 sub new {
77 my ($class, %args) = @_;
78  
79 my @ignore = @{ delete $args{ignore} // [] };
80  
81 my $self = $class->SUPER::new(%args);
82  
83 $self->{_ignore} = { map { $_ => 1 } @ignore };
84  
85 return $self;
86 }
87  
88 =head2 run
89  
90 =head3 PURPOSE
91  
92 Check each runtime dependency's latest release date and flag those that have
93 not had a release in the past C<ABANDONED_YEARS> years.
94  
95 =head3 API SPECIFICATION
96  
97 =head4 INPUT
98  
99 dist Test::CPAN::Health::Distribution required
100 context Hashref optional
101  
102 =head4 OUTPUT
103  
104 L<Test::CPAN::Health::Result> with check_id C<'abandoned_deps'>.
105  
106 =head3 MESSAGES
107  
108 Code | Severity | Message | Resolution
109 ------+----------+-------------------------------------------+-----------
110 AD001 | SKIP | Network checks disabled | Remove --no-network
111 AD002 | SKIP | No META file found | Add META.yml / META.json
112 AD003 | SKIP | No checkable runtime dependencies | n/a
113 AD004 | PASS | All N dependencies are actively maintained|
114 AD005 | WARN | N of M dependencies may be abandoned | Find alternatives
115 AD006 | FAIL | N of M dependencies may be abandoned | Find alternatives
116  
117 =head3 FORMAL SPECIFICATION
118  
119 -- Z schema (placeholder) --
120 AbandonedDepsOp
121 abandoned : N
122 total : N
123 score : 0..100
124 -------------------------------------------------------
125 no_network => status = skip
126 meta = undef => status = skip
127 total = 0 => status = skip
128 score >= 80 => status = pass
129 score >= 60 => status = warn
130 score < 60 => status = fail
131  
132 =head3 SIDE EFFECTS
133  
134 Makes one HTTPS GET request to C<fastapi.metacpan.org> per dependency.
135  
136 =head3 USAGE EXAMPLE
137  
138 my $result = Test::CPAN::Health::Check::AbandonedDeps->new->run($dist);
139 printf "Abandoned: %s\n", $result->summary;
140  
141 =cut
142  
143 sub run {
144 my ($self, $dist, $context) = @_;
145  
146 croak 'dist must be a Test::CPAN::Health::Distribution'
147 unless ref($dist) && $dist->isa('Test::CPAN::Health::Distribution');
148  
149 return $self->_skip('Network checks disabled (--no-network)')
150 if $self->no_network;
151  
152 my $meta = $dist->meta;
153 return $self->_skip('No META file found') unless $meta;
154  
155 my @checkable = _collect_checkable($meta, $self->{_ignore});
156 return $self->_skip('No checkable runtime dependencies found')
157 unless @checkable;
158  
159 my $now = time;
160 my $cutoff = $now - ($ABANDONED_YEARS * $SECS_PER_YEAR);
161 my (@abandoned, @active);
162  
163 for my $mod (@checkable) {
164 my ($is_abandoned, $detail) = _classify_dep($mod, $now, $cutoff);
165 next unless defined $is_abandoned;
166 if ($is_abandoned) { push @abandoned, $detail }
167 else { push @active, $mod }
168 }
169  
170 my $total = @abandoned + @active;
171 return $self->_skip('No dependency release dates available from MetaCPAN')
172 unless $total;
173  
174 my $n_abandoned = scalar @abandoned;
175 my $score = int(($total - $n_abandoned) / $total * 100);
176 # Any abandoned dep is worth at least a warning so details are always shown.
177 my $status = $n_abandoned == 0 ? 'pass'
178 : $score >= $SCORE_WARN ? 'warn'
179 : 'fail';
180  
181 my $summary = $n_abandoned
182 ? "$n_abandoned of $total runtime dependencies may be abandoned "
183 . "(no release in $ABANDONED_YEARS+ years)"
184 : ($total == 2 ? 'Both' : "All $total") . ' checked runtime dependencies are actively maintained';
185  
186 return $self->_result(
187 status => $status,
188 score => $score,
189 summary => $summary,
190 details => [ map { "Potentially abandoned: $_" } sort @abandoned ],
191 data => {
192 name => $self->name,
193 total => $total,
194 abandoned => $n_abandoned,
195 abandoned_mods => \@abandoned,
196 active_mods => \@active,
197 },
198 );
199 }
200  
201 sub _collect_checkable {
202 my ($meta, $ignore_ref) = @_;
203  
204 $ignore_ref //= {};
205  
206 my $prereqs = $meta->effective_prereqs;
207 my $runtime = $prereqs->requirements_for('runtime', 'requires');
208 my %deps = %{ $runtime->as_string_hash };
209 my $use_corelist = eval { require Module::CoreList; 1 };
210  
211 my @checkable;
212 for my $mod (sort keys %deps) {
213 next if $mod eq 'perl';
214 next if $mod =~ / ^ [a-z] /x; # pragmas are lowercase
215 next if $ignore_ref->{$mod};
216 next if $use_corelist && Module::CoreList->first_release($mod);
217 push @checkable, $mod;
218 }
219 return @checkable;
220 }
221  
222 sub _classify_dep {
223 my ($mod, $now, $cutoff) = @_;
224  
225 my ($data, $err) = _http_get("$METACPAN_API/module/$mod");
226 return if $err || !defined $data->{date};
227  
228 my $epoch = _iso8601_to_epoch($data->{date});
229 return unless defined $epoch;
230  
231 my $age_years = ($now - $epoch) / $SECS_PER_YEAR;
232 if ($epoch < $cutoff) {
233 my $detail = sprintf '%s (last release %.1f years ago)', $mod, $age_years;
234 return (1, $detail);
235 }
236 return (0, undef);
237 }
238  
239 # ---------------------------------------------------------------------------
240 # Private helpers
241 # ---------------------------------------------------------------------------
242  
243 # Parse an ISO 8601 date string to a Unix epoch (UTC).
244 # Handles both "2024-01-01T12:00:00" and "2024-01-01T12:00:00.000Z".
245 sub _iso8601_to_epoch {
246 my ($str) = @_;
247 return unless defined $str;
248 my ($y, $mo, $d) = $str =~ / ^ (\d{4}) - (\d{2}) - (\d{2}) /x
249 or return;
250 require Time::Local;
251 return eval {
252 Time::Local::timegm(0, 0, 0, $d + 0, $mo - 1, $y - 1900)
253 };
254 }
255  
256 sub _http_get {
257 my ($url) = @_;
258  
259 my $ua = HTTP::Tiny->new(timeout => $HTTP_TIMEOUT);
260 my $res = $ua->get($url, { headers => { 'Accept' => 'application/json' } });
261  
262 return (undef, "HTTP $res->{status} $res->{reason}") unless $res->{success};
263  
264 my $data = eval { JSON::MaybeXS::decode_json($res->{content}) };
265 return (undef, "JSON parse error: $@") if $@;
266  
267 return ($data, undef);
268 }
269  
270 =head1 AUTHOR
271  
272 Nigel Horne, C<< <njh at nigelhorne.com> >>
273  
274 =head1 LICENSE AND COPYRIGHT
275  
276 Copyright (C) 2026 Nigel Horne.
277  
278 This program is free software; you can redistribute it and/or modify it
279 under the terms of the GNU General Public License as published by the Free
280 Software Foundation; either version 2 of the License, or (at your option)
281 any later version.
282  
283 =cut
284  
285 1;
« blib/lib/Test/CPAN/Health/Check.pm ↑ Summary blib/lib/Test/CPAN/Health/Check/Benchmarks.pm »