Girocco::Notify: Include pusher info in json payload
[girocco.git] / Girocco / Notify.pm
blob32c89bf9beb73a21da1be1175d31df88214a530a
1 package Girocco::Notify;
3 use strict;
4 use warnings;
6 BEGIN {
7 use Girocco::Config;
8 use JSON;
9 use LWP::UserAgent;
13 # This Perl code creates json payload within post-receive hook.
15 sub json_commit {
16 my ($proj, $commit) = @_;
18 my @gcmd = ($Girocco::Config::git_bin, '--git-dir='.$proj->{path});
19 my $fd;
21 open $fd, '-|', @gcmd, 'log', '-1', '--pretty=format:%ae %an%n%ai%n%s%n%n%b', $commit
22 or die "cannot do git log: $! $?";
23 my @l = <$fd>;
24 chomp @l;
25 close $fd;
26 my ($ae, $an) = ($l[0] =~ /^(.*?) (.*)$/);
27 my ($ai) = $l[1];
28 my $msg = join("\n", splice(@l, 2));
29 # Up to three trailing newlines in case of no body.
30 chomp $msg; chomp $msg; chomp $msg;
32 my ($rf, $af, $mf) = ([], [], []);
33 open $fd, '-|', @gcmd, 'diff-tree', '--name-status', '-r', "$commit^", $commit
34 or die "cannot do git diff-tree: $! $?";
35 while (<$fd>) {
36 chomp;
37 my ($s, $file) = split(/\t/, $_);
38 if ($s eq 'M') {
39 push @$mf, $file;
40 } elsif ($s eq 'A') {
41 push @$af, $file;
42 } elsif ($s eq 'R') {
43 push @$rf, $file;
46 close $fd;
48 return {
49 "removed" => $rf,
50 "message" => $msg,
51 "added" => $af,
52 "timestamp" => $ai,
53 "modified" => $mf,
54 "url" => $Girocco::Config::gitweburl."/".$proj->{name}.".git/commit/".$commit,
55 "author" => { "name" => $an, "email" => $ae },
56 "id" => $commit
57 };
60 sub json {
61 my ($url, $proj, $user, $ref, $oldrev, $newrev) = @_;
63 my $pusher = {};
64 if ($user) {
65 $pusher = { "name" => $user->{name} };
66 if ($user->{name} ne 'mob') {
67 $pusher->{"email"} = $user->{email};
71 my $commits = [];
73 foreach my $commit (get_commits($proj, $ref, $oldrev, $newrev)) {
74 push @$commits, json_commit($proj, $commit);
77 # This is backwards-compatible with GitHub (except the GitHub-specific
78 # full project name construction sometimes performed in clients)
79 my $payload = encode_json {
80 "before" => $oldrev,
81 "after" => $newrev,
82 "ref" => $ref,
84 "repository" => {
85 "name" => $proj->{name},
86 # Girocco extension: full_name is full project name,
87 # equivalent to GitHub's "owner[name]/name".
88 "full_name" => $proj->{name}.".git",
90 "url" => $Girocco::Config::gitweburl.'/'.$proj->{name}.".git",
91 # Girocco extension: Pull URL.
92 "pull_url" => $Girocco::Config::gitpullurl.'/'.$proj->{name}.".git",
94 "owner" => { "name" => "", "email" => $proj->{email} }
97 # Girocco extension
98 "pusher" => $pusher,
100 "commits" => $commits
103 # print "$payload\n";
104 my $ua = LWP::UserAgent->new;
105 $ua->timeout(5);
106 $ua->post($url, { payload => $payload });
110 sub get_commits {
111 my ($proj, $ref, $oldrev, $newrev) = @_;
113 my @gcmd = ($Girocco::Config::git_bin, '--git-dir='.$proj->{path});
114 my $fd;
116 open $fd, '-|', @gcmd, 'for-each-ref', '--format=%(refname)', 'refs/heads/'
117 or die "cannot do git for-each-ref: $! $?";
118 my @refs = grep { $_ ne $ref } <$fd>;
119 chomp @refs;
120 close $fd;
122 my @revlims;
123 if (@refs) {
124 open $fd, '-|', @gcmd, 'rev-list', '--not', @refs
125 or die "cannot do git rev-list for revlims: $! $?";
126 @revlims = <$fd>;
127 chomp @revlims;
128 close $fd;
131 my $revspec = (($oldrev =~ /^0+$/) ? $newrev : "$oldrev..$newrev");
132 open $fd, '-|', @gcmd, 'rev-list', @revlims, $revspec
133 or die "cannot do git rev-list: $! $?";
134 my @revs = <$fd>;
135 chomp @revs;
136 close $fd;
138 return @revs;
142 sub ref_change {
143 my ($proj, $user, $ref, $oldrev, $newrev) = @_;
145 chdir($proj->{path});
147 # First, possibly send out various mails
148 if ($proj->{notifymail}) {
149 my $sender;
150 if ($user) {
151 if ($user->{name} eq 'mob') {
152 $sender = "The Mob User <$proj->{email}>";
153 } else {
154 $sender = "$user->{name} <$user->{email}>";
156 } else {
157 $sender = "$proj->{name} <$proj->{email}>";
159 system($Girocco::Config::basedir.'/taskd/mail.sh',
160 "$ref", "$oldrev", "$newrev", $proj->{name},
161 $sender)
162 and warn "mail.sh failed";
165 # Next, send JSON packet to given URL if enabled.
166 if ($proj->{notifyjson}) {
167 json($proj->{notifyjson}, $proj, $user, $ref, $oldrev, $newrev);