[Bps-public-commit] r11546 - in SVN-PropDB: . doc lib/Prophet/Replica/Hiveminder

jesse at bestpractical.com jesse at bestpractical.com
Sat Apr 5 18:40:34 EDT 2008


Author: jesse
Date: Sat Apr  5 18:40:31 2008
New Revision: 11546

Added:
   SVN-PropDB/lib/Prophet/Replica/Hiveminder/
   SVN-PropDB/lib/Prophet/Replica/Hiveminder.pm
   SVN-PropDB/lib/Prophet/Replica/Hiveminder/PullEncoder.pm
Modified:
   SVN-PropDB/   (props changed)
   SVN-PropDB/doc/todo

Log:
 r29254 at 31b:  jesse | 2008-04-05 12:33:31 -1000
 * Beginning of a sketch of the Hiveminder sync source


Modified: SVN-PropDB/doc/todo
==============================================================================
--- SVN-PropDB/doc/todo	(original)
+++ SVN-PropDB/doc/todo	Sat Apr  5 18:40:31 2008
@@ -10,7 +10,7 @@
           - prophet::handle: revision_root, current_edit
           - prophet::test : youngest_rev
           - move {begin,end}_edit, node* methods into repo handler (Prophet::Handle::SVN) ? @done 
-  
+        - refactoring _record_merge_ticket into _record_random_metadata so can be reused for state/txn/etc cache
   
     
     - implement a simple Prophet::Replica::Hiveminder for "personal tasks only"

Added: SVN-PropDB/lib/Prophet/Replica/Hiveminder.pm
==============================================================================
--- (empty file)
+++ SVN-PropDB/lib/Prophet/Replica/Hiveminder.pm	Sat Apr  5 18:40:31 2008
@@ -0,0 +1,156 @@
+use warnings;
+use strict;
+
+package Prophet::Replica::Hiveminder;
+use base qw/Prophet::ForeignReplica/;
+use Params::Validate qw(:all);
+use UNIVERSAL::require;
+
+use Net::Jifty;
+
+use URI;
+use Memoize;
+use Prophet::Handle;
+use Prophet::ChangeSet;
+use Prophet::Replica::Hiveminder::PullEncoder;
+use App::Cache;
+
+__PACKAGE__->mk_accessors(qw/prophet_handle ressource is_resdb rt rt_url rt_queue rt_query/);
+
+our $DEBUG = $Prophet::Handle::DEBUG;
+
+
+=head2 setup
+
+Open a connection to the SVN source identified by C<$self->url>.
+
+XXX TODO, make the _prophet/ directory in the replica configurable
+
+=cut
+
+use File::Temp 'tempdir';
+
+sub setup {
+    my $self = shift;
+    my ( $server, $type, $query ) = $self->{url} =~ m/^hiveminder:(.*?)\|(.*?)\|(.*)$/
+        or die "Can't parse hiveminder server spec";
+    my $uri = URI->new($server);
+    my ( $username, $password );
+    if ( my $auth = $uri->userinfo ) {
+        ( $username, $password ) = split /:/, $auth, 2;
+        $uri->userinfo(undef);
+    }
+    $self->hm_url("$uri");
+
+    $self->hm( Jifyt RT::Client::REST->new( server => $server ) );
+    unless ($username) {
+
+        # XXX belongs to some CLI callback
+        use Term::ReadKey;
+        local $| = 1;
+        print "Username for $uri: ";
+        ReadMode 1;
+        $username = ReadLine 0;
+        chomp $username;
+        print "Password for $username @ $uri: ";
+        ReadMode 2;
+        $password = ReadLine 0;
+        chomp $password;
+        ReadMode 1;
+        print "\n";
+    }
+
+
+    $self->hm( Net::Jifty->new(site => $self->hm_url,
+                        cookie_name => 'JIFTY_SID_HIVEMINDER',
+    
+                        email => $username,
+                        password => $password
+                        ));
+    
+
+    my $cli = Prophet::CLI->new();
+    $self->state_handle( $cli->get_handle_for_replica( $self, 'state' ) );
+}
+
+
+
+
+
+=head2 uuid
+
+Return the replica SVN repository's UUID
+
+=cut
+
+sub uuid {
+    my $self = shift;
+    return $self->uuid_for_url( join( '/', $self->hm_url, $self->username ) );
+}
+
+
+
+=head2 fetch_changesets { after => SEQUENCE_NO } 
+
+Fetch all changesets from the source. 
+
+Returns a reference to an array of L<Prophet::ChangeSet/> objects.
+
+
+=cut
+
+sub fetch_changesets {
+    my $self = shift;
+    my %args = validate( @_, { after => 1 } );
+
+    my $first_rev = ( $args{'after'} + 1 ) || 1;
+
+    my @changesets;
+    my %tix;
+    my $recoder = Prophet::Replica::Hiveminder::PullEncoder->new( { sync_source => $self } );
+    for my $task ( $self->find_matching_tasks ) {
+
+        # XXX: _recode_transactions should ignore txn-id <= $first_rev
+        push @changesets,
+            @{
+            $recoder->run(
+                task => $task,
+                transactions => $self->find_matching_transactions( task => $task->{id}, starting_transaction => $first_rev )
+            )
+            };
+    }
+
+    return [ sort { $a->original_sequence_no <=> $b->original_sequence_no } @changesets ];
+}
+
+sub find_matching_tasks {
+    my $self = shift;
+my $tasks =  $j->act('TaskSearch',  
+               owner            => 'me',
+               group => 0,
+               requestor => 'me',
+               not_complete => 1,
+
+        
+    )->{content}->{tasks};
+    return $tasks;
+}
+
+sub prophet_has_seen_transaction { warn "not yet"; return undef }
+
+sub find_matching_transactions {
+    my $self = shift;
+    my %args = validate( @_, { task => 1, starting_transaction => 1 } );
+
+    my ($task) = validate_pos(@_, 1);
+    my $txns = $j->search('TaskTransaction', task_id => $args{task});
+    foreach my $txn (@{$txns||[]}) {
+        next if $txn < $args{'starting_transaction'};        # Skip things we've pushed
+        next if $self->prophet_has_seen_transaction($txn);
+        $txn->{history_entries} = $j->search('TaskHistory', transaction_id => $txn->{'id'});
+    }
+    return $txns;
+
+}
+
+1;

Added: SVN-PropDB/lib/Prophet/Replica/Hiveminder/PullEncoder.pm
==============================================================================
--- (empty file)
+++ SVN-PropDB/lib/Prophet/Replica/Hiveminder/PullEncoder.pm	Sat Apr  5 18:40:31 2008
@@ -0,0 +1,412 @@
+use warnings;
+use strict;
+
+package Prophet::Replica::RT::PullEncoder;
+use base qw/Class::Accessor/;
+use Params::Validate qw(:all);
+use UNIVERSAL::require;
+use RT::Client::REST       ();
+use RT::Client::REST::User ();
+use RT::Client::REST::Ticket;
+
+use Memoize;
+
+__PACKAGE__->mk_accessors(qw/sync_source/);
+
+our $DEBUG = $Prophet::Handle::DEBUG;
+
+
+
+
+
+sub run {
+    my $self = shift;
+    my %args = validate( @_, { ticket => 1, transactions => 1} );
+
+    $args{'ticket'}->{'id'} =~ s/^ticket\///g;
+    
+    my $ticket = $args{'ticket'};
+
+    warn "Working on " . $ticket->{id};
+    my $create_state = $ticket;;
+    map { $create_state->{$_} = $self->date_to_iso( $create_state->{$_} ) }
+        qw(Created Resolved Told LastUpdated Starts Started);
+
+
+    map { $create_state->{$_} =~ s/ minutes$// } qw(TimeWorked TimeLeft TimeEstimated);
+    my @changesets;
+    for my $txn ( sort { $b->{'id'} <=> $a->{'id'} } @{ $args{'transactions'} } ) {
+        if ( my $sub = $self->can( '_recode_txn_' . $txn->{'Type'} ) ) {
+            my $changeset = Prophet::ChangeSet->new(
+                {   original_source_uuid => $self->sync_source->uuid,
+                    original_sequence_no => $txn->{'id'},
+                }
+            );
+
+            if ( (  $txn->{'Ticket'} ne $ticket->{id} ) && $txn->{'Type'} !~ /^(?:Comment|Correspond)$/ ) {
+                warn "Skipping a data change from a merged ticket" . $txn->{'Ticket'} . ' vs ' . $ticket->{id};
+                next;
+            }
+
+            $sub->(
+                $self,
+                ticket       => $ticket,
+                create_state => $create_state,
+                txn          => $txn,
+                changeset    => $changeset
+            );
+            $self->translate_prop_names($changeset);
+
+            unshift @changesets, $changeset unless $changeset->is_empty;
+        } else {
+            warn "not handling txn type $txn->{Type} for $txn->{id} (Ticket $args{ticket}{id}) yet";
+            die YAML::Dump($txn);
+        }
+
+
+    }
+    return \@changesets;
+}
+
+sub _recode_txn_CommentEmailRecord { return; }
+
+sub _recode_txn_EmailRecord     { return; }
+sub _recode_txn_AddReminder     { return; }
+sub _recode_txn_ResolveReminder { return; }
+sub _recode_txn_DeleteLink      { }
+
+sub _recode_txn_Status {
+    my $self = shift;
+    my %args = validate( @_, { ticket => 1, txn => 1, create_state => 1, changeset => 1 } );
+
+    $args{txn}->{'Type'} = 'Set';
+    return $self->_recode_txn_Set(%args);
+}
+
+sub _recode_txn_Told {
+    my $self = shift;
+    my %args = validate( @_, { ticket => 1, txn => 1, create_state => 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 $change = Prophet::Change->new(
+        {   node_type   => 'ticket',
+            node_uuid   => $self->sync_source->uuid_for_remote_id($args{'create_state'}->{'id'}),
+            change_type => 'update_file'
+        }
+    );
+
+    if ( $args{txn}->{Field} eq 'Queue' ) {
+        my $current_queue = $args{ticket}->{'Queue'};
+        my $user          = $args{txn}->{Creator};
+        if ( $args{txn}->{Description} =~ /Queue changed from (.*) to $current_queue by $user/ ) {
+            $args{txn}->{OldValue} = $1;
+            $args{txn}->{NewValue} = $current_queue;
+        }
+
+    } elsif ( $args{txn}->{Field} eq 'Owner' ) {
+        $args{'txn'}->{NewValue} = $self->resolve_user_id_to( name => $args{'txn'}->{'NewValue'} ),
+            $args{'txn'}->{OldValue}
+            = $self->resolve_user_id_to( name => $args{'txn'}->{'OldValue'} )
+
+    }
+
+    $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'};
+    } 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 );
+    }
+    $change->add_prop_change(
+        name => $args{txn}->{'Field'},
+        old  => $args{txn}->{'OldValue'},
+        new  => $args{txn}->{'NewValue'}
+
+    );
+
+}
+
+*_recode_txn_Steal = \&_recode_txn_Set;
+*_recode_txn_Take  = \&_recode_txn_Set;
+*_recode_txn_Give  = \&_recode_txn_Set;
+
+sub _recode_txn_Create {
+    my $self = shift;
+    my %args = validate( @_, { ticket => 1, txn => 1, create_state => 1, changeset => 1 } );
+
+    my $change = Prophet::Change->new(
+        {   node_type   => 'ticket',
+            node_uuid   => $self->sync_source->uuid_for_remote_id($args{'create_state'}->{'id'}),
+            change_type => 'add_file'
+        }
+    );
+
+    $args{'create_state'}->{ $self->sync_source->uuid . '-id' } = delete $args{'create_state'}->{'id'};
+
+    $args{'changeset'}->add_change( { change => $change } );
+    for my $name ( keys %{ $args{'create_state'} } ) {
+
+        $change->add_prop_change(
+            name => $name,
+            old  => undef,
+            new  => $args{'create_state'}->{$name},
+        );
+
+    }
+
+    $self->_recode_content_update(%args);    # add the create content txn as a seperate change in this changeset
+
+}
+
+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'} },
+        $args{'txn'}->{'NewValue'},
+        $args{'txn'}->{'OldValue'}
+    );
+
+    my $change = Prophet::Change->new(
+        {   node_type   => 'ticket',
+            node_uuid   => $self->sync_source->uuid_for_remote_id($args{'create_state'}->{'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'} },
+        new  => $new_state
+    );
+
+}
+
+sub _recode_content_update {
+    my $self   = shift;
+    my %args   = validate( @_, { ticket => 1, txn => 1, create_state => 1, changeset => 1 } );
+    my $change = Prophet::Change->new(
+        {   node_type   => 'comment',
+            node_uuid   => $self->sync_source->uuid_for_url( $self->sync_source->rt_url . "/transaction/" . $args{'txn'}->{'id'} ),
+            change_type => 'add_file'
+        }
+    );
+    $change->add_prop_change(
+        name => 'type',
+        old  => undef,
+        new  => $args{'txn'}->{'Type'}
+    );
+
+    $change->add_prop_change(
+        name => 'creator',
+        old  => undef,
+        new  => $args{'txn'}->{'Creator'}
+    );
+    $change->add_prop_change(
+        name => 'content',
+        old  => undef,
+        new  => $args{'txn'}->{'Content'}
+    );
+    $change->add_prop_change(
+        name => 'ticket',
+        old  => undef,
+        new  => $args{ticket}->{uuid},
+    );
+    $args{'changeset'}->add_change( { change => $change } );
+}
+
+*_recode_txn_Comment    = \&_recode_content_update;
+*_recode_txn_Correspond = \&_recode_content_update;
+
+sub _recode_txn_AddWatcher {
+    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'} },
+
+        $self->resolve_user_id_to( email => $args{'txn'}->{'NewValue'} ),
+        $self->resolve_user_id_to( email => $args{'txn'}->{'OldValue'} )
+
+    );
+
+    my $change = Prophet::Change->new(
+        {   node_type   => 'ticket',
+            node_uuid   => $self->sync_source->uuid_for_remote_id($args{'create_state'}->{'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'} },
+        new  => $new_state
+    );
+
+}
+
+*_recode_txn_DelWatcher = \&_recode_txn_AddWatcher;
+
+sub _recode_txn_CustomField {
+    my $self = shift;
+    my %args = validate( @_, { ticket => 1, txn => 1, create_state => 1, changeset => 1 } );
+
+    my $new = $args{'txn'}->{'NewValue'};
+    my $old = $args{'txn'}->{'OldValue'};
+    my $name;
+    if ( $args{'txn'}->{'Description'} =~ /^(.*) $new added by/ ) {
+        $name = $1;
+
+    } elsif ( $args{'txn'}->{'Description'} =~ /^(.*) $old delete by/ ) {
+        $name = $1;
+    } else {
+        die "Uh. what to do with txn descriotion " . $args{'txn'}->{'Description'};
+    }
+
+    $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'} },
+        $args{'txn'}->{'NewValue'},
+        $args{'txn'}->{'OldValue'}
+    );
+
+    my $change = Prophet::Change->new(
+        {   node_type   => 'ticket',
+            node_uuid   => $self->sync_source->uuid_for_remote_id($args{'create_state'}->{'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'} },
+        new  => $new_state
+    );
+}
+
+sub resolve_user_id_to {
+    my $self = shift;
+    my $attr = shift;
+    my $id   = shift;
+    return undef unless ($id);
+
+    my $user = RT::Client::REST::User->new( rt => $self->sync_source->rt, id => $id )->retrieve;
+    return $attr eq 'name' ? $user->name : $user->email_address;
+
+}
+
+memoize 'resolve_user_id_to';
+
+
+sub warp_list_to_old_value {
+    my $self         = shift;
+    my $ticket_value = shift || '';
+    my $add          = shift;
+    my $del          = shift;
+
+    my @new = split( /\s*,\s*/, $ticket_value );
+    my @old = grep { $_ ne $add } @new, $del;
+    return join( ", ", @old );
+}
+
+our $MONNUM = {
+    Jan => 1,
+    Feb => 2,
+    Mar => 3,
+    Apr => 4,
+    May => 5,
+    Jun => 6,
+    Jul => 7,
+    Aug => 8,
+    Sep => 9,
+    Oct => 10,
+    Nov => 11,
+    Dec => 12
+};
+
+use DateTime::Format::HTTP;
+
+sub date_to_iso {
+    my $self = shift;
+    my $date = shift;
+
+    return '' if $date eq 'Not set';
+    my $t = DateTime::Format::HTTP->parse_datetime($date);
+    return $t->ymd . " " . $t->hms;
+}
+
+our %PROP_MAP = (
+    subject         => 'summary',
+    status          => 'status',
+    owner           => 'owner',
+    initialpriority => '_delete',
+    finalpriority   => '_delete',
+    told            => '_delete',
+    requestors      => 'reported_by',
+    admincc         => 'admin_cc',
+    refersto        => 'refers_to',
+    referredtoby    => 'referred_to_by',
+    dependson       => 'depends_on',
+    dependedonby    => 'depended_on_by',
+    hasmember       => 'members',
+    memberof        => 'member_of',
+    priority        => 'priority_integer',
+    resolved        => 'completed',
+    due             => 'due',
+    creator         => 'creator',
+    timeworked      => 'time_worked',
+    timeleft        => 'time_left',
+    lastupdated     => '_delete',
+    created         => '_delete',            # we should be porting the create date as a metaproperty
+
+);
+
+sub translate_prop_names {
+    my $self      = shift;
+    my $changeset = shift;
+
+    for my $change ( $changeset->changes ) {
+        next unless $change->node_type eq 'ticket';
+
+        my @new_props;
+        for my $prop ( $change->prop_changes ) {
+            next if ( ( $PROP_MAP{ lc( $prop->name ) } || '' ) eq '_delete' );
+            $prop->name( $PROP_MAP{ lc( $prop->name ) } ) if $PROP_MAP{ lc( $prop->name ) };
+
+            if ( $prop->name eq 'id' ) {
+                $prop->old_value( $prop->old_value . '@' . $changeset->original_source_uuid )
+                    if ( $prop->old_value || '' ) =~ /^\d+$/;
+                $prop->old_value( $prop->new_value . '@' . $changeset->original_source_uuid )
+                    if ( $prop->new_value || '' ) =~ /^\d+$/;
+
+            }
+
+            if ( $prop->name =~ /^cf-(.*)$/ ) {
+                $prop->name( 'custom-' . $1 );
+            }
+
+            push @new_props, $prop;
+
+        }
+        $change->prop_changes( \@new_props );
+
+    }
+    return $changeset;
+}
+
+1;



More information about the Bps-public-commit mailing list