SD/Replica/redmine/PushEncoder.pm untested

filter: Statementstmt 0.0% 0 / 66 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 115 CC 14 SCAR 32.9
CC
14
Cov
0%
integrate_change
42.0
integrate_ticket_update
30.0
_recode_props_for_integrate
30.0
CRAP 26.8
1 package App::SD::Replica::redmine::PushEncoder;
2  
3 use Any::Moose;
4 use Params::Validate;
5  
6 has sync_source => (
7 isa => 'App::SD::Replica::redmine',
8 is => 'rw',
9 required => 1
10 );
11  
12 sub integrate_change {
13 my $self = shift;
14 my ( $change, $changeset ) = validate_pos(
15 @_,
16 { isa => 'Prophet::Change' },
17 { isa => 'Prophet::ChangeSet' }
18 );
19 my ( $id, $record );
20  
21 return
22 if $self->sync_source->app_handle->handle->last_changeset_from_source(
23 $changeset->original_source_uuid ) >= $changeset->original_sequence_no;
24  
25 my $before_integration = time();
26  
27 eval {
28 if ( $change->record_type eq 'ticket'
29 and $change->change_type eq 'add_file' )
30 {
31 $id = $self->integrate_ticket_create( $change, $changeset );
32 $self->sync_source->record_remote_id_for_pushed_record(
33 uuid => $change->record_uuid,
34 remote_id => $id,
35 );
36 }
37 elsif ( $change->record_type eq 'comment'
38 and $change->change_type eq 'add_file' )
39 {
40 $id = $self->integrate_comment( $change, $changeset );
41 }
42 elsif ( $change->record_type eq 'ticket' ) {
43 $id = $self->integrate_ticket_update( $change, $changeset );
44 }
45 else {
46 $self->sync_source->log(
47 'I have no idea what I am doing for ' . $change->record_uuid );
48 return;
49 }
50  
51 $self->sync_source->record_pushed_transactions(
52 start_time => $before_integration,
53 ticket => $id,
54 changeset => $changeset,
55 );
56 };
57  
58 if ( my $err = $@ ) {
59 $self->sync_source->log( "Push error: " . $err );
60 }
61  
62 return $id;
63 }
64  
65 sub integrate_ticket_update {
66 my $self = shift;
67 my ( $change, $changeset ) = validate_pos(
68 @_,
69 { isa => 'Prophet::Change' },
70 { isa => 'Prophet::ChangeSet' }
71 );
72 my $remote_ticket_id
73 = $self->sync_source->remote_id_for_uuid( $change->record_uuid );
74 my $attr = $self->_recode_props_for_integrate($change);
75  
76 my $ticket = Net::Redmine::Ticket->load(
77 connection => $self->sync_source->redmine->connection,
78 id => $remote_ticket_id
79 );
80 for (qw/subject description /) {
81 $ticket->$_( $attr->{$_} ) if $attr->{$_};
82 }
83 if ( $attr->{state} ) {
84 $ticket->status("Open") if $attr->{state} eq 'open';
85 $ticket->status("Closed") if $attr->{state} eq 'closed';
86 }
87 $ticket->save;
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 my $attr = $self->_recode_props_for_integrate($change);
99 my $ticket = $self->sync_source->redmine->create(ticket => $attr);
100 # TODO error
101 return $ticket->{id};
102 }
103  
104 sub integrate_comment {
105 my $self = shift;
106 my ( $change, $changeset ) = validate_pos(
107 @_,
108 { isa => 'Prophet::Change' },
109 { isa => 'Prophet::ChangeSet' }
110 );
111  
112 my %props = map { $_->name => $_->new_value } $change->prop_changes;
113 my $ticket_id
114 = $self->sync_source->remote_id_for_uuid( $props{'ticket'} );
115 my $ticket = Net::Redmine::Ticket->load(
116 connection => $self->sync_source->redmine->connection,
117 id => $ticket_id
118 );
119 $ticket->description( $props{'content'} );
120 $ticket->save;
121 return $ticket_id;
122 }
123  
124 sub _recode_props_for_integrate {
125 my $self = shift;
126 my ($change) = validate_pos( @_, { isa => 'Prophet::Change' } );
127  
128 my %props = map { $_->name => $_->new_value } $change->prop_changes;
129 my %attr;
130  
131 for my $key ( keys %props ) {
132 if ( $key eq 'summary' ) {
133 $attr{subject} = $props{$key};
134 }
135 elsif ( $key eq 'body' ) {
136 $attr{description} = $props{$key};
137 }
138 elsif ( $key eq 'status' ) {
139 $attr{state} = $props{$key} =~ /new|open/ ? 'open' : 'closed';
140 }
141 }
142 return \%attr;
143 }
144  
145 __PACKAGE__->meta->make_immutable;
146 no Any::Moose;
147  
148 1;