Skip to content

Checkjira skip logs #78

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion lib/Git/Hooks/CheckJira.pm
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use Log::Any '$log';
use Git::Hooks;
use Git::Repository::Log;
use Path::Tiny;
use List::MoreUtils qw/last_index uniq/;
use List::MoreUtils qw/any last_index uniq/;

my $PKG = __PACKAGE__;
my $CFG = __PACKAGE__ =~ s/.*::/githooks./r;
Expand Down Expand Up @@ -124,6 +124,16 @@ sub _jql_query {
return $cache->{jql}{$jql};
}

sub _skip_logs {
my ($git, $commit) = @_;

my $cache = $git->cache($PKG);
$cache->{skip_logs_regexes} = [map {qr/$_/} $git->get_config($CFG => 'skip-logs')]
unless exists $cache->{skip_logs_regexes};
my $message = $commit->message;
return any {$message =~ $_} $cache->{skip_logs_regexes}->@*;
}

sub _disconnect_jira {
my ($git) = @_;
delete $git->cache($PKG)->{jira};
Expand Down Expand Up @@ -378,6 +388,8 @@ sub check_commit_msg {

if ($commit->parent() > 1 && $git->get_config_boolean($CFG => 'skip-merges')) {
return 1;
} elsif (_skip_logs($git, $commit)) {
return 1;
} else {
return _check_jira_keys($git, $commit, $ref, uniq(grok_msg_jiras($git, $commit->message)));
}
Expand Down Expand Up @@ -572,6 +584,17 @@ may configure it in a Git configuration file like this:
# fixVersion named after the same major.minor version number.
fixversion = ^refs/heads/(\\d+\\.\\d+)\\. ^$+

# Skip commits created by git-merge. Although, it's better to use the
# skip-merges directive for this. See below.
skip-logs = ^Merge\\s
# Skip commits created by git-revert
skip-logs = ^(?:Reapply|Revert)\\s
# Skip commits with a special mark in them
skip-logs = DONT-CheckJira

# Skip merge commits
skip-merges

=head1 DESCRIPTION

This L<Git::Hooks> plugin hooks itself to the hooks below to guarantee that
Expand Down Expand Up @@ -952,6 +975,15 @@ In this case, the visibility isn't restricted at all.

=back

=head2 skip-logs REGEXP

Use this multi-valued directive to make CheckJira don't check
commits which log messages match any of the REGEXes specified with it.

As the examples in the L<SYNOPSIS> section shows, you may want to make CheckJira
skip commits automatically created by C<git-revert> commands, or to skip commits
with a special mark in them.

=head2 skip-merges BOOL

By default, all commits are checked. You can exempt merge commits from being
Expand Down
46 changes: 22 additions & 24 deletions lib/Git/Repository/Plugin/GitHooks.pm
Original file line number Diff line number Diff line change
Expand Up @@ -607,40 +607,38 @@ sub get_config {
local $/ = "\c@";
$git->run(qw/config --null --list/);
};
$config //= '';

if (defined $CONFIG_ENCODING) {
require Encode;
$config = Encode::decode($CONFIG_ENCODING, $config);
}

if (defined $config) {
# The --null option to git-config makes it output a null character
# after each option/value. The option and value are separated by a
# newline, unless there is no value, in which case, there is no
# newline.
while ($config =~ /([^\cJ]+)(\cJ[^\c@]*|)\c@/sg) {
my ($option, $value) = ($1, $2);
if ($option =~ /(.+)\.(.+)/) {
my ($osection, $okey) = (lc $1, lc $2);
if ($value =~ s/^\cJ//) {
## no critic (ProhibitDeepNests)
if ($value eq 'undef') {
# The 'undef' string is a special mark telling us to
# disregard every previous value already set for
# this variable.
delete $config{$osection}{$okey};
} else {
push @{$config{$osection}{$okey}}, $value;
}
# The --null option to git-config makes it output a null character
# after each option/value. The option and value are separated by a
# newline, unless there is no value, in which case, there is no
# newline.
while ($config =~ /([^\cJ]+)(\cJ[^\c@]*|)\c@/sg) {
my ($option, $value) = ($1, $2);
if ($option =~ /(.+)\.(.+)/) {
my ($osection, $okey) = (lc $1, lc $2);
if ($value =~ s/^\cJ//) {
if ($value eq 'undef') {
# The 'undef' string is a special mark telling us to
# disregard every previous value already set for
# this variable.
delete $config{$osection}{$okey};
} else {
# An option without a value is considered a boolean
# true. We mark it explicitly so instead of leaving it
# undefined because Perl would consider it false.
push @{$config{$osection}{$okey}}, 'true';
push @{$config{$osection}{$okey}}, $value;
}
} else {
croak __PACKAGE__, ": Cannot grok config variable name '$option'.\n";
# An option without a value is considered a boolean
# true. We mark it explicitly so instead of leaving it
# undefined because Perl would consider it false.
push @{$config{$osection}{$okey}}, 'true';
}
} else {
croak __PACKAGE__, ": Cannot grok config variable name '$option'.\n";
}
}

Expand Down