| 1 | package App::GitHooks::Plugin::PrependTicketID; | ||||||||||||
| 2 | |||||||||||||
| 3 | 8 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 8 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | |||||||||||||
| 6 | 8 | ▶ | use base 'App::GitHooks::Plugin'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | |||||||||||||
| 8 | # Internal dependencies. | ||||||||||||
| 9 | 8 | ▶ | use App::GitHooks::Constants qw( :PLUGIN_RETURN_CODES ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 8 | ▶ | use App::GitHooks::Utils; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 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<App::GitHooks::Plugin::RequireTicketID> 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 | 8 | 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<dev1293_my_new_feature>, the | ||||||||||||
| 67 | regular expression will identify C<dev1293> 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<dev1293_my_new_feature> and | ||||||||||||
| 78 | C<ga/dev1293_my_new_feature> will be identified as tied to C<dev1293> 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<project_prefixes> 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<extract_ticket_id_from_branch>. | ||||||||||||
| 93 | |||||||||||||
| 94 | [_] | ||||||||||||
| 95 | normalize_branch_ticket_id = s/^(.*?)(\d+)$/\U$1-$2/ | ||||||||||||
| 96 | |||||||||||||
| 97 | In the example above, C<dev1293_my_new_feature> gave C<dev1293>, which is then | ||||||||||||
| 98 | normalized as C<DEV-1293>. | ||||||||||||
| 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 | 7 | ▶ | { | ||||||||||
|
Subroutine
run_prepare_commit_msg: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||
| 124 | 7 | my ( $class, %args ) = @_; | |||||||||||
| 125 | 7 | my $app = delete( $args{'app'} ); | |||||||||||
| 126 | 7 | my $commit_message = delete( $args{'commit_message'} ); | |||||||||||
| 127 | 7 | 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 | my $branch_ticket_id = App::GitHooks::Utils::get_ticket_id_from_branch_name( $app ); | |||||||||||
| 132 | 7 | ▶ | if ( defined( $branch_ticket_id ) ) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 133 | { | ||||||||||||
| 134 | 5 | 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 | ▶ | if ( !defined( $ticket_id ) ) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 141 | { | ||||||||||||
| 142 | 3 | ▶ | my $commit_prefix_format = $app->get_config()->get_regex( 'PrependTicketID', 'commit_prefix_format' ) // '$ticket_id: '; | ||||||||||
|
Condition100%2/2
$app->get_config->get_regex("PrependTicketID", "commit_prefix_format") // "\$ticket_id: "
MC/DC100%1/1
$app->get_config->get_regex("PrependTicketID", "commit_prefix_format") // "\$ticket_id: "
$app->get_config->get_regex("PrependTicketID", "commit_prefix_format")
Truth table100%2/2
A
A= $app->get_config->get_regex("PrependTicketID", "commit_prefix_format")
| |||||||||||||
| 143 | 3 | $commit_prefix_format =~ s/\$ticket_id/$branch_ticket_id/g; | |||||||||||
| 144 | 3 | $commit_message->update_message( $commit_prefix_format . $commit_message->get_message() ); | |||||||||||
| 145 | } | ||||||||||||
| 146 | } | ||||||||||||
| 147 | |||||||||||||
| 148 | 7 | 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<https://github.com/guillaumeaubert/App-GitHooks-Plugin-PrependTicketID/issues/new>. | ||||||||||||
| 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<https://github.com/guillaumeaubert/App-GitHooks-Plugin-PrependTicketID/issues> | ||||||||||||
| 174 | |||||||||||||
| 175 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||
| 176 | |||||||||||||
| 177 | L<http://annocpan.org/dist/app-githooks-plugin-prependticketid> | ||||||||||||
| 178 | |||||||||||||
| 179 | =item * CPAN Ratings | ||||||||||||
| 180 | |||||||||||||
| 181 | L<http://cpanratings.perl.org/d/app-githooks-plugin-prependticketid> | ||||||||||||
| 182 | |||||||||||||
| 183 | =item * MetaCPAN | ||||||||||||
| 184 | |||||||||||||
| 185 | L<https://metacpan.org/release/App-GitHooks-Plugin-PrependTicketID> | ||||||||||||
| 186 | |||||||||||||
| 187 | =back | ||||||||||||
| 188 | |||||||||||||
| 189 | |||||||||||||
| 190 | =head1 AUTHOR | ||||||||||||
| 191 | |||||||||||||
| 192 | L<Guillaume Aubert|https://metacpan.org/author/AUBERTG>, | ||||||||||||
| 193 | C<< <aubertg at cpan.org> >>. | ||||||||||||
| 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 | 8 | 1; | |||||||||||