| 1 | package App::SD::Replica::rt::PushEncoder; | ||
| 2 | use Any::Moose; | ||
| 3 | |||
| 4 | extends 'App::SD::ForeignReplica::PushEncoder'; | ||
| 5 | |||
| 6 | use Params::Validate; | ||
| 7 | |||
| 8 | has sync_source => | ||
| 9 | ( isa => 'App::SD::Replica::rt', | ||
| 10 | is => 'rw'); | ||
| 11 | |||
| 12 | sub integrate_ticket_update { | ||
| 13 | my $self = shift; | ||
| 14 | my ( $change, $changeset ) = validate_pos( | ||
| 15 | @_, | ||
| 16 | { isa => 'Prophet::Change' }, | ||
| 17 | { isa => 'Prophet::ChangeSet' } | ||
| 18 | ); | ||
| 19 | # Figure out the remote site's ticket ID for this change's record | ||
| 20 | my $remote_ticket_id = $self->sync_source->remote_id_for_uuid( $change->record_uuid ); | ||
| 21 | my $ticket = RT::Client::REST::Ticket->new( | ||
| 22 | rt => $self->sync_source->rt, | ||
| 23 | id => $remote_ticket_id, | ||
| 24 | %{ $self->_recode_props_for_integrate($change) } | ||
| 25 | )->store(); | ||
| 26 | |||
| 27 | return $remote_ticket_id; | ||
| 28 | } | ||
| 29 | |||
| 30 | sub integrate_ticket_create { | ||
| 31 | my $self = shift; | ||
| 32 | my ( $change, $changeset ) = validate_pos( | ||
| 33 | @_, | ||
| 34 | { isa => 'Prophet::Change' }, | ||
| 35 | { isa => 'Prophet::ChangeSet' } | ||
| 36 | ); | ||
| 37 | |||
| 38 | # Build up a ticket object out of all the record's attributes | ||
| 39 | my $ticket = RT::Client::REST::Ticket->new( | ||
| 40 | rt => $self->sync_source->rt, | ||
| 41 | queue => $self->sync_source->rt_queue(), | ||
| 42 | %{ $self->_recode_props_for_integrate($change) } | ||
| 43 | )->store( text => "Not yet pulling in ticket creation comment" ); | ||
| 44 | |||
| 45 | return $ticket->id; | ||
| 46 | } | ||
| 47 | |||
| 48 | sub integrate_comment { | ||
| 49 | my $self = shift; | ||
| 50 | my ($change, $changeset) = validate_pos( @_, { isa => 'Prophet::Change' }, {isa => 'Prophet::ChangeSet'} ); | ||
| 51 | |||
| 52 | my %props = map { $_->name => $_->new_value } $change->prop_changes; | ||
| 53 | my $ticket_id = $self->sync_source->remote_id_for_uuid( $props{'ticket'} ); | ||
| 54 | # Figure out the remote site's ticket ID for this change's record | ||
| 55 | |||
| 56 | |||
| 57 | my $ticket = RT::Client::REST::Ticket->new( rt => $self->sync_source->rt, id => $ticket_id); | ||
| 58 | |||
| 59 | my %content = ( message => $props{'content'}, | ||
| 60 | ); | ||
| 61 | |||
| 62 | if ( ($props{'type'} ||'') eq 'comment' ) { | ||
| 63 | $ticket->comment( %content); | ||
| 64 | } else { | ||
| 65 | $ticket->correspond(%content); | ||
| 66 | } | ||
| 67 | return $ticket_id; | ||
| 68 | } | ||
| 69 | |||
| 70 | sub integrate_attachment { | ||
| 71 | my ($self, $change, $changeset ) = validate_pos( @_, { isa => 'App::SD::Replica::rt::PushEncoder'}, { isa => 'Prophet::Change' }, { isa => 'Prophet::ChangeSet' }); | ||
| 72 | |||
| 73 | my %props = map { $_->name => $_->new_value } $change->prop_changes; | ||
| 74 | my $ticket_id = $self->sync_source->remote_id_for_uuid( $props{'ticket'}); | ||
| 75 | my $ticket = RT::Client::REST::Ticket->new( rt => $self->sync_source->rt, id => $ticket_id ); | ||
| 76 | |||
| 77 | my $tempdir = File::Temp::tempdir( CLEANUP => 1 ); | ||
| 78 | my $file = File::Spec->catfile( $tempdir, ( $props{'name'} || 'unnamed' ) ); | ||
| 79 | open my $fh, '>', $file or die $!; | ||
| 80 | print $fh $props{content}; | ||
| 81 | close $fh; | ||
| 82 | my %content = ( message => '(See attachments)', attachments => ["$file"]); | ||
| 83 | $ticket->correspond(%content); | ||
| 84 | return $ticket_id; | ||
| 85 | } | ||
| 86 | |||
| 87 | sub _recode_props_for_integrate { | ||
| 88 | my $self = shift; | ||
| 89 | my ($change) = validate_pos( @_, { isa => 'Prophet::Change' } ); | ||
| 90 | |||
| 91 | my %props = map { $_->name => $_->new_value } $change->prop_changes; | ||
| 92 | my %attr; | ||
| 93 | |||
| 94 | for my $key ( keys %props ) { | ||
| 95 | next unless ( $key =~ /^(summary|queue|status|owner|custom)/ ); | ||
| 96 | if ( $key =~ /^custom-(.*)/ ) { | ||
| 97 | $attr{cf}->{$1} = $props{$key}; | ||
| 98 | } elsif ( $key eq 'summary' ) { | ||
| 99 | $attr{'subject'} = $props{summary}; | ||
| 100 | } else { | ||
| 101 | $attr{$key} = $props{$key}; | ||
| 102 | } | ||
| 103 | if ( $key eq 'status' ) { | ||
| 104 | $attr{$key} =~ s/^closed$/resolved/; | ||
| 105 | } | ||
| 106 | } | ||
| 107 | return \%attr; | ||
| 108 | } | ||
| 109 | |||
| 110 | |||
| 111 | __PACKAGE__->meta->make_immutable; | ||
| 112 | no Any::Moose; | ||
| 113 | |||
| 114 | 1; |