blib/lib/App/GitHooks/Plugin/ValidateChangelogFormat.pm

filter: Statementstmt 98.4% 62 / 63 Branchbran 81.8% 18 / 22 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 94.4% 17 / 18 Podpod 100% 3 / 3 totaltotal 90.9% 101 / 111 CC 14 SCAR 11.6
CC
14
Cov
92%
run_pre_commit_file
6.0
__ANON__
5.1
__ANON__
4.4
CRAP 3.2
↑ Summary
1 package App::GitHooks::Plugin::ValidateChangelogFormat;
2  
3 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 23 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 23 ▶use base 'App::GitHooks::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # Internal dependencies.
9 23 ▶use App::GitHooks::Constants qw( :PLUGIN_RETURN_CODES );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # External dependencies.
12 23 ▶use CPAN::Changes;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 23 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 23 ▶use version qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16  
17 =head1 NAME
18  
19 App::GitHooks::Plugin::ValidateChangelogFormat - Validate the format of changelog files.
20  
21  
22 =head1 DESCRIPTION
23  
24 This plugin verifies that the changes log conforms to the specifications
25 outlined in C<CPAN::Changes::Spec>.
26  
27  
28 =head1 VERSION
29  
30 Version 1.1.0
31  
32 =cut
33  
34 23 our $VERSION = '1.1.0';
35  
36  
37 =head1 CONFIGURATION OPTIONS
38  
39 This plugin supports the following options in the C<[ValidateChangelogFormat]>
40 section of your C<.githooksrc> file.
41  
42 [ValidateChangelogFormat]
43 version_format_regex = /^v\d+\.\d+\.\d+$/
44 date_format_regex = /^\d{4}-\d{2}-\d{2}$/
45  
46  
47 =head2 version_format_regex
48  
49 A regular expression that will be checked against the version number for each
50 release listed in the change log.
51  
52 version_format_regex = /^v\d+\.\d+\.\d+$/
53  
54 By default, this plugin allows the versioning schemes described in
55 L<CPAN::Meta::Spec/Version-Formats>.
56  
57  
58 =head2 date_format_regex
59  
60 A regular expression that will be checked against the date for each release
61 listed in the change log.
62  
63 date_format_regex = /^\d{4}-\d{2}-\d{2}$/
64  
65 By default, this plugin allows the date formats listed in
66 L<CPAN::Changes::Spec/Date>.
67  
68  
69 =head1 METHODS
70  
71 =head2 get_file_pattern()
72  
73 Return a pattern to filter the files this plugin should analyze.
74  
75 my $file_pattern = App::GitHooks::Plugin::ValidateChangelogFormat->get_file_pattern(
76 app => $app,
77 );
78  
79 By default, this catches files named changes or changelog, with an optional
80 extension of .md or .pod. The name of the files is not case sensitive.
81  
82 =cut
83  
84 sub get_file_pattern
85 21 ▶{
Subroutine get_file_pattern: called CC=1 SCAR=0.0
Pod documented
86 21 return qr/^(?:changes|changelog)(?:\.(?:md|pod))?$/ix;
87 }
88  
89  
90 =head2 get_file_check_description()
91  
92 Return a description of the check performed on files by the plugin and that
93 will be displayed to the user, if applicable, along with an indication of the
94 success or failure of the plugin.
95  
96 my $description = App::GitHooks::Plugin::ValidateChangelogFormat->get_file_check_description();
97  
98 =cut
99  
100 sub get_file_check_description
101 21 ▶{
Subroutine get_file_check_description: called CC=1 SCAR=0.0
Pod documented
102 21 return 'The changelog format matches CPAN::Changes::Spec.';
103 }
104  
105 =head2 run_pre_commit_file()
106  
107 Code to execute for each file as part of the pre-commit hook.
108  
109 my $success = App::GitHooks::Plugin::ValidateChangelogFormat->run_pre_commit_file();
110  
111 The code in this subroutine is mostly adapted from L<Test::CPAN::Changes>.
112  
113 =cut
114  
115 sub run_pre_commit_file
116 10 ▶{
Subroutine run_pre_commit_file: called CC=6 SCAR=18.0
Pod documented
117 10 my ( $class, %args ) = @_;
118 10 my $file = delete( $args{'file'} );
119 10 my $git_action = delete( $args{'git_action'} );
120 10 my $app = delete( $args{'app'} );
121 10 my $repository = $app->get_repository();
122 10 my $config = $app->get_config();
123  
124 # Ignore deleted files.
125 10 ▶ return $PLUGIN_RETURN_SKIPPED
Branch: 1 branch
BranchTrueFalseTotal
if $git_action eq "D" 0 10 10
126 if $git_action eq 'D';
127  
128 # Determine the required date format.
129 10 my $date_format_regex = $config->get_regex( 'ValidateChangelogFormat', 'date_format_regex' );
130 10 ▶ $date_format_regex = defined( $date_format_regex )
Branch: 1 branch
BranchTrueFalseTotal
defined $date_format_regex ? : 2 8 10
131 ? qr/^$date_format_regex$/
132 : qr/^(?:${CPAN::Changes::W3CDTF_REGEX}|${CPAN::Changes::UNKNOWN_VALS})$/x;
133  
134 # Determine the required version format.
135 10 my $version_format_regex = $config->get_regex( 'ValidateChangelogFormat', 'version_format_regex' );
136  
137 # Parse the changelog file.
138 my $changes =
139 try
140 10 ▶ {
Subroutine __ANON__: called CC=1 SCAR=0.0
141 10 return CPAN::Changes->load( $repository->work_tree() . '/' . $file );
142 }
143 catch
144 0 ▶ {
Subroutine __ANON__: not called CC=1 SCAR=6.9
145 0 die "Unable to parse the change log\n";
146 10 };
147  
148 # Verify that the changelog contains releases.
149 10 my @releases = $changes->releases();
150 10 ▶ die "The change log does not contain any releases\n"
Branch: 1 branch
BranchTrueFalseTotal
if scalar @releases == 0 5 5 10
151 if scalar( @releases ) == 0;
152  
153 5 my @errors = ();
154 5 my $count = 0;
155 5 foreach my $release ( @releases )
156 {
157 # Prefix to identify which release has issues in the error messages.
158 5 $count++;
159 5 my $error_prefix = sprintf(
160 "Release %s/%s",
161 $count,
162 scalar( @releases ),
163 );
164  
165 # Validate the release date.
166 try
167 5 ▶ {
Subroutine __ANON__: called CC=4 SCAR=14.9
168 5 my $date = $release->date();
169  
170 5 ▶ die "the release date is missing.\n"
Branch: 1 branch
BranchTrueFalseTotal
if not defined $date or $date eq "" 0 5 5
Condition33%1/3
not defined $date or $date eq ""
l!l&&r!l&&!r
005
MC/DC0%0/2
not defined $date or $date eq ""
not defined $date $date eq ""
Truth table33%1/3
not(A) or B
A= not defined $dateB= $date eq ""
ABresult
1X1
011
000
171 if !defined( $date ) || ( $date eq '' );
172  
173 5 ▶ die "date '$date' is not in the recommended format.\n"
Branch: 1 branch
BranchTrueFalseTotal
if not $date =~ /$date_format_regex/ 1 4 5
174 if $date !~ $date_format_regex;
175 }
176 catch
177 1 ▶ {
Subroutine __ANON__: called CC=1 SCAR=0.0
178 1 push( @errors, "$error_prefix: $_" );
179 5 };
180  
181 # Validate the release version.
182 try
183 5 ▶ {
Subroutine __ANON__: called CC=5 SCAR=16.3
184 5 my $version = $release->version();
185  
186 5 ▶ die "the version number is missing.\n"
Branch: 1 branch
BranchTrueFalseTotal
if $version eq "" 0 5 5
187 if $version eq '';
188  
189 5 ▶ if ( defined( $version_format_regex ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $version_format_regex) { } 2 3 5
190 {
191 2 ▶ die "version '$version' is not a valid version number.\n"
Branch: 1 branch
BranchTrueFalseTotal
if not $version =~ qr/^$version_format_regex$/x 1 1 2
192 if $version !~ qr/^$version_format_regex$/x;
193 }
194 else
195 {
196 3 ▶ die "version '$version' is not a valid version number.\n"
Branch: 1 branch
BranchTrueFalseTotal
if not version::is_lax($version) 0 3 3
197 if !version::is_lax($version);
198 }
199 }
200 catch
201 1 ▶ {
Subroutine __ANON__: called CC=1 SCAR=0.0
202 1 push( @errors, "$error_prefix: $_" );
203 5 };
204  
205 # Verify that a list of changes is present.
206 try
207 5 ▶ {
Subroutine __ANON__: called CC=2 SCAR=6.9
208 5 my $description = $release->changes();
209  
210 5 ▶ die "the release does not contain a description of changes.\n"
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %$description == 0 1 4 5
211 if scalar( keys %$description ) == 0;
212 }
213 catch
214 1 ▶ {
Subroutine __ANON__: called CC=1 SCAR=0.0
215 1 push( @errors, "$error_prefix: $_" );
216 5 };
217 }
218  
219 # Raise an exception with all the errors found, if any.
220 5 ▶ die join( '', @errors ) . "\n"
Branch: 1 branch
BranchTrueFalseTotal
if scalar @errors != 0 3 2 5
221 if scalar( @errors ) != 0;
222  
223 2 return $PLUGIN_RETURN_PASSED;
224 }
225  
226  
227 =head1 SEE ALSO
228  
229 =over 4
230  
231 =item * L<Test::CPAN::Changes>
232  
233 =item * L<CPAN::Changes::Spec>
234  
235 =item * L<CPAN::Changes>
236  
237 =back
238  
239  
240 =head1 BUGS
241  
242 Please report any bugs or feature requests through the web interface at
243 L<https://github.com/guillaumeaubert/App-GitHooks-Plugin-ValidateChangelogFormat/issues/new>.
244 I will be notified, and then you'll automatically be notified of progress on
245 your bug as I make changes.
246  
247  
248 =head1 SUPPORT
249  
250 You can find documentation for this module with the perldoc command.
251  
252 perldoc App::GitHooks::Plugin::ValidateChangelogFormat
253  
254  
255 You can also look for information at:
256  
257 =over
258  
259 =item * GitHub's request tracker
260  
261 L<https://github.com/guillaumeaubert/App-GitHooks-Plugin-ValidateChangelogFormat/issues>
262  
263 =item * AnnoCPAN: Annotated CPAN documentation
264  
265 l<http://annocpan.org/dist/app-githooks-plugin-validatechangelogformat>
266  
267 =item * CPAN Ratings
268  
269 L<http://cpanratings.perl.org/d/app-githooks-plugin-validatechangelogformat>
270  
271 =item * MetaCPAN
272  
273 L<https://metacpan.org/release/App-GitHooks-Plugin-ValidateChangelogFormat>
274  
275 =back
276  
277  
278 =head1 AUTHOR
279  
280 L<Guillaume Aubert|https://metacpan.org/author/AUBERTG>,
281 C<< <aubertg at cpan.org> >>.
282  
283  
284 =head1 COPYRIGHT & LICENSE
285  
286 Copyright 2015-2017 Guillaume Aubert.
287  
288 This code is free software; you can redistribute it and/or modify it under the
289 same terms as Perl 5 itself.
290  
291 This program is distributed in the hope that it will be useful, but WITHOUT ANY
292 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
293 PARTICULAR PURPOSE. See the LICENSE file for more details.
294  
295 =cut
296  
297 23 1;
↑ Summary