File Coverage

blib/lib/Dist/AutomationPolicy.pm
Criterion Covered Total %
statement 75 78 96.1
branch 21 30 70.0
condition 10 18 55.5
subroutine 17 17 100.0
pod 4 6 66.6
total 127 149 85.2


line stmt bran cond sub pod time code
1             package Dist::AutomationPolicy;
2              
3 2     2   549160 use v5.24;
  2         6  
4              
5 2     2   1105 use Moo;
  2         16696  
  2         14  
6              
7 2     2   2663 use Carp qw( croak );
  2         5  
  2         81  
8 2     2   10 use File::ShareDir qw( dist_file );
  2         3  
  2         123  
9 2     2   1479 use JSON ();
  2         19989  
  2         61  
10 2     2   1784 use JSON::Schema::Validate v0.7.0;
  2         96865  
  2         168  
11 2     2   24 use Path::Tiny qw( path );
  2         12  
  2         145  
12 2     2   1238 use PerlX::Maybe qw( maybe );
  2         5709  
  2         9  
13              
14 2     2   1247 use namespace::clean;
  2         37523  
  2         14  
15              
16 2     2   1811 use experimental qw( postderef signatures );
  2         8403  
  2         13  
17              
18             our $VERSION = 'v0.2.2';
19              
20             # ABSTRACT: generate and parse distribution automation policies
21              
22              
23             has version => (
24             is => 'ro',
25             default => 1,
26             required => 1,
27             );
28              
29              
30             has distribution => (
31             is => 'ro',
32             predicate => 1,
33             );
34              
35              
36             has description => (
37             is => 'ro',
38             predicate => 1,
39             );
40              
41              
42             has document => (
43             is => 'ro',
44             predicate => 1,
45             );
46              
47              
48             has code_generation => (
49             is => 'ro',
50             required => 1,
51             );
52              
53              
54             has automated_contributions => (
55             is => 'ro',
56             required => 1,
57             );
58              
59              
60             has automated_actions => (
61             is => 'ro',
62             required => 1,
63             );
64              
65              
66             has models => (
67             is => 'ro',
68             coerce => sub($val) {
69             return $val if ref($val) eq "ARRAY";
70             return [$val];
71             },
72 5     5   165 builder => sub($self) { return [] },
  5         11  
  5         8  
  5         18  
73             );
74              
75              
76             has filename => (
77             is => 'lazy',
78             default => 'CPAN-META/automation-policy.json',
79             );
80              
81             my $json = JSON->new->utf8->pretty->canonical;
82              
83             my $file = path( dist_file( __PACKAGE__ =~ s/::/-/gr, "automation-policy-schema.json" ) );
84              
85             my $schema = JSON::Schema::Validate->new( $json->decode( $file->slurp_raw ), compile => 1, );
86              
87              
88 25     25 1 7839 sub data($self) {
  25         41  
  25         35  
89             #<<<
90             return {
91 25 50       605 version => $self->version,
    50          
    100          
92             maybe distribution => $self->distribution,
93             maybe description => $self->has_description ? $self->description : undef,
94             maybe document => $self->has_document ? $self->document : undef,
95             code_generation => $self->code_generation,
96             automated_contributions => $self->automated_contributions,
97             automated_actions => $self->automated_actions,
98             maybe models => $self->models->[0] ? $self->models : undef,
99             };
100             #>>>
101             }
102              
103              
104 2     2 1 5 sub validate( $self, $data = undef ) {
  2         2  
  2         4  
  2         10  
105 2   33     8 $data //= $self->data;
106 2         7 return $schema->is_valid( $data );
107             }
108              
109              
110 2     2 1 1853 sub to_json($self) {
  2         5  
  2         3  
111 2         26 return $json->encode( $self->data );
112             }
113              
114              
115 9     9 0 114 sub BUILD( $self, $ ) {
  9         16  
  9         15  
116 9 50       26 $schema->is_valid( $self->data ) or die $schema->error;
117             }
118              
119              
120 9     9 0 568640 sub BUILDARGS( $, @args ) {
  9         33  
  9         40  
121              
122 9 50 66     54 if ( @args == 1 && !ref( $args[0] ) ) {
123 0         0 unshift @args, "template";
124             }
125              
126 9 100 66     71 my %args = ( @args == 1 && ref( $args[0] ) eq "HASH" ) ? $args[0]->%* : @args;
127              
128 9 100       36 if ( my $version = $args{version} ) {
129 4 50       15 croak "unsupported version '${version}'" if $version ne 1;
130             }
131              
132              
133 9 100       43 if ( my $template = delete $args{template} ) {
134              
135 3         68 my %templates = (
136              
137             no_automation => {
138             code_generation => "toolchain",
139             automated_contributions => "none",
140             automated_actions => "comment",
141             },
142              
143             issues_only => {
144             code_generation => "toolchain",
145             automated_contributions => "issue",
146             automated_actions => "issue",
147             },
148              
149             human_supervised => {
150             code_generation => "machine_generated",
151             automated_contributions => "code_request",
152             automated_actions => "code_request",
153             },
154              
155             data_driven_updates => {
156             code_generation => "external_sources",
157             automated_contributions => "issue",
158             automated_actions => "release",
159             },
160              
161             full_automation => {
162             code_generation => "code_generation",
163             automated_contributions => "code_request",
164             automated_actions => "release",
165             },
166              
167             );
168              
169 3 50       17 if ( my $match = $templates{$template} ) {
170              
171 3         13 for my $attr ( keys $match->%* ) {
172 9   66     53 $args{$attr} //= $match->{$attr};
173             }
174              
175             }
176             else {
177 0         0 croak "Unsupported template: '${template}'";
178             }
179              
180              
181             }
182              
183 9         251 return \%args;
184             }
185              
186              
187 4     4 1 2122 sub from_json( $class, @args ) {
  4         9  
  4         9  
  4         8  
188              
189              
190 4 100 66     25 if ( @args == 1 && !ref( $args[0] ) ) {
191 1         3 unshift @args, "json";
192             }
193              
194 4 50 33     23 my %args = ( @args == 1 && ref( $args[0] ) eq "HASH" ) ? $args[0]->%* : @args;
195              
196 4 50       18 croak "json is required" unless defined $args{json};
197              
198 4 100       70 $args{json} = $json->decode( $args{json} ) unless ref( $args{json} );
199              
200 4 50       24 if ( my $res = $schema->validate( $args{json} ) ) {
201 4         5628 return $class->new( $args{json} );
202             }
203             else {
204 0           croak $_ for $res->errors;
205             }
206              
207             }
208              
209             1;
210              
211             __END__