| 1 | package App::SD::Replica::lighthouse::PushEncoder; | ||
| 2 | use Any::Moose; | ||
| 3 | use Params::Validate; | ||
| 4 | use Path::Class; | ||
| 5 | |||
| 6 | has sync_source => ( | ||
| 7 | isa => 'App::SD::Replica::lighthouse', | ||
| 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 | return | ||
| 21 | if $self->sync_source->app_handle->handle->last_changeset_from_source( | ||
| 22 | $changeset->original_source_uuid ) >= $changeset->original_sequence_no; | ||
| 23 | my $before_integration = time(); | ||
| 24 | |||
| 25 | eval { | ||
| 26 | if ( $change->record_type eq 'ticket' | ||
| 27 | and $change->change_type eq 'add_file' ) | ||
| 28 | { | ||
| 29 | $id = $self->integrate_ticket_create( $change, $changeset ); | ||
| 30 | $self->sync_source->record_remote_id_for_pushed_record( | ||
| 31 | uuid => $change->record_uuid, | ||
| 32 | remote_id => $id, | ||
| 33 | ); | ||
| 34 | } | ||
| 35 | elsif ( $change->record_type eq 'attachment' | ||
| 36 | and $change->change_type eq 'add_file' ) | ||
| 37 | { | ||
| 38 | $id = $self->integrate_attachment( $change, $changeset ); | ||
| 39 | } | ||
| 40 | elsif ( $change->record_type eq 'comment' | ||
| 41 | and $change->change_type eq 'add_file' ) | ||
| 42 | { | ||
| 43 | $id = $self->integrate_comment( $change, $changeset ); | ||
| 44 | } | ||
| 45 | elsif ( $change->record_type eq 'ticket' ) { | ||
| 46 | $id = $self->integrate_ticket_update( $change, $changeset ); | ||
| 47 | } | ||
| 48 | else { | ||
| 49 | $self->sync_source->log( | ||
| 50 | 'I have no idea what I am doing for ' . $change->record_uuid ); | ||
| 51 | return; | ||
| 52 | } | ||
| 53 | |||
| 54 | $self->sync_source->record_pushed_transactions( | ||
| 55 | start_time => $before_integration, | ||
| 56 | ticket => $id, | ||
| 57 | changeset => $changeset, | ||
| 58 | ); | ||
| 59 | }; | ||
| 60 | |||
| 61 | if ( my $err = $@ ) { | ||
| 62 | $self->sync_source->log( "Push error: " . $err ); | ||
| 63 | } | ||
| 64 | |||
| 65 | return $id; | ||
| 66 | } | ||
| 67 | |||
| 68 | sub integrate_ticket_update { | ||
| 69 | my $self = shift; | ||
| 70 | my ( $change, $changeset ) = validate_pos( | ||
| 71 | @_, | ||
| 72 | { isa => 'Prophet::Change' }, | ||
| 73 | { isa => 'Prophet::ChangeSet' } | ||
| 74 | ); | ||
| 75 | |||
| 76 | # Figure out the remote site's ticket ID for this change's record | ||
| 77 | my $remote_ticket_id = | ||
| 78 | $self->sync_source->remote_id_for_uuid( $change->record_uuid ); | ||
| 79 | my $ticket = $self->sync_source->lighthouse->ticket; | ||
| 80 | $ticket->load( $remote_ticket_id ); | ||
| 81 | my $attr = $self->_recode_props_for_integrate($change); | ||
| 82 | $ticket->update( | ||
| 83 | map { $_ => $attr->{$_} } | ||
| 84 | grep { exists $attr->{$_} } | ||
| 85 | qw/title body state assigned_user_id milestone_id tag/ | ||
| 86 | ); | ||
| 87 | return $remote_ticket_id; | ||
| 88 | } | ||
| 89 | |||
| 90 | sub integrate_comment { | ||
| 91 | my $self = shift; | ||
| 92 | my ( $change, $changeset ) = validate_pos( | ||
| 93 | @_, | ||
| 94 | { isa => 'Prophet::Change' }, | ||
| 95 | { isa => 'Prophet::ChangeSet' } | ||
| 96 | ); | ||
| 97 | |||
| 98 | # Figure out the remote site's ticket ID for this change's record | ||
| 99 | |||
| 100 | my %props = map { $_->name => $_->new_value } $change->prop_changes; | ||
| 101 | my $ticket_id = $self->sync_source->remote_id_for_uuid( $props{'ticket'} ); | ||
| 102 | my $ticket = $self->sync_source->lighthouse->ticket; | ||
| 103 | $ticket->load( $ticket_id ); | ||
| 104 | |||
| 105 | my %content = ( body => $props{'content'} || '' ); | ||
| 106 | |||
| 107 | $ticket->update(%content); | ||
| 108 | return $ticket_id; | ||
| 109 | } | ||
| 110 | |||
| 111 | sub integrate_ticket_attachment { | ||
| 112 | my $self = shift; | ||
| 113 | my ( $change, $changeset ) = validate_pos( | ||
| 114 | @_, | ||
| 115 | { isa => 'Prophet::Change' }, | ||
| 116 | { isa => 'Prophet::ChangeSet' } | ||
| 117 | ); | ||
| 118 | my %props = map { $_->name => $_->new_value } $change->prop_changes; | ||
| 119 | my $ticket_id = $self->sync_source->remote_id_for_uuid( $props{'ticket'} ); | ||
| 120 | |||
| 121 | $self->sync_source->log( | ||
| 122 | 'Warn: Net::Lighthouse does *not* support attachment yet'); | ||
| 123 | return $ticket_id; | ||
| 124 | } | ||
| 125 | |||
| 126 | sub integrate_ticket_create { | ||
| 127 | my $self = shift; | ||
| 128 | my ( $change, $changeset ) = validate_pos( | ||
| 129 | @_, | ||
| 130 | { isa => 'Prophet::Change' }, | ||
| 131 | { isa => 'Prophet::ChangeSet' } | ||
| 132 | ); | ||
| 133 | |||
| 134 | # Build up a ticket object out of all the record's attributes | ||
| 135 | my $ticket = $self->sync_source->lighthouse->ticket; | ||
| 136 | my $attr = $self->_recode_props_for_integrate($change); | ||
| 137 | $ticket->create( | ||
| 138 | map { $_ => $attr->{$_} } | ||
| 139 | grep { exists $attr->{$_} } | ||
| 140 | qw/title body state assigned_user_id milestone_id tag/ | ||
| 141 | ); | ||
| 142 | return $ticket->number; | ||
| 143 | } | ||
| 144 | |||
| 145 | sub _recode_props_for_integrate { | ||
| 146 | my $self = shift; | ||
| 147 | my ($change) = validate_pos( @_, { isa => 'Prophet::Change' } ); | ||
| 148 | |||
| 149 | my %props = map { $_->name => $_->new_value } $change->prop_changes; | ||
| 150 | my %attr; | ||
| 151 | |||
| 152 | for my $key ( keys %props ) { | ||
| 153 | if ( $key eq 'summary' ) { | ||
| 154 | $attr{title} = $props{$key}; | ||
| 155 | } | ||
| 156 | elsif ( $key eq 'status' ) { | ||
| 157 | $attr{state} = $props{$key}; | ||
| 158 | } | ||
| 159 | elsif ( $key eq 'tag' ) { | ||
| 160 | $attr{tag} = $props{$key}; | ||
| 161 | } | ||
| 162 | elsif ( $key eq 'content' ) { | ||
| 163 | $attr{body} = $props{$key}; | ||
| 164 | } | ||
| 165 | elsif ( $key eq 'milestone' ) { | ||
| 166 | my $milestone = $self->sync_source->lighthouse->milestone; | ||
| 167 | if ( $props{$key} ) { | ||
| 168 | eval { $milestone->load( $props{$key} ) }; | ||
| 169 | if ( $@ ) { | ||
| 170 | $self->sync_source->log( | ||
| 171 | "Warn: no milestone $props{$key} exist"); | ||
| 172 | } | ||
| 173 | else { | ||
| 174 | $attr{milestone_id} = $milestone->id; | ||
| 175 | } | ||
| 176 | } | ||
| 177 | else { | ||
| 178 | $attr{milestone_id} = undef, | ||
| 179 | } | ||
| 180 | } | ||
| 181 | elsif ( $key eq 'owner' ) { | ||
| 182 | if ( $props{key} ) { | ||
| 183 | if ( $props{$key} =~ /\((\d+)\)/ ) { | ||
| 184 | $attr{assigned_user_id} = $1; | ||
| 185 | } | ||
| 186 | } | ||
| 187 | else { | ||
| 188 | $attr{assigned_user_id} = undef; | ||
| 189 | } | ||
| 190 | } | ||
| 191 | else { | ||
| 192 | $attr{$key} = $props{$key}; | ||
| 193 | } | ||
| 194 | } | ||
| 195 | $attr{body} ||= ''; | ||
| 196 | return \%attr; | ||
| 197 | } | ||
| 198 | |||
| 199 | __PACKAGE__->meta->make_immutable; | ||
| 200 | no Any::Moose; | ||
| 201 | |||
| 202 | 1; |