File Coverage

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


line stmt bran cond sub pod time code
1 3     3   1910 use v5.10;
  3         12  
2              
3             package CPAN::Audit::Filter;
4 3     3   16 use strict;
  3         5  
  3         65  
5 3     3   28 use warnings;
  3         6  
  3         952  
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 3     3 1 9070 my($class, %params) = @_;
44              
45 3         13 my $self = bless {}, $class;
46 3   100     17 $params{exclude} //= [];
47              
48 3         4 my %excludes = map { uc($_) => 1 } @{ $params{exclude} };
  1         5  
  3         6  
49 3         10 $self->{excludes} = \%excludes;
50              
51 3         6 $self->{ignored} = {};
52              
53 3         11 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 2     2 1 4297 my($self, $advisory) = @_;
78              
79 2 50       3 return 0 unless keys %{$self->{excludes}};
  2         8  
80              
81 2         4 my @ids = map { uc } grep { defined } ($advisory->{id}, @{$advisory->{cves}});
  2         8  
  2         5  
  2         5  
82              
83 2         5 foreach my $id ( @ids ) {
84 2 100       5 next unless $self->{excludes}{$id};
85 1         3 $self->{ignored}{$id}++;
86 1         4 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 710 sub ignored_count { scalar keys %{$_[0]->{ignored}} }
  2         10  
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;