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

filter: Statementstmt 100% 62 / 62 Branchbran 77.7% 14 / 18 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 8 / 8 Podpod 100% 2 / 2 totaltotal 93.6% 89 / 95 CC 13 SCAR 19.2
CC
13
Cov
94%
run_pre_push
11.1
get_pushed_branch_names
3.0
BEGIN
1.0
CRAP 6.8
↑ Summary
1 package App::GitHooks::Plugin::ForceBranchNamePattern;
2  
3 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 12 ▶use base 'App::GitHooks::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # External dependencies.
9 12 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # Internal dependencies.
12 12 ▶use App::GitHooks::Constants qw( :PLUGIN_RETURN_CODES );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 12 ▶use App::GitHooks::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # Uncomment to see debug information.
16 #use Log::Any::Adapter ('Stderr');
17  
18  
19 =head1 NAME
20  
21 App::GitHooks::Plugin::ForceBranchNamePattern - Require branch names to match a given pattern before they can be pushed to the origin.
22  
23  
24 =head1 DESCRIPTION
25  
26 For example, if you define in your .githooksrc file the following:
27  
28 [ForceBranchNamePattern]
29 branch_name_pattern = /^[a-zA-Z0-9]+$/
30  
31 Then a branch named C<TestBranch> can be pushed to the origin, but not one
32 named C<test_branch>.
33  
34 A practical use of this plugin is making Puppet environment out of git
35 branches, since Puppet environment names must be strictly alphanumeric.
36  
37  
38 =head1 VERSION
39  
40 Version 1.1.3
41  
42 =cut
43  
44 12 our $VERSION = '1.1.3';
45  
46  
47 =head1 MINIMUM GIT VERSION
48  
49 This plugin relies on the pre-push hook, which is only available as of git
50 v1.8.2.
51  
52  
53 =head1 CONFIGURATION OPTIONS
54  
55 This plugin supports the following options in your C<.githooksrc> file.
56  
57 project_prefixes = OPS, DEV
58
59 [ForceBranchNamePattern]
60 branch_name_pattern = /^[a-zA-Z0-9]+$/
61  
62  
63 =head2 project_prefixes
64  
65 Optional, a comma-separated list of project prefixes in case you want to use
66 them in the C<branch_name_pattern> regex.
67  
68 This setting must be added in the main section of your C<.githooksrc> file, as
69 it is used by multiple plugins.
70  
71 project_prefixes = OPS, DEV
72  
73  
74 =head2 branch_name_pattern
75  
76 A regular expression that will be used to check branch names before allowing
77 you to push them to the origin.
78  
79 This setting must be added in the C<[ForceBranchNamePattern]> section of your
80 C<.githooksrc> file.
81  
82 # Require alphanumeric branches only.
83 branch_name_pattern = /^[a-zA-Z0-9]+$/
84  
85 # Require branches to start with a JIRA ticket ID followed by an underscore.
86 branch_name_pattern = /^DEV-\d+_/
87  
88 # Require branches to start with a JIRA ticket ID followed by an underscore,
89 # but they can have an optional user prefix.
90 branch_name_pattern = /^(?:[^\/]+\/)?DEV-\d+_/
91  
92 # Re-use "project_prefixes" defined in the main section of the config.
93 branch_name_pattern = /^$project_prefixes-\d+_/
94  
95  
96 =head1 METHODS
97  
98 =head2 run_pre_push()
99  
100 Code to execute as part of the pre-push hook.
101  
102 my $plugin_return_code = App::GitHooks::Plugin::ForceBranchNamePattern->run_pre_push(
103 app => $app,
104 stdin => $stdin,
105 );
106  
107 Arguments:
108  
109 =over 4
110  
111 =item * $app I<(mandatory)>
112  
113 An C<App::GitHooks> object.
114  
115 =item * $stdin I<(mandatory)>
116  
117 The content provided by git on stdin, corresponding to a list of references
118 being pushed.
119  
120 =back
121  
122 =cut
123  
124 sub run_pre_push
125 11 ▶{
Subroutine run_pre_push: called CC=11 SCAR=24.0
Pod documented
126 11 my ( $class, %args ) = @_;
127 11 my $app = delete( $args{'app'} );
128 11 my $stdin = delete( $args{'stdin'} );
129  
130 11 $log->info( 'Entering ForceBranchNamePattern.' );
131  
132 11 my $config = $app->get_config();
133 11 my $repository = $app->get_repository();
134  
135 # Check if we have a branch name pattern specified in the config. If not,
136 # skip this plugin.
137 11 my $branch_name_pattern = $config->get_regex( 'ForceBranchNamePattern', 'branch_name_pattern' );
138 11 ▶ if ( !defined( $branch_name_pattern ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $branch_name_pattern) 1 10 11
139 {
140 1 $log->infof("No 'branch_name_pattern' specified in the [ForceBranchNamePattern] section of the config, skipping plugin.");
141 1 return $PLUGIN_RETURN_SKIPPED;
142 }
143  
144 # Insert valid project prefixes in the branch name verification regex.
145 10 ▶ if ( $branch_name_pattern =~ /\$project_prefixes/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($branch_name_pattern =~ /\$project_prefixes/) 3 7 10
146 {
147 # Make sure we have valid project prefixes defined in the config.
148 3 my $project_prefix_regex = App::GitHooks::Utils::get_project_prefix_regex( $app );
149 3 ▶ if ( !defined( $project_prefix_regex ) || ( $project_prefix_regex eq '' ) )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $project_prefix_regex or $project_prefix_regex eq "") 1 2 3
Condition66%2/3
not defined $project_prefix_regex or $project_prefix_regex eq ""
l!l&&r!l&&!r
012
MC/DC50%1/2
not defined $project_prefix_regex or $project_prefix_regex eq ""
not defined $project_prefix_regex $project_prefix_regex eq ""
Truth table66%2/3
not(A) or B
A= not defined $project_prefix_regexB= $project_prefix_regex eq ""
ABresult
1X1
011
000
150 {
151 1 my $error =
152 "No 'project_prefixes' values specified, but required in the pattern " .
153 "specified by 'branch_name_pattern' in the [ForceBranchNamePattern] " .
154 "section of the config. Please fix your .githooksrc config.";
155 1 $log->error( $error );
156 1 die "$error\n";
157 }
158 2 $branch_name_pattern =~ s/\$project_prefixes/$project_prefix_regex/g;
159 }
160  
161 # Check if we are pushing any branches.
162 9 my @branch_names = get_pushed_branch_names( $app, $stdin );
163 9 $log->infof(
164 "Found %s branch(es) to push: %s.",
165 scalar( @branch_names ),
166 join( ', ', @branch_names ),
167 );
168 9 ▶ return $PLUGIN_RETURN_SKIPPED
Branch: 1 branch
BranchTrueFalseTotal
if scalar @branch_names == 0 0 9 9
169 if ( scalar( @branch_names ) == 0 );
170  
171 # Check if the branch names match the pattern.
172 9 my @incorrect_branch_names = ();
173 9 foreach my $branch_name ( @branch_names )
174 {
175 9 ▶ if ( $branch_name =~ $branch_name_pattern )
Branch: 1 branch
BranchTrueFalseTotal
if ($branch_name =~ /$branch_name_pattern/) { } 5 4 9
176 {
177 5 $log->infof( 'Branch %s matches the required pattern.', $branch_name );
178 }
179 else
180 {
181 4 $log->infof( 'Branch %s does not match the required pattern.', $branch_name );
182 4 push( @incorrect_branch_names, $branch_name );
183 }
184 }
185  
186 9 ▶ if ( scalar( @incorrect_branch_names ) != 0 )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @incorrect_branch_names != 0) 4 5 9
187 {
188 4 ▶ my $error = sprintf(
Branch: 2 branches
BranchTrueFalseTotal
scalar @incorrect_branch_names == 1 ? : 4 0 4
scalar @incorrect_branch_names == 1 ? : 4 0 4
189 "The following %s %s not match the pattern enforced by the git hooks configuration file: %s.\n" .
190 "Branches must match the following pattern: %s.",
191 scalar( @incorrect_branch_names ) == 1 ? 'branch' : 'branches',
192 scalar( @incorrect_branch_names ) == 1 ? 'does' : 'do',
193 join( ', ', @incorrect_branch_names ),
194 "/$branch_name_pattern/",
195 );
196 4 $log->errorf( 'Reporting error back to the hook handler! %s', $error );
197 4 die "$error\n";
198 }
199  
200 5 return $PLUGIN_RETURN_PASSED;
201 }
202  
203  
204 =head1 FUNCTIONS
205  
206 =head2 get_pushed_branch_names()
207  
208 Retrieve a list of the branches being pushed with C<git push>.
209  
210 my $tags = App::GitHooks::Plugin::ForceBranchNamePattern::get_pushed_branch_names(
211 $app,
212 $stdin,
213 );
214  
215 Arguments:
216  
217 =over 4
218  
219 =item * $app I<(mandatory)>
220  
221 An C<App::GitHooks> object.
222  
223 =item * $stdin I<(mandatory)>
224  
225 The content provided by git on stdin, corresponding to a list of references
226 being pushed.
227  
228 =back
229  
230 =cut
231  
232 sub get_pushed_branch_names
233 9 ▶{
Subroutine get_pushed_branch_names: called CC=3 SCAR=11.0
Pod documented
234 9 my ( $app, $stdin ) = @_;
235 9 my $config = $app->get_config();
236  
237 # Analyze each reference being pushed.
238 9 my $branches = {};
239 9 foreach my $line ( @$stdin )
240 {
241 9 chomp( $line );
242 9 $log->debugf( 'Parse STDIN line >%s<.', $line );
243  
244 # Extract the branch information.
245 9 my ( $branch ) = ( $line =~ /^refs\/heads\/(\S+)\b/x );
246 9 ▶ next if !defined( $branch );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $branch 0 9 9
247 9 $log->infof( "Found branch '%s'.", $branch );
248 9 $branches->{ $branch } = 1;
249 }
250  
251 9 return keys %$branches;
252 }
253  
254  
255 =head1 BUGS
256  
257 Please report any bugs or feature requests through the web interface at
258 L<https://github.com/guillaumeaubert/App-GitHooks-Plugin-ForceBranchNamePattern/issues/new>.
259 I will be notified, and then you'll automatically be notified of progress on
260 your bug as I make changes.
261  
262  
263 =head1 SUPPORT
264  
265 You can find documentation for this module with the perldoc command.
266  
267 perldoc App::GitHooks::Plugin::ForceBranchNamePattern
268  
269  
270 You can also look for information at:
271  
272 =over
273  
274 =item * GitHub's request tracker
275  
276 L<https://github.com/guillaumeaubert/App-GitHooks-Plugin-ForceBranchNamePattern/issues>
277  
278 =item * AnnoCPAN: Annotated CPAN documentation
279  
280 L<http://annocpan.org/dist/app-githooks-plugin-forcebranchnamepattern>
281  
282 =item * CPAN Ratings
283  
284 L<http://cpanratings.perl.org/d/app-githooks-plugin-forcebranchnamepattern>
285  
286 =item * MetaCPAN
287  
288 L<https://metacpan.org/release/App-GitHooks-Plugin-ForceBranchNamePattern>
289  
290 =back
291  
292  
293 =head1 AUTHOR
294  
295 L<Guillaume Aubert|https://metacpan.org/author/AUBERTG>,
296 C<< <aubertg at cpan.org> >>.
297  
298  
299 =head1 COPYRIGHT & LICENSE
300  
301 Copyright 2015-2016 Guillaume Aubert.
302  
303 This code is free software; you can redistribute it and/or modify it under the
304 same terms as Perl 5 itself.
305  
306 This program is distributed in the hope that it will be useful, but WITHOUT ANY
307 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
308 PARTICULAR PURPOSE. See the LICENSE file for more details.
309  
310 =cut
311  
312 12 1;
↑ Summary