| 1 | use warnings; | ||
| 2 | |||
| 3 | package Git::Hooks::Notify; | ||
| 4 | # ABSTRACT: Git::Hooks plugin to notify users via email | ||
| 5 | $Git::Hooks::Notify::VERSION = '3.5.0'; | ||
| 6 | use v5.16.0; | ||
| 7 | use utf8; | ||
| 8 | use Log::Any '$log'; | ||
| 9 | use Git::Hooks; | ||
| 10 | use Encode qw/decode/; | ||
| 11 | use Email::Sender::Simple; | ||
| 12 | use Email::Simple; | ||
| 13 | use List::MoreUtils qw/none part/; | ||
| 14 | |||
| 15 | my $CFG = __PACKAGE__ =~ s/.*::/githooks./r; | ||
| 16 | |||
| 17 | sub pretty_log { | ||
| 18 | my ($git, $commits) = @_; | ||
| 19 | |||
| 20 | my @log; | ||
| 21 | |||
| 22 | my $encoding = $git->get_config(i18n => 'commitEncoding') || 'utf-8'; | ||
| 23 | |||
| 24 | foreach my $commit (@$commits) { | ||
| 25 | my $sha1 = $commit->commit; | ||
| 26 | |||
| 27 | my $merge = | ||
| 28 | scalar($commit->parent()) < 2 | ||
| 29 | ? '' | ||
| 30 | : "\nMerge: " . join(' ', $commit->parent); | ||
| 31 | |||
| 32 | 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 | my $datetime = localtime($commit->author_gmtime) . ' ' . $commit->author_tz; | ||
| 43 | |||
| 44 | my $message = decode($encoding, $commit->raw_message . $commit->extra); | ||
| 45 | |||
| 46 | push @log, <<"EOS"; | ||
| 47 | |||
| 48 | commit $sha1$merge | ||
| 49 | Author: $author | ||
| 50 | Date: $datetime | ||
| 51 | |||
| 52 | $message | ||
| 53 | EOS | ||
| 54 | } | ||
| 55 | |||
| 56 | return join('', @log); | ||
| 57 | } | ||
| 58 | |||
| 59 | sub get_transport { | ||
| 60 | my ($git) = @_; | ||
| 61 | |||
| 62 | my $transport = $git->get_config($CFG, 'transport'); | ||
| 63 | |||
| 64 | return unless $transport; | ||
| 65 | |||
| 66 | my @args = split ' ', $transport; | ||
| 67 | |||
| 68 | $transport = shift @args; | ||
| 69 | |||
| 70 | my %args; | ||
| 71 | |||
| 72 | foreach (@args) { | ||
| 73 | my ($arg, $value) = split /=/; | ||
| 74 | $args{$arg} = $value; | ||
| 75 | } | ||
| 76 | |||
| 77 | my $transport_module = "Email::Sender::Transport::$transport"; | ||
| 78 | |||
| 79 | if (eval "require $transport_module") { ## no critic (ProhibitStringyEval) | ||
| 80 | return "$transport_module"->new(\%args); | ||
| 81 | } else { | ||
| 82 | return; | ||
| 83 | } | ||
| 84 | |||
| 85 | } | ||
| 86 | |||
| 87 | sub sha1_link { | ||
| 88 | my ($git, $sha1, $html) = @_; | ||
| 89 | if (my $commit_url = $git->get_config($CFG, 'commit-url')) { | ||
| 90 | $commit_url =~ s/%H/$sha1/g; | ||
| 91 | if ($commit_url =~ /%R/) { | ||
| 92 | # %R must be replaced by the repository name. | ||
| 93 | my $repository_name = $git->repository_name; | ||
| 94 | if (exists $ENV{BB_REPO_SLUG}) { | ||
| 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 | $repository_name =~ s:/:/repos/:; | ||
| 103 | } | ||
| 104 | $commit_url =~ s/%R/$repository_name/g; | ||
| 105 | } | ||
| 106 | return $html ? "<a href=\"$commit_url\">$sha1</a>" : $commit_url; | ||
| 107 | } else { | ||
| 108 | return $sha1; | ||
| 109 | } | ||
| 110 | } | ||
| 111 | |||
| 112 | sub notify { | ||
| 113 | my ($git, $ref, $old_commit, $new_commit, $rule, $message) = @_; | ||
| 114 | |||
| 115 | return 1 unless @{$rule->{recipients}}; | ||
| 116 | |||
| 117 | (my $branch = $ref) =~ s:refs/heads/::; | ||
| 118 | |||
| 119 | my $repository_name = $git->repository_name; | ||
| 120 | my $pusher = $git->authenticated_user || ''; | ||
| 121 | |||
| 122 | my $subject = $git->get_config($CFG => 'subject') | ||
| 123 | || '[Git::Hooks::Notify] repo:%R branch:%B'; | ||
| 124 | |||
| 125 | $subject =~ s/%R/$repository_name/g; | ||
| 126 | $subject =~ s/%B/$branch/g; | ||
| 127 | $subject =~ s/%A/$pusher/g; | ||
| 128 | |||
| 129 | my @headers = ( | ||
| 130 | 'Subject' => $subject, | ||
| 131 | 'To' => join(', ', @{$rule->{recipients}}), | ||
| 132 | ); | ||
| 133 | |||
| 134 | if (my $from = $git->get_config($CFG, 'from')) { | ||
| 135 | push @headers, (From => $from); | ||
| 136 | } | ||
| 137 | |||
| 138 | my $body = $git->get_config($CFG, 'preamble') || ''; | ||
| 139 | |||
| 140 | $body .= "\n" if length $body; | ||
| 141 | |||
| 142 | $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 | if (my @paths = @{$rule->{paths}}) { | ||
| 151 | $body .= join(' ', 'FILTER:', @paths) . "\n"; | ||
| 152 | } | ||
| 153 | |||
| 154 | if (my @extra_options = @{$rule->{options}}) { | ||
| 155 | $body .= join(' ', 'EXTRA OPTIONS:', @extra_options) . "\n"; | ||
| 156 | } | ||
| 157 | |||
| 158 | $body .= $message; | ||
| 159 | |||
| 160 | if ($git->get_config_boolean($CFG, 'html')) { | ||
| 161 | push @headers, ( | ||
| 162 | 'MIME-Version' => '1.0', | ||
| 163 | 'Content-Type' => 'text/html', | ||
| 164 | ); | ||
| 165 | |||
| 166 | require HTML::Entities; | ||
| 167 | my $html = HTML::Entities::encode_entities($body); | ||
| 168 | |||
| 169 | # Replace all sha1's with HTML links | ||
| 170 | $html =~ s/\b[0-9a-f]{40}\b/sha1_link($git, ${^MATCH}, 'html')/egp; | ||
| 171 | # Force line breaks | ||
| 172 | $html =~ s:$:<br/>:gm; | ||
| 173 | # Force indentation of TO: header | ||
| 174 | $html =~ s/(?<=^TO:) {3}/\ \ \ /m; | ||
| 175 | # Force indentation of commit message lines | ||
| 176 | $html =~ s:^ +:' ' x length(${^MATCH}):egmp; | ||
| 177 | # Force indentation of commit numstat lines | ||
| 178 | $html =~ s[^(\d+|-)\t(\d+|-)\t] | ||
| 179 | [$1 . | ||
| 180 | ' ' x (8 - length($1)) . | ||
| 181 | $2 . | ||
| 182 | ' ' x (8 - length($2))]egm; | ||
| 183 | |||
| 184 | $body = <<"EOS"; | ||
| 185 | <html> | ||
| 186 | <body style="font-family: monospace"> | ||
| 187 | $html | ||
| 188 | </body> | ||
| 189 | </html> | ||
| 190 | EOS | ||
| 191 | } else { | ||
| 192 | $body =~ s/\b[0-9a-f]{40}\b/sha1_link($git, ${^MATCH})/egp; | ||
| 193 | } | ||
| 194 | |||
| 195 | my $email = Email::Simple->create( | ||
| 196 | header => \@headers, | ||
| 197 | body => $body, | ||
| 198 | ); | ||
| 199 | |||
| 200 | return Email::Sender::Simple->send( | ||
| 201 | $email, | ||
| 202 | { | ||
| 203 | transport => get_transport($git) || Email::Sender::Simple->default_transport(), | ||
| 204 | }, | ||
| 205 | ); | ||
| 206 | } | ||
| 207 | |||
| 208 | sub grok_rules { | ||
| 209 | my ($git) = @_; | ||
| 210 | |||
| 211 | my @text_rules = $git->get_config($CFG, 'rule'); | ||
| 212 | |||
| 213 | my @rules; | ||
| 214 | |||
| 215 | 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 | my $part = 0; | ||
| 220 | my @partition = part { | ||
| 221 | if ($part == 0) { | ||
| 222 | # refs | ||
| 223 | $part = /^-/ ? 1 : 2 if /^[^^]/; | ||
| 224 | } elsif ($part == 1) { | ||
| 225 | # options | ||
| 226 | $part = 2 if /^[^-]/; | ||
| 227 | } elsif ($part == 2) { | ||
| 228 | # recipients | ||
| 229 | $part = 3 if $_ eq '--'; | ||
| 230 | } elsif ($part == 3) { | ||
| 231 | # -- | ||
| 232 | $part = 4 | ||
| 233 | } elsif ($part == 4) { | ||
| 234 | # pathspecs | ||
| 235 | } | ||
| 236 | $part; | ||
| 237 | } split ' ', $rule; | ||
| 238 | |||
| 239 | push @rules, { | ||
| 240 | refs => $partition[0] || [], | ||
| 241 | options => $partition[1] || [], | ||
| 242 | recipients => $partition[2] || [], | ||
| 243 | paths => $partition[4] || [], | ||
| 244 | }; | ||
| 245 | } | ||
| 246 | |||
| 247 | return @rules; | ||
| 248 | } | ||
| 249 | |||
| 250 | # This routine can act as a post-receive hook. | ||
| 251 | sub notify_affected_refs { | ||
| 252 | my ($git) = @_; | ||
| 253 | |||
| 254 | $log->debug(__PACKAGE__ . "::notify_affected_refs"); | ||
| 255 | |||
| 256 | # We're only interested in branches | ||
| 257 | my @refs = grep {m:^refs/heads/:} $git->get_affected_refs(); | ||
| 258 | |||
| 259 | return 1 unless @refs; | ||
| 260 | |||
| 261 | my @rules = grok_rules($git); | ||
| 262 | |||
| 263 | return 1 unless @rules; | ||
| 264 | |||
| 265 | my $max_count = $git->get_config_integer($CFG, 'max-count') || '10'; | ||
| 266 | |||
| 267 | my @options = ('--numstat', '--first-parent', '-m', "--max-count=$max_count"); | ||
| 268 | |||
| 269 | my $errors = 0; | ||
| 270 | |||
| 271 | foreach my $ref (@refs) { | ||
| 272 | next unless $git->is_reference_enabled($ref); | ||
| 273 | my ($old_commit, $new_commit) = $git->get_affected_ref_range($ref); | ||
| 274 | foreach my $rule (@rules) { | ||
| 275 | next if @{$rule->{refs}} && none {$ref =~ /$_/} @{$rule->{refs}}; | ||
| 276 | |||
| 277 | my @commits = $git->get_commits($old_commit, $new_commit, | ||
| 278 | [@options, @{$rule->{options}}], | ||
| 279 | $rule->{paths}); | ||
| 280 | |||
| 281 | next unless @commits; | ||
| 282 | |||
| 283 | my $message = pretty_log($git, \@commits); | ||
| 284 | |||
| 285 | my $success = eval { notify($git, $ref, $old_commit, $new_commit, $rule, $message) }; | ||
| 286 | unless (defined $success) { | ||
| 287 | if (my $error = $@) { | ||
| 288 | $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 | ++$errors; | ||
| 294 | }; | ||
| 295 | }; | ||
| 296 | } | ||
| 297 | } | ||
| 298 | |||
| 299 | return $errors == 0; | ||
| 300 | } | ||
| 301 | |||
| 302 | # Install hooks | ||
| 303 | POST_RECEIVE(\¬ify_affected_refs); | ||
| 304 | |||
| 305 | 1; | ||
| 306 | |||
| 307 | __END__ |