SD/Replica/github/PushEncoder.pm untested

filter: Statementstmt 0.0% 0 / 69 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 119 CC 14 SCAR 31.3
CC
14
Cov
0%
integrate_change
42.0
_recode_props_for_integrate
30.0
integrate_ticket_update
20.0
CRAP 22.9
1 package App::SD::Replica::github::PushEncoder;
2 use Any::Moose;
3 use Params::Validate;
4 use Path::Class;
5  
6 has sync_source => (
7 isa => 'App::SD::Replica::github',
8 is => 'rw',
9 );
10  
11 sub integrate_change {
12 my $self = shift;
13 my ( $change, $changeset ) = validate_pos(
14 @_,
15 { isa => 'Prophet::Change' },
16 { isa => 'Prophet::ChangeSet' }
17 );
18 my ( $id, $record );
19  
20 # if the original_sequence_no of this changeset is <=
21 # the last changeset our sync source for the original_sequence_no, we can skip it.
22 # XXX TODO - this logic should be at the changeset level, not the cahnge level, as it applies to all
23 # changes in the changeset
24 # warn $self->sync_source->app_handle->handle->last_changeset_from_source(
25 # $changeset->original_source_uuid ), "\n";
26 return
27 if $self->sync_source->app_handle->handle->last_changeset_from_source(
28 $changeset->original_source_uuid ) >= $changeset->original_sequence_no;
29  
30 my $before_integration = time();
31  
32 eval {
33 if ( $change->record_type eq 'ticket'
34 and $change->change_type eq 'add_file' )
35 {
36 $id = $self->integrate_ticket_create( $change, $changeset );
37 $self->sync_source->record_remote_id_for_pushed_record(
38 uuid => $change->record_uuid,
39 remote_id => $id,
40 );
41 }
42 elsif ( $change->record_type eq 'comment'
43 and $change->change_type eq 'add_file' )
44 {
45 $id = $self->integrate_comment( $change, $changeset );
46 }
47 elsif ( $change->record_type eq 'ticket' ) {
48 $id = $self->integrate_ticket_update( $change, $changeset );
49 }
50 else {
51 $self->sync_source->log(
52 'I have no idea what I am doing for ' . $change->record_uuid );
53 return;
54 }
55  
56 $self->sync_source->record_pushed_transactions(
57 start_time => $before_integration,
58 ticket => $id,
59 changeset => $changeset,
60 );
61 };
62  
63 if ( my $err = $@ ) {
64 $self->sync_source->log( "Push error: " . $err );
65 }
66  
67 return $id;
68 }
69  
70 sub integrate_ticket_update {
71 my $self = shift;
72 my ( $change, $changeset ) = validate_pos(
73 @_,
74 { isa => 'Prophet::Change' },
75 { isa => 'Prophet::ChangeSet' }
76 );
77  
78 # Figure out the remote site's ticket ID for this change's record
79 my $remote_ticket_id =
80 $self->sync_source->remote_id_for_uuid( $change->record_uuid );
81 my $ticket = $self->sync_source->github->issue();
82 my $attr = $self->_recode_props_for_integrate($change);
83 $ticket->edit( $remote_ticket_id, $attr->{title}, $attr->{body} );
84 if ( $attr->{status} ) {
85 $ticket->reopen( $remote_ticket_id ) if $attr->{status} eq 'open';
86 $ticket->close( $remote_ticket_id ) if $attr->{status} eq 'closed';
87 }
88 return $remote_ticket_id;
89 }
90  
91 sub integrate_ticket_create {
92 my $self = shift;
93 my ( $change, $changeset ) = validate_pos(
94 @_,
95 { isa => 'Prophet::Change' },
96 { isa => 'Prophet::ChangeSet' }
97 );
98  
99 # Build up a ticket object out of all the record's attributes
100 my $ticket = $self->sync_source->github->issue;
101 my $attr = $self->_recode_props_for_integrate($change);
102 my $new =
103 $ticket->open( $attr->{title}, $attr->{body} );
104 # TODO: better error handler?
105 if ( $new->{error} ) {
106 die "\n\n$new->{error}";
107 }
108 return $new->{number};
109 }
110  
111 sub integrate_comment {
112 my $self = shift;
113 my ( $change, $changeset ) = validate_pos(
114 @_,
115 { isa => 'Prophet::Change' },
116 { isa => 'Prophet::ChangeSet' }
117 );
118  
119 # Figure out the remote site's ticket ID for this change's record
120  
121 my %props = map { $_->name => $_->new_value } $change->prop_changes;
122 my $ticket_id = $self->sync_source->remote_id_for_uuid( $props{'ticket'} );
123 my $ticket = $self->sync_source->github->issue();
124 $ticket->comment($ticket_id, $props{'content'});
125 return $ticket_id;
126 }
127  
128 sub _recode_props_for_integrate {
129 my $self = shift;
130 my ($change) = validate_pos( @_, { isa => 'Prophet::Change' } );
131  
132 my %props = map { $_->name => $_->new_value } $change->prop_changes;
133 my %attr;
134  
135 for my $key ( keys %props ) {
136 if ( $key eq 'summary' ) {
137 $attr{title} = $props{$key};
138 }
139 elsif ( $key eq 'body' ) {
140 $attr{$key} = $props{$key};
141 }
142 elsif ( $key eq 'status' ) {
143 $attr{state} = $props{$key} =~ /new|open/ ? 'open' : 'closed';
144 }
145 }
146 return \%attr;
147 }
148  
149 __PACKAGE__->meta->make_immutable;
150 no Any::Moose;
151  
152 1;