blib/lib/Git/Hooks/Notify.pm

filter: Statementstmt 92.9% 131 / 141 Branchbran 60.3% 35 / 58 Conditioncond 70.8% 17 / 24 MC/DCmcdc 46.1% 6 / 13 Subroutinesub 100% 17 / 17 Podpod 0.0% 0 / 6 totaltotal 79.5% 206 / 259 CC 46 SCAR 21.9
CC
46
Cov
82%
__ANON__
15.3
notify_affected_refs
13.6
notify
11.2
CRAP 8.9
« blib/lib/Git/Hooks.pm ↑ Summary blib/lib/Git/Hooks/GerritChangeId.pm »
1 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package Git::Hooks::Notify;
4 # ABSTRACT: Git::Hooks plugin to notify users via email
5 11 $Git::Hooks::Notify::VERSION = '4.0.0';
6 11 ▶use v5.30.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use Log::Any '$log';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use Git::Hooks;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶use Encode qw/decode/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 11 ▶use Email::Sender::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 11 ▶use Email::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 11 ▶use List::MoreUtils qw/none part/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 11 my $CFG = __PACKAGE__ =~ s/.*::/githooks./r;
16  
17 9 ▶sub pretty_log {
Subroutine pretty_log: called CC=4 SCAR=14.0
Pod undocumented
18 9 my ($git, $commits) = @_;
19  
20 9 my @log;
21  
22 9 ▶ my $encoding = $git->get_config(i18n => 'commitEncoding') || 'utf-8';
Condition50%1/2
$git->get_config('i18n', 'commitEncoding') || 'utf-8'
l!l
09
MC/DC0%0/1
$git->get_config('i18n', 'commitEncoding') || 'utf-8'
$git->get_config('i18n', 'commitEncoding')
Truth table50%1/2
A
A= $git->get_config('i18n', 'commitEncoding')
Aresult
11
00
23  
24 9 foreach my $commit (@$commits) {
25 9 my $sha1 = $commit->commit;
26  
27 9 ▶ my $merge =
Branch: 1 branch
BranchTrueFalseTotal
scalar $commit->parent < 2 ? : 9 0 9
28 scalar($commit->parent()) < 2
29 ? ''
30 : "\nMerge: " . join(' ', $commit->parent);
31  
32 9 my $author = decode($encoding, $commit->author_name . ' <' . $commit->author_email . '>');
33  
34 # FIXME: The Git::Repository::Log's *_localtime and *_gmtime methods
35 # confuse me. From what I saw, the command "git log --pretty-raw" shows
36 # datetimes in localtime plus TZ. The *_gmtime methods return the values
37 # as is. But the *_localtime methods apply the TZ skew to them. So, in
38 # order to show localtimes it seems that I have either to call
39 # "localtime($c->author_gmtime)" or "gmtime($c->author_localtime)". I'll
40 # have to think a bit more about this later to convince myself that this
41 # is right.
42 9 my $datetime = localtime($commit->author_gmtime) . ' ' . $commit->author_tz;
43  
44 9 my $message = decode($encoding, $commit->raw_message . $commit->extra);
45  
46 9 push @log, <<"EOS";
47  
48 commit $sha1$merge
49 Author: $author
50 Date: $datetime
51  
52 $message
53 EOS
54 }
55  
56 9 return join('', @log);
57 }
58  
59 9 ▶sub get_transport {
Subroutine get_transport: called CC=4 SCAR=14.1
Pod undocumented
60 9 my ($git) = @_;
61  
62 9 my $transport = $git->get_config($CFG, 'transport');
63  
64 9 ▶ return unless $transport;
Branch: 1 branch
BranchTrueFalseTotal
unless $transport 0 9 9
65  
66 9 my @args = split ' ', $transport;
67  
68 9 $transport = shift @args;
69  
70 9 my %args;
71  
72 9 foreach (@args) {
73 9 my ($arg, $value) = split /=/;
74 9 $args{$arg} = $value;
75 }
76  
77 9 my $transport_module = "Email::Sender::Transport::$transport";
78  
79 9 ▶ if (eval "require $transport_module") { ## no critic (ProhibitStringyEval)
Branch: 1 branch
BranchTrueFalseTotal
if (eval "require $transport_module") { } 9 0 9
80 9 return "$transport_module"->new(\%args);
81 } else {
82 0 return;
83 }
84  
85 }
86  
87 27 ▶sub sha1_link {
Subroutine sha1_link: called CC=5 SCAR=18.7
Pod undocumented
88 27 my ($git, $sha1, $html) = @_;
89 27 ▶ if (my $commit_url = $git->get_config($CFG, 'commit-url')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $commit_url = $git->get_config($CFG, 'commit-url')) { } 12 15 27
90 12 $commit_url =~ s/%H/$sha1/g;
91 12 ▶ if ($commit_url =~ /%R/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($commit_url =~ /%R/u) 0 12 12
92 # %R must be replaced by the repository name.
93 0 my $repository_name = $git->repository_name;
94 0 ▶ if (exists $ENV{BB_REPO_SLUG}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ENV{"BB_REPO_SLUG"}) 0 0 0
95 # HACK: for Bitbucket Server the repository name is composed: a
96 # project ID and a repository name separated by a slash. We have
97 # to insert a "repos/" string between these two parts in order
98 # to construct a valid URL. Ideally we should be able to get the
99 # repository name and the project name separately, but I'll live
100 # with this hack for now, since, as far as I know, only
101 # Bitbucket has this notion of a "project".
102 0 $repository_name =~ s:/:/repos/:;
103 }
104 0 $commit_url =~ s/%R/$repository_name/g;
105 }
106 12 ▶ return $html ? "<a href=\"$commit_url\">$sha1</a>" : $commit_url;
Branch: 1 branch
BranchTrueFalseTotal
$html ? : 3 9 12
107 } else {
108 15 return $sha1;
109 }
110 }
111  
112 9 ▶sub notify {
Subroutine notify: called CC=11 SCAR=24.2
Pod undocumented
113 9 my ($git, $ref, $old_commit, $new_commit, $rule, $message) = @_;
114  
115 9 ▶ return 1 unless $rule->{recipients}->@*;
Branch: 1 branch
BranchTrueFalseTotal
unless @{$rule->{"recipients"}} 0 9 9
116  
117 9 (my $branch = $ref) =~ s:refs/heads/::;
118  
119 9 my $repository_name = $git->repository_name;
120 9 ▶ my $pusher = $git->authenticated_user || '';
Condition50%1/2
$git->authenticated_user || ''
l!l
09
MC/DC0%0/1
$git->authenticated_user || ''
$git->authenticated_user
Truth table50%1/2
A
A= $git->authenticated_user
Aresult
11
00
121  
122 9 ▶ my $subject = $git->get_config($CFG => 'subject')
Condition100%2/2
$git->get_config($CFG, 'subject') || '[Git::Hooks::Notify] repo:%R branch:%B'
l!l
81
MC/DC100%1/1
$git->get_config($CFG, 'subject') || '[Git::Hooks::Notify] repo:%R branch:%B'
$git->get_config($CFG, 'subject')
Truth table100%2/2
A
A= $git->get_config($CFG, 'subject')
Aresult
11
00
123 || '[Git::Hooks::Notify] repo:%R branch:%B';
124  
125 9 $subject =~ s/%R/$repository_name/g;
126 9 $subject =~ s/%B/$branch/g;
127 9 $subject =~ s/%A/$pusher/g;
128  
129 my @headers = (
130 'Subject' => $subject,
131 9 'To' => join(', ', $rule->{recipients}->@*),
132 );
133  
134 9 ▶ if (my $from = $git->get_config($CFG, 'from')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $from = $git->get_config($CFG, "from")) 9 0 9
135 9 push @headers, (From => $from);
136 }
137  
138 9 ▶ my $body = $git->get_config($CFG, 'preamble') || '';
Condition100%2/2
$git->get_config($CFG, 'preamble') || ''
l!l
54
MC/DC100%1/1
$git->get_config($CFG, 'preamble') || ''
$git->get_config($CFG, 'preamble')
Truth table100%2/2
A
A= $git->get_config($CFG, 'preamble')
Aresult
11
00
139  
140 9 ▶ $body .= "\n" if length $body;
Branch: 1 branch
BranchTrueFalseTotal
if length $body 5 4 9
141  
142 9 $body .= <<"EOS";
143 REPOSITORY: $repository_name
144 BRANCH: $branch
145 PUSHED BY: $pusher
146 FROM: $old_commit
147 TO: $new_commit
148 EOS
149  
150 9 ▶ if (my @paths = $rule->{paths}->@*) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@paths) = @{$rule->{"paths"}}) 1 8 9
151 1 $body .= join(' ', 'FILTER:', @paths) . "\n";
152 }
153  
154 9 ▶ if (my @extra_options = $rule->{options}->@*) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@extra_options) = @{$rule->{"options"}}) 0 9 9
155 0 $body .= join(' ', 'EXTRA OPTIONS:', @extra_options) . "\n";
156 }
157  
158 9 $body .= $message;
159  
160 9 ▶ if ($git->get_config_boolean($CFG, 'html')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($git->get_config_boolean($CFG, 'html')) { } 1 8 9
161 1 push @headers, (
162 'MIME-Version' => '1.0',
163 'Content-Type' => 'text/html',
164 );
165  
166 1 require HTML::Entities;
167 1 my $html = HTML::Entities::encode_entities($body);
168  
169 # Replace all sha1's with HTML links
170 1 $html =~ s/\b[0-9a-f]{40}\b/sha1_link($git, ${^MATCH}, 'html')/egp;
171 # Force line breaks
172 1 $html =~ s:$:<br/>:gm;
173 # Force indentation of TO: header
174 1 $html =~ s/(?<=^TO:) {3}/\&nbsp;\&nbsp;\&nbsp;/m;
175 # Force indentation of commit message lines
176 1 $html =~ s:^ +:'&nbsp;' x length(${^MATCH}):egmp;
177 # Force indentation of commit numstat lines
178 1 $html =~ s[^(\d+|-)\t(\d+|-)\t]
179 1 [$1 .
180 '&nbsp;' x (8 - length($1)) .
181 $2 .
182 '&nbsp;' x (8 - length($2))]egm;
183  
184 1 $body = <<"EOS";
185 <html>
186 <body style="font-family: monospace">
187 $html
188 </body>
189 </html>
190 EOS
191 } else {
192 8 $body =~ s/\b[0-9a-f]{40}\b/sha1_link($git, ${^MATCH})/egp;
193 }
194  
195 9 my $email = Email::Simple->create(
196 header => \@headers,
197 body => $body,
198 );
199  
200 9 ▶ return Email::Sender::Simple->send(
Condition33%1/3
get_transport($git) || 'Email::Sender::Simple'->default_transport
l!l&&r!l&&!r
900
MC/DC0%0/2
get_transport($git) || 'Email::Sender::Simple'->default_transport
get_transport($git) 'Email::Sender::Simple'->default_transport
Truth table33%1/3
A || B
A= get_transport($git)B= 'Email::Sender::Simple'->default_transport
ABresult
1X1
011
000
201 $email,
202 {
203 transport => get_transport($git) || Email::Sender::Simple->default_transport(),
204 },
205 );
206 }
207  
208 11 ▶sub grok_rules {
Subroutine grok_rules: called CC=6 SCAR=19.1
Pod undocumented
209 11 my ($git) = @_;
210  
211 11 my @text_rules = $git->get_config($CFG, 'rule');
212  
213 11 my @rules;
214  
215 11 foreach my $rule (@text_rules) {
216 # We use the List::MoreUtils::part function to parse a rule after
217 # splitting it on whitespaces.
218  
219 11 my $part = 0;
220 17 ▶ my @partition = part {
Subroutine __ANON__: called CC=10 SCAR=27.3
221 17 ▶ if ($part == 0) {
Branch: 5 branches
BranchTrueFalseTotal
if ($part == 0) { } 13 4 17
elsif ($part == 1) { } 0 4 4
elsif ($part == 2) { } 2 2 4
elsif ($part == 3) { } 2 0 2
elsif ($part == 4) { } 0 0 0
222 # refs
223 13 ▶ $part = /^-/ ? 1 : 2 if /^[^^]/;
Branch: 2 branches
BranchTrueFalseTotal
if /^[^^]/u 11 2 13
/^-/u ? : 0 11 11
224 } elsif ($part == 1) {
225 # options
226 0 ▶ $part = 2 if /^[^-]/;
Branch: 1 branch
BranchTrueFalseTotal
if /^[^-]/u 0 0 0
227 } elsif ($part == 2) {
228 # recipients
229 2 ▶ $part = 3 if $_ eq '--';
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "--" 2 0 2
230 } elsif ($part == 3) {
231 # --
232 2 $part = 4
233 } elsif ($part == 4) {
234 # pathspecs
235 }
236 17 $part;
237 11 } split ' ', $rule;
238  
239 11 ▶ push @rules, {
Condition75%6/8
$partition[0] || []
l!l
29
$partition[1] || []
l!l
011
$partition[2] || []
l!l
110
$partition[4] || []
l!l
29
MC/DC100%1/1
$partition[0] || []
$partition[0]
MC/DC0%0/1
$partition[1] || []
$partition[1]
MC/DC0%0/1
$partition[2] || []
$partition[2]
MC/DC100%1/1
$partition[4] || []
$partition[4]
Truth table100%2/2
A
A= $partition[0]
Aresult
11
00
Truth table50%1/2
A
A= $partition[1]
Aresult
11
00
Truth table50%1/2
A
A= $partition[2]
Aresult
11
00
Truth table100%2/2
A
A= $partition[4]
Aresult
11
00
240 refs => $partition[0] || [],
241 options => $partition[1] || [],
242 recipients => $partition[2] || [],
243 paths => $partition[4] || [],
244 };
245 }
246  
247 11 return @rules;
248 }
249  
250 # This routine can act as a post-receive hook.
251 11 ▶sub notify_affected_refs {
Subroutine notify_affected_refs: called CC=12 SCAR=26.1
Pod undocumented
252 11 my ($git) = @_;
253  
254 11 $log->debug(__PACKAGE__ . "::notify_affected_refs");
255  
256 # We're only interested in branches
257 11 my @refs = grep {m:^refs/heads/:} $git->get_affected_refs();
258  
259 11 ▶ return 1 unless @refs;
Branch: 1 branch
BranchTrueFalseTotal
unless @refs 0 11 11
260  
261 11 my @rules = grok_rules($git);
262  
263 11 ▶ return 1 unless @rules;
Branch: 1 branch
BranchTrueFalseTotal
unless @rules 0 11 11
264  
265 11 ▶ my $max_count = $git->get_config_integer($CFG, 'max-count') || '10';
Condition50%1/2
$git->get_config_integer($CFG, 'max-count') || '10'
l!l
011
MC/DC0%0/1
$git->get_config_integer($CFG, 'max-count') || '10'
$git->get_config_integer($CFG, 'max-count')
Truth table50%1/2
A
A= $git->get_config_integer($CFG, 'max-count')
Aresult
11
00
266  
267 11 my @options = ('--numstat', '--first-parent', '-m', "--max-count=$max_count");
268  
269 11 my $errors = 0;
270  
271 11 foreach my $ref (@refs) {
272 11 ▶ next unless $git->is_reference_enabled($ref);
Branch: 1 branch
BranchTrueFalseTotal
unless $git->is_reference_enabled($ref) 0 11 11
273 11 my ($old_commit, $new_commit) = $git->get_affected_ref_range($ref);
274 11 foreach my $rule (@rules) {
275 11 ▶ next if $rule->{refs}->@* && none {$ref =~ /$_/} $rule->{refs}->@*;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if @{$rule->{"refs"}} and &none(sub { $ref =~ /$_/u; } , @{$$rule{"refs"}}) 1 10 11
Condition100%3/3
@{$rule->{"refs"}} and &none(sub { $ref =~ /$_/u; } , @{$$rule{"refs"}})
!ll&&!rl&&r
911
MC/DC100%2/2
@{$rule->{"refs"}} and &none(sub { $ref =~ /$_/u; } , @{$$rule{"refs"}})
@{$rule->{"refs"}} &none(sub { $ref =~ /$_/u; } , @{$$rule{"refs"}})
Truth table100%3/3
A and B
A= @{$rule->{"refs"}}B= &none(sub { $ref =~ /$_/u; } , @{$$rule{"refs"}})
ABresult
0X0
100
111
276  
277 my @commits = $git->get_commits($old_commit, $new_commit,
278 [@options, $rule->{options}->@*],
279 10 $rule->{paths});
280  
281 10 ▶ next unless @commits;
Branch: 1 branch
BranchTrueFalseTotal
unless @commits 1 9 10
282  
283 9 my $message = pretty_log($git, \@commits);
284  
285 9 my $success = eval { notify($git, $ref, $old_commit, $new_commit, $rule, $message) };
286 9 ▶ unless (defined $success) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $success) 0 9 9
287 0 ▶ if (my $error = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 0 0
288 0 $git->fault(
289 sprintf('I could not send mail to the following recipients: %s\n',
290 join(", ", $error->recipients)),
291 {ref => $ref, details => $error->message}
292 );
293 0 ++$errors;
294 };
295 };
296 }
297 }
298  
299 11 return $errors == 0;
300 }
301  
302 # Install hooks
303 11 POST_RECEIVE(\&notify_affected_refs);
304  
305 11 1;
306  
307 __END__
« blib/lib/Git/Hooks.pm ↑ Summary blib/lib/Git/Hooks/GerritChangeId.pm »