[Bps-public-commit] r13964 - in sd/trunk/lib/App/SD/Replica: RT

jesse at bestpractical.com jesse at bestpractical.com
Thu Jul 10 16:36:44 EDT 2008


Author: jesse
Date: Thu Jul 10 16:36:43 2008
New Revision: 13964

Modified:
   sd/trunk/lib/App/SD/Replica/RT.pm
   sd/trunk/lib/App/SD/Replica/RT/PullEncoder.pm

Log:
* Refactor to remove this whole idea of having two versions of a ticket passed around

Modified: sd/trunk/lib/App/SD/Replica/RT.pm
==============================================================================
--- sd/trunk/lib/App/SD/Replica/RT.pm	(original)
+++ sd/trunk/lib/App/SD/Replica/RT.pm	Thu Jul 10 16:36:43 2008
@@ -228,7 +228,7 @@
 
     require App::SD::Replica::RT::PullEncoder;
     my $recoder = App::SD::Replica::RT::PullEncoder->new( { sync_source => $self } );
-    $recoder->pull( query => $self->rt_query, after => $args{'after'}, callback => $args{'callback'});
+    $recoder->run( query => $self->rt_query, after => $args{'after'}, callback => $args{'callback'});
 
 }
 

Modified: sd/trunk/lib/App/SD/Replica/RT/PullEncoder.pm
==============================================================================
--- sd/trunk/lib/App/SD/Replica/RT/PullEncoder.pm	(original)
+++ sd/trunk/lib/App/SD/Replica/RT/PullEncoder.pm	Thu Jul 10 16:36:43 2008
@@ -13,44 +13,86 @@
     ( isa => 'App::SD::Replica::RT',
       is => 'rw');
 
-
-our $DEBUG = $Prophet::Handle::DEBUG;
-
-
-
-sub pull {
+sub run {
     my $self = shift;
-    my %args = validate( @_,
+    my %args = validate(
+        @_,
         {   after    => 1,
             callback => 1,
-            query => 1
+            query    => 1
         }
     );
 
     my $first_rev = ( $args{'after'} + 1 ) || 1;
 
-    for my $id ( $self->find_matching_tickets($args{'query'} )) {
-        # XXX: _recode_transactions should ignore txn-id <= $first_rev
-        $args{callback}->($_)
-            for @{
-            $self->run(
-                ticket => $self->sync_source->rt->show( type => 'ticket', id => $id ),
-                transactions => $self->find_matching_transactions(
-                    ticket               => $id,
-                    starting_transaction => $first_rev
-                ),
+    for my $id ( $self->find_matching_tickets( $args{'query'} ) ) {
+        my $ticket
+            = $self->sync_source->rt->show( type => 'ticket', id => $id );
+        my $txns = $self->find_matching_transactions(
+            ticket               => $id,
+            starting_transaction => $first_rev
+        );
+
+        $ticket = $self->_translate_final_ticket_state($ticket);
+        my $changesets = $self->transcode(
+            ticket       => $ticket,
+            transactions => $txns
+        );
+        $args{callback}->($_) for (@$changesets);
+    }
+}
 
-            )
-            };
+sub transcode {
+    my $self = shift;
+    my %args = validate( @_, { ticket => 1, transactions => 1, attachments => 0 } );
+    my $ticket_before = $args{'ticket'};
+    my @changesets;
+    for my $txn ( sort { $b->{'id'} <=> $a->{'id'} } @{ $args{'transactions'} } ) {
+        my $changeset = $self->txn_to_changeset( $txn,$ticket_before);
+        next unless $changeset->has_changes;
+        unshift @changesets, $changeset;
     }
+
+    return \@changesets;
+}
+
+
+sub _translate_final_ticket_state {
+    my $self   = shift;
+    my $ticket = shift;
+
+    $ticket->{'id'} =~ s/^ticket\///g;
+    map { $ticket->{ $self->sync_source->uuid . '-' . lc($_) } = delete $ticket->{$_} }
+        (qw(Queue id));
+    map { delete $ticket->{$_} if ( !defined $ticket->{$_} || $ticket->{$_} eq '' ) } keys %$ticket;
+    map { $ticket->{$_} = $self->date_to_iso( $ticket->{$_} ) }
+        qw(Created Resolved Told LastUpdated Starts Started);
+    map { $ticket->{$_} =~ s/ minutes$// if defined $ticket->{$_} }
+        qw(TimeWorked TimeLeft TimeEstimated);
+    $ticket->{'Status'} =~ s/^(resolved|rejected)$/closed/;
+    return $ticket;
 }
 
+=head2 find_matching_tickets QUERY
+
+Returns an RT::Client ticket collection for all tickets found matching your QUERY string.
+
+=cut
+
+
 sub find_matching_tickets {
     my $self = shift;
     my ($query) = validate_pos(@_, 1);
     return $self->sync_source->rt->search( type => 'ticket', query => $query );
 }
 
+
+=head2 find_matching_transactions { ticket => $id, starting_transaction => $num }
+
+Returns a reference to an array of all transactions (as hashes) on ticket $id after transaction $num.
+
+=cut
+
 sub find_matching_transactions {
     my $self = shift;
     my %args = validate( @_, { ticket => 1, starting_transaction => 1 } );
@@ -58,11 +100,11 @@
 
     my $rt_handle = $self->sync_source->rt;
 
-    for my $txn ( sort $rt_handle->get_transaction_ids( parent_id => $args{ticket} ) ) {
+    for my $txn ( sort $rt_handle->get_transaction_ids( parent_id => $args{'ticket'} ) ) {
         next if $txn < $args{'starting_transaction'}; # Skip things we've pushed
         next if $self->sync_source->prophet_has_seen_transaction($txn);
         my $txn_hash = $rt_handle->get_transaction(
-            parent_id => $args{ticket},
+            parent_id => $args{'ticket'},
             id        => $txn,
             type      => 'ticket'
         );
@@ -83,36 +125,8 @@
     return \@txns;
 }
 
-
-
-sub run {
-    my $self = shift;
-    my %args = validate( @_, { ticket => 1, transactions => 1, attachments => 0 } );
-
-    $args{'ticket'}->{'id'} =~ s/^ticket\///g;
-    for (qw(Queue id)) {
-    $args{'ticket'}->{ $self->sync_source->uuid . '-'.lc($_) } = delete $args{'ticket'}->{$_};
-    }
-
-    my $ticket = $args{'ticket'};
-    map { delete $ticket->{$_} if (!defined $ticket->{$_}  || $ticket->{$_} eq '') } keys %$ticket;
-    map { $ticket->{$_} = $self->date_to_iso( $ticket->{$_} ) } qw(Created Resolved Told LastUpdated Starts Started);
-    map { $ticket->{$_} =~ s/ minutes$// if defined $ticket->{$_} } qw(TimeWorked TimeLeft TimeEstimated);
-    $ticket->{'Status'} =~ s/^(resolved|rejected)$/closed/;
-    my $create_state = $ticket;
-
-    my @changesets;
-    for my $txn ( sort { $b->{'id'} <=> $a->{'id'} } @{ $args{'transactions'} } ) {
-            my $changeset = $self->txn_to_changeset($txn, $ticket, $create_state);
-            unshift @changesets, $changeset if $changeset->has_changes;
-        }
-
-    return \@changesets;
-}
-
-
 sub txn_to_changeset {
-    my ($self, $txn, $ticket, $create_state) = (@_);
+    my ($self, $txn, $ticket_before) = (@_);
     
         if ($ENV{'SD_DEBUG'}) {warn YAML::Dump($txn); use YAML;}
         if ( my $sub = $self->can( '_recode_txn_' . $txn->{'Type'} ) ) {
@@ -122,8 +136,8 @@
                 }
             );
 
-            if ( ( $txn->{'Ticket'} ne $ticket->{$self->sync_source->uuid . '-id'} ) && $txn->{'Type'} !~ /^(?:Comment|Correspond)$/ ) {
-                warn "Skipping a data change from a merged ticket" . $txn->{'Ticket'} . ' vs ' . $ticket->{$self->sync_source->uuid . '-id'};
+            if ( ( $txn->{'Ticket'} ne $ticket_before->{$self->sync_source->uuid . '-id'} ) && $txn->{'Type'} !~ /^(?:Comment|Correspond)$/ ) {
+                warn "Skipping a data change from a merged ticket" . $txn->{'Ticket'} . ' vs ' . $ticket_before->{$self->sync_source->uuid . '-id'};
                 next;
             }
 
@@ -132,12 +146,12 @@
             delete $txn->{'OldValue'} if ( $txn->{'OldValue'} eq '');
             delete $txn->{'NewValue'} if ( $txn->{'NewValue'} eq '');
 
-            $sub->( $self, ticket       => $ticket, create_state => $create_state, txn          => $txn, changeset    => $changeset);
+            $sub->( $self, ticket_before => $ticket_before, txn          => $txn, changeset    => $changeset);
             $self->translate_prop_names($changeset);
 
             if (my $attachments = delete $txn->{'_attachments'}) {
                foreach my $attach (@$attachments) { 
-                    $self->_recode_attachment_create( ticket => $ticket, txn => $txn, changeset =>$changeset, attachment => $attach); 
+                    $self->_recode_attachment_create( ticket_before => $ticket_before, txn => $txn, changeset =>$changeset, attachment => $attach); 
                }
             }
 
@@ -151,7 +165,7 @@
 
 sub _recode_attachment_create {
     my $self   = shift;
-    my %args   = validate( @_, { ticket => 1, txn => 1, changeset => 1, attachment => 1 } );
+    my %args   = validate( @_, { ticket_before => 1, txn => 1, changeset => 1, attachment => 1 } );
     my $change = Prophet::Change->new(
         {   record_type => 'attachment',
             record_uuid => $self->sync_source->uuid_for_url( $self->sync_source->rt_url . "/attachment/" . $args{'attachment'}->{'id'} ),
@@ -163,7 +177,7 @@
     $change->add_prop_change( name => 'creator', old  => undef, new  => $self->resolve_user_id_to( email => $args{'attachment'}->{'Creator'}));
     $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  => $self->sync_source->uuid_for_remote_id( $args{'ticket'}->{ $self->sync_source->uuid . '-id'} ));
+    $change->add_prop_change( name => 'ticket', old  => undef, new  => $self->sync_source->uuid_for_remote_id( $args{'ticket_before'}->{ $self->sync_source->uuid . '-id'} ));
     $args{'changeset'}->add_change( { change => $change } );
 }
 
@@ -177,7 +191,7 @@
 
 sub _recode_txn_Status {
     my $self = shift;
-    my %args = validate( @_, { ticket => 1, txn => 1, create_state => 1, changeset => 1 } );
+    my %args = validate( @_, { txn => 1, ticket_before => 1, changeset => 1 } );
 
     $args{txn}->{'Type'} = 'Set';
         for my $type(qw(NewValue OldValue)) {
@@ -188,14 +202,14 @@
 
 sub _recode_txn_Told {
     my $self = shift;
-    my %args = validate( @_, { ticket => 1, txn => 1, create_state => 1, changeset => 1 } );
+    my %args = validate( @_, { txn => 1, ticket_before => 1, changeset => 1 } );
     $args{txn}->{'Type'} = 'Set';
     return $self->_recode_txn_Set(%args);
 }
 
 sub _recode_txn_Set {
     my $self = shift;
-    my %args = validate( @_, { ticket => 1, txn => 1, create_state => 1, changeset => 1 } );
+    my %args = validate( @_, { txn => 1, ticket_before => 1, changeset => 1 } );
         
     
     
@@ -203,13 +217,13 @@
 
     my $change = Prophet::Change->new(
         {   record_type   => 'ticket',
-            record_uuid   => $self->sync_source->uuid_for_remote_id( $args{'create_state'}->{$self->sync_source->uuid . '-id'} ),
+            record_uuid   => $self->sync_source->uuid_for_remote_id( $args{'ticket_before'}->{$self->sync_source->uuid . '-id'} ),
             change_type => 'update_file'
         }
     );
 
     if ( $args{txn}->{Field} eq 'Queue' ) {
-        my $current_queue = $args{ticket}->{$self->sync_source->uuid .'-queue'};
+        my $current_queue = $args{'ticket_before'}->{$self->sync_source->uuid .'-queue'};
         my $user          = $args{txn}->{Creator};
         if ( $args{txn}->{Description} =~ /Queue changed from (.*) to $current_queue by $user/ ) {
             $args{txn}->{OldValue} = $1;
@@ -222,11 +236,11 @@
     }
 
     $args{'changeset'}->add_change( { change => $change } );
-    if ( $args{'create_state'}->{ $args{txn}->{Field} } eq $args{txn}->{'NewValue'} ) {
-        $args{'create_state'}->{ $args{txn}->{Field} } = $args{txn}->{'OldValue'};
+    if ( $args{'ticket_before'}->{ $args{txn}->{Field} } eq $args{txn}->{'NewValue'} ) {
+        $args{'ticket_before'}->{ $args{txn}->{Field} } = $args{txn}->{'OldValue'};
     } else {
-        $args{'create_state'}->{ $args{txn}->{Field} } = $args{txn}->{'OldValue'};
-        warn $args{'create_state'}->{ $args{txn}->{Field} } . " != " . $args{txn}->{'NewValue'} . "\n\n" . YAML::Dump( \%args ); use YAML;
+        $args{'ticket_before'}->{ $args{txn}->{Field} } = $args{txn}->{'OldValue'};
+        warn $args{'ticket_before'}->{ $args{txn}->{Field} } . " != " . $args{txn}->{'NewValue'} . "\n\n" . YAML::Dump( \%args ); use YAML;
     }
     $change->add_prop_change(
         name => $args{txn}->{'Field'},
@@ -243,22 +257,22 @@
 
 sub _recode_txn_Create {
     my $self = shift;
-    my %args = validate( @_, { ticket => 1, txn => 1, create_state => 1, changeset => 1 } );
+    my %args = validate( @_, {  txn => 1, ticket_before => 1, changeset => 1 } );
 
     my $change = Prophet::Change->new(
         {   record_type   => 'ticket',
-            record_uuid   => $self->sync_source->uuid_for_remote_id( $args{'create_state'}->{$self->sync_source->uuid . '-id'} ),
+            record_uuid   => $self->sync_source->uuid_for_remote_id( $args{'ticket_before'}->{$self->sync_source->uuid . '-id'} ),
             change_type => 'add_file'
         }
     );
 
     $args{'changeset'}->add_change( { change => $change } );
-    for my $name ( keys %{ $args{'create_state'} } ) {
+    for my $name ( keys %{ $args{'ticket_before'} } ) {
 
         $change->add_prop_change(
             name => $name,
             old  => undef,
-            new  => $args{'create_state'}->{$name},
+            new  => $args{'ticket_before'}->{$name},
         );
 
     }
@@ -269,24 +283,24 @@
 
 sub _recode_txn_AddLink {
     my $self      = shift;
-    my %args      = validate( @_, { ticket => 1, txn => 1, create_state => 1, changeset => 1 } );
-    my $new_state = $args{'create_state'}->{ $args{'txn'}->{'Field'} };
-    $args{'create_state'}->{ $args{'txn'}->{'Field'} } = $self->warp_list_to_old_value(
-        $args{'create_state'}->{ $args{'txn'}->{'Field'} },
+    my %args      = validate( @_, { txn => 1, ticket_before => 1, changeset => 1 } );
+    my $new_state = $args{'ticket_before'}->{ $args{'txn'}->{'Field'} };
+    $args{'ticket_before'}->{ $args{'txn'}->{'Field'} } = $self->warp_list_to_old_value(
+        $args{'ticket_before'}->{ $args{'txn'}->{'Field'} },
         $args{'txn'}->{'NewValue'},
         $args{'txn'}->{'OldValue'}
     );
 
     my $change = Prophet::Change->new(
         {   record_type   => 'ticket',
-            record_uuid   => $self->sync_source->uuid_for_remote_id( $args{'create_state'}->{$self->sync_source->uuid . '-id'} ),
+            record_uuid   => $self->sync_source->uuid_for_remote_id( $args{'ticket_before'}->{$self->sync_source->uuid . '-id'} ),
             change_type => 'update_file'
         }
     );
     $args{'changeset'}->add_change( { change => $change } );
     $change->add_prop_change(
         name => $args{'txn'}->{'Field'},
-        old  => $args{'create_state'}->{ $args{'txn'}->{'Field'} },
+        old  => $args{'ticket_before'}->{ $args{'txn'}->{'Field'} },
         new  => $new_state
     );
 
@@ -294,7 +308,7 @@
 
 sub _recode_content_update {
     my $self   = shift;
-    my %args   = validate( @_, { ticket => 1, txn => 1, create_state => 1, changeset => 1 } );
+    my %args   = validate( @_, { txn => 1, ticket_before => 1, changeset => 1 } );
     my $change = Prophet::Change->new(
         {   record_type => 'comment',
             record_uuid => $self->sync_source->uuid_for_url( $self->sync_source->rt_url . "/transaction/" . $args{'txn'}->{'id'} ),
@@ -307,7 +321,7 @@
     $change->add_prop_change( name => 'type', old  => undef, new  => $args{'txn'}->{'Type'});
     $change->add_prop_change( name => 'creator', old  => undef, new  => $self->resolve_user_id_to( email => $args{'txn'}->{'Creator'}));
     $change->add_prop_change( name => 'content', old  => undef, new  => $args{'txn'}->{'Content'});
-    $change->add_prop_change( name => 'ticket', old  => undef, new  => $self->sync_source->uuid_for_remote_id( $args{'ticket'}->{ $self->sync_source->uuid . '-id'} ));
+    $change->add_prop_change( name => 'ticket', old  => undef, new  => $self->sync_source->uuid_for_remote_id( $args{'ticket_before'}->{ $self->sync_source->uuid . '-id'} ));
     $args{'changeset'}->add_change( { change => $change } );
 }
 
@@ -316,12 +330,12 @@
 
 sub _recode_txn_AddWatcher {
     my $self = shift;
-    my %args = validate( @_, { ticket => 1, txn => 1, create_state => 1, changeset => 1 } );
+    my %args = validate( @_, { txn => 1, ticket_before => 1, changeset => 1 } );
 
-    my $new_state = $args{'create_state'}->{ $args{'txn'}->{'Field'} };
+    my $new_state = $args{'ticket_before'}->{ $args{'txn'}->{'Field'} };
 
-    $args{'create_state'}->{ $args{'txn'}->{'Field'} } = $self->warp_list_to_old_value(
-        $args{'create_state'}->{ $args{'txn'}->{'Field'} },
+    $args{'ticket_before'}->{ $args{'txn'}->{'Field'} } = $self->warp_list_to_old_value(
+        $args{'ticket_before'}->{ $args{'txn'}->{'Field'} },
 
         $self->resolve_user_id_to( email => $args{'txn'}->{'NewValue'} ),
         $self->resolve_user_id_to( email => $args{'txn'}->{'OldValue'} )
@@ -330,14 +344,14 @@
 
     my $change = Prophet::Change->new(
         {   record_type   => 'ticket',
-            record_uuid   => $self->sync_source->uuid_for_remote_id( $args{'create_state'}->{$self->sync_source->uuid . '-id'} ),
+            record_uuid   => $self->sync_source->uuid_for_remote_id( $args{'ticket_before'}->{$self->sync_source->uuid . '-id'} ),
             change_type => 'update_file'
         }
     );
     $args{'changeset'}->add_change( { change => $change } );
     $change->add_prop_change(
         name => $args{'txn'}->{'Field'},
-        old  => $args{'create_state'}->{ $args{'txn'}->{'Field'} },
+        old  => $args{'ticket_before'}->{ $args{'txn'}->{'Field'} },
         new  => $new_state
     );
 
@@ -347,7 +361,7 @@
 
 sub _recode_txn_CustomField {
     my $self = shift;
-    my %args = validate( @_, { ticket => 1, txn => 1, create_state => 1, changeset => 1 } );
+    my %args = validate( @_, { txn => 1, ticket_before => 1, changeset => 1 } );
 
     my $new = $args{'txn'}->{'NewValue'};
     my $old = $args{'txn'}->{'OldValue'};
@@ -363,16 +377,16 @@
 
     $args{'txn'}->{'Field'} = "CF-" . $name;
 
-    my $new_state = $args{'create_state'}->{ $args{'txn'}->{'Field'} };
-    $args{'create_state'}->{ $args{'txn'}->{'Field'} } = $self->warp_list_to_old_value(
-        $args{'create_state'}->{ $args{'txn'}->{'Field'} },
+    my $new_state = $args{'ticket_before'}->{ $args{'txn'}->{'Field'} };
+    $args{'ticket_before'}->{ $args{'txn'}->{'Field'} } = $self->warp_list_to_old_value(
+        $args{'ticket_before'}->{ $args{'txn'}->{'Field'} },
         $args{'txn'}->{'NewValue'},
         $args{'txn'}->{'OldValue'}
     );
 
     my $change = Prophet::Change->new(
         {   record_type   => 'ticket',
-            record_uuid   => $self->sync_source->uuid_for_remote_id( $args{'create_state'}->{$self->sync_source->uuid . '-id'} ),
+            record_uuid   => $self->sync_source->uuid_for_remote_id( $args{'ticket_before'}->{$self->sync_source->uuid . '-id'} ),
             change_type => 'update_file'
         }
     );
@@ -380,7 +394,7 @@
     $args{'changeset'}->add_change( { change => $change } );
     $change->add_prop_change(
         name => $args{'txn'}->{'Field'},
-        old  => $args{'create_state'}->{ $args{'txn'}->{'Field'} },
+        old  => $args{'ticket_before'}->{ $args{'txn'}->{'Field'} },
         new  => $new_state
     );
 }
@@ -404,11 +418,11 @@
 
 sub warp_list_to_old_value {
     my $self         = shift;
-    my $ticket_value = shift ||'';
+    my $current_value = shift ||'';
     my $add          = shift;
     my $del          = shift;
 
-    my @new = grep { defined } split( /\s*,\s*/, $ticket_value );
+    my @new = grep { defined } split( /\s*,\s*/, $current_value );
     my @old = (grep { defined $_ && $_ ne $add } @new, $del ) || ();
     return join( ", ", @old );
 }



More information about the Bps-public-commit mailing list