1 package Girocco
::Notify
;
17 # This Perl code creates json payload within post-receive hook.
20 my ($proj, $commit) = @_;
22 my @gcmd = ($Girocco::Config
::git_bin
, '--git-dir='.$proj->{path
});
25 open $fd, '-|', @gcmd, 'log', '-1', '--pretty=format:%ae %an%n%ai%n%s%n%n%b', $commit
26 or die "cannot do git log: $! $?";
30 my ($ae, $an) = ($l[0] =~ /^(.*?) (.*)$/);
32 my $msg = join("\n", splice(@l, 2));
33 # Up to three trailing newlines in case of no body.
34 chomp $msg; chomp $msg; chomp $msg;
36 my ($rf, $af, $mf) = ([], [], []);
37 open $fd, '-|', @gcmd, 'diff-tree', '--name-status', '-r', "$commit^", $commit
38 or die "cannot do git diff-tree: $! $?";
41 my ($s, $file) = split(/\t/, $_);
58 "url" => $Girocco::Config
::gitweburl
."/".$proj->{name
}.".git/commit/".$commit,
59 "author" => { "name" => $an, "email" => $ae },
65 my ($url, $proj, $user, $ref, $oldrev, $newrev) = @_;
69 $pusher = { "name" => $user->{name
} };
70 if ($user->{name
} ne 'mob') {
71 $pusher->{"email"} = $user->{email
};
77 foreach my $commit (get_commits
($proj, $ref, $oldrev, $newrev)) {
78 push @
$commits, json_commit
($proj, $commit);
81 # This is backwards-compatible with GitHub (except the GitHub-specific
82 # full project name construction sometimes performed in clients)
83 my $payload = encode_json
{
89 "name" => $proj->{name
},
90 # Girocco extension: full_name is full project name,
91 # equivalent to GitHub's "owner[name]/name".
92 "full_name" => $proj->{name
}.".git",
94 "url" => $Girocco::Config
::gitweburl
.'/'.$proj->{name
}.".git",
95 # Girocco extension: Pull URL.
96 "pull_url" => $Girocco::Config
::gitpullurl
.'/'.$proj->{name
}.".git",
98 "owner" => { "name" => "", "email" => $proj->{email
} }
104 "commits" => $commits
107 # print "$payload\n";
108 my $ua = LWP
::UserAgent
->new;
110 $ua->post($url, { payload
=> $payload });
115 my ($cianame, $proj, $branch, $commit) = @_;
117 my @gcmd = ($Girocco::Config
::git_bin
, '--git-dir='.$proj->{path
});
120 open $fd, '-|', @gcmd, 'log', '-1', '--pretty=format:%an <%ae>%n%at%n%s', $commit
121 or die "cannot do git log: $! $?";
125 foreach (@l) { s/&/&/g; s/</</g; s/>/>/g; }
126 my ($a, $at, $subj) = @l;
129 open $fd, '-|', @gcmd, 'diff-tree', '--name-status', '-r', "$commit^", $commit
130 or die "cannot do git diff-tree: $! $?";
133 s/&/&/g; s/</</g; s/>/>/g;
134 my ($status, $file) = split(/\t/, $_);
139 my $rev = substr($commit, 0, 12);
144 <name>Girocco::Notify</name>
145 <version>1.0</version>
148 <project>$cianame</project>
150 if ($branch ne 'master') { # XXX: Check HEAD instead
151 $msg .= "<branch>$branch</branch>";
153 $msg .= "</source>\n";
154 $msg .= "<timestamp>$at</timestamp>\n";
155 $msg .= "<body><commit><author>$a</author><revision>$rev</revision>\n";
156 $msg .= "<url>$Girocco::Config::gitweburl/$proj->{name}.git/commit/$commit</url>\n";
158 foreach (@f) { $msg .= "<file>$_</file>\n"; }
159 $msg .= "</files><log>$subj</log></commit></body></message>\n";
162 my $rpc_client = new RPC
::XML
::Client
"http://cia.vc/RPC2";
163 my $rpc_request = RPC
::XML
::request
->new('hub.deliver', $msg);
164 #my $rpc_response = $rpc_client->send_request($rpc_request);
165 #ref $rpc_response or print STDERR "XML-RPC Error: $RPC::XML::ERROR\n";
166 print STDERR
"$proj->{name}.git: CIA.vc is defunct ($cianame)\n";
170 my ($cianame, $proj, $ref, $oldrev, $newrev) = @_;
172 # CIA notifications for branches only
174 $branch =~ s
#^refs/heads/## or return;
176 foreach my $commit (get_commits
($proj, $ref, $oldrev, $newrev)) {
177 cia_commit
($cianame, $proj, $branch, $commit);
183 my ($proj, $ref, $oldrev, $newrev) = @_;
185 my @gcmd = ($Girocco::Config
::git_bin
, '--git-dir='.$proj->{path
});
188 open $fd, '-|', @gcmd, 'for-each-ref', '--format=%(refname)', 'refs/heads/'
189 or die "cannot do git for-each-ref: $! $?";
192 @refs = grep { $_ ne $ref } @refs;
197 open $fd, '-|', @gcmd, 'rev-parse', '--not', @refs
198 or die "cannot do git rev-list for revlims: $! $?";
204 my $revspec = (($oldrev =~ /^0+$/) ?
$newrev : "$oldrev..$newrev");
205 open $fd, '-|', @gcmd, 'rev-list', @revlims, $revspec
206 or die "cannot do git rev-list: $! $?";
216 my ($proj, $user, $ref, $oldrev, $newrev) = @_;
218 chdir($proj->{path
});
220 # First, possibly send out various mails
221 if ($proj->{notifymail
}) {
225 if ($user->{name
} eq 'mob') {
226 $sender = "The Mob User <$proj->{email}>";
228 $sender = "$user->{name} <$user->{email}>";
229 $xtrahdr = "X-User-UUID: $user->{uuid}" if $user->{uuid
};
232 $sender = "$proj->{name} <$proj->{email}>";
234 system($Girocco::Config
::basedir
.'/taskd/mail.sh',
235 "$ref", "$oldrev", "$newrev", $proj->{name
},
237 and warn "mail.sh failed";
240 # Next, send JSON packet to given URL if enabled.
241 if ($proj->{notifyjson
}) {
242 json
($proj->{notifyjson
}, $proj, $user, $ref, $oldrev, $newrev);
245 # Also send CIA notifications.
246 if ($proj->{notifycia
}) {
247 cia
($proj->{notifycia
}, $proj, $ref, $oldrev, $newrev);