SD/Replica/trac/PushEncoder.pm untested

filter: Statementstmt 0.0% 0 / 68 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 105 CC 4 SCAR 20.2
CC
4
Cov
0%
_recode_props_for_integrate
20.0
after_integrate_change
2.0
integrate_ticket_update
2.0
CRAP 7.5
1 package App::SD::Replica::trac::PushEncoder;
2 use Any::Moose;
3 use Params::Validate;
4 use Time::HiRes qw/usleep/;
5  
6 has sync_source =>
7 ( isa => 'App::SD::Replica::trac',
8 is => 'rw');
9  
10 extends 'App::SD::ForeignReplica::PushEncoder';
11  
12 sub after_integrate_change {
13 usleep(1100); # trac only accepts one ticket update per second. Yes.
14 }
15  
16 sub integrate_ticket_update {
17 my $self = shift;
18 my ( $change, $changeset ) = validate_pos(
19 @_,
20 { isa => 'Prophet::Change' },
21 { isa => 'Prophet::ChangeSet' }
22 );
23  
24 # Figure out the remote site's ticket ID for this change's record
25 my $remote_ticket_id =
26 $self->sync_source->remote_id_for_uuid( $change->record_uuid );
27 my $ticket = Net::Trac::Ticket->new( connection => $self->sync_source->trac);
28 $ticket->load($remote_ticket_id) or
29 die "couldn't load remote track ticket $remote_ticket_id\n";
30 $ticket->update( %{ $self->_recode_props_for_integrate($change) } ) or
31 die "couldn't update remote track ticket $remote_ticket_id\n";
32 return $remote_ticket_id;
33 }
34  
35 sub integrate_ticket_create {
36 my $self = shift;
37 my ( $change, $changeset ) = validate_pos(
38 @_,
39 { isa => 'Prophet::Change' },
40 { isa => 'Prophet::ChangeSet' }
41 );
42  
43 # Build up a ticket object out of all the record's attributes
44 my $ticket = Net::Trac::Ticket->new(
45 connection => $self->sync_source->trac);
46 my $id = $ticket->create( %{ $self->_recode_props_for_integrate($change) });
47  
48 return $id
49 }
50  
51 sub integrate_comment {
52 my $self = shift;
53 my ($change, $changeset) = validate_pos( @_,
54 { isa => 'Prophet::Change' }, {isa => 'Prophet::ChangeSet'} );
55  
56 # Figure out the remote site's ticket ID for this change's record
57  
58 my %props = map { $_->name => $_->new_value } $change->prop_changes;
59  
60 my $ticket_id = $self->sync_source->remote_id_for_uuid( $props{'ticket'} );
61 my $ticket = Net::Trac::Ticket->new( connection => $self->sync_source->trac);
62 $ticket->load($ticket_id);
63 $ticket->comment( $props{content});
64 return $ticket_id;
65 }
66  
67 sub integrate_attachment {
68 my ($self, $change, $changeset ) = validate_pos( @_,
69 { isa => 'App::SD::Replica::trac::PushEncoder'},
70 { isa => 'Prophet::Change' },
71 { isa => 'Prophet::ChangeSet' });
72  
73 my %props = map { $_->name => $_->new_value } $change->prop_changes;
74  
75 my $ticket_id = $self->sync_source->remote_id_for_uuid( $props{'ticket'} );
76 my $ticket = Net::Trac::Ticket->new( connection => $self->sync_source->trac);
77 $ticket->load($ticket_id);
78  
79 my $tempdir = File::Temp::tempdir( CLEANUP => 1 );
80 my $file = File::Spec->catfile( $tempdir, ( $props{'name'} || 'unnamed' ) );
81 open my $fh, '>', $file or die $!;
82 print $fh $props{content};
83 close $fh;
84 $ticket->attach( file => $file) || die "Could not attach file for ticket $ticket_id";
85 return $ticket_id;
86 }
87  
88 sub _recode_props_for_integrate {
89 my $self = shift;
90 my ($change) = validate_pos( @_, { isa => 'Prophet::Change' } );
91  
92 my %props = map { $_->name => $_->new_value } $change->prop_changes;
93 my %attr;
94  
95 for my $key ( keys %props ) {
96 next unless ( $key =~ /^(summary|status|owner)/ );
97 if ( $key eq 'status' ) {
98 my $active_statuses =
99 $self->sync_source->database_settings->{active_statuses};
100 if ( grep { $props{$key} eq $_ } @$active_statuses, 'closed' ) {
101 $attr{$key} = $props{$key};
102 }
103 else {
104 $attr{$key} = 'closed';
105 $attr{resolution} = $props{$key};
106 }
107  
108 }
109 else {
110 $attr{$key} = $props{$key};
111 }
112 }
113 return \%attr;
114 }
115  
116 __PACKAGE__->meta->make_immutable;
117 no Any::Moose;
118  
119 1;