File Coverage

blib/lib/App/GitHooks/Plugin/PrependTicketID.pm
Criterion Covered Total %
statement 27 27 100.0
branch 4 4 100.0
condition 2 2 100.0
subroutine 6 6 100.0
pod 1 1 100.0
total 40 40 100.0


line stmt bran cond sub pod time code
1             package App::GitHooks::Plugin::PrependTicketID;
2              
3 8     8   1636575 use strict;
  8         13  
  8         223  
4 8     8   32 use warnings;
  8         11  
  8         217  
5              
6 8     8   31 use base 'App::GitHooks::Plugin';
  8         16  
  8         1281  
7              
8             # Internal dependencies.
9 8     8   993 use App::GitHooks::Constants qw( :PLUGIN_RETURN_CODES );
  8         5844  
  8         1289  
10 8     8   707 use App::GitHooks::Utils;
  8         4040  
  8         1770  
11              
12              
13             =head1 NAME
14              
15             App::GitHooks::Plugin::PrependTicketID - Derive a ticket ID from the branch name and prepend it to the commit-message.
16              
17              
18             =head1 DESCRIPTION
19              
20             If you are using the C to force entering a ticket ID with each commit, it can become tedious if you need to do a lot of commits with the same ticket ID on a feature branch.
21              
22             To help with this, this plugin derives a ticket ID from the branch name and prepends it to the commit message.
23              
24              
25             =head1 VERSION
26              
27             Version 1.2.0
28              
29             =cut
30              
31             our $VERSION = '1.2.0';
32              
33              
34             =head1 CONFIGURATION OPTIONS
35              
36             This plugin supports the following options in the main section of your
37             C<.githooksrc> file:
38              
39             project_prefixes = DEV
40             extract_ticket_id_from_branch = /^($project_prefixes\d+)/
41             normalize_branch_ticket_id = s/^(.*?)(\d+)$/\U$1-$2/
42              
43             Additionally, the plugin supports the following option in the
44             C<[PrependTicketID]> section of your C<.githooksrc> file:
45              
46             [PrependTicketID]
47             commit_prefix_format = /$ticket_id: /
48              
49              
50             =head2 project_prefixes
51              
52             The list of valid ticket prefixes.
53              
54             [_]
55             project_prefixes = OPS, DEV, TEST
56              
57              
58             =head2 extract_ticket_id_from_branch
59              
60             A regular expression with one capturing group that will extract the ticket ID
61             from a branch name.
62              
63             [_]
64             extract_ticket_id_from_branch = /^($project_prefixes\d+)/
65              
66             In the example above, if a branch is named C, the
67             regular expression will identify C as the ticket ID corresponding to
68             that branch.
69              
70             Note that:
71              
72             =over 4
73              
74             =item *
75              
76             Prefixes used for private branches are recognized properly and ignored
77             accordingly. In other words, both C and
78             C will be identified as tied to C with the
79             regex above.
80              
81             =item *
82              
83             $project_prefixes is replaced at run time by the prefixes listed in the
84             C configuration option, to avoid duplication of information.
85              
86             =back
87              
88              
89             =head2 normalize_branch_ticket_id
90              
91             A replacement expression to normalize the ticket ID extracted with
92             C.
93              
94             [_]
95             normalize_branch_ticket_id = s/^(.*?)(\d+)$/\U$1-$2/
96              
97             In the example above, C gave C, which is then
98             normalized as C.
99              
100              
101             =head2 commit_prefix_format
102              
103             A regular expression that will be used to format the prefix of the commit message.
104              
105             By default, this plugin uses C<$ticket_id: >, but the following example would
106             turn it into C<($ticket_id) >:
107              
108             [PrependTicketID]
109             commit_prefix_format = /($ticket_id) /
110              
111              
112             =head1 METHODS
113              
114             =head2 run_prepare_commit_msg()
115              
116             Code to execute as part of the prepare-commit-msg hook.
117              
118             my $success = App::GitHooks::Plugin::PrependTicketID->run_prepare_commit_msg();
119              
120             =cut
121              
122             sub run_prepare_commit_msg
123             {
124 7     7 1 30048 my ( $class, %args ) = @_;
125 7         18 my $app = delete( $args{'app'} );
126 7         11 my $commit_message = delete( $args{'commit_message'} );
127 7         30 my $repository = $app->get_repository();
128              
129             # If we're on a branch that includes a ticket ID, prepend it to the commit
130             # message.
131 7         344871 my $branch_ticket_id = App::GitHooks::Utils::get_ticket_id_from_branch_name( $app );
132 7 100       88528 if ( defined( $branch_ticket_id ) )
133             {
134 5         78 my $ticket_id = $commit_message->get_ticket_id();
135              
136             # Don't add the prefix again if a ticket ID is found in the commit message.
137             # This catches ticket IDs specified via git commit -m "..." and edits via
138             # git commit --amend.
139              
140 5 100       888 if ( !defined( $ticket_id ) )
141             {
142 3   100     19 my $commit_prefix_format = $app->get_config()->get_regex( 'PrependTicketID', 'commit_prefix_format' ) // '$ticket_id: ';
143 3         121 $commit_prefix_format =~ s/\$ticket_id/$branch_ticket_id/g;
144 3         54 $commit_message->update_message( $commit_prefix_format . $commit_message->get_message() );
145             }
146             }
147              
148 7         102 return $PLUGIN_RETURN_PASSED;
149             }
150              
151              
152             =head1 BUGS
153              
154             Please report any bugs or feature requests through the web interface at
155             L.
156             I will be notified, and then you'll automatically be notified of progress on
157             your bug as I make changes.
158              
159              
160             =head1 SUPPORT
161              
162             You can find documentation for this module with the perldoc command.
163              
164             perldoc App::GitHooks::Plugin::PrependTicketID
165              
166              
167             You can also look for information at:
168              
169             =over
170              
171             =item * GitHub's request tracker
172              
173             L
174              
175             =item * AnnoCPAN: Annotated CPAN documentation
176              
177             L
178              
179             =item * CPAN Ratings
180              
181             L
182              
183             =item * MetaCPAN
184              
185             L
186              
187             =back
188              
189              
190             =head1 AUTHOR
191              
192             L,
193             C<< >>.
194              
195              
196             =head1 COPYRIGHT & LICENSE
197              
198             Copyright 2013-2017 Guillaume Aubert.
199              
200             This code is free software; you can redistribute it and/or modify it under the
201             same terms as Perl 5 itself.
202              
203             This program is distributed in the hope that it will be useful, but WITHOUT ANY
204             WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
205             PARTICULAR PURPOSE. See the LICENSE file for more details.
206              
207             =cut
208              
209             1;