File Coverage

blib/lib/App/GitHooks/Plugin/PrependTicketID.pm
Criterion Covered Total %
statement 24 24 100.0
branch 4 4 100.0
condition n/a
subroutine 6 6 100.0
pod 1 1 100.0
total 35 35 100.0


line stmt bran cond sub pod time code
1             package App::GitHooks::Plugin::PrependTicketID;
2              
3 7     7   2144000 use strict;
  7         17  
  7         756  
4 7     7   36 use warnings;
  7         13  
  7         200  
5              
6 7     7   35 use base 'App::GitHooks::Plugin';
  7         17  
  7         1573  
7              
8             # Internal dependencies.
9 7     7   1351 use App::GitHooks::Constants qw( :PLUGIN_RETURN_CODES );
  7         317  
  7         1075  
10 7     7   1109 use App::GitHooks::Utils;
  7         3735  
  7         1488  
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.0.1
28              
29             =cut
30              
31             our $VERSION = '1.0.1';
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              
44             =head2 project_prefixes
45              
46             The list of valid ticket prefixes.
47              
48             project_prefixes = OPS, DEV, TEST
49              
50              
51             =head2 extract_ticket_id_from_branch
52              
53             A regular expression with one capturing group that will extract the ticket ID
54             from a branch name.
55              
56             extract_ticket_id_from_branch = /^($project_prefixes\d+)/
57              
58             In the example above, if a branch is named C, the
59             regular expression will identify C as the ticket ID corresponding to
60             that branch.
61              
62             Note that:
63              
64             =over 4
65              
66             =item *
67              
68             Prefixes used for private branches are recognized properly and ignored
69             accordingly. In other words, both C and
70             C will be identified as tied to C with the
71             regex above.
72              
73             =item *
74              
75             $project_prefixes is replaced at run time by the prefixes listed in the
76             C configuration option, to avoid duplication of information.
77              
78             =back
79              
80              
81             =head2 normalize_branch_ticket_id
82              
83             A replacement expression to normalize the ticket ID extracted with
84             C.
85              
86             normalize_branch_ticket_id = s/^(.*?)(\d+)$/\U$1-$2/
87              
88             In the example above, C gave C, which is then
89             normalized as C.
90              
91              
92             =head1 METHODS
93              
94             =head2 run_prepare_commit_msg()
95              
96             Code to execute as part of the prepare-commit-msg hook.
97              
98             my $success = App::GitHooks::Plugin::PrependTicketID->run_prepare_commit_msg();
99              
100             =cut
101              
102             sub run_prepare_commit_msg
103             {
104 6     6 1 40276 my ( $class, %args ) = @_;
105 6         21 my $app = delete( $args{'app'} );
106 6         16 my $commit_message = delete( $args{'commit_message'} );
107 6         34 my $repository = $app->get_repository();
108              
109             # If we're on a branch that includes a ticket ID, prepend it to the commit
110             # message.
111 6         899851 my $branch_ticket_id = App::GitHooks::Utils::get_ticket_id_from_branch_name( $app );
112 6 100       186984 if ( defined( $branch_ticket_id ) )
113             {
114 4         145 my $ticket_id = $commit_message->get_ticket_id();
115              
116             # Don't add the prefix again if a ticket ID is found in the commit message.
117             # This catches ticket IDs specified via git commit -m "..." and edits via
118             # git commit --amend.
119              
120 4 100       1243 $commit_message->update_message( $branch_ticket_id . ': ' . $commit_message->get_message() )
121             if !defined( $ticket_id );
122             }
123              
124 6         139 return $PLUGIN_RETURN_PASSED;
125             }
126              
127              
128             =head1 BUGS
129              
130             Please report any bugs or feature requests through the web interface at
131             L.
132             I will be notified, and then you'll automatically be notified of progress on
133             your bug as I make changes.
134              
135              
136             =head1 SUPPORT
137              
138             You can find documentation for this module with the perldoc command.
139              
140             perldoc App::GitHooks::Plugin::PrependTicketID
141              
142              
143             You can also look for information at:
144              
145             =over
146              
147             =item * GitHub's request tracker
148              
149             L
150              
151             =item * AnnoCPAN: Annotated CPAN documentation
152              
153             L
154              
155             =item * CPAN Ratings
156              
157             L
158              
159             =item * MetaCPAN
160              
161             L
162              
163             =back
164              
165              
166             =head1 AUTHOR
167              
168             L,
169             C<< >>.
170              
171              
172             =head1 COPYRIGHT & LICENSE
173              
174             Copyright 2013-2014 Guillaume Aubert.
175              
176             This program is free software: you can redistribute it and/or modify it under
177             the terms of the GNU General Public License version 3 as published by the Free
178             Software Foundation.
179              
180             This program is distributed in the hope that it will be useful, but WITHOUT ANY
181             WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
182             PARTICULAR PURPOSE. See the GNU General Public License for more details.
183              
184             You should have received a copy of the GNU General Public License along with
185             this program. If not, see http://www.gnu.org/licenses/
186              
187             =cut
188              
189             1;