File Coverage

blib/lib/CPAN/Audit/Filter.pm
Criterion Covered Total %
statement 31 31 100.0
branch 4 4 100.0
condition 2 2 100.0
subroutine 6 6 100.0
pod 3 3 100.0
total 46 46 100.0


line stmt bran cond sub pod time code
1 14     14   201912 use v5.10;
  14         52  
2              
3             package CPAN::Audit::Filter;
4 14     14   70 use strict;
  14         20  
  14         272  
5 14     14   40 use warnings;
  14         20  
  14         3902  
6              
7             our $VERSION = "1.001";
8              
9             =encoding utf8
10              
11             =head1 NAME
12              
13             CPAN::Audit::Filter - manage the reports / CVEs to ignore
14              
15             =head1 SYNOPSIS
16              
17             use CPAN::Audit::Filter;
18              
19             my $filter = CPAN::Audit::Filter->new( exclude => $array_ref );
20              
21             my $query = CPAN::Audit::Query->new(...);
22             my $advisories = $query->advisories_for( $distname, $version_range );
23              
24             foreach my $advisory ( $advisories->@* ) {
25             next if $filter->excludes($advisory);
26             ...
27             }
28              
29             =head1 DESCRIPTION
30              
31             =head2 Class methods
32              
33             =over 4
34              
35             =item * new( exclude => ARRAYREF )
36              
37             The values in the array ref for C are uppercased before
38             they are stored.
39              
40             =cut
41              
42             sub new {
43 11     11 1 8534 my($class, %params) = @_;
44              
45 11         35 my $self = bless {}, $class;
46 11   100     62 $params{exclude} //= [];
47              
48 11         20 my %excludes = map { uc($_) => 1 } @{ $params{exclude} };
  1         4  
  11         33  
49 11         117 $self->{excludes} = \%excludes;
50              
51 11         39 $self->{ignored} = {};
52              
53 11         42 return $self;
54             }
55              
56              
57             =back
58              
59             =head2 Instance methods
60              
61             =over 4
62              
63             =item * excludes( $advisory )
64              
65             Returns true if this instance excludes either the ID or any of the
66             CVEs for ADVISORY, a hash as returned by L. This
67             hash has these keys:
68              
69             id - a string, such as Some-Module-001
70             cves - an array reference of CVE strings, such as CVE-2022-001
71              
72             The values extracted from the hash are uppercased before use.
73              
74             =cut
75              
76             sub excludes {
77 121     121 1 4432 my($self, $advisory) = @_;
78              
79 121 100       144 return 0 unless keys %{$self->{excludes}};
  121         387  
80              
81 2         3 my @ids = map { uc } grep { defined } ($advisory->{id}, @{$advisory->{cves}});
  2         6  
  2         4  
  2         4  
82              
83 2         3 foreach my $id ( @ids ) {
84 2 100       5 next unless $self->{excludes}{$id};
85 1         3 $self->{ignored}{$id}++;
86 1         2 return 1;
87             }
88              
89 1         3 return 0;
90             }
91              
92             =item * ignored_count
93              
94             Return the count of the advisories that were ignored. Each ID or CVE
95             value only counts once.
96              
97             =cut
98              
99 2     2 1 576 sub ignored_count { scalar keys %{$_[0]->{ignored}} }
  2         8  
100              
101             =back
102              
103             =head1 LICENSE
104              
105             Copyright (C) 2022 Graham TerMarsch
106              
107             This library is free software; you can redistribute it and/or modify
108             it under the same terms as Perl itself.
109              
110             =cut
111              
112             1;