SD/Replica/gcode/PullEncoder.pm untested

filter: Statementstmt 0.0% 0 / 259 Branchbran 0.0% 0 / 78 Conditioncond 0.0% 0 / 28 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 413 CC 40 SCAR 48.7
CC
40
Cov
0%
translate_ticket_state
342.0
find_matching_tickets
110.0
transcode_one_txn
42.0
CRAP 129.9
1 package App::SD::Replica::gcode::PullEncoder;
2 use Any::Moose;
3 extends 'App::SD::ForeignReplica::PullEncoder';
4  
5 use Params::Validate qw(:all);
6 use Memoize;
7 use DateTime;
8  
9 has sync_source => (
10 isa => 'App::SD::Replica::gcode',
11 is => 'rw',
12 );
13  
14 my %PROP_MAP = %App::SD::Replica::gcode::PROP_MAP;
15  
16 sub ticket_id {
17 my $self = shift;
18 return shift->id;
19 }
20  
21 sub _translate_final_ticket_state {
22 my $self = shift;
23 my $ticket = shift;
24  
25 my @labels = @{$ticket->labels};
26 my %prop;
27 my @tags;
28  
29 for my $label (@labels) {
30 if ( $label =~ /(.*?)-(.*)/ ) {
31 $prop{lc $1} = $2;
32 }
33 else {
34 push @tags, $label;
35 }
36 }
37  
38 my $ticket_data = {
39 $self->sync_source->uuid . '-id' => $ticket->id,
40 owner => $ticket->owner,
41 created => $ticket->reported->ymd . ' ' . $ticket->reported->hms,
42 reporter => $ticket->reporter,
43 status => $self->translate_prop_status( $ticket->status ),
44 summary => $ticket->summary,
45 description => $ticket->description,
46 tags => (join ', ', @tags),
47 cc => $ticket->cc,
48 };
49  
50 for my $p ( keys %prop ) {
51 $ticket_data->{$p} = $prop{$p};
52 }
53  
54 # delete undefined and empty fields
55 delete $ticket_data->{$_}
56 for grep { !defined $ticket_data->{$_} || $ticket_data->{$_} eq '' || $ticket_data->{$_} eq '----' }
57 keys %$ticket_data;
58  
59 return $ticket_data;
60 }
61  
62 =head2 find_matching_tickets QUERY
63  
64 Returns a array of all tickets found matching your QUERY hash.
65  
66 =cut
67  
68 sub find_matching_tickets {
69 my $self = shift;
70 my %args = (@_);
71 my $query = $args{query};
72 my %query;
73 if ($query) {
74 if ( $query =~ /=/ ) {
75 %query = map { /(.+)=(.*)/; $1 => $2 }
76 split /&/, $query;
77 }
78 else {
79 $query{q} = $query;
80 }
81 }
82  
83 my $last_changeset_seen_dt = $self->_only_pull_tickets_modified_after()
84 || DateTime->from_epoch( epoch => 0 );
85 $self->sync_source->log("Searching for tickets. This can take a very long time on initial sync or if you haven't synced in a long time.");
86 require Net::Google::Code;
87  
88 if ( $Net::Google::Code::VERSION lt '0.15' ) {
89 die
90 "query support is only for Net::Google::Code version not less than 0.15"
91 if $args{query};
92 require Net::Google::Code::Issue::Search;
93 my $search =
94 Net::Google::Code::Issue::Search->new(
95 project => $self->sync_source->project, );
96  
97 if ( $search->updated_after($last_changeset_seen_dt) ) {
98 return $search->results;
99 }
100 else {
101 return [];
102 }
103 }
104 else {
105 my $issue = Net::Google::Code::Issue->new(
106 map { $_ => $self->sync_source->gcode->$_ }
107 grep { $self->sync_source->gcode->$_ }
108 qw/project email password/ );
109  
110 if ( keys %query == 0 ) {
111  
112 # we can use old updated_after method here if no query strings
113 # loading issue by checking feeds update is more effective, if
114 # possible
115 local $Net::Google::Code::Issue::USE_HYBRID = 0;
116 require Net::Google::Code::Issue::Search;
117 my $search =
118 Net::Google::Code::Issue::Search->new(
119 project => $self->sync_source->project, );
120  
121 # 0 here is to not fallback to ->search method
122 if ( $search->updated_after( $last_changeset_seen_dt, 0 ) ) {
123 return $search->results;
124 }
125 }
126  
127 $query{can} ||= 'all';
128 $query{max_results} ||= 1_000_000_000;
129 delete $query{q} unless defined $query{q};
130 my $results = $issue->list( %query,
131 updated_min => $query{updated_min}
132 && $query{updated_min} gt "$last_changeset_seen_dt"
133 ? $query{updated_min}
134 : "$last_changeset_seen_dt" );
135  
136 $_->load for @$results;
137 return $results;
138 }
139 }
140  
141 sub _only_pull_tickets_modified_after {
142 my $self = shift;
143  
144 my $last_pull = $self->sync_source->upstream_last_modified_date();
145 return unless $last_pull;
146 my $before = App::SD::Util::string_to_datetime($last_pull);
147 $self->log_debug( "Failed to parse '" . $self->sync_source->upstream_last_modified_date() . "' as a timestamp. That means we have to sync ALL history") unless ($before);
148 return $before;
149 }
150  
151 sub translate_ticket_state {
152 my $self = shift;
153 my $ticket = shift;
154 my $transactions = shift;
155  
156 my $final_state = $self->_translate_final_ticket_state($ticket);
157 my %earlier_state = %{$final_state};
158  
159 my $pre_txn;
160 for my $txn ( sort { $b->{'serial'} <=> $a->{'serial'} } @$transactions ) {
161 $txn->{post_state} = {%earlier_state};
162  
163 if ( $txn->{serial} == 0 ) {
164 $txn->{pre_state} = {};
165 last;
166 }
167  
168 my $updates = $txn->{object}->updates;
169  
170 for my $prop (qw(owner status summary)) {
171 next unless exists $updates->{$prop};
172 my $value = delete $updates->{$prop};
173 $value = '' if ($value eq '----');
174 if ( my $sub = $self->can( 'translate_prop_' . $prop ) ) {
175 $value = $sub->( $self, $value );
176 }
177  
178 $earlier_state{ $PROP_MAP{$prop} } =
179 $self->warp_list_to_old_value( $earlier_state{ $PROP_MAP{$prop} },
180 $value, undef );
181 $txn->{post_state}{ $PROP_MAP{$prop} } = $value;
182 }
183  
184 if ( $updates->{cc} ) {
185 my $value = delete $updates->{cc};
186 my $is_delete;
187 my @cc = split /\s+/, $value;
188 for my $value (@cc) {
189 if ( $value =~ /^-(.*)$/ ) {
190 $is_delete = 1;
191 $value = $1;
192 }
193  
194 $earlier_state{ $PROP_MAP{cc} } =
195 $self->warp_list_to_old_value( $earlier_state{cc},
196 $is_delete ? ( undef, $value ) : ( $value, undef ) );
197 }
198 }
199  
200 if ( $updates->{mergedinto} ) {
201 my $value = delete $updates->{mergedinto};
202 my $is_delete;
203 if ( $value =~ /^-(.*)$/ ) {
204 $is_delete = 1;
205 $value = $1;
206 }
207  
208 $earlier_state{ $PROP_MAP{mergedinto} } =
209 $self->warp_list_to_old_value( $earlier_state{mergedinto},
210 $is_delete ? ( undef, $value ) : ( $value, undef ) );
211 $txn->{post_state}{ $PROP_MAP{mergedinto} } = $value
212 unless $is_delete;
213 }
214  
215 if ( $updates->{labels} ) {
216 my $values = delete $updates->{labels};
217 for my $value (@$values) {
218 my $is_delete;
219 if ( $value =~ /^-(.*)$/ ) {
220 $is_delete = 1;
221 $value = $1;
222 }
223  
224 my $name;
225 if ( $value =~ /(.*?)-(.*)/ ) {
226 $name = lc $1;
227 $value = $2;
228 }
229 else {
230 $name = 'labels';
231 }
232  
233 $name = $PROP_MAP{$name} || $name;
234  
235 $earlier_state{$name} =
236 $self->warp_list_to_old_value( $earlier_state{$name},
237 $is_delete ? ( undef, $value ) : ( $value, undef ) );
238 }
239 }
240  
241 $txn->{pre_state} = {%earlier_state};
242 $pre_txn->{pre_state} = $txn->{post_state} if $pre_txn;
243 $pre_txn = $txn;
244 }
245  
246 # XXX try our best to find historical info
247 # e.g.
248 # comemnt 3 has summary: "foo"
249 # comment 4 and 5 don't have summary changes
250 # comment 6 has summary: "bar"
251 # then we can set comment 4 and 5's summary to 'foo'
252 my @sorted = sort { $b->{'serial'} <=> $a->{'serial'} } @$transactions;
253 for ( my $i = 0 ; $i < @sorted ; $i++ ) {
254 for my $prop (qw(owner status summary)) {
255 if ( !$sorted[$i]->{post_state}{ $PROP_MAP{$prop} } ) {
256 ( $sorted[$i]->{post_state}{ $PROP_MAP{$prop} } ) =
257 grep { $_ }
258 map { $_->{post_state}{ $PROP_MAP{$prop} } }
259 @sorted[ $i + 1 .. $#sorted ];
260 $sorted[$i]->{post_state}{ $PROP_MAP{$prop} } ||= '';
261 }
262 }
263 }
264  
265 return \%earlier_state, $final_state;
266 }
267  
268 =head2 find_matching_transactions { ticket => $id, starting_transaction => $num }
269  
270 Returns a reference to an array of all transactions (as hashes) on ticket $id after transaction $num.
271  
272 =cut
273  
274 sub find_matching_transactions {
275 my $self = shift;
276 my %args = validate( @_, { ticket => 1, starting_transaction => 1 } );
277 my @raw_txns = @{ $args{ticket}->comments };
278  
279 my @txns;
280 for my $txn ( sort { $a->sequence <=> $b->sequence } @raw_txns ) {
281 my $txn_date = $txn->date->epoch;
282  
283 # Skip things we know we've already pulled
284 next if $txn_date < ( $args{'starting_transaction'} || 0 );
285  
286 # Skip things we've pushed
287 next if (
288 $self->sync_source->foreign_transaction_originated_locally(
289 $txn_date, $args{'ticket'}->id
290 )
291 );
292  
293 # ok. it didn't originate locally. we might want to integrate it
294 push @txns,
295 {
296 timestamp => $txn->date,
297 serial => $txn->sequence,
298 object => $txn,
299 };
300 }
301 $self->sync_source->log_debug('Done looking at pulled txns');
302  
303 return \@txns;
304 }
305  
306 sub transcode_create_txn {
307 my $self = shift;
308 my $txn = shift;
309 my $create_data = shift;
310 my $final_data = shift;
311 my $ticket_id = $final_data->{ $self->sync_source->uuid . '-id' };
312 my $ticket_uuid =
313 $self->sync_source->uuid_for_remote_id($ticket_id);
314 my $creator =
315 $self->resolve_user_id_to( email_address => $create_data->{reporter} );
316 my $created = $final_data->{created};
317  
318 my $changeset = Prophet::ChangeSet->new(
319 {
320 original_source_uuid => $ticket_uuid,
321 original_sequence_no => 0,
322 creator => $creator,
323 created => $created,
324 }
325 );
326  
327 my $change = Prophet::Change->new(
328 {
329 record_type => 'ticket',
330 record_uuid => $ticket_uuid,
331 change_type => 'add_file',
332 }
333 );
334  
335 for my $prop ( keys %{ $txn->{post_state} } ) {
336 $change->add_prop_change(
337 name => $prop,
338 new => ref( $txn->{post_state}->{$prop} ) eq 'ARRAY'
339 ? join( ', ', @{ $txn->{post_state}->{$prop} } )
340 : $txn->{post_state}->{$prop},
341 );
342 }
343 $changeset->add_change( { change => $change } );
344  
345 for my $att ( @{ $txn->{object}->attachments } ) {
346 $self->_recode_attachment_create(
347 ticket_uuid => $ticket_uuid,
348 txn => $txn->{object},
349 changeset => $changeset,
350 attachment => $att,
351 );
352 }
353 return $changeset;
354 }
355  
356 # we might get return:
357 # 0 changesets if it was a null txn
358 # 1 changeset if it was a normal txn
359 # 2 changesets if we needed to to some magic fixups.
360  
361 sub transcode_one_txn {
362 my $self = shift;
363 my $txn_wrapper = shift;
364 my $older_ticket_state = shift;
365 my $newer_ticket_state = shift;
366  
367 my $txn = $txn_wrapper->{object};
368 if ( $txn_wrapper->{serial} == 0 ) {
369 return $self->transcode_create_txn( $txn_wrapper, $older_ticket_state,
370 $newer_ticket_state );
371 }
372  
373 my $ticket_id = $newer_ticket_state->{ $self->sync_source->uuid . '-id' };
374 my $ticket_uuid =
375 $self->sync_source->uuid_for_remote_id( $ticket_id );
376 my $changeset = Prophet::ChangeSet->new(
377 {
378 original_source_uuid => $ticket_uuid,
379 original_sequence_no => $txn->sequence,
380 creator =>
381 $self->resolve_user_id_to( email_address => $txn->author ),
382 created => $txn->date->ymd . " " . $txn->date->hms
383 }
384 );
385  
386 my $change = Prophet::Change->new(
387 {
388 record_type => 'ticket',
389 record_uuid => $ticket_uuid,
390 change_type => 'update_file'
391 }
392 );
393  
394 for my $prop ( keys %{ $txn_wrapper->{post_state} } ) {
395 my $new = $txn_wrapper->{post_state}->{$prop};
396 my $old = $txn_wrapper->{pre_state}->{$prop};
397 $change->add_prop_change(
398 name => $prop,
399 new => $new,
400 old => defined $old ? $old : '',
401 ) unless $new eq $old;
402 }
403  
404 # warn "right here, we need to deal with changed data that gcode failed to record";
405 my %updates = %{ $txn->updates };
406  
407 my $props = $txn->updates;
408 foreach my $prop ( keys %{ $props || {} } ) {
409 $prop = lc $prop;
410 $change->add_prop_change(
411 name => $PROP_MAP{$prop} || $prop,
412 old => $txn_wrapper->{pre_state}->{$PROP_MAP{$prop}},
413 new => $txn_wrapper->{post_state}->{$PROP_MAP{$prop}}
414 );
415 }
416  
417 $changeset->add_change( { change => $change } )
418 if $change->has_prop_changes;
419  
420 $self->_include_change_comment( $changeset, $ticket_uuid, $txn );
421  
422 return unless $changeset->has_changes;
423 return $changeset;
424 }
425  
426 sub _include_change_comment {
427 my $self = shift;
428 my $changeset = shift;
429 my $ticket_uuid = shift;
430 my $txn = shift;
431  
432 my $comment = $self->new_comment_creation_change();
433
434 if ( my $content = $txn->content ) {
435 if ( $content !~ /^\s*$/s ) {
436 $comment->add_prop_change(
437 name => 'created',
438 new => $txn->date->ymd . ' ' . $txn->date->hms,
439 );
440 $comment->add_prop_change(
441 name => 'creator',
442 new =>
443 $self->resolve_user_id_to( email_address => $txn->author ),
444 );
445 $comment->add_prop_change( name => 'content', new => $content );
446 $comment->add_prop_change(
447 name => 'content_type',
448 new => 'text/plain',
449 );
450 $comment->add_prop_change( name => 'ticket', new => $ticket_uuid, );
451  
452 $changeset->add_change( { change => $comment } );
453 }
454 }
455  
456 for my $att ( @{ $txn->attachments } ) {
457 $self->_recode_attachment_create(
458 ticket_uuid => $ticket_uuid,
459 txn => $txn,
460 changeset => $changeset,
461 attachment => $att,
462 );
463 }
464 }
465  
466 sub _recode_attachment_create {
467 my $self = shift;
468 my %args =
469 validate( @_,
470 { ticket_uuid => 1, txn => 1, changeset => 1, attachment => 1 } );
471 my $change = Prophet::Change->new(
472 {
473 record_type => 'attachment',
474 record_uuid => $self->sync_source->uuid_for_url(
475 $self->sync_source->remote_url
476 . "/attachment/"
477 . $args{'attachment'}->id,
478 ),
479 change_type => 'add_file',
480 }
481 );
482  
483 $change->add_prop_change(
484 name => 'content_type',
485 old => undef,
486 new => $args{'attachment'}->content_type,
487 );
488  
489 $change->add_prop_change(
490 name => 'created',
491 old => undef,
492 new => $args{'txn'}->date->ymd . ' ' . $args{'txn'}->date->hms,
493 );
494 $change->add_prop_change(
495 name => 'creator',
496 old => undef,
497 new =>
498 $self->resolve_user_id_to( email_address => $args{'txn'}->author )
499 );
500  
501 $change->add_prop_change(
502 name => 'content',
503 old => undef,
504 new => $args{'attachment'}->content,
505 );
506 $change->add_prop_change(
507 name => 'name',
508 old => undef,
509 new => $args{'attachment'}->name,
510 );
511 $change->add_prop_change(
512 name => 'ticket',
513 old => undef,
514 new => $args{ticket_uuid},
515 );
516 $args{'changeset'}->add_change( { change => $change } );
517 }
518  
519 sub translate_prop_status {
520 my $self = shift;
521 my $status = shift;
522 return lc($status);
523 }
524  
525 sub resolve_user_id_to {
526 my $self = shift;
527 my $to = shift;
528 my $id = shift;
529 return $id . '@gmail.com';
530  
531 }
532  
533 __PACKAGE__->meta->make_immutable;
534 no Any::Moose;
535 1;