| 1 | package App::SD::Replica::rt::PullEncoder; | ||
| 2 | use Any::Moose; use strict; | ||
| 3 | extends 'App::SD::ForeignReplica::PullEncoder'; | ||
| 4 | |||
| 5 | use Params::Validate qw(:all); | ||
| 6 | use Memoize; | ||
| 7 | use App::SD::Util; | ||
| 8 | |||
| 9 | has sync_source => | ||
| 10 | ( isa => 'App::SD::Replica::rt', | ||
| 11 | is => 'rw'); | ||
| 12 | |||
| 13 | sub ticket_id { | ||
| 14 | my $self = shift; | ||
| 15 | my $ticket = shift; | ||
| 16 | return $ticket->{id}; | ||
| 17 | } | ||
| 18 | |||
| 19 | sub translate_ticket_state { | ||
| 20 | my $self = shift; | ||
| 21 | my $ticket = shift; | ||
| 22 | my $transactions = shift; | ||
| 23 | |||
| 24 | # undefine empty fields, we'll delete after cleaning | ||
| 25 | $ticket->{$_} = undef for | ||
| 26 | grep defined $ticket->{$_} && $ticket->{$_} eq '', | ||
| 27 | keys %$ticket; | ||
| 28 | |||
| 29 | $ticket->{'id'} =~ s/^ticket\///g; | ||
| 30 | |||
| 31 | $ticket->{ $self->sync_source->uuid . '-' . lc($_) } = delete $ticket->{$_} | ||
| 32 | for qw(Queue id); | ||
| 33 | |||
| 34 | delete $ticket->{'Owner'} if lc($ticket->{'Owner'}) eq 'nobody'; | ||
| 35 | $ticket->{'Owner'} = $self->resolve_user_id_to( email_address => $ticket->{'Owner'} ) | ||
| 36 | if $ticket->{'Owner'}; | ||
| 37 | |||
| 38 | # normalize names of watchers to variant with suffix 's' | ||
| 39 | foreach my $field (qw(Requestor Cc AdminCc)) { | ||
| 40 | if ( defined $ticket->{$field} && defined $ticket->{$field .'s'} ) { | ||
| 41 | die "It's impossible! Ticket has '$field' and '${field}s'"; | ||
| 42 | } elsif ( defined $ticket->{$field} ) { | ||
| 43 | $ticket->{$field .'s'} = delete $ticket->{$field}; | ||
| 44 | } | ||
| 45 | } | ||
| 46 | |||
| 47 | for my $date (grep defined $ticket->{$_}, qw(Created Resolved Told LastUpdated Due Starts Started)) { | ||
| 48 | my $dt = App::SD::Util::string_to_datetime($ticket->{$date}); | ||
| 49 | if ($dt) { | ||
| 50 | $ticket->{$date} = $dt->ymd('-')." ".$dt->hms(":"); | ||
| 51 | } else { | ||
| 52 | delete $ticket->{$date} | ||
| 53 | } | ||
| 54 | } | ||
| 55 | |||
| 56 | $ticket->{$_} =~ s/ minutes$// | ||
| 57 | for grep defined $ticket->{$_}, qw(TimeWorked TimeLeft TimeEstimated); | ||
| 58 | |||
| 59 | $ticket->{'Status'} =~ $self->translate_status($ticket->{'Status'}); | ||
| 60 | |||
| 61 | # delete undefined and empty fields | ||
| 62 | delete $ticket->{$_} for | ||
| 63 | grep !defined $ticket->{$_} || $ticket->{$_} eq '', | ||
| 64 | keys %$ticket; | ||
| 65 | |||
| 66 | return $ticket, {%$ticket}; | ||
| 67 | } | ||
| 68 | |||
| 69 | =head2 find_matching_tickets query => QUERY | ||
| 70 | |||
| 71 | Returns an RT::Client ticket collection for all tickets found matching your QUERY string. | ||
| 72 | |||
| 73 | =cut | ||
| 74 | |||
| 75 | sub find_matching_tickets { | ||
| 76 | my $self = shift; | ||
| 77 | my %args = validate(@_,{query => 1}); | ||
| 78 | my $query = $args{query}; | ||
| 79 | # If we've ever synced, we can limit our search to only newer things | ||
| 80 | if ( my $before = $self->_only_pull_tickets_modified_after ) { | ||
| 81 | $query = "($query) AND LastUpdated >= '" . $before->ymd('-') . " " . $before->hms(':') . "'"; | ||
| 82 | $self->sync_source->log( "Skipping all tickets not updated since " . $before->iso8601 ); | ||
| 83 | } | ||
| 84 | return [map { | ||
| 85 | Prophet::CLI->end_pager(); | ||
| 86 | # squelch chatty RT::Client::REST "Unknown key" warnings unless debugging turned on | ||
| 87 | local $SIG{__WARN__} = sub { $self->sync_source->log_debug(@_) }; | ||
| 88 | my $hash = $self->sync_source->rt->show( type => 'ticket', id => $_ ); | ||
| 89 | $hash->{id} =~ s|^ticket/||g; | ||
| 90 | $hash | ||
| 91 | } $self->sync_source->rt->search( type => 'ticket', query => $query )]; | ||
| 92 | } | ||
| 93 | |||
| 94 | |||
| 95 | =head2 find_matching_transactions { ticket => $id, starting_transaction => $num } | ||
| 96 | |||
| 97 | Returns a reference to an array of all transactions (as hashes) on ticket $id after transaction $num. | ||
| 98 | |||
| 99 | =cut | ||
| 100 | |||
| 101 | sub find_matching_transactions { | ||
| 102 | my $self = shift; | ||
| 103 | my %args = validate( @_, { ticket => 1, starting_transaction => 1 } ); | ||
| 104 | my @txns; | ||
| 105 | |||
| 106 | my $rt_handle = $self->sync_source->rt; | ||
| 107 | |||
| 108 | my $ticket_id = $self->ticket_id( $args{ticket} ); | ||
| 109 | |||
| 110 | my $latest = $self->sync_source->app_handle->handle->last_changeset_from_source( | ||
| 111 | $self->sync_source->uuid_for_remote_id($ticket_id) ) || 0; | ||
| 112 | |||
| 113 | for my $txn ( sort $rt_handle->get_transaction_ids( parent_id => $ticket_id ) ) { | ||
| 114 | |||
| 115 | # Skip things calling code told us to skip | ||
| 116 | next if $txn < $args{'starting_transaction'}; | ||
| 117 | |||
| 118 | # skip things we had on our last pull | ||
| 119 | next if $txn <= $latest; | ||
| 120 | |||
| 121 | # Skip things we've pushed | ||
| 122 | next if $self->sync_source->foreign_transaction_originated_locally( $txn, $ticket_id ); | ||
| 123 | my $txn_hash = $rt_handle->get_transaction( parent_id => $ticket_id, id => $txn, type => 'ticket' ); | ||
| 124 | if ( my $attachments = delete $txn_hash->{'Attachments'} ) { | ||
| 125 | for my $attach ( split( /\n/, $attachments ) ) { | ||
| 126 | next unless ( $attach =~ /^(\d+):/ ); | ||
| 127 | my $id = $1; | ||
| 128 | my $a = $rt_handle->get_attachment( parent_id => $ticket_id, id => $id ); | ||
| 129 | |||
| 130 | push( @{ $txn_hash->{_attachments} }, $a ) if ( $a->{Filename} ); | ||
| 131 | |||
| 132 | } | ||
| 133 | |||
| 134 | } | ||
| 135 | push @txns, | ||
| 136 | { | ||
| 137 | timestamp => App::SD::Util::string_to_datetime( $txn_hash->{Created} ), | ||
| 138 | serial => $txn_hash->{id}, | ||
| 139 | object => $txn_hash | ||
| 140 | }; | ||
| 141 | } | ||
| 142 | return \@txns; | ||
| 143 | } | ||
| 144 | |||
| 145 | sub transcode_one_txn { | ||
| 146 | my ($self, $txn_wrapper, $ticket_initial_state, $ticket) = (@_); | ||
| 147 | |||
| 148 | my $txn = $txn_wrapper->{object}; | ||
| 149 | |||
| 150 | my $sub = $self->can( '_recode_txn_' . $txn->{'Type'} ); | ||
| 151 | unless ( $sub ) { | ||
| 152 | die "Transaction type $txn->{Type} (for transaction $txn->{id}) not implemented yet"; | ||
| 153 | } | ||
| 154 | my $changeset = Prophet::ChangeSet->new( | ||
| 155 | { original_source_uuid => $self->sync_source->uuid_for_remote_id( $ticket->{ $self->sync_source->uuid . '-id' } ), | ||
| 156 | original_sequence_no => $txn->{'id'}, | ||
| 157 | created => $txn->{'Created'}, | ||
| 158 | creator => $self->resolve_user_id_to( email_address => $txn->{'Creator'} ), | ||
| 159 | } | ||
| 160 | ); | ||
| 161 | |||
| 162 | if ( $txn->{'Ticket'} ne $ticket->{$self->sync_source->uuid . '-id'} | ||
| 163 | && $txn->{'Type'} !~ /^(?:Comment|Correspond)$/ | ||
| 164 | ) { | ||
| 165 | warn "Skipping a data change from a merged ticket " . $txn->{'Ticket'} .' vs '. $ticket->{$self->sync_source->uuid . '-id'} . "\n"; | ||
| 166 | return; | ||
| 167 | } | ||
| 168 | |||
| 169 | delete $txn->{'OldValue'} if ( $txn->{'OldValue'} eq ''); | ||
| 170 | delete $txn->{'NewValue'} if ( $txn->{'NewValue'} eq ''); | ||
| 171 | |||
| 172 | $sub->( $self, ticket => $ticket, txn => $txn, changeset => $changeset); | ||
| 173 | $self->translate_prop_names($changeset); | ||
| 174 | |||
| 175 | if (my $attachments = delete $txn->{'_attachments'}) { | ||
| 176 | for my $attach (@$attachments) { | ||
| 177 | $self->_recode_attachment_create( ticket => $ticket, txn => $txn, changeset =>$changeset, attachment => $attach); | ||
| 178 | } | ||
| 179 | } | ||
| 180 | |||
| 181 | return $changeset; | ||
| 182 | } | ||
| 183 | |||
| 184 | |||
| 185 | { # Recoding RT transactions | ||
| 186 | |||
| 187 | sub _recode_attachment_create { | ||
| 188 | my $self = shift; | ||
| 189 | my %args = validate( @_, { ticket => 1, txn => 1, changeset => 1, attachment => 1 } ); | ||
| 190 | my $change = Prophet::Change->new( | ||
| 191 | { record_type => 'attachment', | ||
| 192 | record_uuid => $self->sync_source->uuid_for_url( $self->sync_source->remote_url . "/attachment/" . $args{'attachment'}->{'id'} ), | ||
| 193 | change_type => 'add_file' | ||
| 194 | } | ||
| 195 | ); | ||
| 196 | $change->add_prop_change( name => 'content_type', old => undef, new => $args{'attachment'}->{'ContentType'}); | ||
| 197 | $change->add_prop_change( name => 'created', old => undef, new => $args{'txn'}->{'Created'} ); | ||
| 198 | $change->add_prop_change( name => 'creator', old => undef, new => $self->resolve_user_id_to( email_address => $args{'attachment'}->{'Creator'})); | ||
| 199 | $change->add_prop_change( name => 'content', old => undef, new => $args{'attachment'}->{'Content'}); | ||
| 200 | $change->add_prop_change( name => 'name', old => undef, new => $args{'attachment'}->{'Filename'}); | ||
| 201 | $change->add_prop_change( name => 'ticket', old => undef, new => $self->sync_source->uuid_for_remote_id( $args{'ticket'}->{ $self->sync_source->uuid . '-id'} )); | ||
| 202 | $args{'changeset'}->add_change( { change => $change } ); | ||
| 203 | } | ||
| 204 | |||
| 205 | sub _recode_txn_Keyword {} # RT 2 - unused | ||
| 206 | sub _recode_txn_CommentEmailRecord { return; } | ||
| 207 | |||
| 208 | sub _recode_txn_EmailRecord { return; } | ||
| 209 | sub _recode_txn_AddReminder { return; } | ||
| 210 | sub _recode_txn_ResolveReminder { return; } | ||
| 211 | sub _recode_txn_DeleteLink { } | ||
| 212 | |||
| 213 | sub _recode_txn_Status { | ||
| 214 | my $self = shift; | ||
| 215 | my %args = validate( @_, { txn => 1, ticket => 1, changeset => 1 } ); | ||
| 216 | |||
| 217 | $args{txn}->{'Type'} = 'Set'; | ||
| 218 | return $self->_recode_txn_Set(%args); | ||
| 219 | } | ||
| 220 | |||
| 221 | sub _recode_txn_Told { | ||
| 222 | my $self = shift; | ||
| 223 | my %args = validate( @_, { txn => 1, ticket => 1, changeset => 1 } ); | ||
| 224 | $args{txn}->{'Type'} = 'Set'; | ||
| 225 | return $self->_recode_txn_Set(%args); | ||
| 226 | } | ||
| 227 | |||
| 228 | sub _recode_txn_Set { | ||
| 229 | my $self = shift; | ||
| 230 | my %args = validate( @_, { txn => 1, ticket => 1, changeset => 1 } ); | ||
| 231 | |||
| 232 | my $change = Prophet::Change->new( | ||
| 233 | { record_type => 'ticket', | ||
| 234 | record_uuid => $self->sync_source->uuid_for_remote_id( $args{'ticket'}->{$self->sync_source->uuid . '-id'} ), | ||
| 235 | change_type => 'update_file' | ||
| 236 | } | ||
| 237 | ); | ||
| 238 | |||
| 239 | my ($field, $old, $new) = @{ $args{txn} }{qw(Field OldValue NewValue)}; | ||
| 240 | |||
| 241 | if ( $field eq 'Queue' ) { | ||
| 242 | my $current_queue = $args{'ticket'}->{$self->sync_source->uuid .'-queue'}; | ||
| 243 | my $user = $args{txn}->{Creator}; | ||
| 244 | if ( $args{txn}->{Description} =~ /Queue changed from (.*) to $current_queue by $user/ ) { | ||
| 245 | $old = $1; | ||
| 246 | $new = $current_queue; | ||
| 247 | } | ||
| 248 | } elsif ( $field eq 'Status' ) { | ||
| 249 | $new = $self->translate_status($new); | ||
| 250 | $old = $self->translate_status($old); | ||
| 251 | |||
| 252 | } elsif ( $field eq 'Owner' ) { | ||
| 253 | $new = $self->resolve_user_id_to( email_address => $new ); | ||
| 254 | $old = $self->resolve_user_id_to( email_address => $old ); | ||
| 255 | } | ||
| 256 | |||
| 257 | $args{'changeset'}->add_change( { change => $change } ); | ||
| 258 | |||
| 259 | # XXX: This line is kind of magic | ||
| 260 | # TODO: check if it's sill needed | ||
| 261 | $args{'ticket'}->{ $field } = $old; | ||
| 262 | |||
| 263 | $change->add_prop_change( name => $field, old => $old, new => $new ); | ||
| 264 | } | ||
| 265 | |||
| 266 | *_recode_txn_Steal = \&_recode_txn_Set; | ||
| 267 | *_recode_txn_Take = \&_recode_txn_Set; | ||
| 268 | *_recode_txn_Give = \&_recode_txn_Set; | ||
| 269 | |||
| 270 | sub _recode_txn_Create { | ||
| 271 | my $self = shift; | ||
| 272 | my %args = validate( @_, { txn => 1, ticket => 1, changeset => 1 } ); | ||
| 273 | |||
| 274 | my $change = Prophet::Change->new( { | ||
| 275 | record_type => 'ticket', | ||
| 276 | record_uuid => $self->sync_source->uuid_for_remote_id( | ||
| 277 | $args{'ticket'}->{$self->sync_source->uuid . '-id'} | ||
| 278 | ), | ||
| 279 | change_type => 'add_file' | ||
| 280 | } ); | ||
| 281 | |||
| 282 | $args{'changeset'}->add_change( { change => $change } ); | ||
| 283 | for my $name ( keys %{ $args{'ticket'} } ) { | ||
| 284 | $change->add_prop_change( | ||
| 285 | name => $name, | ||
| 286 | old => undef, | ||
| 287 | new => $args{'ticket'}->{$name}, | ||
| 288 | ); | ||
| 289 | } | ||
| 290 | |||
| 291 | $self->_recode_content_update(%args); # add the create content txn as a seperate change in this changeset | ||
| 292 | } | ||
| 293 | |||
| 294 | *_recode_txn_Link = \&_recode_txn_AddLink; | ||
| 295 | sub _recode_txn_AddLink { | ||
| 296 | # XXX, TODO: syncing links doesn't work | ||
| 297 | return; | ||
| 298 | |||
| 299 | my $self = shift; | ||
| 300 | my %args = validate( @_, { txn => 1, ticket => 1, changeset => 1 } ); | ||
| 301 | my $new_state = $args{'ticket'}->{ $args{'txn'}->{'Field'} }; | ||
| 302 | $args{'ticket'}->{ $args{'txn'}->{'Field'} } = $self->warp_list_to_old_value( | ||
| 303 | $args{'ticket'}->{ $args{'txn'}->{'Field'} }, | ||
| 304 | $args{'txn'}->{'NewValue'}, | ||
| 305 | $args{'txn'}->{'OldValue'} | ||
| 306 | ); | ||
| 307 | |||
| 308 | my $change = Prophet::Change->new( { | ||
| 309 | record_type => 'ticket', | ||
| 310 | record_uuid => $self->sync_source->uuid_for_remote_id( | ||
| 311 | $args{'ticket'}->{$self->sync_source->uuid . '-id'} | ||
| 312 | ), | ||
| 313 | change_type => 'update_file', | ||
| 314 | } ); | ||
| 315 | $change->add_prop_change( | ||
| 316 | name => $args{'txn'}->{'Field'}, | ||
| 317 | old => $args{'ticket'}->{ $args{'txn'}->{'Field'} }, | ||
| 318 | new => $new_state | ||
| 319 | ); | ||
| 320 | $args{'changeset'}->add_change( { change => $change } ); | ||
| 321 | } | ||
| 322 | |||
| 323 | sub _recode_content_update { | ||
| 324 | my $self = shift; | ||
| 325 | my %args = validate( @_, { txn => 1, ticket => 1, changeset => 1 } ); | ||
| 326 | my $url = $self->sync_source->remote_url . "/transaction/" . $args{'txn'}->{'id'}; | ||
| 327 | my $change = Prophet::Change->new( { | ||
| 328 | record_type => 'comment', | ||
| 329 | record_uuid => $self->sync_source->uuid_for_url( $url ), | ||
| 330 | change_type => 'add_file', | ||
| 331 | } ); | ||
| 332 | |||
| 333 | $change->add_prop_change( name => 'created', new => $args{'txn'}->{'Created'}); | ||
| 334 | $change->add_prop_change( name => 'type', new => $args{'txn'}->{'Type'}); | ||
| 335 | $change->add_prop_change( name => 'creator', new => $self->resolve_user_id_to( | ||
| 336 | email_address => $args{'txn'}->{'Creator'} | ||
| 337 | ) ); | ||
| 338 | $change->add_prop_change( name => 'content', new => $args{'txn'}->{'Content'}); | ||
| 339 | $change->add_prop_change( name => 'ticket', new => $self->sync_source->uuid_for_remote_id( | ||
| 340 | $args{'ticket'}->{ $self->sync_source->uuid . '-id'} | ||
| 341 | ) ); | ||
| 342 | $args{'changeset'}->add_change( { change => $change } ); | ||
| 343 | } | ||
| 344 | |||
| 345 | *_recode_txn_Comment = \&_recode_content_update; | ||
| 346 | *_recode_txn_Correspond = \&_recode_content_update; | ||
| 347 | |||
| 348 | sub _recode_txn_AddWatcher { | ||
| 349 | my $self = shift; | ||
| 350 | my %args = validate( @_, { txn => 1, ticket => 1, changeset => 1 } ); | ||
| 351 | |||
| 352 | my $type = $args{'txn'}->{'Field'}; | ||
| 353 | |||
| 354 | my $new_state = $args{'ticket'}->{ $type .'s' }; | ||
| 355 | $args{'ticket'}->{ $type .'s' } = $self->warp_list_to_old_value( | ||
| 356 | $new_state, | ||
| 357 | $self->resolve_user_id_to( email_address => $args{'txn'}->{'NewValue'} ), | ||
| 358 | $self->resolve_user_id_to( email_address => $args{'txn'}->{'OldValue'} ) | ||
| 359 | ); | ||
| 360 | |||
| 361 | my $change = Prophet::Change->new({ | ||
| 362 | record_type => 'ticket', | ||
| 363 | record_uuid => $self->sync_source->uuid_for_remote_id( | ||
| 364 | $args{'ticket'}->{$self->sync_source->uuid . '-id'} | ||
| 365 | ), | ||
| 366 | change_type => 'update_file' | ||
| 367 | } ); | ||
| 368 | $change->add_prop_change( | ||
| 369 | name => $args{'txn'}->{'Field'}, | ||
| 370 | old => $args{'ticket'}->{ $args{'txn'}->{'Field'} .'s' }, | ||
| 371 | new => $new_state | ||
| 372 | ); | ||
| 373 | $args{'changeset'}->add_change( { change => $change } ); | ||
| 374 | } | ||
| 375 | |||
| 376 | *_recode_txn_DelWatcher = \&_recode_txn_AddWatcher; | ||
| 377 | |||
| 378 | sub _recode_txn_CustomField { | ||
| 379 | my $self = shift; | ||
| 380 | my %args = validate( @_, { txn => 1, ticket => 1, changeset => 1 } ); | ||
| 381 | |||
| 382 | my $new = $args{'txn'}->{'NewValue'}; | ||
| 383 | my $old = $args{'txn'}->{'OldValue'}; | ||
| 384 | my $name; | ||
| 385 | if ( $args{'txn'}->{'Description'} =~ /^(.*) $new added by/ ) { | ||
| 386 | $name = $1; | ||
| 387 | } | ||
| 388 | elsif ( $args{'txn'}->{'Description'} =~ /^(.*) changed to $new by/ ) { | ||
| 389 | $name = $1; | ||
| 390 | |||
| 391 | } elsif ( $args{'txn'}->{'Description'} =~ /^(.*) $old deleted by/ ) { | ||
| 392 | $name = $1; | ||
| 393 | } else { | ||
| 394 | die "Unknown transaction description " . $args{'txn'}->{'Description'}; | ||
| 395 | } | ||
| 396 | |||
| 397 | $args{'txn'}->{'Field'} = "CF-" . $name; | ||
| 398 | |||
| 399 | my $new_state = $args{'ticket'}->{ $args{'txn'}->{'Field'} }; | ||
| 400 | $args{'ticket'}->{ $args{'txn'}->{'Field'} } = $self->warp_list_to_old_value( | ||
| 401 | $args{'ticket'}->{ $args{'txn'}->{'Field'} }, | ||
| 402 | $args{'txn'}->{'NewValue'}, | ||
| 403 | $args{'txn'}->{'OldValue'} | ||
| 404 | ); | ||
| 405 | |||
| 406 | my $change = Prophet::Change->new( | ||
| 407 | { record_type => 'ticket', | ||
| 408 | record_uuid => $self->sync_source->uuid_for_remote_id( $args{'ticket'}->{$self->sync_source->uuid . '-id'} ), | ||
| 409 | change_type => 'update_file' | ||
| 410 | } | ||
| 411 | ); | ||
| 412 | |||
| 413 | $args{'changeset'}->add_change( { change => $change } ); | ||
| 414 | $change->add_prop_change( | ||
| 415 | name => $args{'txn'}->{'Field'}, | ||
| 416 | old => $args{'ticket'}->{ $args{'txn'}->{'Field'} }, | ||
| 417 | new => $new_state | ||
| 418 | ); | ||
| 419 | } | ||
| 420 | |||
| 421 | } | ||
| 422 | |||
| 423 | |||
| 424 | sub resolve_user_id_to { | ||
| 425 | my $self = shift; | ||
| 426 | my $attr = shift; | ||
| 427 | my $id = shift; | ||
| 428 | return undef unless $id; | ||
| 429 | |||
| 430 | local $@; | ||
| 431 | my $user = eval { | ||
| 432 | Prophet::CLI->end_pager(); | ||
| 433 | # squelch chatty RT::Client::REST "Unknown key" warnings | ||
| 434 | local $SIG{__WARN__} = sub { $self->sync_source->log_debug(@_) }; | ||
| 435 | RT::Client::REST::User->new( rt => $self->sync_source->rt, id => $id )->retrieve; | ||
| 436 | }; | ||
| 437 | if ( my $err = $@ ) { | ||
| 438 | warn $err; | ||
| 439 | return $attr eq 'name' ? 'Unknown user' : 'unknown@localhost'; | ||
| 440 | } | ||
| 441 | my $name = $user->name; | ||
| 442 | if ( lc $name eq 'nobody' ) { | ||
| 443 | return $attr eq 'name' ? 'nobody' : undef; | ||
| 444 | } | ||
| 445 | elsif ( lc $name eq 'RT_System' ) { | ||
| 446 | return $attr eq 'name' ? 'system' : undef; | ||
| 447 | } else { | ||
| 448 | return $user->$attr(); | ||
| 449 | } | ||
| 450 | } | ||
| 451 | |||
| 452 | memoize 'resolve_user_id_to'; | ||
| 453 | |||
| 454 | our %PROP_MAP = ( | ||
| 455 | subject => 'summary', | ||
| 456 | status => 'status', | ||
| 457 | owner => 'owner', | ||
| 458 | initialpriority => '_delete', | ||
| 459 | finalpriority => '_delete', | ||
| 460 | told => '_delete', | ||
| 461 | requestor => 'reporter', | ||
| 462 | requestors => 'reporter', | ||
| 463 | cc => 'cc', | ||
| 464 | ccs => 'cc', | ||
| 465 | admincc => 'admin_cc', | ||
| 466 | adminccs => 'admin_cc', | ||
| 467 | refersto => 'refers_to', | ||
| 468 | referredtoby => 'referred_to_by', | ||
| 469 | dependson => 'depends_on', | ||
| 470 | dependedonby => 'depended_on_by', | ||
| 471 | hasmember => 'members', | ||
| 472 | memberof => 'member_of', | ||
| 473 | priority => 'priority_integer', | ||
| 474 | resolved => 'completed', | ||
| 475 | due => 'due', | ||
| 476 | creator => 'creator', | ||
| 477 | timeworked => 'time_worked', | ||
| 478 | timeleft => 'time_left', | ||
| 479 | timeestimated => 'time_estimated', | ||
| 480 | lastupdated => '_delete', | ||
| 481 | created => 'created', | ||
| 482 | queue => 'queue', | ||
| 483 | starts => '_delete', | ||
| 484 | started => '_delete', | ||
| 485 | ); | ||
| 486 | |||
| 487 | sub translate_status { | ||
| 488 | my $self = shift; | ||
| 489 | my $status = shift; | ||
| 490 | |||
| 491 | $status =~ s/^resolved$/closed/; | ||
| 492 | |||
| 493 | |||
| 494 | return $status; | ||
| 495 | } | ||
| 496 | |||
| 497 | sub translate_prop_names { | ||
| 498 | my $self = shift; | ||
| 499 | my $changeset = shift; | ||
| 500 | |||
| 501 | for my $change ( $changeset->changes ) { | ||
| 502 | next unless $change->record_type eq 'ticket'; | ||
| 503 | |||
| 504 | my @new_props; | ||
| 505 | for my $prop ( $change->prop_changes ) { | ||
| 506 | next if ( ( $PROP_MAP{ lc( $prop->name ) } || '' ) eq '_delete' ); | ||
| 507 | $prop->name( $PROP_MAP{ lc( $prop->name ) } ) if $PROP_MAP{ lc( $prop->name ) }; | ||
| 508 | # Normalize away undef -> "" and vice-versa | ||
| 509 | for (qw/new_value old_value/) { | ||
| 510 | $prop->$_("") if !defined ($prop->$_()); | ||
| 511 | } | ||
| 512 | next if ( $prop->old_value eq $prop->new_value); | ||
| 513 | |||
| 514 | if ( $prop->name =~ /^cf-(.*)$/ ) { | ||
| 515 | $prop->name( 'custom-' . $1 ); | ||
| 516 | } | ||
| 517 | |||
| 518 | push @new_props, $prop; | ||
| 519 | |||
| 520 | } | ||
| 521 | $change->prop_changes( \@new_props ); | ||
| 522 | |||
| 523 | } | ||
| 524 | return $changeset; | ||
| 525 | } | ||
| 526 | |||
| 527 | __PACKAGE__->meta->make_immutable; | ||
| 528 | no Any::Moose; | ||
| 529 | 1; |