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   352736 use v5.10;
  14         59  
2              
3             package CPAN::Audit::Filter;
4 14     14   86 use strict;
  14         23  
  14         371  
5 14     14   89 use warnings;
  14         26  
  14         5158  
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 17311 my($class, %params) = @_;
44              
45 11         44 my $self = bless {}, $class;
46 11   100     97 $params{exclude} //= [];
47              
48 11         27 my %excludes = map { uc($_) => 1 } @{ $params{exclude} };
  1         6  
  11         43  
49 11         120 $self->{excludes} = \%excludes;
50              
51 11         31 $self->{ignored} = {};
52              
53 11         53 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 70     70 1 7891 my($self, $advisory) = @_;
78              
79 70 100       101 return 0 unless keys %{$self->{excludes}};
  70         402  
80              
81 2         6 my @ids = map { uc } grep { defined } ($advisory->{id}, @{$advisory->{cves}});
  2         9  
  2         6  
  2         8  
82              
83 2         5 foreach my $id ( @ids ) {
84 2 100       9 next unless $self->{excludes}{$id};
85 1         4 $self->{ignored}{$id}++;
86 1         4 return 1;
87             }
88              
89 1         4 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 1090 sub ignored_count { scalar keys %{$_[0]->{ignored}} }
  2         14  
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;