SD/Replica/trac/PullEncoder.pm untested

filter: Statementstmt 0.0% 0 / 170 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 45 MC/DCmcdc 0.0% 0 / 24 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 315 CC 25 SCAR 40.8
CC
25
Cov
0%
transcode_one_txn
156.0
translate_prop_names
56.0
transcode_create_txn
20.0
CRAP 59.2
1 package App::SD::Replica::trac::PullEncoder;
2 use Any::Moose;
3 extends 'App::SD::ForeignReplica::PullEncoder';
4  
5 use Params::Validate qw(:all);
6 use Memoize;
7  
8 use Prophet::ChangeSet;
9 use Prophet::Change;
10  
11 has sync_source => (
12 isa => 'App::SD::Replica::trac',
13 is => 'rw',
14 );
15  
16  
17 sub ticket_id {
18 my $self = shift;
19 my $ticket = shift;
20 return $ticket->id;
21 }
22  
23 sub ticket_last_modified {
24 my $self = shift;
25 my $ticket = shift;
26 return $ticket->last_modified;
27 }
28  
29 sub translate_ticket_state {
30 my $self = shift;
31 my $ticket_object = shift;
32 my $transactions = shift;
33 my $content = $ticket_object->description;
34 my $ticket_data = {
35  
36 $self->sync_source->uuid . '-id' => $ticket_object->id,
37  
38 owner => ( $ticket_object->owner || undef ),
39 type => ($ticket_object->type || undef),
40 created => ( $ticket_object->created->ymd . " " . $ticket_object->created->hms ),
41 reporter => ( $ticket_object->reporter || undef ),
42 status => ( $ticket_object->status || undef ),
43 summary => ( $ticket_object->summary || undef ),
44 description => ( $content||undef),
45 tags => ( $ticket_object->keywords || undef ),
46 component => ( $ticket_object->component || undef ),
47 milestone => ( $ticket_object->milestone || undef ),
48 priority => ( $ticket_object->priority || undef ),
49 severity => ( $ticket_object->severity || undef ),
50 cc => ( $ticket_object->cc || undef ),
51 };
52  
53 # delete undefined and empty fields
54 delete $ticket_data->{$_}
55 for grep !defined $ticket_data->{$_} || $ticket_data->{$_} eq '', keys %$ticket_data;
56  
57 return $ticket_data, {%$ticket_data};
58 }
59  
60 =head2 find_matching_tickets QUERY
61  
62 Returns a array of all tickets found matching your QUERY hash.
63  
64 =cut
65  
66 sub find_matching_tickets {
67 my $self = shift;
68 my %query = (@_);
69 my $last_changeset_seen_dt = $self->_only_pull_tickets_modified_after();
70 $self->sync_source->log("Searching for tickets");
71  
72 my $search = Net::Trac::TicketSearch->new(
73 connection => $self->sync_source->trac, limit => 9999 );
74 $search->query(%query);
75 my @results = @{$search->results};
76 $self->sync_source->log("Trimming things after our last pull");
77 if ($last_changeset_seen_dt) {
78 # >= is wasteful but may catch race conditions
79 @results = grep {$_->last_modified >= $last_changeset_seen_dt} @results;
80 }
81 return \@results;
82 }
83  
84 =head2 find_matching_transactions { ticket => $id, starting_transaction => $num }
85  
86 Returns a reference to an array of all transactions (as hashes) on ticket $id
87 after transaction $num.
88  
89 =cut
90  
91 sub find_matching_transactions {
92 my $self = shift;
93 my %args = validate( @_, { ticket => 1, starting_transaction => 1 } );
94 my @raw_txns = @{$args{ticket}->history->entries};
95  
96 my @txns;
97 # XXX TODO make this one loop.
98 for my $txn ( sort { $a->date cmp $b->date} @raw_txns) {
99 my $txn_date = $txn->date->epoch;
100 # Skip things we know we've already pulled
101 next if $txn_date < ( $args{'starting_transaction'} ||0 );
102 # Skip things we've pushed
103 next if ($self->sync_source->foreign_transaction_originated_locally($txn_date, $args{'ticket'}->id) );
104  
105 # ok. it didn't originate locally. we might want to integrate it
106 push @txns, { timestamp => $txn->date,
107 serial => $txn->date->epoch,
108 object => $txn};
109 }
110 $self->sync_source->log_debug('Done looking at pulled txns');
111 return \@txns;
112 }
113  
114 sub build_initial_ticket_state {
115 my $self = shift;
116 my $final_state = shift;
117 my $ticket_object = shift;
118  
119 my %initial_state = %{$final_state};
120  
121 for my $txn ( reverse @{ $ticket_object->history->entries } ) {
122 for my $pc ( values %{ $txn->prop_changes } ) {
123 unless ( $initial_state{ $pc->property } eq $pc->new_value ) {
124 warn "I was expecting "
125 . $pc->property
126 . " to be "
127 . $pc->new_value
128 . " but it was actually "
129 . $initial_state{ $pc->property };
130 }
131 $initial_state{ $pc->property } = $pc->old_value;
132  
133 }
134 }
135 return \%initial_state;
136 }
137  
138 sub transcode_create_txn {
139 my $self = shift;
140 my $txn = shift;
141 my $create_data = shift;
142 my $final_data = shift;
143 my $ticket = $txn->ticket;
144 # this sequence_no only works because trac tickets only allow one update
145 # per ticket per second.
146 # we decrement by 1 on the off chance that someone created and
147 # updated the ticket in the first second
148 my $changeset = Prophet::ChangeSet->new(
149 { original_source_uuid => $self->sync_source->uuid_for_remote_id( $ticket->id ),
150 original_sequence_no => ( $ticket->created->epoch-1),
151 creator => $self->resolve_user_id_to( email_address => $create_data->{reporter} ),
152 created => $ticket->created->ymd ." ".$ticket->created->hms
153 }
154 );
155  
156 my $change = Prophet::Change->new(
157 { record_type => 'ticket',
158 record_uuid => $self->sync_source->uuid_for_remote_id( $ticket->id ),
159 change_type => 'add_file'
160 }
161 );
162  
163 for my $prop ( keys %$create_data ) {
164 next unless defined $create_data->{$prop};
165 next if $prop =~ /^(?:patch)$/;
166 $change->add_prop_change( name => $prop, old => '', new => $create_data->{$prop} );
167 }
168  
169 $changeset->add_change( { change => $change } );
170  
171 if ( my $att = $txn->attachment ) {
172 $self->_recode_attachment_create(
173 ticket => $ticket,
174 txn => $txn,
175 changeset => $changeset,
176 attachment => $att,
177 );
178 }
179 return $changeset;
180 }
181  
182 # we might get return:
183 # 0 changesets if it was a null txn
184 # 1 changeset if it was a normal txn
185 # 2 changesets if we needed to to some magic fixups.
186 sub transcode_one_txn {
187 my ( $self, $txn_wrapper, $ticket, $ticket_final ) = (@_);
188  
189 my $txn = $txn_wrapper->{object};
190  
191 if ($txn->is_create) {
192 return $self->transcode_create_txn($txn,$ticket,$ticket_final);
193 }
194  
195 my $ticket_uuid = $self->sync_source->uuid_for_remote_id( $ticket->{ $self->sync_source->uuid . '-id' } );
196  
197 my $changeset = Prophet::ChangeSet->new(
198 { original_source_uuid => $ticket_uuid,
199 original_sequence_no => $txn->date->epoch, # see comment on ticket
200 # create changeset
201 creator => $self->resolve_user_id_to( email_address => $txn->author ),
202 created => $txn->date->ymd . " " . $txn->date->hms
203 }
204 );
205  
206 my $change = Prophet::Change->new(
207 { record_type => 'ticket',
208 record_uuid => $ticket_uuid,
209 change_type => 'update_file'
210 }
211 );
212  
213 # warn "right here, we need to deal with changed data that trac failed to record";
214  
215 foreach my $prop_change ( values %{ $txn->prop_changes || {} } ) {
216 my $new = $prop_change->new_value;
217 my $old = $prop_change->old_value;
218 my $property = $prop_change->property;
219 next if $property =~ /^(?:patch)$/;
220  
221 $old = undef if ( $old eq '' );
222 $new = undef if ( $new eq '' );
223  
224 if (!exists $ticket_final->{$property}) {
225 $ticket_final->{$property} = $new;
226 $ticket->{$property} = $new;
227 }
228  
229  
230 # walk back $ticket's state
231 if ( ( !defined $new && !defined $ticket->{$property} )
232 || ( defined $new && defined $ticket->{$property} && $ticket->{$property} eq $new ) )
233 {
234 $ticket->{$property} = $old;
235 }
236  
237 $change->add_prop_change( name => $property, old => $old, new => $new );
238  
239 }
240  
241 $changeset->add_change( { change => $change } ) if $change->has_prop_changes;
242  
243 my $comment = $self->new_comment_creation_change();
244  
245 if ( my $content = $txn->content ) {
246 if ( $content !~ /^\s*$/s ) {
247 $comment->add_prop_change( name => 'created', new => $txn->date->ymd . ' ' . $txn->date->hms);
248 $comment->add_prop_change( name => 'creator', new => $self->resolve_user_id_to( email_address => $txn->author ));
249 $comment->add_prop_change( name => 'content', new => $content );
250 $comment->add_prop_change( name => 'content_type', new => 'text/html' );
251 $comment->add_prop_change( name => 'ticket', new => $ticket_uuid);
252  
253 $changeset->add_change( { change => $comment } );
254 }
255 }
256  
257 if ( my $att = $txn->attachment ) {
258 $self->_recode_attachment_create(
259 ticket => $ticket,
260 txn => $txn,
261 changeset => $changeset,
262 attachment => $att,
263 );
264 }
265  
266 return unless $changeset->has_changes;
267  
268 return $changeset;
269 }
270  
271 sub _recode_attachment_create {
272 my $self = shift;
273 my %args =
274 validate( @_,
275 { ticket => 1, txn => 1, changeset => 1, attachment => 1 } );
276 my $change = Prophet::Change->new(
277 {
278 record_type => 'attachment',
279 record_uuid => $self->sync_source->uuid_for_url(
280 $self->sync_source->remote_url
281 . "/attachment/"
282 . $args{'attachment'}->date->epoch
283 ),
284 change_type => 'add_file',
285 }
286 );
287 $change->add_prop_change(
288 name => 'content_type',
289 old => undef,
290 new => $args{'attachment'}->content_type,
291 );
292 $change->add_prop_change(
293 name => 'created',
294 old => undef,
295 new => $args{'attachment'}->date->ymd . ' '
296 . $args{'attachment'}->date->hms
297 );
298 $change->add_prop_change(
299 name => 'creator',
300 old => undef,
301 new => $self->resolve_user_id_to(
302 email_address => $args{'attachment'}->author
303 ),
304 );
305 $change->add_prop_change(
306 name => 'content',
307 old => undef,
308 new => $args{'attachment'}->content,
309 );
310 $change->add_prop_change(
311 name => 'name',
312 old => undef,
313 new => $args{'attachment'}->filename,
314 );
315 $change->add_prop_change(
316 name => 'ticket',
317 old => undef,
318 new => $self->sync_source->uuid_for_remote_id(
319 $args{'ticket'}->{ $self->sync_source->uuid . '-id' }
320 )
321 );
322 $args{'changeset'}->add_change( { change => $change } );
323 }
324  
325 my %PROP_MAP;
326 sub translate_prop_names {
327 my $self = shift;
328 my $changeset = shift;
329  
330 for my $change ( $changeset->changes ) {
331 next unless $change->record_type eq 'ticket';
332  
333 my @new_props;
334 for my $prop ( $change->prop_changes ) {
335 next if ( ( $PROP_MAP{ lc( $prop->name ) } || '' ) eq '_delete' );
336 $prop->name( $PROP_MAP{ lc( $prop->name ) } ) if $PROP_MAP{ lc( $prop->name ) };
337  
338 # Normalize away undef -> "" and vice-versa
339 for (qw/new_value old_value/) {
340 $prop->$_("") if !defined( $prop->$_() );
341 }
342 next if ( $prop->old_value eq $prop->new_value );
343  
344 if ( $prop->name =~ /^cf-(.*)$/ ) {
345 $prop->name( 'custom-' . $1 );
346 }
347  
348 push @new_props, $prop;
349  
350 }
351 $change->prop_changes( \@new_props );
352  
353 }
354 return $changeset;
355 }
356  
357 sub resolve_user_id_to {
358 my $self = shift;
359 my $to = shift;
360 my $id = shift;
361 return $id . '@trac-instance.local';
362  
363 }
364  
365 __PACKAGE__->meta->make_immutable;
366 no Any::Moose;
367 1;