1 package Girocco
::Notify
;
13 # This Perl code creates json payload within post-receive hook.
16 my ($proj, $commit) = @_;
18 my @gcmd = ($Girocco::Config
::git_bin
, '--git-dir='.$proj->{path
});
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: $! $?";
26 my ($ae, $an) = ($l[0] =~ /^(.*?) (.*)$/);
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: $! $?";
37 my ($s, $file) = split(/\t/, $_);
54 "url" => $Girocco::Config
::gitweburl
."/".$proj->{name
}.".git/commit/".$commit,
55 "author" => { "name" => $an, "email" => $ae },
61 my ($url, $proj, $user, $ref, $oldrev, $newrev) = @_;
65 $pusher = { "name" => $user->{name
} };
66 if ($user->{name
} ne 'mob') {
67 $pusher->{"email"} = $user->{email
};
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
{
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
} }
100 "commits" => $commits
103 # print "$payload\n";
104 my $ua = LWP
::UserAgent
->new;
106 $ua->post($url, { payload
=> $payload });
111 my ($proj, $ref, $oldrev, $newrev) = @_;
113 my @gcmd = ($Girocco::Config
::git_bin
, '--git-dir='.$proj->{path
});
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>;
124 open $fd, '-|', @gcmd, 'rev-list', '--not', @refs
125 or die "cannot do git rev-list for revlims: $! $?";
131 my $revspec = (($oldrev =~ /^0+$/) ?
$newrev : "$oldrev..$newrev");
132 open $fd, '-|', @gcmd, 'rev-list', @revlims, $revspec
133 or die "cannot do git rev-list: $! $?";
143 my ($proj, $user, $ref, $oldrev, $newrev) = @_;
145 chdir($proj->{path
});
147 # First, possibly send out various mails
148 if ($proj->{notifymail
}) {
151 if ($user->{name
} eq 'mob') {
152 $sender = "The Mob User <$proj->{email}>";
154 $sender = "$user->{name} <$user->{email}>";
157 $sender = "$proj->{name} <$proj->{email}>";
159 system($Girocco::Config
::basedir
.'/taskd/mail.sh',
160 "$ref", "$oldrev", "$newrev", $proj->{name
},
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);