diff options
author | franck cuny <franck@lumberjaph.net> | 2010-10-03 11:39:27 +0200 |
---|---|---|
committer | franck cuny <franck@lumberjaph.net> | 2010-10-03 11:39:27 +0200 |
commit | bc6e18fbdbe7b716cb7c13e308c2a66a69193fcd (patch) | |
tree | ee5c9b3ea7bab636e7a75c05743cd0373fde8f3c /lib/jitterbug/Hook.pm | |
parent | add schema (diff) | |
download | jitterbug-bc6e18fbdbe7b716cb7c13e308c2a66a69193fcd.tar.gz |
use dbic
Diffstat (limited to '')
-rw-r--r-- | lib/jitterbug/Hook.pm | 74 |
1 files changed, 57 insertions, 17 deletions
diff --git a/lib/jitterbug/Hook.pm b/lib/jitterbug/Hook.pm index e5d1082..9492ebe 100644 --- a/lib/jitterbug/Hook.pm +++ b/lib/jitterbug/Hook.pm @@ -1,15 +1,18 @@ package jitterbug::Hook; use Dancer ':syntax'; -use jitterbug::Plugin::Redis; +use Dancer::Plugin::DBIC; + +use Try::Tiny; setting serializer => 'JSON'; post '/' => sub { my $payload = params->{payload}; + # don't confuse poster, and don't care about it if (!defined $payload) { - # don't confuse poster, and don't care about it + error("no payload in input"); status 200; return; } @@ -17,29 +20,66 @@ post '/' => sub { $payload = from_json($payload); my $repo = $payload->{repository}->{name}; - my $repo_key = key_project($repo); + my $project = schema->resultset('Project')->find( { name => $repo } ); - if ( !redis->exists($repo_key) ) { - my $project = { - name => $repo, - url => $payload->{repository}->{url}, - description => $payload->{repository}->{description}, - owner => $payload->{repository}->{owner}, + if ( !$project ) { + debug("need to create a new project"); + try { + schema->txn_do( + sub { + $project = schema->resultset('Project')->create( + { + name => $repo, + url => $payload->{repository}->{url}, + description => + $payload->{repository}->{description}, + owner => to_json($payload->{repository}->{owner}), + } + ); + } + ); + } + catch { + error($_); }; - redis->set( $repo_key, to_json($project) ); - redis->sadd( key_projects, $repo ); } my $last_commit = pop @{ $payload->{commits} }; - - $last_commit->{repo} = $payload->{repository}->{url}; - $last_commit->{project} = $repo; $last_commit->{compare} = $payload->{compare}; + $last_commit->{pusher} = $payload->{pushed}; + $last_commit->{ref} = $payload->{ref}; + + try { + schema->txn_do( + sub { + schema->resultset('Commit')->create( + { + sha256 => $last_commit->{id}, + content => to_json($last_commit), + projectid => $project->projectid, + timestamp => $last_commit->{timestamp}, + } + ); + } + ); + } + catch { + debug($_); + }; - my $task_key = key_task_repo($repo); - redis->set( $task_key, to_json($last_commit) ); + try { + schema->txn_do( + sub { + schema->resultset('Task')->create( + {sha256 => $last_commit->{id}, projectid => $project->projectid} + ); + } + ); + }catch{ + debug($_); + }; - redis->sadd( key_tasks, $task_key ); + debug("hook accepted"); { updated => $repo }; }; |