From bc6e18fbdbe7b716cb7c13e308c2a66a69193fcd Mon Sep 17 00:00:00 2001 From: franck cuny Date: Sun, 3 Oct 2010 11:39:27 +0200 Subject: use dbic --- lib/jitterbug/Hook.pm | 74 ++++++++++++++++++++++++++++++++++----------- lib/jitterbug/Project.pm | 34 ++++++++++++--------- lib/jitterbug/Task.pm | 6 ++-- lib/jitterbug/WebService.pm | 2 +- 4 files changed, 80 insertions(+), 36 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 }; }; diff --git a/lib/jitterbug/Project.pm b/lib/jitterbug/Project.pm index 44c6844..30821ef 100644 --- a/lib/jitterbug/Project.pm +++ b/lib/jitterbug/Project.pm @@ -1,20 +1,18 @@ package jitterbug::Project; use Dancer ':syntax'; -use jitterbug::Plugin::Redis; +use Dancer::Plugin::DBIC; use jitterbug::Plugin::Template; use DateTime; use XML::Feed; get '/:project' => sub { - my $project = params->{project}; + my $project = + schema->resultset('Project')->find( { name => params->{project} } ); - my $res = redis->get( key_project($project) ); - - send_error( "Project $project not found", 404 ) if !$res; - - my $desc = from_json($res); + send_error( "Project " . params->{project} . " not found", 404 ) + unless $project; my $builds = _sorted_builds($project); @@ -28,16 +26,20 @@ get '/:project' => sub { my @days = sort {$b cmp $a} keys %$commits; template 'project/index', - { project => $project, days => \@days, builds => $commits, %$desc }; + {project => $project, days => \@days, commits => $commits}; }; get '/:project/feed' => sub { - my $project = params->{project}; + my $project = + schema->resultset('Project')->find( { name => params->{project} } ); + + send_error( "Project " . params->{project} . " not found", 404 ) + unless $project; my $builds = _sorted_builds($project); my $feed = XML::Feed->new('Atom'); - $feed->title('builds for '.$project); + $feed->title('builds for '.$project->name); foreach my $build (@$builds) { foreach my $version (keys %{$build->{version}}) { @@ -61,14 +63,16 @@ get '/:project/feed' => sub { sub _sorted_builds { my $project = shift; - my @ids = redis->smembers( key_builds_project($project) ); + my $commits = + schema->resultset('Commit') + ->search( { projectid => $project->projectid } ); my @builds; - foreach my $id (@ids) { - my $res = redis->get($id); - push @builds, from_json($res) if $res; + while ( my $c = $commits->next ) { + push @builds, from_json( $c->content ); } - @builds = sort {$b->{timestamp} cmp $a->{timestamp}} @builds; + + @builds = sort { $b->{timestamp} cmp $a->{timestamp} } @builds; \@builds; } diff --git a/lib/jitterbug/Task.pm b/lib/jitterbug/Task.pm index bef99bc..f14a9c6 100644 --- a/lib/jitterbug/Task.pm +++ b/lib/jitterbug/Task.pm @@ -1,19 +1,19 @@ package jitterbug::Task; use Dancer ':syntax'; -use jitterbug::Plugin::Redis; +use Dancer::Plugin::DBIC; use jitterbug::Plugin::Template; get '/:task_id' => sub { my $task_id = params->{task_id}; - my $task = redis->get($task_id); + my $task = schema->resultset('Task')->search($task_id); if (!$task) { render_error("task doesn't exists", 404); } - template 'task/index', {task => from_json($task)}; + template 'task/index', {task => $task }; }; 1; diff --git a/lib/jitterbug/WebService.pm b/lib/jitterbug/WebService.pm index c8be8fd..65f6963 100644 --- a/lib/jitterbug/WebService.pm +++ b/lib/jitterbug/WebService.pm @@ -1,7 +1,7 @@ package jitterbug::WebService; use Dancer ':syntax'; -use jitterbug::Plugin::Redis; +use Dancer::Plugin::DBIC; use File::Spec; -- cgit 1.4.1