Skip to content

Commit

Permalink
Merge branch 'gci_hook' of https://github.com/fernandobrito/parrot in…
Browse files Browse the repository at this point in the history
…to fbrito-gci-hook
  • Loading branch information
cotto committed Dec 26, 2010
2 parents 5599e9f + dc425d8 commit 5c8240a
Showing 1 changed file with 43 additions and 15 deletions.
58 changes: 43 additions & 15 deletions tools/dev/github_post_receive.pl
@@ -1,4 +1,4 @@
#! perl
#!/usr/bin/perl
# Copyright (C) 2010, Parrot Foundation.

use strict;
Expand All @@ -7,7 +7,7 @@
use CGI;
use JSON::Any;
use Mail::Sendmail;
use Tie::Function;
use Net::GitHub;

use DateTime::Format::ISO8601;
use DateTime::TimeZone::Local;
Expand All @@ -22,15 +22,25 @@ =head1 DESCRIPTION
=cut


my $github = Net::GitHub->new(
owner => 'fernandobrito', repo => 'parrot'
);

my $from = 'brito@localhost';
my @send_list = ( 'b11046@pjjkp.com' );
my $auth_token = 'abc123';

my $q = CGI->new;
print $q->header;

if( my $p = $q->param('payload') ) {
$p = JSON::Any->new->decode( $p );
#use Data::Dumper;
#warn Dumper $p;
if( $q->param('token') ne $auth_token ) {
print $q->h1("'token' GET param is wrong");
die();
}

#tie my %short, 'Tie::Function' => sub { substr( shift(), 0, 8 ) };
if( my $p_json = $q->param('payload') ) {
my $p = JSON::Any->new->decode( $p_json );

my $commits =
join '',
Expand All @@ -48,18 +58,23 @@ =head1 DESCRIPTION
$file_change_count = "$file_change_count ".($file_change_count > 1 ? 'files changed' : 'file changed');
my $file_changes = join "\n", @file_changes;

my $diff = diff_from_github_api($c{id});

$c{message} = indent($c{message});

<<EOF;
commit $c{id}
$c{url}
Author: $c{author}{name} <$c{author}{email}>
Date: $c{timestamp}
commit $c{id}
$c{url}
$c{message}
$file_change_count:
$file_changes
$file_change_count
$diff
==================
EOF
} @{$p->{commits}};
Expand All @@ -70,26 +85,26 @@ =head1 DESCRIPTION
my $commit_cnt = scalar @{$p->{commits}};
$commit_cnt = "$commit_cnt ".( $commit_cnt > 1 ? 'commits' : 'commit' );

my %mail = ( From => 'github-commits@bugs.sgn.cornell.edu',
my %mail = ( From => $from,
Subject => "[$p->{repository}{owner}{name}/$p->{repository}{name}($head)] $commit_cnt - GitHub",
Message => <<EOF,
HEAD is now $p->{after}
Home: $p->{repository}->{url}
Browse: $p->{repository}->{url}/tree/$head
Commits: $p->{repository}->{url}/commits/$head
------------------
==================
$commits
EOF
);

my @send_list = ( 'parrot-commits@lists.parrot.org' );
foreach my $to ( @send_list ) {
sendmail( %mail, To => $to )
or warn "error sending to $to: $Mail::Sendmail::error";
or print $q->p("error sending to $to: $Mail::Sendmail::error");
}

#print "OK. Log says:\n", $Mail::Sendmail::log;
print $q->p(%mail);
print $q->p("OK. Log says:\n", $Mail::Sendmail::log);
}

sub short_commitname {
Expand All @@ -110,6 +125,19 @@ sub fmt_time {
return $d->strftime(q|%a %m/%d/%y, %I:%m %p %Z|);
}

sub diff_from_github_api {
my $sha1 = shift;
my $output = '';

my $co_details = $github->commit->show( $sha1 );

for my $files_changed (@{$co_details->{modified}}) {
$output .= $files_changed->{diff} . "\n";
}

return $output;
}

# Local Variables:
# mode: cperl
# cperl-indent-level: 4
Expand Down

0 comments on commit 5c8240a

Please sign in to comment.