| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package App::GitHooks::Plugin::ForceRegularUpdate; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 5 |  |  | 5 |  | 738872 | use strict; | 
|  | 5 |  |  |  |  | 7 |  | 
|  | 5 |  |  |  |  | 117 |  | 
| 4 | 5 |  |  | 5 |  | 15 | use warnings; | 
|  | 5 |  |  |  |  | 4 |  | 
|  | 5 |  |  |  |  | 101 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 5 |  |  | 5 |  | 16 | use base 'App::GitHooks::Plugin'; | 
|  | 5 |  |  |  |  | 6 |  | 
|  | 5 |  |  |  |  | 674 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  | # External dependencies. | 
| 9 | 5 |  |  | 5 |  | 305 | use Carp; | 
|  | 5 |  |  |  |  | 5 |  | 
|  | 5 |  |  |  |  | 186 |  | 
| 10 | 5 |  |  | 5 |  | 2300 | use File::Slurp (); | 
|  | 5 |  |  |  |  | 24347 |  | 
|  | 5 |  |  |  |  | 111 |  | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | # Internal dependencies. | 
| 13 | 5 |  |  | 5 |  | 461 | use App::GitHooks::Constants qw( :PLUGIN_RETURN_CODES ); | 
|  | 5 |  |  |  |  | 3433 |  | 
|  | 5 |  |  |  |  | 2736 |  | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | =head1 NAME | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | App::GitHooks::Plugin::ForceRegularUpdate - Force running a specific tool at regular intervals. | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | # TODO: description of how to write a tool that generates the timestamp file. | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  | =head1 VERSION | 
| 27 |  |  |  |  |  |  |  | 
| 28 |  |  |  |  |  |  | Version 1.0.5 | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | =cut | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | our $VERSION = '1.0.5'; | 
| 33 |  |  |  |  |  |  |  | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | =head1 CONFIGURATION OPTIONS | 
| 36 |  |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  | This plugin supports the following options in the C<[BlockProductionCommits]> | 
| 38 |  |  |  |  |  |  | section of your C<.githooksrc> file. | 
| 39 |  |  |  |  |  |  |  | 
| 40 |  |  |  |  |  |  | [BlockProductionCommits] | 
| 41 |  |  |  |  |  |  | max_update_age = 2 * 24 * 3600 # 2 days | 
| 42 |  |  |  |  |  |  | description = ./my_updater.sh | 
| 43 |  |  |  |  |  |  | env_variable = my_environment | 
| 44 |  |  |  |  |  |  | env_safe_regex = /^development$/ | 
| 45 |  |  |  |  |  |  | update_file = /var/local/.last_update.txt | 
| 46 |  |  |  |  |  |  |  | 
| 47 |  |  |  |  |  |  | =head2 max_update_age | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | This indicates the maximum amount of time that may have elapsed since the last | 
| 50 |  |  |  |  |  |  | update, before commits are blocked. | 
| 51 |  |  |  |  |  |  |  | 
| 52 |  |  |  |  |  |  | max_update_age = 2 * 24 * 3600 # 2 days | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  | Note that this configuration option supports comments at the end, for | 
| 55 |  |  |  |  |  |  | readability. | 
| 56 |  |  |  |  |  |  |  | 
| 57 |  |  |  |  |  |  |  | 
| 58 |  |  |  |  |  |  | =head2 description | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  | The name of the tool to run to perform an update that will reset the time | 
| 61 |  |  |  |  |  |  | counter. | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  | description = ./my_updater.sh | 
| 64 |  |  |  |  |  |  |  | 
| 65 |  |  |  |  |  |  |  | 
| 66 |  |  |  |  |  |  | =head2 env_variable | 
| 67 |  |  |  |  |  |  |  | 
| 68 |  |  |  |  |  |  | Optional, the name of the environment variable to use to determine the | 
| 69 |  |  |  |  |  |  | environment (production, staging, development, etc). | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  | env_variable = my_environment | 
| 72 |  |  |  |  |  |  |  | 
| 73 |  |  |  |  |  |  |  | 
| 74 |  |  |  |  |  |  | =head2 env_regex | 
| 75 |  |  |  |  |  |  |  | 
| 76 |  |  |  |  |  |  | Optional, but required if C is used. | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | A regular expression that indicates that this plugin should be run when it is | 
| 79 |  |  |  |  |  |  | matched. | 
| 80 |  |  |  |  |  |  |  | 
| 81 |  |  |  |  |  |  | env_safe_regex = /^development$/ | 
| 82 |  |  |  |  |  |  |  | 
| 83 |  |  |  |  |  |  | The example above only checks for regular updates when | 
| 84 |  |  |  |  |  |  | C<$ENV{'my_environment'} =~ /^development$/>. | 
| 85 |  |  |  |  |  |  |  | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  | =head2 update_file | 
| 88 |  |  |  |  |  |  |  | 
| 89 |  |  |  |  |  |  | The path to the file that stores the unix time of the last upgrade. This is the | 
| 90 |  |  |  |  |  |  | file your update tool should write the current unix time to upon successful | 
| 91 |  |  |  |  |  |  | completion. | 
| 92 |  |  |  |  |  |  |  | 
| 93 |  |  |  |  |  |  | update_file = /var/local/.last_update.txt | 
| 94 |  |  |  |  |  |  |  | 
| 95 |  |  |  |  |  |  | Note that you can use an absolute path, or a relative path. If relative, the | 
| 96 |  |  |  |  |  |  | path will be relative to the root of the current git repository. | 
| 97 |  |  |  |  |  |  |  | 
| 98 |  |  |  |  |  |  |  | 
| 99 |  |  |  |  |  |  | =head1 METHODS | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  | =head2 run_pre_commit() | 
| 102 |  |  |  |  |  |  |  | 
| 103 |  |  |  |  |  |  | Code to execute as part of the pre-commit hook. | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | my $success = App::GitHooks::Plugin::ForceRegularUpdate->run_pre_commit(); | 
| 106 |  |  |  |  |  |  |  | 
| 107 |  |  |  |  |  |  | =cut | 
| 108 |  |  |  |  |  |  |  | 
| 109 |  |  |  |  |  |  | sub run_pre_commit | 
| 110 |  |  |  |  |  |  | { | 
| 111 | 4 |  |  | 4 | 1 | 14909 | my ( $class, %args ) = @_; | 
| 112 | 4 |  |  |  |  | 7 | my $app = delete( $args{'app'} ); | 
| 113 | 4 |  |  |  |  | 16 | my $repository = $app->get_repository(); | 
| 114 | 4 |  |  |  |  | 136579 | my $config = $app->get_config(); | 
| 115 |  |  |  |  |  |  |  | 
| 116 |  |  |  |  |  |  | # Verify we have the max update age configured. | 
| 117 | 4 |  |  |  |  | 67 | my $max_update_age = $config->get( 'ForceRegularUpdate', 'max_update_age' ); | 
| 118 | 4 | 50 |  |  |  | 68 | croak "'max_update_age' must be defined in the [ForceRegularUpdate] section of your .githooksrc file" | 
| 119 |  |  |  |  |  |  | if !defined $max_update_age; | 
| 120 | 4 |  |  |  |  | 23 | $max_update_age =~ s/^\s+//; | 
| 121 | 4 |  |  |  |  | 83 | $max_update_age =~ s/\s*(?:#.*)$//; | 
| 122 | 4 | 50 |  |  |  | 27 | croak "'max_update_age' in the [ForceRegularUpdate] section must be an integer expressing seconds" | 
| 123 |  |  |  |  |  |  | if $max_update_age !~ /^\d+$/; | 
| 124 |  |  |  |  |  |  |  | 
| 125 |  |  |  |  |  |  | # Verify we have a description. | 
| 126 | 4 |  |  |  |  | 14 | my $description = $config->get( 'ForceRegularUpdate', 'description' ); | 
| 127 | 4 | 50 | 33 |  |  | 76 | croak "'description' must be defined in the [ForceRegularUpdate] section of your .githooksrc file" | 
| 128 |  |  |  |  |  |  | if !defined( $description ) || ( $description !~ /\w/ ); | 
| 129 |  |  |  |  |  |  |  | 
| 130 |  |  |  |  |  |  | # Check if we have environment restrictions. | 
| 131 | 4 |  |  |  |  | 15 | my $env_variable = $config->get( 'ForceRegularUpdate', 'env_variable' ); | 
| 132 | 4 |  |  |  |  | 48 | my $env_regex = $config->get_regex( 'ForceRegularUpdate', 'env_regex' ); | 
| 133 | 4 | 50 |  |  |  | 99 | if ( defined( $env_variable ) ) | 
| 134 |  |  |  |  |  |  | { | 
| 135 | 4 | 50 |  |  |  | 13 | croak "You defined an environment variable to check against, but not a regex to use, in the [ForceRegularUpdate] section" | 
| 136 |  |  |  |  |  |  | if !defined( $env_regex ); | 
| 137 |  |  |  |  |  |  |  | 
| 138 |  |  |  |  |  |  | return $PLUGIN_RETURN_SKIPPED | 
| 139 | 4 | 100 | 50 |  |  | 96 | if ( $ENV{ $env_variable } // '' ) !~ $env_regex; | 
| 140 |  |  |  |  |  |  | } | 
| 141 |  |  |  |  |  |  |  | 
| 142 |  |  |  |  |  |  | # Retrieve the file that specifies the time of last update. | 
| 143 | 3 |  |  |  |  | 10 | my $update_file = $config->get( 'ForceRegularUpdate', 'update_file' ); | 
| 144 | 3 | 50 |  |  |  | 26 | croak "'update_file' must be defined in the [ForceRegularUpdate] section of your .githooksrc file" | 
| 145 |  |  |  |  |  |  | if !defined( $update_file ); | 
| 146 | 3 |  |  |  |  | 7 | $update_file =~ s/\$ENV\{'([^']+)'\}/$ENV{$1}/xeg; | 
|  | 0 |  |  |  |  | 0 |  | 
| 147 | 3 |  |  |  |  | 4 | $update_file =~ s/\$ENV\{"([^"]+)"\}/$ENV{$1}/xeg; | 
|  | 0 |  |  |  |  | 0 |  | 
| 148 | 3 |  |  |  |  | 6 | $update_file =~ s/\$ENV\{([^\}]+)\}/$ENV{$1}/xeg; | 
|  | 0 |  |  |  |  | 0 |  | 
| 149 |  |  |  |  |  |  |  | 
| 150 |  |  |  |  |  |  | # Check if the update was ever performed. | 
| 151 | 3 |  |  |  |  | 14 | my $failure_character = $app->get_failure_character(); | 
| 152 | 3 | 100 |  |  |  | 118 | if ( ! -e $update_file ) | 
| 153 |  |  |  |  |  |  | { | 
| 154 | 1 |  |  |  |  | 9 | print $app->wrap( | 
| 155 |  |  |  |  |  |  | $app->color( 'red', "$failure_character It appears that you have never performed $description on this machine - please do that before committing.\n" ), | 
| 156 |  |  |  |  |  |  | "", | 
| 157 |  |  |  |  |  |  | ); | 
| 158 | 1 |  |  |  |  | 46 | return $PLUGIN_RETURN_FAILED; | 
| 159 |  |  |  |  |  |  | } | 
| 160 |  |  |  |  |  |  |  | 
| 161 |  |  |  |  |  |  | # Retrieve the value of the file and check whether it's within the bounds | 
| 162 |  |  |  |  |  |  | # allowed. | 
| 163 | 2 |  |  |  |  | 15 | my $last_update = File::Slurp::read_file( $update_file ); | 
| 164 | 2 |  |  |  |  | 136 | chomp( $last_update ); | 
| 165 | 2 | 100 | 33 |  |  | 47 | if ( !defined( $last_update )                       # Invalid format. | 
|  |  |  | 66 |  |  |  |  | 
|  |  |  | 66 |  |  |  |  | 
| 166 |  |  |  |  |  |  | || ( $last_update !~ /^\d+$/ )                 # Invalid format. | 
| 167 |  |  |  |  |  |  | || ( $last_update < time() - $max_update_age ) # Not updated in a long time. | 
| 168 |  |  |  |  |  |  | || ( $last_update > time() + 10 )              # Nice try setting the timestamp in the future to not have to update. | 
| 169 |  |  |  |  |  |  | ) | 
| 170 |  |  |  |  |  |  | { | 
| 171 | 1 |  |  |  |  | 8 | print $app->wrap( | 
| 172 |  |  |  |  |  |  | $app->color( | 
| 173 |  |  |  |  |  |  | 'red', | 
| 174 |  |  |  |  |  |  | "$failure_character It appears that you haven't performed $description on this machine for a long time. Please do that and try to commit again.\n" | 
| 175 |  |  |  |  |  |  | ), | 
| 176 |  |  |  |  |  |  | "", | 
| 177 |  |  |  |  |  |  | ); | 
| 178 | 1 |  |  |  |  | 45 | return $PLUGIN_RETURN_FAILED; | 
| 179 |  |  |  |  |  |  | } | 
| 180 |  |  |  |  |  |  |  | 
| 181 | 1 |  |  |  |  | 13 | return $PLUGIN_RETURN_PASSED; | 
| 182 |  |  |  |  |  |  | } | 
| 183 |  |  |  |  |  |  |  | 
| 184 |  |  |  |  |  |  |  | 
| 185 |  |  |  |  |  |  | =head1 BUGS | 
| 186 |  |  |  |  |  |  |  | 
| 187 |  |  |  |  |  |  | Please report any bugs or feature requests through the web interface at | 
| 188 |  |  |  |  |  |  | L. | 
| 189 |  |  |  |  |  |  | I will be notified, and then you'll automatically be notified of progress on | 
| 190 |  |  |  |  |  |  | your bug as I make changes. | 
| 191 |  |  |  |  |  |  |  | 
| 192 |  |  |  |  |  |  |  | 
| 193 |  |  |  |  |  |  | =head1 SUPPORT | 
| 194 |  |  |  |  |  |  |  | 
| 195 |  |  |  |  |  |  | You can find documentation for this module with the perldoc command. | 
| 196 |  |  |  |  |  |  |  | 
| 197 |  |  |  |  |  |  | perldoc App::GitHooks::Plugin::ForceRegularUpdate | 
| 198 |  |  |  |  |  |  |  | 
| 199 |  |  |  |  |  |  |  | 
| 200 |  |  |  |  |  |  | You can also look for information at: | 
| 201 |  |  |  |  |  |  |  | 
| 202 |  |  |  |  |  |  | =over | 
| 203 |  |  |  |  |  |  |  | 
| 204 |  |  |  |  |  |  | =item * GitHub's request tracker | 
| 205 |  |  |  |  |  |  |  | 
| 206 |  |  |  |  |  |  | L | 
| 207 |  |  |  |  |  |  |  | 
| 208 |  |  |  |  |  |  | =item * AnnoCPAN: Annotated CPAN documentation | 
| 209 |  |  |  |  |  |  |  | 
| 210 |  |  |  |  |  |  | L | 
| 211 |  |  |  |  |  |  |  | 
| 212 |  |  |  |  |  |  | =item * CPAN Ratings | 
| 213 |  |  |  |  |  |  |  | 
| 214 |  |  |  |  |  |  | L | 
| 215 |  |  |  |  |  |  |  | 
| 216 |  |  |  |  |  |  | =item * MetaCPAN | 
| 217 |  |  |  |  |  |  |  | 
| 218 |  |  |  |  |  |  | L | 
| 219 |  |  |  |  |  |  |  | 
| 220 |  |  |  |  |  |  | =back | 
| 221 |  |  |  |  |  |  |  | 
| 222 |  |  |  |  |  |  |  | 
| 223 |  |  |  |  |  |  | =head1 AUTHOR | 
| 224 |  |  |  |  |  |  |  | 
| 225 |  |  |  |  |  |  | L, | 
| 226 |  |  |  |  |  |  | C<<  >>. | 
| 227 |  |  |  |  |  |  |  | 
| 228 |  |  |  |  |  |  |  | 
| 229 |  |  |  |  |  |  | =head1 COPYRIGHT & LICENSE | 
| 230 |  |  |  |  |  |  |  | 
| 231 |  |  |  |  |  |  | Copyright 2013-2016 Guillaume Aubert. | 
| 232 |  |  |  |  |  |  |  | 
| 233 |  |  |  |  |  |  | This code is free software; you can redistribute it and/or modify it under the | 
| 234 |  |  |  |  |  |  | same terms as Perl 5 itself. | 
| 235 |  |  |  |  |  |  |  | 
| 236 |  |  |  |  |  |  | This program is distributed in the hope that it will be useful, but WITHOUT ANY | 
| 237 |  |  |  |  |  |  | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A | 
| 238 |  |  |  |  |  |  | PARTICULAR PURPOSE. See the LICENSE file for more details. | 
| 239 |  |  |  |  |  |  |  | 
| 240 |  |  |  |  |  |  | =cut | 
| 241 |  |  |  |  |  |  |  | 
| 242 |  |  |  |  |  |  | 1; |