[Bps-public-commit] r16306 - in sd/trunk: lib/App/SD/Replica/hm t

ruz at bestpractical.com ruz at bestpractical.com
Thu Oct 16 01:20:05 EDT 2008


Author: ruz
Date: Thu Oct 16 01:20:04 2008
New Revision: 16306

Added:
   sd/trunk/t/sd-hm-update.t
Modified:
   sd/trunk/lib/App/SD/Replica/hm.pm
   sd/trunk/lib/App/SD/Replica/hm/PullEncoder.pm
   sd/trunk/lib/App/SD/Replica/hm/PushEncoder.pm

Log:
* first step towards pushing updates into HM
* move property maps to higher level of abstraction

Modified: sd/trunk/lib/App/SD/Replica/hm.pm
==============================================================================
--- sd/trunk/lib/App/SD/Replica/hm.pm	(original)
+++ sd/trunk/lib/App/SD/Replica/hm.pm	Thu Oct 16 01:20:04 2008
@@ -155,6 +155,54 @@
     return "/task/".$id;
 }
 
+our %PROP_MAP = (
+    owner_id                 => 'owner',
+    requestor_id             => 'reported_by',
+    priority                 => 'priority_integer',
+    completed_at             => 'completed',
+    due                      => 'due',
+    creator                  => 'creator',
+    attachment_count         => '_delete',
+    depended_on_by_count     => '_delete',
+    depended_on_by_summaries => '_delete',
+    depends_on_count         => '_delete',
+    depends_on_summaries     => '_delete',
+    group_id                 => '_delete',
+    last_repeat              => '_delete',
+    repeat_days_before_due   => '_delete',
+    repeat_every             => '_delete',
+    repeat_of                => '_delete',
+    repeat_next_create       => '_delete',
+    repeat_period            => '_delete',
+    repeat_stacking          => '_delete',
+
+);
+
+our %REV_PROP_MAP = ();
+while ( my ($k, $v) = each %PROP_MAP ) {
+    if ( $REV_PROP_MAP{ $v } ) {
+        $REV_PROP_MAP{ $v } = [ $REV_PROP_MAP{ $v } ]
+            unless ref $REV_PROP_MAP{ $v };
+        push @{ $REV_PROP_MAP{ $v } }, $k;
+    } else {
+        $REV_PROP_MAP{ $v } = $k;
+    }
+}
+
+sub property_map {
+    my $self = shift;
+    my $dir = shift || 'pull';
+    if ( $dir eq 'pull' ) {
+        return %PROP_MAP;
+    }
+    elsif ( $dir eq 'push' ) {
+        return %REV_PROP_MAP;
+    }
+    else {
+        die "unknown direction";
+    }
+}
+
 __PACKAGE__->meta->make_immutable;
 no Moose;
 1;

Modified: sd/trunk/lib/App/SD/Replica/hm/PullEncoder.pm
==============================================================================
--- sd/trunk/lib/App/SD/Replica/hm/PullEncoder.pm	(original)
+++ sd/trunk/lib/App/SD/Replica/hm/PullEncoder.pm	Thu Oct 16 01:20:04 2008
@@ -145,33 +145,12 @@
     Dec => 12
 };
 
-our %PROP_MAP = (
-    owner_id                 => 'owner',
-    requestor_id             => 'reported_by',
-    priority                 => 'priority_integer',
-    completed_at             => 'completed',
-    due                      => 'due',
-    creator                  => 'creator',
-    attachment_count         => '_delete',
-    depended_on_by_count     => '_delete',
-    depended_on_by_summaries => '_delete',
-    depends_on_count         => '_delete',
-    depends_on_summaries     => '_delete',
-    group_id                 => '_delete',
-    last_repeat              => '_delete',
-    repeat_days_before_due   => '_delete',
-    repeat_every             => '_delete',
-    repeat_of                => '_delete',
-    repeat_next_create       => '_delete',
-    repeat_period            => '_delete',
-    repeat_stacking          => '_delete',
-
-);
-
 sub translate_props {
     my $self      = shift;
     my $changeset = shift;
 
+    my %PROP_MAP = $self->sync_source->property_map('pull');
+
     for my $change ( $changeset->changes ) {
         next unless $change->record_type eq 'ticket';
         my @new_props;

Modified: sd/trunk/lib/App/SD/Replica/hm/PushEncoder.pm
==============================================================================
--- sd/trunk/lib/App/SD/Replica/hm/PushEncoder.pm	(original)
+++ sd/trunk/lib/App/SD/Replica/hm/PushEncoder.pm	Thu Oct 16 01:20:04 2008
@@ -52,8 +52,6 @@
     return $id;
 }
 
-
-
 sub integrate_ticket_create {
     my $self = shift;
     my ( $change, $changeset ) = validate_pos( @_, { isa => 'Prophet::Change' }, { isa => 'Prophet::ChangeSet' } );
@@ -88,7 +86,8 @@
         or die "Couldn't get remote id of SD ticket";
 
     my $email = $self->comment_as_email( \%props );
-    my $status = $self->sync_source->hm->act( 'CreateTaskEmail',
+    my $status = $self->sync_source->hm->act(
+        'CreateTaskEmail',
         task_id => $ticket_id,
         message => $email->as_string,
     );
@@ -98,12 +97,29 @@
 }
 
 sub integrate_ticket_update {
-    warn "update not implemented yet";
+    my $self = shift;
+    my ($change, $changeset) = validate_pos( @_, { isa => 'Prophet::Change' }, {isa => 'Prophet::ChangeSet'} );
+
+    my %props = $self->translate_props( $change );
+    return unless %props;
+
+    my $ticket_id = $self->sync_source->remote_id_for_uuid( $change->record_uuid )
+        or die "Couldn't get remote id of SD ticket";
+
+    my $status = $self->sync_source->hm->act(
+        'UpdateTask',
+        id => $ticket_id,
+        %props,
+    );
+    return $status->{'content'}{'id'} if $status->{'success'};
+
+    die "Couldn't integrate comment: ". $status->{'error'};
 }
 
 sub _recode_props_for_create {
     my $self = shift;
     my $attr = $self->_recode_props_for_integrate(@_);
+
     my $source_props = $self->sync_source->props;
     return $attr unless $source_props;
 
@@ -148,8 +164,8 @@
     my ($change) = validate_pos( @_, { isa => 'Prophet::Change' } );
 
     my %props = map { $_->name => $_->new_value } $change->prop_changes;
-    my %attr;
 
+    my %attr;
     for my $key ( keys %props ) {
         # XXX: fill me in
         #        next unless ( $key =~ /^(summary|queue|status|owner|custom)/ );
@@ -158,8 +174,22 @@
     return \%attr;
 }
 
+sub translate_props {
+    my $self     = shift;
+    my ($change) = validate_pos( @_, { isa => 'Prophet::Change' } );
+
+    my %PROP_MAP = $self->sync_source->property_map('push');
+
+    my %props = map { $_->name => $_->new_value } $change->prop_changes;
+    delete $props{ $_ } for @{ delete $PROP_MAP{'_delete'} };
+    while ( my ($k, $v) = each %PROP_MAP ) {
+        next unless exists $props{$k};
+        $props{$v} = delete $props{$k};
+    }
+    return %props;
+}
+
 __PACKAGE__->meta->make_immutable;
 no Moose;
 
 1;
-

Added: sd/trunk/t/sd-hm-update.t
==============================================================================
--- (empty file)
+++ sd/trunk/t/sd-hm-update.t	Thu Oct 16 01:20:04 2008
@@ -0,0 +1,64 @@
+#!/usr/bin/env perl
+use warnings;
+use strict;
+use Prophet::Test;
+use App::SD::Test;
+
+BEGIN {
+    if ( $ENV{'JIFTY_APP_ROOT'} ) {
+        plan tests => 7;
+        require File::Temp;
+        $ENV{'PROPHET_REPO'} = $ENV{'SD_REPO'} = File::Temp::tempdir( CLEANUP => 0 ) . '/_svb';
+        diag $ENV{'PROPHET_REPO'};
+        eval "use Jifty";
+        push @INC, File::Spec->catdir( Jifty::Util->app_root, "lib" );
+    } else {
+        plan skip_all => "You must define a JIFTY_APP_ROOT environment variable which points to your hiveminder source tree";
+    }
+}
+
+eval 'use BTDT::Test; 1;' or die "$@";
+
+my $server = BTDT::Test->make_server;
+my $URL    = $server->started_ok;
+$URL =~ s|http://|http://onlooker\@example.com:something@|;
+my $sd_hm_url = "hm:$URL";
+
+ok( 1, "Loaded the test script" );
+my $root = BTDT::CurrentUser->superuser;
+my $as_root = BTDT::Model::User->new( current_user => $root );
+$as_root->load_by_cols( email => 'onlooker at example.com' );
+my ( $val, $msg ) = $as_root->set_accepted_eula_version( Jifty->config->app('EULAVersion') );
+ok( $val, $msg );
+my $GOODUSER = BTDT::CurrentUser->new( email => 'onlooker at example.com' );
+$GOODUSER->user_object->set_accepted_eula_version( Jifty->config->app('EULAVersion') );
+
+my $task = BTDT::Model::Task->new( current_user => $GOODUSER );
+$task->create(
+    summary     => "YATTA",
+    description => '',
+);
+my $remote_id = $task->id;
+
+my ($yatta_uuid, $yatta_id);
+{
+    my ($ret, $out, $err) = run_script( 'sd', [ 'pull', '--from', $sd_hm_url ] );
+
+    run_output_matches( 'sd', [qw(ticket list --regex .)], [qr/(.*?)(?{ $yatta_uuid = $1 }) YATTA (.*)/] );
+    ( $ret, $out, $err ) = run_script( 'sd', [ qw(ticket show --batch --id), $yatta_uuid ] );
+    ($yatta_id, $yatta_uuid) = ($1, $2) if $out =~ /^id: (\d+)\s*\((.*)\)/m;
+}
+
+is_script_output( 'sd', [ qw(ticket update --uuid), $yatta_uuid, qw(-- --summary BLABLA) ],
+    [qr/ticket \d+ \(\Q$yatta_uuid\E\) updated./], # stdout
+    [undef],             # stderr
+    "updated summary"
+);
+{
+    my ( $ret, $out, $err ) = run_script( 'sd', [ 'push','--to', $sd_hm_url ] );
+
+    my $task = BTDT::Model::Task->new( current_user => $GOODUSER );
+    ok( $task->load_by_cols( summary => 'BLABLA' ), "loaded a task" );
+    is( $task->id, $remote_id, "the same task" );
+}
+



More information about the Bps-public-commit mailing list