[Bps-public-commit] SD branch, master, updated. 0.70-36-gde37c2b

sunnavy at bestpractical.com sunnavy at bestpractical.com
Thu Sep 3 09:49:48 EDT 2009


The branch, master has been updated
       via  de37c2b45a9f9877b65906e93c0cf2defc404360 (commit)
      from  f02bcca6ccc39d9355b277cead100da74709f0e9 (commit)

Summary of changes:
 lib/App/SD/Replica/lighthouse/PullEncoder.pm |  243 +++++++++++++++++---------
 lib/App/SD/Replica/lighthouse/PushEncoder.pm |   23 +++-
 2 files changed, 184 insertions(+), 82 deletions(-)

- Log -----------------------------------------------------------------
commit de37c2b45a9f9877b65906e93c0cf2defc404360
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Thu Sep 3 21:49:40 2009 +0800

    attachment support for lighthouse, but push only has skeleton: waiting for their api updates

diff --git a/lib/App/SD/Replica/lighthouse/PullEncoder.pm b/lib/App/SD/Replica/lighthouse/PullEncoder.pm
index dd11038..b405e9a 100644
--- a/lib/App/SD/Replica/lighthouse/PullEncoder.pm
+++ b/lib/App/SD/Replica/lighthouse/PullEncoder.pm
@@ -66,8 +66,10 @@ Returns a reference to an array of all transactions (as hashes) on ticket $id af
 sub find_matching_transactions {
     my $self     = shift;
     my %args     = validate( @_, { ticket => 1, starting_transaction => 1 } );
-    my @raw_txns = @{$args{ticket}->versions};
+    my @raw_versions = @{ $args{ticket}->versions };
+    my @raw_attachments = @{ $args{ticket}->attachments|| [] };
 
+    my @raw_txns = ( @raw_versions, @raw_attachments );
     my @txns;
     for my $txn ( @raw_txns ) {
         my $txn_date = $txn->created_at->epoch;
@@ -173,97 +175,121 @@ sub transcode_one_txn {
     my $ticket_uuid =
       $self->sync_source->uuid_for_remote_id( $ticket->number );
 
-    my $changeset = Prophet::ChangeSet->new(
-        {
-            original_source_uuid => $ticket_uuid,
-            original_sequence_no => $txn->created_at->epoch,
-            creator => $self->resolve_user_id_to( undef, $txn->creator_name ),
-            created => $txn->created_at->ymd . " " . $txn->created_at->hms
-        }
-    );
+    my $changeset;
 
-    my $change = Prophet::Change->new(
-        {
-            record_type => 'ticket',
-            record_uuid => $ticket_uuid,
-            change_type => 'update_file'
-        }
-    );
-    my $diffable_attrs = $txn->diffable_attributes;
-    if (keys %$diffable_attrs) {
-        my %hash  = (
-            ':tag'           => 'tag',
-            ':milestone'     => 'milestone_id',
-            ':assigned_user' => 'assigned_user_id',
-            ':state'         => 'state',
+    if ( $txn->can('filename') ) {
+        $changeset = Prophet::ChangeSet->new(
+            {
+                original_source_uuid => $ticket_uuid,
+                original_sequence_no => $txn->created_at->epoch,
+                creator =>
+                  $self->resolve_user_id_to( undef, $txn->uploader_id ),
+                created => $txn->created_at->ymd . " " . $txn->created_at->hms
+            }
         );
-        for my $attr (keys %$diffable_attrs) {
-            next unless $hash{$attr};
-            my $method = $hash{$attr};
-
-            if ( $attr eq ':milestone' ) {
-                my $old = $diffable_attrs->{$attr};
-                my $old_title;
-                if ($old) {
-
-                    # find milestone title
-                    my $milestone = $self->sync_source->lighthouse->milestone;
-                    $milestone->load($old);
-                    $old_title = $milestone->title;
-                }
-                $change->add_prop_change(
-                    name => 'milestone',
-                    new  => $ticket->milestone_id
-                    ? $ticket->milestone_title
-                    : undef,
-                    old => $old_title || $old,
-                );
+
+        # it's an attachment
+        $self->_recode_attachment_create(
+            ticket_uuid => $ticket_uuid,
+            changeset   => $changeset,
+            attachment  => $txn,
+        );
+    }
+    else {
+        $changeset = Prophet::ChangeSet->new(
+            {
+                original_source_uuid => $ticket_uuid,
+                original_sequence_no => $txn->created_at->epoch,
+                creator =>
+                  $self->resolve_user_id_to( undef, $txn->creator_name ),
+                created => $txn->created_at->ymd . " " . $txn->created_at->hms
             }
-            elsif ( $attr eq ':assigned_user' ) {
-                my $old = $diffable_attrs->{$attr};
-                my $old_with_name;
-                if ($old) {
-                    require Net::Lighthouse::User;
-                    my $user = Net::Lighthouse::User->new(
-                        map { $_ => $self->sync_source->lighthouse->$_ }
-                          grep { $self->sync_source->lighthouse->$_ }
-                          qw/account
-                          email password token/
-                    );
-                    eval { $user->load($old) };
-                    if ($@) {
-                        warn "can't load user $old on lighthouse";
+        );
+
+        my $change = Prophet::Change->new(
+            {
+                record_type => 'ticket',
+                record_uuid => $ticket_uuid,
+                change_type => 'update_file'
+            }
+        );
+        my $diffable_attrs = $txn->diffable_attributes;
+        if ( keys %$diffable_attrs ) {
+            my %hash = (
+                ':tag'           => 'tag',
+                ':milestone'     => 'milestone_id',
+                ':assigned_user' => 'assigned_user_id',
+                ':state'         => 'state',
+            );
+            for my $attr ( keys %$diffable_attrs ) {
+                next unless $hash{$attr};
+                my $method = $hash{$attr};
+
+                if ( $attr eq ':milestone' ) {
+                    my $old = $diffable_attrs->{$attr};
+                    my $old_title;
+                    if ($old) {
+
+                        # find milestone title
+                        my $milestone =
+                          $self->sync_source->lighthouse->milestone;
+                        $milestone->load($old);
+                        $old_title = $milestone->title;
                     }
-                    else {
-                        $old_with_name = $user->name . '(' . $user->id . ')';
+                    $change->add_prop_change(
+                        name => 'milestone',
+                        new  => $ticket->milestone_id
+                        ? $ticket->milestone_title
+                        : undef,
+                        old => $old_title || $old,
+                    );
+                }
+                elsif ( $attr eq ':assigned_user' ) {
+                    my $old = $diffable_attrs->{$attr};
+                    my $old_with_name;
+                    if ($old) {
+                        require Net::Lighthouse::User;
+                        my $user = Net::Lighthouse::User->new(
+                            map { $_ => $self->sync_source->lighthouse->$_ }
+                              grep { $self->sync_source->lighthouse->$_ }
+                              qw/account
+                              email password token/
+                        );
+                        eval { $user->load($old) };
+                        if ($@) {
+                            warn "can't load user $old on lighthouse";
+                        }
+                        else {
+                            $old_with_name =
+                              $user->name . '(' . $user->id . ')';
+                        }
                     }
+                    $change->add_prop_change(
+                        name => 'owner',
+                        new  => $ticket->assigned_user_id
+                        ? ( $ticket->assigned_user_name . '('
+                              . $ticket->assigned_user_id
+                              . ')' )
+                        : undef,
+                        $old_with_name ? ( old => $old_with_name ) : (),
+                    );
                 }
-                $change->add_prop_change(
-                    name => 'owner',
-                    new  => $ticket->assigned_user_id
-                    ? ( $ticket->assigned_user_name . '('
-                          . $ticket->assigned_user_id
-                          . ')' )
-                    : undef,
-                    $old_with_name ? ( old => $old_with_name ) : (),
-                );
-            }
-            else {
+                else {
 
-                $change->add_prop_change(
-                    name => $PROP_MAP{ $hash{$attr} } || $hash{$attr},
-                    new  => $txn->$method,
-                    old  => $diffable_attrs->{$attr},
-                );
+                    $change->add_prop_change(
+                        name => $PROP_MAP{ $hash{$attr} } || $hash{$attr},
+                        new  => $txn->$method,
+                        old  => $diffable_attrs->{$attr},
+                    );
+                }
             }
         }
-    }
 
-    $changeset->add_change( { change => $change } )
-      if $change->has_prop_changes;
+        $changeset->add_change( { change => $change } )
+          if $change->has_prop_changes;
 
-
-    $self->_include_change_comment( $changeset, $ticket_uuid, $txn );
+        $self->_include_change_comment( $changeset, $ticket_uuid, $txn );
+    }
 
     return unless $changeset->has_changes;
     return $changeset;
@@ -298,6 +324,61 @@ sub _include_change_comment {
     }
 }
 
+sub _recode_attachment_create {
+    my $self = shift;
+    my %args =
+      validate( @_,
+        { ticket_uuid => 1, changeset => 1, attachment => 1 } );
+    my $change = Prophet::Change->new(
+        {
+            record_type => 'attachment',
+            record_uuid => $self->sync_source->uuid_for_url(
+                    $self->sync_source->remote_url
+                  . "/attachment/"
+                  . $args{'attachment'}->id,
+            ),
+            change_type => 'add_file',
+        }
+    );
+
+    $change->add_prop_change(
+        name => 'content_type',
+        old  => undef,
+        new  => $args{'attachment'}->content_type,
+    );
+
+    $change->add_prop_change(
+        name => 'created',
+        old  => undef,
+        new  => $args{attachment}->created_at->ymd . ' '
+          . $args{attachment}->created_at->hms,
+    );
+    $change->add_prop_change(
+        name => 'creator',
+        old  => undef,
+        new =>
+          $self->resolve_user_id_to( email_address =>
+              $args{'attachment'}->uploader_id )
+    );
+
+    $change->add_prop_change(
+        name => 'content',
+        old  => undef,
+        new  => $args{'attachment'}->content,
+    );
+    $change->add_prop_change(
+        name => 'name',
+        old  => undef,
+        new  => $args{'attachment'}->filename,
+    );
+    $change->add_prop_change(
+        name => 'ticket',
+        old  => undef,
+        new  => $args{ticket_uuid},
+    );
+    $args{'changeset'}->add_change( { change => $change } );
+}
+
 sub translate_prop_status {
     my $self   = shift;
     my $status = shift;
diff --git a/lib/App/SD/Replica/lighthouse/PushEncoder.pm b/lib/App/SD/Replica/lighthouse/PushEncoder.pm
index 1ca4980..254921c 100644
--- a/lib/App/SD/Replica/lighthouse/PushEncoder.pm
+++ b/lib/App/SD/Replica/lighthouse/PushEncoder.pm
@@ -32,6 +32,11 @@ sub integrate_change {
                 remote_id => $id,
             );
         }
+        elsif ( $change->record_type eq 'attachment'
+            and $change->change_type eq 'add_file' )
+        {
+            $id = $self->integrate_attachment( $change, $changeset );
+        }
         elsif ( $change->record_type eq 'comment'
             and $change->change_type eq 'add_file' )
         {
@@ -103,6 +108,21 @@ sub integrate_comment {
     return $ticket_id;
 }
 
+sub integrate_ticket_attachment {
+    my $self = shift;
+    my ( $change, $changeset ) = validate_pos(
+        @_,
+        { isa => 'Prophet::Change' },
+        { isa => 'Prophet::ChangeSet' }
+    );
+    my %props     = map { $_->name => $_->new_value } $change->prop_changes;
+    my $ticket_id = $self->sync_source->remote_id_for_uuid( $props{'ticket'} );
+
+    $self->sync_source->log(
+        'Warn: Net::Lighthouse does *not* support attachment yet');
+    return $ticket_id;
+}
+
 sub integrate_ticket_create {
     my $self = shift;
     my ( $change, $changeset ) = validate_pos(
@@ -144,7 +164,8 @@ sub _recode_props_for_integrate {
             if ( $props{$key} ) {
                 eval { $milestone->load( $props{$key} ) };
                 if ( $@ ) {
-                    warn "no milestone $props{$key} exist";
+                    $self->sync_source->log(
+                        "Warn: no milestone $props{$key} exist");
                 }
                 else {
                     $attr{milestone_id} = $milestone->id;

-----------------------------------------------------------------------



More information about the Bps-public-commit mailing list