| 1 | use warnings; | ||
| 2 | |||
| 3 | package Git::Hooks::CheckJira; | ||
| 4 | # ABSTRACT: Git::Hooks plugin which requires citation of Jira issues in commit messages | ||
| 5 | $Git::Hooks::CheckJira::VERSION = '3.5.0'; | ||
| 6 | use v5.16.0; | ||
| 7 | use utf8; | ||
| 8 | use Log::Any '$log'; | ||
| 9 | use Git::Hooks; | ||
| 10 | use Git::Repository::Log; | ||
| 11 | use Path::Tiny; | ||
| 12 | use List::MoreUtils qw/last_index uniq/; | ||
| 13 | |||
| 14 | my $PKG = __PACKAGE__; | ||
| 15 | my $CFG = __PACKAGE__ =~ s/.*::/githooks./r; | ||
| 16 | |||
| 17 | ############# | ||
| 18 | # Grok hook configuration, check it and set defaults. | ||
| 19 | |||
| 20 | sub _setup_config { | ||
| 21 | my ($git) = @_; | ||
| 22 | |||
| 23 | my $config = $git->get_config(); | ||
| 24 | |||
| 25 | $config->{lc $CFG} //= {}; | ||
| 26 | |||
| 27 | my $default = $config->{lc $CFG}; | ||
| 28 | |||
| 29 | # Default matchkey for matching default Jira keys. | ||
| 30 | $default->{matchkey} //= ['\b[A-Z][A-Z]+-\d+\b']; | ||
| 31 | |||
| 32 | $default->{require} //= ['true']; | ||
| 33 | $default->{unresolved} //= ['true']; | ||
| 34 | |||
| 35 | return; | ||
| 36 | } | ||
| 37 | |||
| 38 | ########## | ||
| 39 | |||
| 40 | sub grok_msg_jiras { | ||
| 41 | my ($git, $msg) = @_; | ||
| 42 | |||
| 43 | my $matchkey = $git->get_config($CFG => 'matchkey'); | ||
| 44 | my @matchlog = $git->get_config($CFG => 'matchlog'); | ||
| 45 | |||
| 46 | # Grok the Jira issue keys from the commit log | ||
| 47 | if (@matchlog) { | ||
| 48 | my @keys; | ||
| 49 | foreach my $matchlog (@matchlog) { | ||
| 50 | if (my ($match) = ($msg =~ /$matchlog/)) { | ||
| 51 | push @keys, ($match =~ /$matchkey/go); | ||
| 52 | } | ||
| 53 | } | ||
| 54 | return @keys; | ||
| 55 | } else { | ||
| 56 | return $msg =~ /$matchkey/go; | ||
| 57 | } | ||
| 58 | } | ||
| 59 | |||
| 60 | sub _jira { | ||
| 61 | my ($git) = @_; | ||
| 62 | |||
| 63 | my $cache = $git->cache($PKG); | ||
| 64 | |||
| 65 | # Connect to Jira if not yet connected | ||
| 66 | unless (exists $cache->{jira}) { | ||
| 67 | unless (eval { require JIRA::REST; }) { | ||
| 68 | $git->fault(<<"EOS", {details => $@}); | ||
| 69 | I could not load the JIRA::REST Perl module. | ||
| 70 | |||
| 71 | I need it to talk to your Jira server, as configured by the | ||
| 72 | $CFG.jiraurl, $CFG.jirauser, and $CFG.jirapass | ||
| 73 | options in your Git configuration. | ||
| 74 | |||
| 75 | Please, install the module or disable these options to proceed. | ||
| 76 | EOS | ||
| 77 | return; | ||
| 78 | } | ||
| 79 | |||
| 80 | my %jira; | ||
| 81 | for my $option (qw/jiraurl jirauser jirapass/) { | ||
| 82 | $jira{$option} = $git->get_config($CFG => $option) | ||
| 83 | or $git->fault(<<'EOS', {option => $option}) | ||
| 84 | The option is missing from the configuration. | ||
| 85 | It's required in order to connect to the Jira server. | ||
| 86 | EOS | ||
| 87 | and return; | ||
| 88 | } | ||
| 89 | $jira{jiraurl} =~ s:/+$::; # trim trailing slashes from the URL | ||
| 90 | |||
| 91 | my $jira = eval { JIRA::REST->new($jira{jiraurl}, $jira{jirauser}, $jira{jirapass}) }; | ||
| 92 | length $@ | ||
| 93 | and $git->fault(<<"EOS", {details => $@}) | ||
| 94 | Cannot connect to the Jira server at '$jira{jiraurl}' as '$jira{jirauser}. | ||
| 95 | |||
| 96 | Please, check your $CFG.jiraurl, $CFG.jirauser, | ||
| 97 | and $CFG.jirapass configuration options. | ||
| 98 | EOS | ||
| 99 | and return; | ||
| 100 | $cache->{jira} = $jira; | ||
| 101 | } | ||
| 102 | |||
| 103 | return $cache->{jira}; | ||
| 104 | } | ||
| 105 | |||
| 106 | sub _jql_query { | ||
| 107 | my ($git, $jql) = @_; | ||
| 108 | |||
| 109 | my $cache = $git->cache($PKG); | ||
| 110 | |||
| 111 | unless (exists $cache->{jql}{$jql}) { | ||
| 112 | my $jira = _jira($git); | ||
| 113 | $jira->set_search_iterator({ | ||
| 114 | jql => $jql, | ||
| 115 | fields => $git->get_config($CFG => 'check-code') | ||
| 116 | ? [qw/*all/] | ||
| 117 | : [qw/assignee fixVersions resolution/], | ||
| 118 | }); | ||
| 119 | while (my $issue = $jira->next_issue) { | ||
| 120 | $cache->{jql}{$jql}{$issue->{key}} = $issue; | ||
| 121 | } | ||
| 122 | } | ||
| 123 | |||
| 124 | return $cache->{jql}{$jql}; | ||
| 125 | } | ||
| 126 | |||
| 127 | sub _disconnect_jira { | ||
| 128 | my ($git) = @_; | ||
| 129 | delete $git->cache($PKG)->{jira}; | ||
| 130 | return; | ||
| 131 | } | ||
| 132 | |||
| 133 | sub check_codes { | ||
| 134 | my ($git) = @_; | ||
| 135 | |||
| 136 | my $cache = $git->cache($PKG); | ||
| 137 | |||
| 138 | unless (exists $cache->{codes}) { | ||
| 139 | $cache->{codes} = []; | ||
| 140 | for my $type (qw/check-code check-code-ref/) { | ||
| 141 | CODE: | ||
| 142 | foreach my $check ($git->get_config($CFG => $type)) { | ||
| 143 | my $code; | ||
| 144 | if ($check =~ s/^file://) { | ||
| 145 | $code = do $check; | ||
| 146 | unless ($code) { | ||
| 147 | ## no critic (ProhibitDeepNests) | ||
| 148 | if (length $@) { | ||
| 149 | $git->fault("I couldn't parse option value ($check).", | ||
| 150 | {option => $type, details => $@}); | ||
| 151 | } elsif (! defined $code) { | ||
| 152 | $git->fault("I couldn't do option value ($check).", | ||
| 153 | {option => $type, details => $!}); | ||
| 154 | } else { | ||
| 155 | $git->fault("I couldn't run option value ($check).", | ||
| 156 | {option => $type}); | ||
| 157 | } | ||
| 158 | ## use critic (ProhibitDeepNests) | ||
| 159 | next CODE; | ||
| 160 | } | ||
| 161 | } else { | ||
| 162 | $code = eval $check; ## no critic (BuiltinFunctions::ProhibitStringyEval) | ||
| 163 | length $@ | ||
| 164 | and $git->fault("I couldn't parse option value.", | ||
| 165 | {option => $type, details => $@}) | ||
| 166 | and next CODE; | ||
| 167 | } | ||
| 168 | defined $code and ref $code and ref $code eq 'CODE' | ||
| 169 | or $git->fault("The option value must end with a code-ref.", | ||
| 170 | {option => $type}) | ||
| 171 | and next CODE; | ||
| 172 | push @{$cache->{codes}}, [$type => $code]; | ||
| 173 | } | ||
| 174 | } | ||
| 175 | } | ||
| 176 | |||
| 177 | return @{$cache->{codes}}; | ||
| 178 | } | ||
| 179 | |||
| 180 | sub _check_jira_keys { ## no critic (ProhibitExcessComplexity) | ||
| 181 | my ($git, $commit, $ref, @keys) = @_; | ||
| 182 | |||
| 183 | unless (@keys) { | ||
| 184 | if ($git->get_config_boolean($CFG => 'require')) { | ||
| 185 | $git->fault(<<'EOS', {commit => $commit}); | ||
| 186 | The commit must cite a Jira in its message. | ||
| 187 | |||
| 188 | Please, amend your commit to insert a Jira key. | ||
| 189 | EOS | ||
| 190 | return 0; | ||
| 191 | } else { | ||
| 192 | return 1; | ||
| 193 | } | ||
| 194 | } | ||
| 195 | |||
| 196 | my %issues; # cache all grokked issues | ||
| 197 | |||
| 198 | my $errors = 0; | ||
| 199 | |||
| 200 | ############ | ||
| 201 | # JQL checks | ||
| 202 | |||
| 203 | { | ||
| 204 | # Build a list of JQL terms | ||
| 205 | |||
| 206 | # Starting with a check to see if all Jira keys exist | ||
| 207 | my @jqls = ("key IN (@{[join(',', @keys)]})"); | ||
| 208 | |||
| 209 | # global JQL | ||
| 210 | if (my $jql = $git->get_config($CFG => 'jql')) { | ||
| 211 | push @jqls, $jql unless $jql eq 'undef'; | ||
| 212 | } | ||
| 213 | |||
| 214 | # ref-specific JQL | ||
| 215 | foreach my $refjql (reverse $git->get_config($CFG => 'ref-jql')) { | ||
| 216 | my ($match_ref, $jql) = split ' ', $refjql, 2; | ||
| 217 | if ($ref =~ $match_ref) { | ||
| 218 | push @jqls, $jql unless $jql eq 'undef'; | ||
| 219 | last; | ||
| 220 | } | ||
| 221 | } | ||
| 222 | |||
| 223 | # AND JQLs | ||
| 224 | if (my @and_jql = $git->get_config($CFG => 'and-jql')) { | ||
| 225 | my $first = 1 + last_index {$_ eq 'undef'} @and_jql; | ||
| 226 | push @jqls, @and_jql[$first .. $#and_jql]; | ||
| 227 | } | ||
| 228 | |||
| 229 | # Conjunct all terms in a single JQL expression | ||
| 230 | my $JQL = '(' . join(') AND (', @jqls) . ')'; | ||
| 231 | |||
| 232 | # Squeeze multiple whitespaces in a single space to make it appear | ||
| 233 | # neatly in error messages. | ||
| 234 | $JQL =~ s/\s{2,}/ /g; | ||
| 235 | |||
| 236 | my $issues = _jql_query($git, $JQL); | ||
| 237 | |||
| 238 | @issues{keys %$issues} = values %$issues; # cache all matched issues | ||
| 239 | |||
| 240 | if (my @issues_not_found = sort grep { ! exists $issues->{$_} } @keys) { | ||
| 241 | # Some issue keys were cited but not found in Jira | ||
| 242 | ++$errors; | ||
| 243 | local $, = ' '; | ||
| 244 | $git->fault(<<"EOS", {commit => $commit}); | ||
| 245 | The commit cites the following invalid issues: | ||
| 246 | |||
| 247 | @issues_not_found | ||
| 248 | |||
| 249 | The issues do not match the following JQL expression: | ||
| 250 | |||
| 251 | $JQL | ||
| 252 | |||
| 253 | Please, update your issues or fix your $CFG git configuration. | ||
| 254 | EOS | ||
| 255 | } | ||
| 256 | } | ||
| 257 | |||
| 258 | # Return prematurely if there are no issues to check | ||
| 259 | return $errors == 0 unless %issues; | ||
| 260 | |||
| 261 | ################ | ||
| 262 | # Non-JQL checks | ||
| 263 | { | ||
| 264 | my $unresolved = $git->get_config_boolean($CFG => 'unresolved'); | ||
| 265 | my $by_assignee = $git->get_config_boolean($CFG => 'by-assignee'); | ||
| 266 | my @versions; | ||
| 267 | foreach ($git->get_config($CFG => 'fixversion')) { | ||
| 268 | my ($branch, $version) = split ' ', $_, 2; | ||
| 269 | my $last_paren_match; | ||
| 270 | if ($branch =~ /^\^/) { | ||
| 271 | next unless $ref =~ qr/$branch/; | ||
| 272 | $last_paren_match = $+; | ||
| 273 | } else { | ||
| 274 | next unless $ref eq $branch; | ||
| 275 | } | ||
| 276 | if ($version =~ /^\^/) { | ||
| 277 | $version =~ s/\$\+/\Q$last_paren_match\E/g if defined $last_paren_match; | ||
| 278 | push @versions, qr/$version/; | ||
| 279 | } else { | ||
| 280 | $version =~ s/\$\+/$last_paren_match/g if defined $last_paren_match; | ||
| 281 | push @versions, $version; | ||
| 282 | } | ||
| 283 | } | ||
| 284 | |||
| 285 | ISSUE: | ||
| 286 | while (my ($key, $issue) = each %issues) { | ||
| 287 | if ($unresolved && defined $issue->{fields}{resolution}) { | ||
| 288 | $git->fault(<<"EOS", {commit => $commit, option => 'unresolved'}); | ||
| 289 | The commit cites issue $key which is already resolved. | ||
| 290 | |||
| 291 | The option in your configuration requires that all Jira issues be unresolved. | ||
| 292 | EOS | ||
| 293 | ++$errors; | ||
| 294 | next ISSUE; | ||
| 295 | } | ||
| 296 | |||
| 297 | VERSION: | ||
| 298 | foreach my $version (@versions) { | ||
| 299 | foreach my $fixversion (@{$issue->{fields}{fixVersions}}) { | ||
| 300 | if (ref $version) { | ||
| 301 | next VERSION if $fixversion->{name} =~ $version; | ||
| 302 | } else { | ||
| 303 | next VERSION if $fixversion->{name} eq $version; | ||
| 304 | } | ||
| 305 | } | ||
| 306 | $git->fault(<<"EOS", {commit => $commit, option => 'fixversion'}); | ||
| 307 | The commit cites issue $key which is invalid. | ||
| 308 | |||
| 309 | Commits on '$ref' must cite issues associated with a fixVersion matching | ||
| 310 | '$version' according to the configuration option. | ||
| 311 | EOS | ||
| 312 | ++$errors; | ||
| 313 | next ISSUE; | ||
| 314 | } | ||
| 315 | |||
| 316 | if ($by_assignee) { | ||
| 317 | my $user = $git->authenticated_user() | ||
| 318 | or $git->fault(<<'EOS') | ||
| 319 | Internal error: I cannot get your username to authorize you. | ||
| 320 | Please check your Git::Hooks configuration with regards to the function | ||
| 321 | https://metacpan.org/pod/Git::Repository::Plugin::GitHooks#authenticated_user | ||
| 322 | EOS | ||
| 323 | and ++$errors | ||
| 324 | and next ISSUE; | ||
| 325 | |||
| 326 | if (my $assignee = $issue->{fields}{assignee}) { | ||
| 327 | my $name = $assignee->{name}; | ||
| 328 | $user eq $name | ||
| 329 | or $git->fault(<<"EOS", {commit => $commit, option => 'by-assignee'}) | ||
| 330 | The commit cites issue $key which is assigned to '$name'. | ||
| 331 | The option requires that cited issues be assigned to you ($user). | ||
| 332 | Please, update your issue. | ||
| 333 | EOS | ||
| 334 | and ++$errors | ||
| 335 | and next KEY; | ||
| 336 | } else { | ||
| 337 | $git->fault(<<"EOS", {commit => $commit, option => 'by-assignee'}); | ||
| 338 | The commit cites issue $key which is unassigned. | ||
| 339 | The option requires that cited issues be assigned to you ($user). | ||
| 340 | Please, update your issue. | ||
| 341 | EOS | ||
| 342 | ++$errors; | ||
| 343 | next KEY; | ||
| 344 | } | ||
| 345 | } | ||
| 346 | } | ||
| 347 | } | ||
| 348 | |||
| 349 | ############# | ||
| 350 | # Code checks | ||
| 351 | |||
| 352 | foreach my $code (check_codes($git)) { | ||
| 353 | if (my $jira = _jira($git)) { | ||
| 354 | my ($type, $sub) = @$code; | ||
| 355 | my $ok = $type eq 'check-code' | ||
| 356 | ? eval { $sub->($git, $commit, $jira, values %issues) } | ||
| 357 | : eval { $sub->($git, $ref, $commit, $jira, values %issues) } | ||
| 358 | ; | ||
| 359 | if (defined $ok) { | ||
| 360 | ++$errors unless $ok; | ||
| 361 | } elsif (length $@) { | ||
| 362 | $git->fault('Error while evaluating option value.', | ||
| 363 | {option => $type, details => $@}); | ||
| 364 | ++$errors; | ||
| 365 | } | ||
| 366 | } else { | ||
| 367 | ++$errors; | ||
| 368 | } | ||
| 369 | } continue { | ||
| 370 | $git->check_timeout(); | ||
| 371 | } | ||
| 372 | |||
| 373 | return $errors == 0; | ||
| 374 | } | ||
| 375 | |||
| 376 | sub check_commit_msg { | ||
| 377 | my ($git, $commit, $ref) = @_; | ||
| 378 | |||
| 379 | if ($commit->parent() > 1 && $git->get_config_boolean($CFG => 'skip-merges')) { | ||
| 380 | return 1; | ||
| 381 | } else { | ||
| 382 | return _check_jira_keys($git, $commit, $ref, uniq(grok_msg_jiras($git, $commit->message))); | ||
| 383 | } | ||
| 384 | } | ||
| 385 | |||
| 386 | sub check_patchset { | ||
| 387 | my ($git, $branch, $commit) = @_; | ||
| 388 | |||
| 389 | return check_commit_msg($git, $commit, $branch) ? 0 : 1; | ||
| 390 | } | ||
| 391 | |||
| 392 | sub check_message_file { | ||
| 393 | my ($git, $msg, $current_branch) = @_; | ||
| 394 | |||
| 395 | # Remove comment lines from the message file contents. | ||
| 396 | $msg =~ s/^#[^\n]*\n//mgs; | ||
| 397 | |||
| 398 | # Construct a fake commit object to pass to the check_commit_msg | ||
| 399 | my $commit = Git::Repository::Log->new( | ||
| 400 | commit => '<new>', | ||
| 401 | author => 'Fake Author <author@example.net> 1234567890 -0300', | ||
| 402 | committer => 'Fake Committer <committer@example.net> 1234567890 -0300', | ||
| 403 | message => $msg, | ||
| 404 | ); | ||
| 405 | |||
| 406 | return check_commit_msg($git, $commit, $current_branch) ? 0 : 1; | ||
| 407 | } | ||
| 408 | |||
| 409 | sub check_ref { | ||
| 410 | my ($git, $ref) = @_; | ||
| 411 | |||
| 412 | my $errors = 0; | ||
| 413 | |||
| 414 | foreach my $commit ($git->get_affected_ref_commits($ref)) { | ||
| 415 | check_commit_msg($git, $commit, $ref) | ||
| 416 | or ++$errors; | ||
| 417 | } | ||
| 418 | |||
| 419 | _disconnect_jira($git); | ||
| 420 | |||
| 421 | return $errors == 0; | ||
| 422 | } | ||
| 423 | |||
| 424 | sub notify_commit_msg { | ||
| 425 | my ($git, $commit, $ref, $visibility) = @_; | ||
| 426 | |||
| 427 | my @keys = uniq(grok_msg_jiras($git, $commit->message)); | ||
| 428 | |||
| 429 | return 0 unless @keys; | ||
| 430 | |||
| 431 | my $jira = _jira($git) or return 1; | ||
| 432 | |||
| 433 | my $show = $git->run(show => '--stat', $commit->commit); | ||
| 434 | |||
| 435 | my %comment = ( | ||
| 436 | body => <<"EOS", | ||
| 437 | [$PKG] commit refers to this issue: | ||
| 438 | |||
| 439 | {noformat} | ||
| 440 | $show | ||
| 441 | {noformat} | ||
| 442 | EOS | ||
| 443 | ); | ||
| 444 | $comment{visibility} = $visibility if $visibility; | ||
| 445 | |||
| 446 | my $errors = 0; | ||
| 447 | |||
| 448 | foreach my $key (@keys) { | ||
| 449 | eval { $jira->POST("/issue/$key/comment", undef, \%comment); 1; } | ||
| 450 | or $git->fault("I could not add a comment to Jira issue $key.", | ||
| 451 | {commit => $commit, details => $@}) | ||
| 452 | and ++$errors; | ||
| 453 | } | ||
| 454 | |||
| 455 | return $errors; | ||
| 456 | } | ||
| 457 | |||
| 458 | sub notify_ref { | ||
| 459 | my ($git, $ref, $visibility) = @_; | ||
| 460 | |||
| 461 | my $errors = 0; | ||
| 462 | |||
| 463 | foreach my $commit ($git->get_affected_ref_commits($ref)) { | ||
| 464 | $errors += notify_commit_msg($git, $commit, $ref, $visibility); | ||
| 465 | } | ||
| 466 | |||
| 467 | return $errors == 0; | ||
| 468 | } | ||
| 469 | |||
| 470 | # This routine can act as a post-receive hook. | ||
| 471 | sub notify_affected_refs { | ||
| 472 | my ($git) = @_; | ||
| 473 | |||
| 474 | $log->debug(__PACKAGE__ . "::notify_affected_refs"); | ||
| 475 | |||
| 476 | _setup_config($git); | ||
| 477 | |||
| 478 | my $comment = $git->get_config($CFG => 'comment'); | ||
| 479 | |||
| 480 | return 1 unless defined $comment; | ||
| 481 | |||
| 482 | my $visibility; | ||
| 483 | if ($comment =~ /^(role|group):(.+)/) { | ||
| 484 | $visibility = { | ||
| 485 | type => $1, | ||
| 486 | value => $2, | ||
| 487 | }; | ||
| 488 | } elsif ($comment ne 'all') { | ||
| 489 | $git->fault(<<"EOS", {option => $comment}); | ||
| 490 | Configuration error. | ||
| 491 | |||
| 492 | The option is defined as '$comment', but | ||
| 493 | the valid values are 'role:ROLE', 'group:GROUP', or 'all'. | ||
| 494 | Please, check your git configuration. | ||
| 495 | EOS | ||
| 496 | return 0; | ||
| 497 | } | ||
| 498 | |||
| 499 | my $errors = 0; | ||
| 500 | |||
| 501 | foreach my $ref ($git->get_affected_refs()) { | ||
| 502 | next unless $git->is_reference_enabled($ref); | ||
| 503 | $errors += notify_ref($git, $ref, $visibility); | ||
| 504 | } | ||
| 505 | |||
| 506 | _disconnect_jira($git); | ||
| 507 | |||
| 508 | return $errors == 0; | ||
| 509 | } | ||
| 510 | |||
| 511 | # Install hooks | ||
| 512 | my $options = { | ||
| 513 | config => \&_setup_config, | ||
| 514 | destroy => \&_disconnect_jira, | ||
| 515 | }; | ||
| 516 | |||
| 517 | GITHOOKS_CHECK_AFFECTED_REFS(\&check_ref, $options); | ||
| 518 | GITHOOKS_CHECK_PATCHSET(\&check_patchset, $options); | ||
| 519 | GITHOOKS_CHECK_MESSAGE_FILE(\&check_message_file, $options); | ||
| 520 | |||
| 521 | POST_RECEIVE(\¬ify_affected_refs); | ||
| 522 | |||
| 523 | 1; | ||
| 524 | |||
| 525 | __END__ |