Skip to content

Commit 145dcaf

Browse files
committed
New email webhook.
This still needs a touch of work. In particular, the Change Date and Push Date have differing formats as that's how github sends the data. I intend to actually parse them and make them consistent.
1 parent 4d69293 commit 145dcaf

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed

git_hooks/email_hook.conf

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# This can be used in a VirtualHost or globally
2+
#
3+
# The directory is pointing into a checked out copy of
4+
# https://github.com/MythTV/extras
5+
#
6+
# This requires mod_perl2 for proper operation
7+
8+
Alias /git_hooks/ "/opt/git/extras/git_hooks/"
9+
<Directory "/opt/git/extras/git_hooks/">
10+
SetHandler perl-script
11+
PerlResponseHandler ModPerl::Registry
12+
PerlOptions +ParseHeaders
13+
Options +ExecCGI
14+
Order allow,deny
15+
Allow from all
16+
</Directory>

git_hooks/email_hook.pl

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#! /usr/bin/perl
2+
# vim:ts=4:sw=4:ai:et:si:sts=4
3+
use strict;
4+
use warnings;
5+
use Apache2::Const -compile => qw(M_POST HTTP_METHOD_NOT_ALLOWED);
6+
use CGI;
7+
use JSON;
8+
use Mail::Send;
9+
10+
my $debug = 0;
11+
12+
my $r = shift;
13+
14+
unless ($r->method_number == Apache2::Const::M_POST) {
15+
$r->allowed($r->allowed | (1 << Apache2::Const::M_POST));
16+
$r->status(Apache2::Const::HTTP_METHOD_NOT_ALLOWED);
17+
return;
18+
}
19+
20+
$r->content_type('text/html');
21+
$r->print();
22+
23+
# Payload is described at http://help.github.com/post-receive-hooks/
24+
my $json = JSON->new->utf8;
25+
my $payload = CGI->new->param('payload');
26+
$payload = $json->decode($payload);
27+
28+
if ( $debug ) {
29+
open FH, ">", "/tmp/dump.json";
30+
print FH $json->pretty->encode($payload);
31+
close FH;
32+
}
33+
34+
my $repository = $payload->{"repository"}->{"name"};
35+
my $branch = $payload->{"ref"};
36+
$branch =~ s/^refs\/.*?\///;
37+
38+
# These maybe should go into a config file later
39+
my %headers = (
40+
"From" => 'MythTV <noreply@mythtv.org>',
41+
"To" => 'mythtv-commits@mythtv.org',
42+
"Reply-to" => 'mythtv-dev@mythtv.org',
43+
"X-Repository" => $repository,
44+
);
45+
46+
foreach my $commit ( @{$payload->{"commits"}} ) {
47+
my $longsha = $commit->{"id"};
48+
my $shortsha = substr $longsha, 0, 7;
49+
my $changeurl = $commit->{"url"};
50+
$changeurl =~ s/$longsha$/$shortsha/;
51+
52+
my $subject = "$repository commit: $shortsha by " .
53+
$commit->{"author"}->{"username"};
54+
55+
my $email = <<EOF;
56+
Author: $commit->{"author"}->{"name"} <$commit->{"author"}->{"email"}>
57+
Change Date: $commit->{"timestamp"}
58+
Push Date: $payload->{"repository"}->{"pushed_at"}
59+
Repository: $repository
60+
Branch: $branch
61+
New Revision: $commit->{"id"}
62+
Changeset: $changeurl
63+
64+
Log:
65+
66+
$commit->{"message"}
67+
68+
EOF
69+
70+
my @array = @{$commit->{"added"}};
71+
if ($#array != -1) {
72+
$email .= "Added:\n\n " . join("\n ", @array) . "\n\n";
73+
}
74+
75+
@array = @{$commit->{"removed"}};
76+
if ($#array != -1) {
77+
$email .= "Removed:\n\n " . join("\n ", @array) . "\n\n";
78+
}
79+
80+
@array = @{$commit->{"modified"}};
81+
if ($#array != -1) {
82+
$email .= "Modified:\n\n " . join("\n ", @array) . "\n\n";
83+
}
84+
85+
# Send the email
86+
my $msg = Mail::Send->new;
87+
$msg->subject($subject);
88+
foreach my $h (keys %headers) {
89+
$msg->set($h, $headers{$h});
90+
}
91+
92+
my $fh = $msg->open;
93+
print $fh $email;
94+
$fh->close;
95+
}
96+

0 commit comments

Comments
 (0)