editproj.cgi: suppress "operation aborted" for "view all"
[girocco.git] / Girocco / Notify.pm
blob1337f77bcf12199ea47b6814877ce67f2c378290
1 package Girocco::Notify;
3 use strict;
4 use warnings;
6 use Girocco::Config;
7 use Girocco::Util;
8 use Girocco::HashUtil qw(hmac_sha1);
9 my $have_hmac_sha256;
10 BEGIN {eval{
11 $have_hmac_sha256 = 0;
12 require Digest::SHA;
13 Digest::SHA->import( qw(hmac_sha256) );
14 $have_hmac_sha256 = 1;
17 use LWP::UserAgent;
19 #use RPC::XML;
20 #use RPC::XML::Client;
22 # This Perl code creates json payload within post-receive hook.
24 sub _fixlaxiso {
25 # The %ai %ci formats are ISO 8601 like "1999-12-31 23:59:59 -0100"
26 # The %aI %cI strict ISO 8601 formats aren't available until Git v2.2.0
27 local $_ = shift;
28 s/ /T/; # "1999-12-31 23:59:59 -0100" -> "1999-12-31T23:59:59 -0100"
29 s/ //; # "1999-12-31T23:59:59 -0100" -> "1999-12-31T23:59:59-0100"
30 s/(?<!:)(\d\d)$/:$1/; # "1999-12-31T23:59:59-0100" -> "1999-12-31T23:59:59-01:00"
31 return $_;
34 sub json_commit {
35 my ($proj, $commit, $root) = @_;
37 my @gcmd = ($Girocco::Config::git_bin, '--git-dir='.$proj->{path});
38 my $fd;
40 open $fd, '-|', @gcmd, 'log', '-1', '--pretty=format:%T%n%ae %an%n%ai%n%ce %cn%n%ci%n%s%n%n%b', $commit, '--'
41 or die "cannot do git log: $! $?";
42 my @l = <$fd>;
43 chomp @l;
44 close $fd;
45 my ($tr) = $l[0];
46 my ($ae, $an) = ($l[1] =~ /^(.*?) (.*)$/);
47 #my ($ai) = _fixlaxiso($l[2]);
48 my ($ce, $cn) = ($l[3] =~ /^(.*?) (.*)$/);
49 my ($ci) = _fixlaxiso($l[4]);
50 my $msg = join("\n", splice(@l, 5));
51 # Up to three trailing newlines in case of no body.
52 chomp $msg; chomp $msg; chomp $msg;
54 my ($rf, $af, $mf) = ([], [], []);
55 my $parent = ($commit eq $root) ? "--root" : "$commit^";
56 open $fd, '-|', @gcmd, 'diff-tree', '--name-status', '-r', $parent, $commit, '--'
57 or die "cannot do git diff-tree: $! $?";
58 while (<$fd>) {
59 chomp;
60 my ($s, $file) = split(/\t/, $_);
61 if ($s eq 'M') {
62 push @$mf, $file;
63 } elsif ($s eq 'A') {
64 push @$af, $file;
65 } elsif ($s eq 'R') {
66 push @$rf, $file;
69 close $fd;
71 return {
72 "removed" => $rf,
73 "message" => $msg,
74 "added" => $af,
75 "timestamp" => $ci,
76 "modified" => $mf,
77 "url" => $Girocco::Config::gitweburl."/".$proj->{name}.".git/commit/".$commit,
78 "author" => { "name" => $an, "email" => $ae },
79 "committer" => { "name" => $cn, "email" => $ce },
80 "distinct" => json_bool(1),
81 "id" => $commit,
82 "tree_id" => $tr
83 };
86 sub _jsonagent {
87 my $proj = shift;
88 return "girocco/1.0 (JSON Push Notification) (" .
89 $Girocco::Config::gitweburl."/".$proj->{name}.".git" .
90 ")";
93 sub json {
94 my ($url, $proj, $user, $ref, $oldrev, $newrev, $forced) = @_;
96 my $pusher = {};
97 my $sender = {};
98 if ($user) {
99 $pusher = { "name" => $user->{name} };
100 $sender = { "login" => $user->{name} };
101 if ($user->{name} ne 'mob') {
102 $pusher->{"email"} = $user->{email};
106 my $commits = [];
108 my @commits = get_commits($proj, $ref, $oldrev, $newrev);
109 my $root = ($oldrev =~ /^0+$/) ? $commits[$#commits] : "";
110 foreach my $commit (@commits) {
111 push @$commits, json_commit($proj, $commit, $root);
114 # This is backwards-compatible with GitHub (except the GitHub-specific
115 # full project name construction sometimes performed in clients)
116 my $payload = to_json {
117 "before" => $oldrev,
118 "after" => $newrev,
119 "ref" => $ref,
120 "created" => json_bool($oldrev =~ /^0+$/ ? 1 : 0),
121 "deleted" => json_bool($newrev =~ /^0+$/ ? 1 : 0),
122 "forced" => json_bool($forced ? 1 : 0),
124 "repository" => {
125 "name" => $proj->{name},
126 # Girocco extension: full_name is full project name,
127 # equivalent to GitHub's "owner[name]/name".
128 "full_name" => $proj->{name}.".git",
130 "url" => $Girocco::Config::gitweburl.'/'.$proj->{name}.".git",
131 # Girocco extension: Pull URL.
132 "pull_url" => $Girocco::Config::gitpullurl.'/'.$proj->{name}.".git",
134 "owner" => { "name" => "", "email" => $proj->{email} }
137 "pusher" => $pusher,
138 "sender" => $sender,
139 "commits" => $commits
142 # print "$payload\n";
143 my $ua = LWP::UserAgent->new(agent => _jsonagent($proj));
144 $ua->timeout(5);
145 my $ct = $proj->{jsontype};
146 if (!defined($ct) || $ct ne 'application/json') {
147 $ct = 'application/x-www-form-urlencoded';
149 use bytes;
150 # Spaces are expected to be encoded as '+' rather than %20
151 # This is not part of any RFC, but matches the behavior
152 # of the specification that we're emulating here
153 $payload =~ s/([^ A-Za-z0-9_.~-])/sprintf("%%%02X",ord($1))/ge;
154 $payload =~ s/[ ]/+/g; # expected but NOT part of any RFC!
156 $payload = 'payload=' . $payload;
158 my @headers = ( Content_Type => $ct, Content_Length => length($payload) );
159 my $hmackey = $proj->{jsonsecret};
160 if (defined($hmackey) && $hmackey ne "") {
161 my $sig = "sha1=".lc(unpack('H*',hmac_sha1($hmackey, $payload)));
162 push(@headers, X_Hub_Signature => $sig);
163 if ($have_hmac_sha256) {
164 # Yes, the argument order is different! #%@#%^@!
165 # hmac_sha1 is provided by Girocco::HashUtil and it uses the
166 # sane order of "key" then "text" as that's the order they are
167 # mentioned in RFC 2104. The @#%^^@* Digest::SHA module, on the
168 # other hand, is the one providing hmac_sha256 and for some
169 # inscrutable reason uses the order of "text" then "key"!
170 my $sig256 = "sha256=".lc(unpack('H*',hmac_sha256($payload, $hmackey)));
171 push(@headers, X_Hub_Signature_256 => $sig256);
174 $ua->post($url, @headers, Content => $payload);
178 sub cia_commit {
179 my ($cianame, $proj, $branch, $commit, $root) = @_;
181 my @gcmd = ($Girocco::Config::git_bin, '--git-dir='.$proj->{path});
182 my $fd;
184 open $fd, '-|', @gcmd, 'log', '-1', '--pretty=format:%an <%ae>%n%at%n%s', $commit, '--'
185 or die "cannot do git log: $! $?";
186 my @l = <$fd>;
187 chomp @l;
188 close $fd;
189 foreach (@l) { s/&/&amp;/g; s/</&lt;/g; s/>/&gt;/g; }
190 my ($a, $at, $subj) = @l;
192 my @f;
193 my $parent = ($commit eq $root) ? "--root" : "$commit^";
194 open $fd, '-|', @gcmd, 'diff-tree', '--name-status', '-r', $parent, $commit, '--'
195 or die "cannot do git diff-tree: $! $?";
196 while (<$fd>) {
197 chomp;
198 s/&/&amp;/g; s/</&lt;/g; s/>/&gt;/g;
199 my ($status, $file) = split(/\t/, $_);
200 push @f, $file;
202 close $fd;
204 my $rev = substr($commit, 0, 12);
206 my $msg = <<EOT;
207 <message>
208 <generator>
209 <name>Girocco::Notify</name>
210 <version>1.0</version>
211 </generator>
212 <source>
213 <project>$cianame</project>
215 if ($branch ne 'master') { # XXX: Check HEAD instead
216 $msg .= "<branch>$branch</branch>";
218 $msg .= "</source>\n";
219 $msg .= "<timestamp>$at</timestamp>\n";
220 $msg .= "<body><commit><author>$a</author><revision>$rev</revision>\n";
221 $msg .= "<url>$Girocco::Config::gitweburl/$proj->{name}.git/commit/$commit</url>\n";
222 $msg .= "<files>\n";
223 foreach (@f) { $msg .= "<file>$_</file>\n"; }
224 $msg .= "</files><log>$subj</log></commit></body></message>\n";
226 # print "$msg\n";
227 #my $rpc_client = new RPC::XML::Client "http://cia.vc/RPC2";
228 #my $rpc_request = RPC::XML::request->new('hub.deliver', $msg);
229 #my $rpc_response = $rpc_client->send_request($rpc_request);
230 #ref $rpc_response or print STDERR "XML-RPC Error: $RPC::XML::ERROR\n";
233 sub cia {
234 my ($cianame, $proj, $ref, $oldrev, $newrev) = @_;
236 # CIA notifications for branches only
237 my $branch = $ref;
238 $branch =~ s#^refs/heads/## or return;
240 my @commits = get_commits($proj, $ref, $oldrev, $newrev);
241 my $root = ($oldrev =~ /^0+$/) ? $commits[$#commits] : "";
242 foreach my $commit (@commits) {
243 cia_commit($cianame, $proj, $branch, $commit, $root);
245 print "$proj->{name}.git: CIA.vc is defunct ($cianame)\n";
249 sub get_commits {
250 my ($proj, $ref, $oldrev, $newrev) = @_;
252 return () if $newrev =~ /^0+$/;
254 my @gcmd = ($Girocco::Config::git_bin, '--git-dir='.$proj->{path});
255 my $fd;
257 open $fd, '-|', @gcmd, 'for-each-ref', '--format=%(refname)', 'refs/heads/'
258 or die "cannot do git for-each-ref: $! $?";
259 my @refs = <$fd>;
260 chomp @refs;
261 @refs = grep { $_ ne $ref } @refs;
262 close $fd;
264 my @revlims;
265 if (@refs) {
266 open $fd, '-|', @gcmd, 'rev-parse', '--not', @refs
267 or die "cannot do git rev-list for revlims: $! $?";
268 @revlims = <$fd>;
269 chomp @revlims;
270 close $fd;
273 my $revspec = (($oldrev =~ /^0+$/) ? $newrev : "$oldrev..$newrev");
274 open $fd, '-|', @gcmd, 'rev-list', @revlims, $revspec
275 or die "cannot do git rev-list: $! $?";
276 my @revs = <$fd>;
277 chomp @revs;
278 close $fd;
280 return @revs;
283 sub _get_sender_uuid {
284 my ($proj, $user) = @_;
286 my $sender;
287 my $xtrahdr = '';
288 my $senderemail = $proj->{email};
289 defined($senderemail) && $senderemail ne ''
290 or $senderemail = $Girocco::Config::sender;
291 if ($user) {
292 if ($user->{name} eq 'mob') {
293 $sender = "The Mob User <$senderemail>";
294 } else {
295 my $useremail = $user->{email};
296 defined($useremail) && $useremail ne ''
297 or $useremail = $senderemail;
298 $sender = "$user->{name} <$useremail>";
299 $xtrahdr = "X-User-UUID: $user->{uuid}" if $user->{uuid};
301 } else {
302 $sender = "$proj->{name} <$senderemail>";
305 return ($sender, $xtrahdr);
308 sub _notify_for_ref {
309 $_[0] =~ m{^refs/heads/.} || $_[0] =~ m{^refs/tags/.};
312 my $_ref_change;
313 BEGIN {$_ref_change = sub {
314 my ($proj, $user, $ref, $oldrev, $newrev) = @_;
315 _notify_for_ref($ref) or return (0, 0, undef);
317 my $mail_sh_ran = 0;
318 my $git_ran = 0;
319 my $ind = undef;
320 chdir($proj->{path});
322 # First, possibly send out various mails
323 if (($ref =~ m{^refs/heads/.} && $proj->{notifymail}) ||
324 ($ref =~ m{^refs/tags/.} && ($proj->{notifymail} || $proj->{notifytag}))) {
325 my ($sender, $xtrahdr) = _get_sender_uuid($proj, $user);
326 my @cmd = ($Girocco::Config::basedir.'/taskd/mail.sh',
327 "$ref", "$oldrev", "$newrev", $proj->{name},
328 $sender, $xtrahdr);
329 if (system(@cmd)) {
330 if ($? < 0 && exists($ENV{MAIL_SH_OTHER_BRANCHES})) {
331 # Let's go again
332 delete $ENV{MAIL_SH_OTHER_BRANCHES};
333 system(@cmd);
335 $? and warn "mail.sh failed";
337 $mail_sh_ran = 1;
340 # Next, send JSON packet to given URL if enabled.
341 if ($proj->{notifyjson}) {
342 ($ind, $git_ran) = ref_indicator($proj->{path}, $oldrev, $newrev);
343 json($proj->{notifyjson}, $proj, $user, $ref, $oldrev, $newrev, $ind eq '...');
346 # Also send CIA notifications.
347 if ($proj->{notifycia}) {
348 cia($proj->{notifycia}, $proj, $ref, $oldrev, $newrev);
351 return ($mail_sh_ran, $git_ran, $ind);
354 # ref_changes($proj, $user, [$coderef,] [$oldrefs,] @changes)
355 # $coderef gets called with ($oldrev, $newrev, $refname, $mail_sh_ran, $git_ran, $ind)
356 # as each update is processed where $mail_sh_ran will be true if mail.sh was run
357 # $ran_git will be true if git was run and $ind will be undef unless ref_indicator
358 # was run in which case it will be the ref_indicator '..' or '...' result
359 # $oldrefs is an optional hash of $$oldrefs{$refname} = $oldvalue
360 # @changes is array of [$oldrev, $newrev, $ref]
361 sub ref_changes {
362 use IPC::Open2;
364 my $proj = shift;
365 my $user = shift;
366 my $proc;
367 my $oldrefs;
368 $proc = shift if ref($_[0]) eq 'CODE';
369 $oldrefs = shift if ref($_[0]) eq 'HASH';
370 return if !@_ || ref($_[0]) ne 'ARRAY' || @{$_[0]} != 3 ||
371 !${$_[0]}[0] || !${$_[0]}[1] || !${$_[0]}[2];
373 # run custom notify if present
374 # hook protocol is the same as for Git's post-receive hook where each
375 # line sent to the hook's stdin has the form:
376 # oldhash newhash fullrefname
377 # with the following additions:
378 # * four command line arguments are passed:
379 # 1. project name (e.g. "proj" "proj/fork" "proj/fork/sub" etc.)
380 # 2. "user" responsible for the changes
381 # 3. total number of lines coming on stdin
382 # 4. how many of those lines are "context" lines (sent first)
383 # * "context" lines are sent first before any actual change lines
384 # * "context" lines have the same format except oldhash equals newhash
385 # * "context" lines give the value unchanged refs had at the time
386 # the changes to the non-"context" refs were made
387 # * currently "context" lines are only provided for "refs/heads/..."
388 # * current directory will always be the repository's top-level $GIT_DIR
389 # * the PATH is guaranteed to find the correct Git, utils and $basedir/bin
390 # * the hook need not consume all (or any) of stdin
391 # * the exit code for the hook is ignored (just like Git's post-receive)
392 # * the GIROCCO_BASEDIR environment variable is set to $Girocco::Config::basedir
393 my $customhook;
394 if (($customhook = $proj->_has_notifyhook)) {{
395 my @argv = ();
396 my $argv0;
397 if (is_shellish($customhook)) {
398 $argv0 = $Girocco::Config::posix_sh_bin || "/bin/sh";
399 push(@argv, $argv0, "-c", $customhook.' "$@"');
400 } else {
401 -f $customhook && -x _ or last;
402 $argv0 = $customhook;
404 my $username = "";
405 $username = $user->{name} if $user && defined($user->{name});
406 $username ne "" or $username = "-";
407 push(@argv, $argv0, $proj->{name}, $username);
408 my @mod = grep({$$_[2] =~ m{^refs/.} && $$_[1] ne "" && $$_[2] ne "" && $$_[1] ne $$_[2]} @_);
409 my %modref = map({($$_[2] => 1)} @mod);
410 my %same = ();
411 do {do {$same{$_} = $$oldrefs{$_} if !exists($modref{$_})} foreach keys %$oldrefs} if $oldrefs;
412 my @same = ();
413 push(@same, [$same{$_}, $same{$_}, $_]) foreach sort keys %same;
414 push(@argv, @same + @mod, @same + 0);
415 chdir($proj->{path}) or last;
416 local $ENV{GIROCCO_BASEDIR} = $Girocco::Config::basedir;
417 local $ENV{PATH} = util_path;
418 if (open my $hookpipe, '|-', @argv) {
419 local $SIG{'PIPE'} = sub {};
420 print $hookpipe map("$$_[0] $$_[1] $$_[2]\n", @same, @mod);
421 close $hookpipe;
422 } else {
423 print STDERR "$proj->{name}: failed to run notifyhook \"$customhook\": $!\n";
427 # don't even try the fancy stuff if there are too many heads
428 my $maxheads = int(100000 / (1 + length(${$_[0]}[0])));
429 my $newheadsdone = 0;
430 my @newheads = grep({$$_[2] =~ m{^refs/heads/.} && $$_[1] !~ /^0+$/} @_);
432 if ($oldrefs && @newheads && keys(%$oldrefs) + @newheads <= $maxheads) {{
433 my $projarg = '--git-dir='.$proj->{path};
435 # git merge-base --independent requires v1.7.3 or later
436 # We run it and if it fails just end up not processing indep heads last
437 # There is no version of merge-base --independent that accepts stdin revs
438 my %indep = map { $_ => 1 } split(' ',
439 get_git($projarg, 'merge-base', '--independent', map($$_[1], @newheads)) || '');
441 # We pass the revisions on stdin so this should never fail unless one of
442 # the input revisions is no longer valid (which should never happen).
443 # However, if it does, we just fall back to the old non-fancy technique.
444 my ($inp, $out, $pid2);
445 $pid2 = eval { open2($out, $inp, $Girocco::Config::git_bin, $projarg,
446 'rev-list', '--no-walk', '--reverse', '--stdin') };
447 $pid2 or last;
449 local $SIG{'PIPE'} = sub {};
450 print $inp map("$$_[1]\n", @newheads);
451 close $inp;
453 my @ordered = <$out>;
454 close $out;
455 waitpid $pid2, 0;
456 @ordered or last; # if we got nothing it failed
457 chomp(@ordered);
459 my %updates = ();
460 push(@{$updates{$$_[1]}}, $_) foreach @newheads;
461 my %curheads = %$oldrefs;
462 my $headcmp = sub {
463 if ($_[0] eq 'refs/heads/master') {
464 return ($_[1] eq 'refs/heads/master') ? 0 : -1;
465 } elsif ($_[1] eq 'refs/heads/master') {
466 return 1;
467 } else {
468 return $_[0] cmp $_[1];
471 foreach (grep(!$indep{$_}, @ordered), grep($indep{$_}, @ordered)) {
472 foreach my $change (sort {&$headcmp($$a[2], $$b[2])} @{$updates{$_}}) {
473 my ($old, $new, $ref) = @$change;
474 $ENV{MAIL_SH_OTHER_BRANCHES} = join(' ', values(%curheads));
475 my ($mail_sh_ran, $git_ran, $ind) =
476 &$_ref_change($proj, $user, $ref, $old, $new);
477 &$proc($old, $new, $ref, $mail_sh_ran, $git_ran, $ind) if $proc;
478 $curheads{$ref} = $new;
481 $newheadsdone = 1;
484 delete $ENV{MAIL_SH_OTHER_BRANCHES};
485 foreach (@_) {
486 my ($old, $new, $ref) = @$_;
487 if (!$newheadsdone || $ref !~ m{^refs/heads/.} || $new =~ /^0+$/) {
488 my $ran_mail_sh = &$_ref_change($proj, $user, $ref, $old, $new);
489 &$proc($old, $new, $ref, $ran_mail_sh) if $proc;