Plugin/CycloneDX.pm

filter: Statementstmt 60.0% 3 / 5 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 71.4% 5 / 7 CC 1 SCAR 1.4
CC
1
Cov
60%
BEGIN
1.3
BEGIN
1.0
CRAP 1.1
↑ Summary Role/CycloneDXSource.pm »
1 package Dist::Zilla::Plugin::CycloneDX;
2 $Dist::Zilla::Plugin::CycloneDX::VERSION = '0.001';
3 1 ▶use 5.020;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 0 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=2.6
6 with qw/Dist::Zilla::Role::FileGatherer/;
7 use MooseX::Types::Moose 'Str';
8 use namespace::autoclean;
9 use experimental qw/signatures postderef lexical_subs/;
10 no autovivification;
11  
12 use Carp;
13 use Dist::Zilla::File::InMemory;
14 use Time::Piece;
15 use URI::PackageURL;
16  
17 use SBOM::CycloneDX 1.08;
18 use SBOM::CycloneDX::Component;
19 use SBOM::CycloneDX::ExternalReference;
20 use SBOM::CycloneDX::License;
21 use SBOM::CycloneDX::Metadata;
22 use SBOM::CycloneDX::Metadata::Lifecycle;
23 use SBOM::CycloneDX::OrganizationalContact;
24 use SBOM::CycloneDX::Tools;
25 use SBOM::CycloneDX::Util qw/cpan_meta_to_spdx_license cyclonedx_component/;
26  
27 has filename => (
28 is => 'ro',
29 isa => Str,
30 lazy => 1,
31 default => sub($self) {
32 my $name = $self->zilla->name;
33 my $version = $self->zilla->version;
34 return "CPAN-META/$name-$version.cdx.json";
35 }
36 );
37  
38 my %simple_resource = (
39 homepage => 'website',
40 x_mailing_list => 'mailing-list',
41 x_MailingList => 'mailing-list',
42 x_mailinglist => 'mailing-list',
43 x_mastodon => 'social',
44 x_twitter => 'social',
45 x_wiki => 'documentation',
46 );
47  
48 my sub make_externs($module, $resources) {
49 my @externs = SBOM::CycloneDX::ExternalReference->new(type => 'documentation', url => "https://metacpan.org/pod/$module");
50 if (my $tracker = $resources->{bugtracker}{web}) {
51 push @externs, SBOM::CycloneDX::ExternalReference->new(type => 'issue-tracker', url => $tracker);
52 }
53 if (my $repo = $resources->{repository}{web}) {
54 push @externs, SBOM::CycloneDX::ExternalReference->new(type => 'vcs', url => $repo);
55 }
56 if (my $irc = $resources->{x_IRC}) {
57 my $url = ref $irc ? $irc->{url} : $irc;
58 push @externs, SBOM::CycloneDX::ExternalReference->new(type => 'chat', url => $url);
59 }
60 for my $key (keys %simple_resource) {
61 if (my $url = $resources->{$key}) {
62 push @externs, SBOM::CycloneDX::ExternalReference->new(type => $simple_resource{$key}, url => $url);
63 }
64 }
65 return sort { $a->type cmp $b->type } @externs;
66 }
67  
68 sub make_purl($name, $version) {
69 return URI::PackageURL->new(
70 type => 'cpan',
71 namespace => '',
72 name => $name,
73 version => $version,
74 );
75 }
76  
77 my sub make_root_component($meta) {
78 my $purl = make_purl($meta->{name}, $meta->{version});
79 my $bom_ref = sprintf '%s@%s', $meta->{name}, $meta->{version};
80 my $module = $meta->{name} =~ s/-/::/gr;
81 my @externs = make_externs($module, $meta->{resources});
82  
83 return SBOM::CycloneDX::Component->new(
84 type => 'library',
85 group => 'CPAN',
86 name => $meta->{name},
87 version => $meta->{version},
88 description => $meta->{abstract},
89 bom_ref => $bom_ref,
90 purl => $purl,
91 external_references => \@externs,
92 );
93 }
94  
95 my sub to_organization($author) {
96 my ($name, $email) = $author =~ /([^<]+?)\s+<([^>]+)>/ or croak "Could not parse author '$author'";
97 return SBOM::CycloneDX::OrganizationalContact->new(name => $name, email => $email);
98 }
99  
100 my sub make_licenses($meta) {
101 if (my $spdx_expr = $meta->{x_spdx_expression}) {
102 return SBOM::CycloneDX::License->new($spdx_expr);
103 } else {
104 my @raw = map { cpan_meta_to_spdx_license($_) } $meta->{license}->@*;
105 return map { SBOM::CycloneDX::License->new($_) } @raw;
106 }
107 }
108  
109 my %self_resources = (
110 bugtracker => {
111 web => 'https://github.com/Leont/dist-zilla-plugin-cyclonedx/issues',
112 },
113 repository => {
114 web => 'https://github.com/Leont/dist-zilla-plugin-cyclonedx',
115 },
116 x_IRC => 'irc://irc.perl.org/#distzilla',
117 );
118  
119 my sub make_tool_component($module, $dist, $resources = {}) {
120 my @externs = make_externs($module, $resources);
121 my $version = $module->VERSION;
122 my %args = (
123 type => 'library',
124 group => 'CPAN',
125 name => $dist,
126 external_references => \@externs,
127 );
128 if ($version) {
129 $args{version} = $version;
130 $args{purl} = make_purl($dist, $version);
131 }
132 return SBOM::CycloneDX::Component->new(%args);
133 }
134  
135 my sub make_tools($plugins) {
136 my $distname = __PACKAGE__ =~ s/::/-/gr;
137 my @tools = make_tool_component(__PACKAGE__, $distname, \%self_resources);
138 push @tools, map { make_tool_component(ref($_), $_->cyclonedx_dist, $_->cyclonedx_resources) } $plugins->@*;
139 push @tools, cyclonedx_component;
140  
141 return SBOM::CycloneDX::Tools->new(components => \@tools);
142 }
143  
144 my sub make_metadata($meta, $plugins) {
145 my $component = make_root_component($meta);
146 my @authors = map { to_organization($_) } $meta->{author}->@*;
147 my $timestamp = SBOM::CycloneDX::Timestamp->new($ENV{TIMESTAMP} // scalar gmtime);
148 my @licenses = make_licenses($meta);
149 my @lifecycle = SBOM::CycloneDX::Metadata::Lifecycle->new(phase => 'pre-build');
150 my $tools = make_tools($plugins);
151  
152 return SBOM::CycloneDX::Metadata->new(
153 component => $component,
154 authors => \@authors,
155 timestamp => $timestamp,
156 licenses => \@licenses,
157 lifecycles => \@lifecycle,
158 tools => $tools,
159 );
160 }
161  
162 sub gather_files($self) {
163 my @plugins = $self->zilla->plugins_with(-CycloneDXSource)->@*;
164 my $metadata = make_metadata($self->zilla->distmeta, \@plugins);
165 my $bom = SBOM::CycloneDX->new(metadata => $metadata);
166  
167 for my $plugin (@plugins) {
168 $plugin->add_to_bom($bom);
169 }
170  
171 local $SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /Net::IDN::Encode/ };
172 if (my @errors = $bom->validate) {
173 croak "Errors found in BOM: @errors";
174 }
175  
176 $self->add_file(Dist::Zilla::File::InMemory->new(
177 name => $self->filename,
178 encoded_content => $bom->to_string,
179 ));
180  
181 return;
182 }
183  
184 __PACKAGE__->meta->make_immutable;
185  
186 1;
187  
188 # ABSTRACT: CycloneDX files in dzil generated dists
189  
190 __END__
↑ Summary Role/CycloneDXSource.pm »