Skip to content
This repository has been archived by the owner on Apr 13, 2021. It is now read-only.

Commit

Permalink
Hacked in github integration
Browse files Browse the repository at this point in the history
  • Loading branch information
techman83 committed Apr 26, 2016
1 parent 9d91009 commit c083c43
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 6 deletions.
5 changes: 3 additions & 2 deletions bin/ckan-webhooks
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ use File::Path qw(mkpath);

# VERSION

# TODO: Investigate config passing
$ENV{XKAN_GHSECRET} or die 'XKAN_GHSECRET needs to be set';

if ( -e "/tmp/xKan_netkan.lock" ) {
unlink "/tmp/xKan_netkan.lock";
}
Expand All @@ -31,8 +34,6 @@ if (config->{environment} eq 'development') {
config->{logger}{location} = config->{appdir};
}

use Data::Dumper;

set serializer => 'JSON';

use App::KSP_CKAN::WebHooks;
Expand Down
5 changes: 5 additions & 0 deletions init/ckan-webhooks
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ WORKDIR=/home/$USER/CKAN-Webhooks
DAEOPTS="-E production -s Twiggy /home/$USER/perl5/bin/ckan-webhooks"
DAEMON=/home/$USER/perl5/bin/plackup

# TODO: This could use improvement.
GH_SECRET=$WORKDIR/ghsecret
test -e $GH_SECRET || exit 0
export XKAN_GHSECRET=`cat $GH_SECRET`

test -x $DAEMON || exit 0
export PERL5LIB=$PERL5LIB:/home/$USER/perl5/lib/perl5

Expand Down
77 changes: 73 additions & 4 deletions lib/App/KSP_CKAN/WebHooks.pm
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package App::KSP_CKAN::WebHooks;

use Dancer2 appname => "xKanHooks";
use App::KSP_CKAN::WebHooks::InflateNetKAN;
use Method::Signatures 20140224;
use Digest::SHA qw(hmac_sha1_hex);
use File::Basename 'basename';
use AnyEvent::Util;
use Try::Tiny;
use File::Touch;
Expand Down Expand Up @@ -29,6 +32,74 @@ post '/inflate' => sub {
send_error "An array of identifiers is required", 400;
}

inflate_netkans(identifiers => \@identifiers);

status(204);
return;
};

post '/gh_inflate' => sub {
my $signature = request->header('x-hub-signature');
my $body = request->content;

if ( ! defined $signature ) {
send_error("Post header 'x-hub-signature required'", 400);
}

if ( ! $body ) {
send_error("post content required", 400);
}

if ( $signature ne calc_gh_signature( body => $body) ) {
send_error("Signature mismatch", 400);
}

my @commits;
try {
@commits = @{from_json(request->body)->{commits}};
};

if ($#commits == -1) {
info("No commits received");
send_error "An array of commits is required", 400;
}

inflate_github(commits => \@commits);

status(204);
return;
};

method calc_gh_signature($body) {
return 'sha1='.hmac_sha1_hex($body, $ENV{XKAN_GHSECRET});
}

method inflate_github($commits) {
my @files;
foreach my $commit (@{$commits}) {
push(@files, (@{$commit->{added}},@{$commit->{modified}}));
}

if ($#files == -1) {
info("Nothing add/modified");
return;
}

my @netkans;
foreach my $file (@files) {
# Lets only try to send NetKAN actual netkans.
push(@netkans, basename($file,".netkan")) if $file =~ /\.netkan$/;
}

if ($#netkans == -1) {
info("No netkans found in file list");
return;
}

inflate_netkans(identifiers => \@netkans);
}

method inflate_netkans($identifiers) {
fork_call {
my $inflater = App::KSP_CKAN::WebHooks::InflateNetKAN->new();

Expand All @@ -44,7 +115,7 @@ post '/inflate' => sub {
debug("Locking environment");
touch("/tmp/xKan_netkan.lock");

foreach my $netkan (@identifiers) {
foreach my $netkan (@{$identifiers}) {
info("Inflating $netkan");
$inflater->inflate($netkan);
info("Completed $netkan");
Expand All @@ -56,9 +127,7 @@ post '/inflate' => sub {
unlink("/tmp/xKan_netkan.lock");
return;
};

status(204);
return;
};
}

1;

0 comments on commit c083c43

Please sign in to comment.