| 1 | # no critic (ValuesAndExpressions::RequireInterpolationOfMetachars) | ||
| 2 | ## no critic (InputOutput::RequireCheckedSyscalls) | ||
| 3 | # | ||
| 4 | |||
| 5 | package Git::MoreHooks::TriggerJenkins; | ||
| 6 | |||
| 7 | use strict; | ||
| 8 | use warnings; | ||
| 9 | use utf8; | ||
| 10 | |||
| 11 | # ABSTRACT: Git::Hooks plugin to create and remove jobs from Jenkins | ||
| 12 | |||
| 13 | our $VERSION = '0.021'; | ||
| 14 | |||
| 15 | |||
| 16 | |||
| 17 | use Git::Hooks; | ||
| 18 | use Module::Load qw{ load }; | ||
| 19 | use Path::Tiny; | ||
| 20 | use Log::Any qw{$log}; | ||
| 21 | use Carp; | ||
| 22 | use Const::Fast; | ||
| 23 | |||
| 24 | my $PKG = __PACKAGE__; | ||
| 25 | ( my $CFG = __PACKAGE__ ) =~ s/.*::/githooks./msx; | ||
| 26 | |||
| 27 | const my $LAST_CHAR_IN_STRING => -1; | ||
| 28 | const my $EMPTY_STRING => q{}; | ||
| 29 | const my $SPACE => q{ }; | ||
| 30 | |||
| 31 | |||
| 32 | # Hook configuration. | ||
| 33 | sub setup_config { | ||
| 34 | my ($git) = @_; | ||
| 35 | |||
| 36 | my $config = $git->get_config(); | ||
| 37 | $log->debugf( "setup_config(): Current Git config:\n%s.", $config ); | ||
| 38 | $config->{ lc $CFG } //= {}; | ||
| 39 | my $default = $config->{ lc $CFG }; | ||
| 40 | |||
| 41 | # Default values given as an array. | ||
| 42 | # Array is default interpretation of config. | ||
| 43 | $default->{'create-new'} //= ['1']; | ||
| 44 | $default->{'quiet'} //= ['0']; | ||
| 45 | $default->{'force'} //= ['0']; | ||
| 46 | $default->{'job-template'} //= ['-unit-test JenkinsJobTemplate.tt2']; | ||
| 47 | $default->{'job-template-var'} //= []; | ||
| 48 | |||
| 49 | # TODO Fix create-new -> create-job or maybe opposite! | ||
| 50 | # TODO Check valid values! | ||
| 51 | return; | ||
| 52 | } | ||
| 53 | |||
| 54 | sub configure_a_new_job { | ||
| 55 | my ( $git, $tpl_filename, %template_vars ) = @_; | ||
| 56 | # if ( !eval { require Template; } ) { | ||
| 57 | if ( !eval { load 'Template'; 1; } ) { | ||
| 58 | $git->error( $PKG, 'Install Module Template (package Template-Toolkit)' . ' to use this plugin!' ); | ||
| 59 | return; | ||
| 60 | } | ||
| 61 | my $config = { | ||
| 62 | 'INCLUDE_PATH' => Path::Tiny::path(q{.})->realpath, | ||
| 63 | 'ENCODING' => 'utf8', | ||
| 64 | 'INTERPOLATE' => 0, | ||
| 65 | 'ANYCASE' => 0, | ||
| 66 | 'ABSOLUTE' => 1, | ||
| 67 | 'RELATIVE' => 1, | ||
| 68 | }; | ||
| 69 | my $template = Template->new($config); | ||
| 70 | my $xml_ready; | ||
| 71 | $template->process( $tpl_filename, \%template_vars, \$xml_ready, ) || $git->error( $PKG, $template->error() ); | ||
| 72 | return $xml_ready; | ||
| 73 | } | ||
| 74 | |||
| 75 | sub trigger_branch { | ||
| 76 | my ( $git, $ref ) = @_; | ||
| 77 | $log->tracef( 'Entering trigger_branch(%s)', $ref ); | ||
| 78 | |||
| 79 | my $cache = $git->cache($PKG); | ||
| 80 | my $jenkins = $cache->{'jenkins'}; | ||
| 81 | if ( !defined $jenkins ) { | ||
| 82 | croak('Internal error: No Jenkins in Git::Hooks cache!'); | ||
| 83 | } | ||
| 84 | my $user = $git->authenticated_user() // '[undefined.user]'; | ||
| 85 | |||
| 86 | my %job_names; | ||
| 87 | if ( $git->get_config( $CFG => 'project' ) ) { | ||
| 88 | $job_names{ $git->get_config( $CFG => 'project' ) } = 'dummy-filename'; | ||
| 89 | } else { | ||
| 90 | %job_names = job_names_and_template_filenames( $git, $ref ); | ||
| 91 | } | ||
| 92 | |||
| 93 | # If project not exists, create it. | ||
| 94 | foreach my $job_name ( keys %job_names ) { | ||
| 95 | $log->debugf( 'Handle job (name:%s)', $job_name ); | ||
| 96 | |||
| 97 | my $this_job = get_job_from_jenkins( $git, $job_name ); | ||
| 98 | my %job_info = ( | ||
| 99 | 'description' => $job_name, | ||
| 100 | 'branch' => $ref, | ||
| 101 | ); | ||
| 102 | if ( defined $git->get_config( $CFG => 'email-domain' ) ) { | ||
| 103 | $job_info{'recipients'} = $user . q{@} . $git->get_config( $CFG => 'email-domain' ); | ||
| 104 | } | ||
| 105 | if ( defined $git->get_config( $CFG => 'override-message-address' ) ) { | ||
| 106 | my $overriding_addresses = $EMPTY_STRING; | ||
| 107 | my @overrides = $git->get_config( $CFG => 'override-message-address' ); | ||
| 108 | foreach my $override (@overrides) { | ||
| 109 | my ( $refspec, $address ) = split q{ }, $override, 2; | ||
| 110 | if ( $ref =~ m/$refspec/msx ) { | ||
| 111 | $overriding_addresses .= length $overriding_addresses > 0 ? $SPACE : $EMPTY_STRING; | ||
| 112 | $overriding_addresses .= $address; | ||
| 113 | } | ||
| 114 | } | ||
| 115 | |||
| 116 | # Override only if matching branch name was found! | ||
| 117 | if ( exists $job_info{'recipients'} && $overriding_addresses ) { | ||
| 118 | $job_info{'recipients'} = $overriding_addresses; | ||
| 119 | } | ||
| 120 | } | ||
| 121 | foreach my $var ( $git->get_config( $CFG => 'job-template-var' ) ) { | ||
| 122 | my ( $var_name, $var_value ) = split qr/=/msx, $var, 2; | ||
| 123 | $job_info{$var_name} = $var_value; | ||
| 124 | } | ||
| 125 | my $xml_conf = configure_a_new_job( $git, $job_names{$job_name}, %job_info ); | ||
| 126 | $log->debugf( 'New job: %s', $xml_conf ); | ||
| 127 | $this_job = $jenkins->create_job( $job_name, $xml_conf ); | ||
| 128 | $log->debugf( 'Created job: %s', $this_job ); | ||
| 129 | |||
| 130 | # Trigger build | ||
| 131 | my $triggered = $jenkins->trigger_build($job_name); | ||
| 132 | if ( !defined $triggered ) { | ||
| 133 | $git->errorf( $PKG, 'Failed to trigger job \'%s\'.', $job_name ); | ||
| 134 | if ( $git->get_config( $CFG => 'quiet' ) eq '0' ) { | ||
| 135 | print "Failed to trigger job '$job_name'.\n"; | ||
| 136 | } | ||
| 137 | return; | ||
| 138 | } | ||
| 139 | else { | ||
| 140 | if ( $git->get_config( $CFG => 'quiet' ) eq '0' ) { | ||
| 141 | print "Job '$job_name' triggered to build.\n"; | ||
| 142 | } | ||
| 143 | } | ||
| 144 | $log->debugf('All jobs triggered to Jenkins.'); | ||
| 145 | |||
| 146 | # Verify and tell user the status | ||
| 147 | my $build_queue = $jenkins->build_queue(); | ||
| 148 | my $project_url; | ||
| 149 | my $why; | ||
| 150 | |||
| 151 | # TODO Write this to handle all jobs triggered by me! | ||
| 152 | foreach my $item ( @{ $build_queue->{'items'} } ) { | ||
| 153 | if ( $item->{'task'}->{'name'} eq $job_name ) { | ||
| 154 | $project_url = $item->{'task'}->{'url'}; | ||
| 155 | $why = $item->{'why'}; | ||
| 156 | last; | ||
| 157 | } | ||
| 158 | } | ||
| 159 | if ( defined $project_url ) { | ||
| 160 | print "URL: $project_url\n"; | ||
| 161 | } | ||
| 162 | if ( defined $why ) { | ||
| 163 | print "Status: $why\n"; | ||
| 164 | } | ||
| 165 | } | ||
| 166 | $log->tracef( 'Exiting trigger_branch():%s', '1' ); | ||
| 167 | return 1; | ||
| 168 | } | ||
| 169 | |||
| 170 | sub match_regexp_config_item { | ||
| 171 | my ( $git, $item_name, $match ) = @_; | ||
| 172 | my $item = $git->get_config( $CFG => $item_name ); | ||
| 173 | if ( $item =~ m/^!/msx ) { | ||
| 174 | $log->debugf('Regexp starts with \'!\'. Match with negation!'); | ||
| 175 | $item = substr $item, 1; # All string except first character. | ||
| 176 | return $match !~ m/$item/msx; | ||
| 177 | } | ||
| 178 | else { | ||
| 179 | return $match =~ m/$item/msx; | ||
| 180 | } | ||
| 181 | } | ||
| 182 | |||
| 183 | sub delete_job { | ||
| 184 | my ( $git, $ref ) = @_; | ||
| 185 | $log->tracef( 'Entering delete_job(%s)', $ref ); | ||
| 186 | |||
| 187 | my $cache = $git->cache($PKG); | ||
| 188 | my $jenkins = $cache->{'jenkins'}; | ||
| 189 | if ( !defined $jenkins ) { | ||
| 190 | croak('Internal error: No Jenkins in Git::Hooks cache!'); | ||
| 191 | } | ||
| 192 | my %job_names = job_names_and_template_filenames( $git, $ref ); | ||
| 193 | foreach my $job_name ( keys %job_names ) { | ||
| 194 | my $this_job = get_job_from_jenkins( $git, $job_name ); | ||
| 195 | if ( defined $this_job ) { | ||
| 196 | |||
| 197 | # Delete the job | ||
| 198 | my $deleted = $jenkins->delete_project($job_name); | ||
| 199 | if ( !defined $deleted ) { | ||
| 200 | $git->error( $PKG, "Failed to delete job '$job_name'!" ); | ||
| 201 | return; | ||
| 202 | } | ||
| 203 | if ( $git->get_config( $CFG => 'quiet' ) eq '0' ) { | ||
| 204 | print "Job '$job_name' deleted from Jenkins.\n"; | ||
| 205 | } | ||
| 206 | } | ||
| 207 | else { | ||
| 208 | if ( $git->get_config( $CFG => 'quiet' ) eq '0' ) { | ||
| 209 | print "Job '$job_name' not in Jenkins. Not deleted.\n"; | ||
| 210 | } | ||
| 211 | } | ||
| 212 | } | ||
| 213 | $log->tracef( 'Exiting delete_job():%s', '1' ); | ||
| 214 | return 1; | ||
| 215 | } | ||
| 216 | |||
| 217 | sub job_names_and_template_filenames { | ||
| 218 | my ( $git, $ref ) = @_; | ||
| 219 | $log->tracef( 'Entering job_names_and_template_filenames(%s)', $ref ); | ||
| 220 | my ($branch) = $ref =~ m/^[^\/]+\/[^\/]+\/([[:graph:]]+)$/msx; | ||
| 221 | ( my $job_name = $branch ) =~ s/\//-/msx; # Replace slash-char with dash-char. | ||
| 222 | ## no critic (ValuesAndExpressions::ProhibitCommaSeparatedStatements) | ||
| 223 | my %job_names = map { $job_name . ( split q{ } )[0], ( split q{ } )[1] } $git->get_config( $CFG => 'job-template' ); | ||
| 224 | ## use critic (ValuesAndExpressions::ProhibitCommaSeparatedStatements) | ||
| 225 | $log->tracef( 'Exiting job_names_and_template_filenames():%s', \%job_names ); | ||
| 226 | return %job_names; | ||
| 227 | } | ||
| 228 | |||
| 229 | # Get the job/project from Jenkins if it exists. | ||
| 230 | sub get_job_from_jenkins { | ||
| 231 | my ( $git, $job_name ) = @_; | ||
| 232 | my $cache = $git->cache($PKG); | ||
| 233 | my $jenkins = $cache->{'jenkins'}; | ||
| 234 | if ( !defined $jenkins ) { | ||
| 235 | croak('Internal error: No Jenkins in Git::Hooks cache!'); | ||
| 236 | } | ||
| 237 | my $jobs = $jenkins->current_status( | ||
| 238 | { | ||
| 239 | 'extra_params' => { | ||
| 240 | 'tree' => 'jobs[name,color]' | ||
| 241 | } | ||
| 242 | } | ||
| 243 | ); | ||
| 244 | my $this_job; | ||
| 245 | foreach my $job ( @{ $jobs->{'jobs'} } ) { | ||
| 246 | if ( $job->{'name'} eq $job_name ) { | ||
| 247 | $this_job = $job; | ||
| 248 | last; | ||
| 249 | } | ||
| 250 | } | ||
| 251 | return $this_job; | ||
| 252 | } | ||
| 253 | |||
| 254 | # This routine can act as a post-receive hook. | ||
| 255 | sub handle_affected_refs { | ||
| 256 | my ($git) = @_; | ||
| 257 | |||
| 258 | setup_config($git); | ||
| 259 | |||
| 260 | # Connect to Jenkins if not already connected. Check from cache. | ||
| 261 | my $cache = $git->cache($PKG); | ||
| 262 | if ( !exists $cache->{'jenkins'} ) { | ||
| 263 | if ( !eval { load 'Jenkins::API'; } ) { | ||
| 264 | $git->error( $PKG, 'Install Module Jenkins::API to use this plugin!' ); | ||
| 265 | return; | ||
| 266 | } | ||
| 267 | my %jenkins_config; | ||
| 268 | for my $option (qw/base-url api-key api-token/) { | ||
| 269 | $jenkins_config{$option} = $git->get_config( $CFG => $option ) | ||
| 270 | or $git->error( $PKG, "missing $CFG.$option configuration attribute" ) | ||
| 271 | and return; | ||
| 272 | } | ||
| 273 | $jenkins_config{'base-url'} =~ s/[\/]{1,}$//msx; # trim trailing slashes from the URL | ||
| 274 | my $jenkins = Jenkins::API->new( | ||
| 275 | { | ||
| 276 | 'base_url' => $jenkins_config{'base-url'}, | ||
| 277 | 'api_key' => $jenkins_config{'api-key'}, | ||
| 278 | 'api_pass' => $jenkins_config{'api-token'}, | ||
| 279 | } | ||
| 280 | ); | ||
| 281 | my $jenkins_version = $jenkins->check_jenkins_url(); | ||
| 282 | if ( !$jenkins_version ) { | ||
| 283 | $git->error( $PKG, 'Not able to connect Jenkins at address' . " '$jenkins_config{'base-url'}'!" ); | ||
| 284 | return; | ||
| 285 | } | ||
| 286 | else { | ||
| 287 | if ( $git->get_config( $CFG => 'quiet' ) eq '0' ) { | ||
| 288 | print "Jenkins version: $jenkins_version\n"; | ||
| 289 | } | ||
| 290 | } | ||
| 291 | |||
| 292 | # Set Jenkins in the cache. | ||
| 293 | $cache->{'jenkins'} = $jenkins; | ||
| 294 | } | ||
| 295 | |||
| 296 | foreach my $ref ( $git->get_affected_refs() ) { | ||
| 297 | if ( ! $git->is_ref_enabled( $ref, $git->get_config( $CFG => 'ref' ) ) ) { | ||
| 298 | if ( $git->get_config( $CFG => 'quiet' ) eq '0' ) { | ||
| 299 | print "Ref '$ref' not enabled.\n"; | ||
| 300 | } | ||
| 301 | next; | ||
| 302 | } | ||
| 303 | |||
| 304 | my $new_commit = ( $git->get_affected_ref_range($ref) )[1]; | ||
| 305 | if ( $new_commit eq $git->undef_commit() ) { # Just zeros, deleted branch. | ||
| 306 | delete_job( $git, $ref ); | ||
| 307 | } | ||
| 308 | else { | ||
| 309 | my @commits = $git->get_affected_ref_commits($ref); | ||
| 310 | $log->debugf( 'commit_ids (from get_affected_ref_commit_ids(%s):%s', $ref, | ||
| 311 | [map {$_->commit()} @commits] ); | ||
| 312 | my $nr_msg_ok_to_trigger = 0; | ||
| 313 | foreach my $commit (@commits) { | ||
| 314 | my $commit_msg = $commit->message(); | ||
| 315 | $log->debugf( 'commit message: \'%s\'.', ( substr $commit_msg, 0, $LAST_CHAR_IN_STRING ) ); | ||
| 316 | if ( !$git->get_config( $CFG => 'allow-commit-msg' ) ) { | ||
| 317 | $log->debugf('No option allow-commit-msg. Allow all.'); | ||
| 318 | $nr_msg_ok_to_trigger++; | ||
| 319 | |||
| 320 | # TODO Shouldn't we have 'last' here? | ||
| 321 | } | ||
| 322 | if ( match_regexp_config_item( $git, 'allow-commit-msg', $commit_msg ) ) { | ||
| 323 | $log->debugf('Allowed commit msg.'); | ||
| 324 | $nr_msg_ok_to_trigger++; | ||
| 325 | |||
| 326 | # TODO Shouldn't we have 'next' here? | ||
| 327 | } | ||
| 328 | } | ||
| 329 | $log->debugf( 'nr_msg_ok_to_trigger: %s.', $nr_msg_ok_to_trigger ); | ||
| 330 | if ( $nr_msg_ok_to_trigger > 0 ) { | ||
| 331 | trigger_branch( $git, $ref ); | ||
| 332 | } | ||
| 333 | } | ||
| 334 | } | ||
| 335 | |||
| 336 | # Disconnect from Jenkins | ||
| 337 | $git->clean_cache($PKG); | ||
| 338 | |||
| 339 | return 1; | ||
| 340 | } | ||
| 341 | |||
| 342 | sub handle_commit_at_client { | ||
| 343 | my ($git) = @_; | ||
| 344 | |||
| 345 | my $current_branch = $git->get_current_branch(); | ||
| 346 | if ( ! $git->is_ref_enabled( $current_branch, $git->get_config( $CFG => 'ref' ) ) ) { | ||
| 347 | if ( $git->get_config( $CFG => 'quiet' ) eq '0' ) { | ||
| 348 | print "Ref '$current_branch' not enabled.\n"; | ||
| 349 | } | ||
| 350 | return 1; | ||
| 351 | } | ||
| 352 | |||
| 353 | # TODO handle_ref()! | ||
| 354 | } | ||
| 355 | |||
| 356 | # Install hooks | ||
| 357 | POST_COMMIT \&handle_commit_at_client; | ||
| 358 | POST_RECEIVE \&handle_affected_refs; | ||
| 359 | 1; | ||
| 360 | |||
| 361 | __END__ |