SD/Replica/redmine/PullEncoder.pm untested

filter: Statementstmt 0.0% 0 / 110 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 149 CC 6 SCAR 19.7
CC
6
Cov
0%
transcode_one_txn
20.0
translate_ticket_state
6.0
_include_change_comment
6.0
CRAP 7.2
1 package App::SD::Replica::redmine::PullEncoder;
2 use Any::Moose;
3 extends 'App::SD::ForeignReplica::PullEncoder';
4  
5 # use YAML::XS qw(Dump);
6 use Params::Validate qw(:all);
7  
8 has sync_source => (
9 isa => 'App::SD::Replica::redmine',
10 is => 'rw',
11 required => 1
12 );
13  
14 sub ticket_id {
15 my ($self, $ticket) = @_;
16 return $ticket->id;
17 }
18  
19 sub find_matching_tickets {
20 my $self = shift;
21 my %query = (@_);
22  
23 my $redmine = $self->sync_source->redmine;
24 my $search = $redmine->search_ticket( $query{query} );
25  
26 my @results = $search->results;
27 return \@results;
28 }
29  
30 sub find_matching_transactions {
31 my $self = shift;
32 my %args = validate( @_, { ticket => 1, starting_transaction => 1 } );
33  
34 my @txns;
35 my $raw_txn = $args{ticket}->histories;
36  
37 for my $txn (@$raw_txn) {
38 push @txns, {
39 timestamp => $txn->date->epoch,
40 serial => $txn->id,
41 object => $txn
42 }
43 }
44  
45 # if ($args{ticket}->id == 420) {
46 # die Dump({
47 # ticket_id => $args{ticket}->id,
48 # txn => [ map { { id => $_->{serial}, comment => $_->{comment} } } @txns ]
49 # });
50 # }
51  
52 return \@txns;
53 }
54  
55 sub translate_ticket_state {
56 my $self = shift;
57 my $ticket = shift;
58 my $transactions = shift;
59  
60 my $final_state = {
61 $self->sync_source->uuid . '-id' => $ticket->id,
62 status => lc($ticket->status),
63 summary => $ticket->subject,
64 description => $ticket->description,
65 priority => $ticket->priority,
66 created => $ticket->created_at->ymd . " " . $ticket->created_at->hms,
67 creator => $ticket->author->email
68 };
69 my $initial_state = {%$final_state};
70  
71 for my $txn ( sort { $b->{'serial'} <=> $a->{'serial'} } @$transactions ) {
72 $txn->{post_state} = { %$final_state };
73  
74 if ($txn->{serial} == 0) {
75 $txn->{pre_state} = {};
76 last;
77 }
78  
79 my $property_changes = $txn->{object}->property_changes;
80 while (my ($name, $changes) = each(%$property_changes)) {
81 $initial_state->{$name} = $changes->{from};
82 }
83  
84 $txn->{pre_state} = {%$initial_state};
85 }
86  
87 return $initial_state, $final_state;
88 }
89  
90 sub transcode_one_txn {
91 my $self = shift;
92 my $txn_wrapper = shift;
93 my $older_ticket_state = shift;
94 my $newer_ticket_state = shift;
95  
96 my $txn = $txn_wrapper->{object};
97  
98 if ($txn_wrapper->{serial} == 0) {
99 return $self->transcode_create_txn($txn_wrapper, $older_ticket_state, $newer_ticket_state);
100 }
101  
102 my $ticket_id = $newer_ticket_state->{ $self->sync_source->uuid . '-id' };
103 my $ticket_uuid = $self->sync_source->uuid_for_remote_id($ticket_id);
104 my $creator = $newer_ticket_state->{creator};
105 my $created = $newer_ticket_state->{created};
106  
107 my $changeset = Prophet::ChangeSet->new({
108 original_source_uuid => $ticket_uuid,
109 original_sequence_no => $txn->id,
110 creator => $creator,
111 created => $created,
112 });
113  
114 my $change = Prophet::Change->new({
115 record_type => 'ticket',
116 record_uuid => $ticket_uuid,
117 change_type => 'update_file',
118 });
119  
120 for my $prop ( keys %{ $txn_wrapper->{post_state} } ) {
121 my $new = $txn_wrapper->{post_state}->{$prop};
122 my $old = $txn_wrapper->{pre_state}->{$prop};
123  
124 next unless defined($new) && defined($old);
125  
126 $change->add_prop_change(
127 name => $prop,
128 new => $new,
129 old => $old,
130 ) unless $new eq $old;
131 }
132  
133 $changeset->add_change({ change => $change });
134  
135 $self->_include_change_comment($changeset, $ticket_uuid, $txn);
136 return $changeset;
137 }
138  
139 sub _include_change_comment {
140 my $self = shift;
141 my $changeset = shift;
142 my $ticket_uuid = shift;
143 my $txn = shift;
144  
145 my $comment = $self->new_comment_creation_change();
146  
147 my $content = $txn->note || "";
148  
149 if ( $content !~ /^\s*$/s ) {
150 $comment->add_prop_change(
151 name => 'created',
152 new => $txn->date->ymd . ' ' . $txn->date->hms,
153 );
154 $comment->add_prop_change(
155 name => 'creator',
156 new => 'ccc@example.com',
157 );
158 $comment->add_prop_change( name => 'content', new => $content );
159 $comment->add_prop_change(
160 name => 'content_type',
161 new => 'text/plain',
162 );
163 $comment->add_prop_change( name => 'ticket', new => $ticket_uuid, );
164  
165 $changeset->add_change( { change => $comment } );
166 }
167 }
168  
169 sub transcode_create_txn {
170 my $self = shift;
171 my $txn = shift;
172 my $create_data = shift;
173 my $final_data = shift;
174  
175 my $ticket_id = $final_data->{ $self->sync_source->uuid . '-id' };
176 my $ticket_uuid = $self->sync_source->uuid_for_remote_id($ticket_id);
177 my $creator = 'xxx@example.com';
178 my $created = $final_data->{created};
179  
180 my $changeset = Prophet::ChangeSet->new({
181 original_source_uuid => $ticket_uuid,
182 original_sequence_no => 0,
183 creator => $creator,
184 created => $created,
185 });
186  
187 my $change = Prophet::Change->new({
188 record_type => 'ticket',
189 record_uuid => $ticket_uuid,
190 change_type => 'add_file',
191 });
192  
193 while ( my ($name, $value) = each %{ $txn->{post_state} }) {
194 $change->add_prop_change(
195 name => $name,
196 new => $value
197 )
198 }
199  
200 $changeset->add_change({ change => $change });
201  
202 # for my $att ( @{ $txn->{object}->attachments } ) {
203 # $self->_recode_attachment_create(
204 # ticket_uuid => $ticket_uuid,
205 # txn => $txn->{object},
206 # changeset => $changeset,
207 # attachment => $att,
208 # );
209 # }
210  
211 return $changeset;
212 }
213  
214 sub translate_prop_status {}
215  
216 sub resolve_user_id_to {
217 my $self = shift;
218 my $to = shift;
219 my $id = shift;
220 return $id;
221 }
222  
223  
224 __PACKAGE__->meta->make_immutable;
225 no Any::Moose;
226 1;