| 1 | package App::SD::Replica::lighthouse::PullEncoder; | ||
| 2 | use Any::Moose; | ||
| 3 | extends 'App::SD::ForeignReplica::PullEncoder'; | ||
| 4 | |||
| 5 | use Params::Validate qw(:all); | ||
| 6 | use Memoize; | ||
| 7 | use DateTime; | ||
| 8 | use Net::Lighthouse::User; | ||
| 9 | |||
| 10 | has sync_source => ( | ||
| 11 | isa => 'App::SD::Replica::lighthouse', | ||
| 12 | is => 'rw', | ||
| 13 | ); | ||
| 14 | |||
| 15 | my %PROP_MAP = %App::SD::Replica::lighthouse::PROP_MAP; | ||
| 16 | |||
| 17 | sub ticket_id { | ||
| 18 | my $self = shift; | ||
| 19 | return shift->number; | ||
| 20 | } | ||
| 21 | |||
| 22 | =head2 translate_ticket_state | ||
| 23 | |||
| 24 | =cut | ||
| 25 | |||
| 26 | sub translate_ticket_state { | ||
| 27 | my $self = shift; | ||
| 28 | my $ticket = shift; | ||
| 29 | |||
| 30 | return $ticket; | ||
| 31 | } | ||
| 32 | |||
| 33 | =head2 find_matching_tickets QUERY | ||
| 34 | |||
| 35 | Returns a array of all tickets found matching your QUERY hash. | ||
| 36 | |||
| 37 | =cut | ||
| 38 | |||
| 39 | sub find_matching_tickets { | ||
| 40 | my $self = shift; | ||
| 41 | my %args = (@_); | ||
| 42 | my $last_changeset_seen_dt = $self->_only_pull_tickets_modified_after() | ||
| 43 | || DateTime->from_epoch( epoch => 0 ); | ||
| 44 | my @tickets = | ||
| 45 | $self->sync_source->lighthouse->tickets( query => $args{query} ); | ||
| 46 | my @updated = map { $_->load( $_->number ); $_ } | ||
| 47 | grep { $_->{updated_at} ge $last_changeset_seen_dt } @tickets; | ||
| 48 | return \@updated; | ||
| 49 | } | ||
| 50 | |||
| 51 | sub _only_pull_tickets_modified_after { | ||
| 52 | my $self = shift; | ||
| 53 | |||
| 54 | my $last_pull = $self->sync_source->upstream_last_modified_date(); | ||
| 55 | return unless $last_pull; | ||
| 56 | my $before = App::SD::Util::string_to_datetime($last_pull); | ||
| 57 | $self->log_debug( "Failed to parse '" . $self->sync_source->upstream_last_modified_date() . "' as a timestamp. That means we have to sync ALL history") unless ($before); | ||
| 58 | return $before; | ||
| 59 | } | ||
| 60 | |||
| 61 | =head2 find_matching_transactions { ticket => $id, starting_transaction => $num } | ||
| 62 | |||
| 63 | Returns a reference to an array of all transactions (as hashes) on ticket $id after transaction $num. | ||
| 64 | |||
| 65 | =cut | ||
| 66 | |||
| 67 | sub find_matching_transactions { | ||
| 68 | my $self = shift; | ||
| 69 | my %args = validate( @_, { ticket => 1, starting_transaction => 1 } ); | ||
| 70 | my $sequence = 0; | ||
| 71 | # hack, let's add sequence for comments | ||
| 72 | my @raw_versions = | ||
| 73 | map { $_->{sequence} = $sequence++; $_ } $args{ticket}->versions; | ||
| 74 | my @raw_attachments = $args{ticket}->attachments; | ||
| 75 | |||
| 76 | my @raw_txns = ( @raw_versions, @raw_attachments ); | ||
| 77 | my @txns; | ||
| 78 | for my $txn ( @raw_txns ) { | ||
| 79 | my $txn_date = $txn->created_at->epoch; | ||
| 80 | |||
| 81 | # Skip things we know we've already pulled | ||
| 82 | next if $txn_date < ( $args{'starting_transaction'} || 0 ); | ||
| 83 | |||
| 84 | # Skip things we've pushed | ||
| 85 | next | ||
| 86 | if ( | ||
| 87 | $self->sync_source->foreign_transaction_originated_locally( | ||
| 88 | ( defined $txn->{sequence} ? $txn->{sequence} : $txn->id ), | ||
| 89 | $args{'ticket'}->number | ||
| 90 | ) | ||
| 91 | ); | ||
| 92 | |||
| 93 | push @txns, | ||
| 94 | { | ||
| 95 | timestamp => $txn->created_at, | ||
| 96 | object => $txn, | ||
| 97 | serial => defined $txn->{sequence} ? $txn->{sequence} : $txn->id, | ||
| 98 | }; | ||
| 99 | } | ||
| 100 | |||
| 101 | $self->sync_source->log_debug('Done looking at pulled txns'); | ||
| 102 | return \@txns; | ||
| 103 | } | ||
| 104 | |||
| 105 | sub transcode_create_txn { | ||
| 106 | my $self = shift; | ||
| 107 | my $txn = shift; | ||
| 108 | my $ticket = $txn->{object}; | ||
| 109 | |||
| 110 | my $ticket_uuid = | ||
| 111 | $self->sync_source->uuid_for_remote_id($ticket->number); | ||
| 112 | my $creator = $self->resolve_user_id_to( undef, $ticket->creator_name ); | ||
| 113 | my $created = $ticket->created_at; | ||
| 114 | my $changeset = Prophet::ChangeSet->new( | ||
| 115 | { | ||
| 116 | original_source_uuid => $ticket_uuid, | ||
| 117 | original_sequence_no => $ticket->{sequence}, | ||
| 118 | creator => $creator, | ||
| 119 | created => $created->ymd . " " . $created->hms | ||
| 120 | } | ||
| 121 | ); | ||
| 122 | |||
| 123 | my $change = Prophet::Change->new( | ||
| 124 | { | ||
| 125 | record_type => 'ticket', | ||
| 126 | record_uuid => $ticket_uuid, | ||
| 127 | change_type => 'add_file', | ||
| 128 | } | ||
| 129 | ); | ||
| 130 | |||
| 131 | for my $prop (qw/title body state /) { | ||
| 132 | next unless $ticket->$prop; | ||
| 133 | $change->add_prop_change( | ||
| 134 | name => $PROP_MAP{$prop} || $prop, | ||
| 135 | new => $ticket->$prop, | ||
| 136 | ); | ||
| 137 | } | ||
| 138 | |||
| 139 | if ( $ticket->assigned_user_id ) { | ||
| 140 | $change->add_prop_change( | ||
| 141 | name => 'owner', | ||
| 142 | new => $ticket->assigned_user_name . '(' | ||
| 143 | . $ticket->assigned_user_id . ')', | ||
| 144 | ); | ||
| 145 | } | ||
| 146 | |||
| 147 | if ( $ticket->milestone_id ) { | ||
| 148 | $change->add_prop_change( | ||
| 149 | name => 'milestone', | ||
| 150 | new => $ticket->milestone_title, | ||
| 151 | ); | ||
| 152 | } | ||
| 153 | |||
| 154 | $change->add_prop_change( | ||
| 155 | name => $self->sync_source->uuid . '-id', | ||
| 156 | new => $ticket->number, | ||
| 157 | ); | ||
| 158 | |||
| 159 | $changeset->add_change( { change => $change } ); | ||
| 160 | return $changeset; | ||
| 161 | } | ||
| 162 | |||
| 163 | # we might get return: | ||
| 164 | # 0 changesets if it was a null txn | ||
| 165 | # 1 changeset if it was a normal txn | ||
| 166 | # 2 changesets if we needed to to some magic fixups. | ||
| 167 | |||
| 168 | sub transcode_one_txn { | ||
| 169 | my $self = shift; | ||
| 170 | my $txn_wrapper = shift; | ||
| 171 | my $ticket = shift; | ||
| 172 | |||
| 173 | my $txn = $txn_wrapper->{object}; | ||
| 174 | if ( defined $txn->{sequence} && $txn->{sequence} == 0 ) { | ||
| 175 | return $self->transcode_create_txn($txn_wrapper); | ||
| 176 | } | ||
| 177 | |||
| 178 | my $ticket_uuid = | ||
| 179 | $self->sync_source->uuid_for_remote_id( $ticket->number ); | ||
| 180 | |||
| 181 | my $changeset; | ||
| 182 | |||
| 183 | if ( $txn->can('filename') ) { | ||
| 184 | $changeset = Prophet::ChangeSet->new( | ||
| 185 | { | ||
| 186 | original_source_uuid => $ticket_uuid, | ||
| 187 | original_sequence_no => $txn->id, | ||
| 188 | creator => | ||
| 189 | $self->resolve_user_id_to( undef, $txn->uploader_id ), | ||
| 190 | created => $txn->created_at->ymd . " " . $txn->created_at->hms | ||
| 191 | } | ||
| 192 | ); | ||
| 193 | |||
| 194 | # it's an attachment | ||
| 195 | $self->_recode_attachment_create( | ||
| 196 | ticket_uuid => $ticket_uuid, | ||
| 197 | changeset => $changeset, | ||
| 198 | attachment => $txn, | ||
| 199 | ); | ||
| 200 | } | ||
| 201 | else { | ||
| 202 | $changeset = Prophet::ChangeSet->new( | ||
| 203 | { | ||
| 204 | original_source_uuid => $ticket_uuid, | ||
| 205 | original_sequence_no => $txn->{sequence}, | ||
| 206 | creator => | ||
| 207 | $self->resolve_user_id_to( undef, $txn->creator_name ), | ||
| 208 | created => $txn->created_at->ymd . " " . $txn->created_at->hms | ||
| 209 | } | ||
| 210 | ); | ||
| 211 | |||
| 212 | my $change = Prophet::Change->new( | ||
| 213 | { | ||
| 214 | record_type => 'ticket', | ||
| 215 | record_uuid => $ticket_uuid, | ||
| 216 | change_type => 'update_file' | ||
| 217 | } | ||
| 218 | ); | ||
| 219 | my $diffable_attrs = $txn->diffable_attributes; | ||
| 220 | if ( keys %$diffable_attrs ) { | ||
| 221 | my %hash = ( | ||
| 222 | ':tag' => 'tag', | ||
| 223 | ':milestone' => 'milestone_id', | ||
| 224 | ':assigned_user' => 'assigned_user_id', | ||
| 225 | ':state' => 'state', | ||
| 226 | ); | ||
| 227 | for my $attr ( keys %$diffable_attrs ) { | ||
| 228 | next unless $hash{$attr}; | ||
| 229 | my $method = $hash{$attr}; | ||
| 230 | |||
| 231 | if ( $attr eq ':milestone' ) { | ||
| 232 | my $old = $diffable_attrs->{$attr}; | ||
| 233 | my $old_title; | ||
| 234 | if ($old) { | ||
| 235 | |||
| 236 | # find milestone title | ||
| 237 | my $milestone = | ||
| 238 | $self->sync_source->lighthouse->milestone; | ||
| 239 | $milestone->load($old); | ||
| 240 | $old_title = $milestone->title; | ||
| 241 | } | ||
| 242 | $change->add_prop_change( | ||
| 243 | name => 'milestone', | ||
| 244 | new => $ticket->milestone_id | ||
| 245 | ? $ticket->milestone_title | ||
| 246 | : undef, | ||
| 247 | old => $old_title || $old, | ||
| 248 | ); | ||
| 249 | } | ||
| 250 | elsif ( $attr eq ':assigned_user' ) { | ||
| 251 | my $old = $diffable_attrs->{$attr}; | ||
| 252 | my $old_with_name; | ||
| 253 | if ($old) { | ||
| 254 | require Net::Lighthouse::User; | ||
| 255 | my $user = Net::Lighthouse::User->new( | ||
| 256 | map { $_ => $self->sync_source->lighthouse->$_ } | ||
| 257 | grep { $self->sync_source->lighthouse->$_ } | ||
| 258 | qw/account auth/ | ||
| 259 | ); | ||
| 260 | eval { $user->load($old) }; | ||
| 261 | if ($@) { | ||
| 262 | warn "can't load user $old on lighthouse"; | ||
| 263 | } | ||
| 264 | else { | ||
| 265 | $old_with_name = | ||
| 266 | $user->name . '(' . $user->id . ')'; | ||
| 267 | } | ||
| 268 | } | ||
| 269 | $change->add_prop_change( | ||
| 270 | name => 'owner', | ||
| 271 | new => $ticket->assigned_user_id | ||
| 272 | ? ( $ticket->assigned_user_name . '(' | ||
| 273 | . $ticket->assigned_user_id | ||
| 274 | . ')' ) | ||
| 275 | : undef, | ||
| 276 | $old_with_name ? ( old => $old_with_name ) : (), | ||
| 277 | ); | ||
| 278 | } | ||
| 279 | else { | ||
| 280 | |||
| 281 | $change->add_prop_change( | ||
| 282 | name => $PROP_MAP{ $hash{$attr} } || $hash{$attr}, | ||
| 283 | new => $txn->$method, | ||
| 284 | old => $diffable_attrs->{$attr}, | ||
| 285 | ); | ||
| 286 | } | ||
| 287 | } | ||
| 288 | } | ||
| 289 | |||
| 290 | $changeset->add_change( { change => $change } ) | ||
| 291 | if $change->has_prop_changes; | ||
| 292 | |||
| 293 | $self->_include_change_comment( $changeset, $ticket_uuid, $txn ); | ||
| 294 | } | ||
| 295 | |||
| 296 | return unless $changeset->has_changes; | ||
| 297 | return $changeset; | ||
| 298 | } | ||
| 299 | |||
| 300 | sub _include_change_comment { | ||
| 301 | my $self = shift; | ||
| 302 | my $changeset = shift; | ||
| 303 | my $ticket_uuid = shift; | ||
| 304 | my $txn = shift; | ||
| 305 | |||
| 306 | my $comment = $self->new_comment_creation_change(); | ||
| 307 | |||
| 308 | if ( my $content = $txn->body ) { | ||
| 309 | if ( $content !~ /^\s*$/s ) { | ||
| 310 | $comment->add_prop_change( | ||
| 311 | name => 'created', | ||
| 312 | new => $txn->created_at->ymd . ' ' . $txn->created_at->hms, | ||
| 313 | ); | ||
| 314 | $comment->add_prop_change( | ||
| 315 | name => 'creator', | ||
| 316 | new => $self->resolve_user_id_to( undef => $txn->creator_name ), | ||
| 317 | ); | ||
| 318 | $comment->add_prop_change( name => 'content', new => $content ); | ||
| 319 | $comment->add_prop_change( | ||
| 320 | name => 'content_type', | ||
| 321 | new => 'text/plain', | ||
| 322 | ); | ||
| 323 | $comment->add_prop_change( name => 'ticket', new => $ticket_uuid, ); | ||
| 324 | $changeset->add_change( { change => $comment } ); | ||
| 325 | } | ||
| 326 | } | ||
| 327 | } | ||
| 328 | |||
| 329 | sub _recode_attachment_create { | ||
| 330 | my $self = shift; | ||
| 331 | my %args = | ||
| 332 | validate( @_, | ||
| 333 | { ticket_uuid => 1, changeset => 1, attachment => 1 } ); | ||
| 334 | my $change = Prophet::Change->new( | ||
| 335 | { | ||
| 336 | record_type => 'attachment', | ||
| 337 | record_uuid => $self->sync_source->uuid_for_url( | ||
| 338 | $self->sync_source->remote_url | ||
| 339 | . "/attachment/" | ||
| 340 | . $args{'attachment'}->id, | ||
| 341 | ), | ||
| 342 | change_type => 'add_file', | ||
| 343 | } | ||
| 344 | ); | ||
| 345 | |||
| 346 | $change->add_prop_change( | ||
| 347 | name => 'content_type', | ||
| 348 | old => undef, | ||
| 349 | new => $args{'attachment'}->content_type, | ||
| 350 | ); | ||
| 351 | |||
| 352 | $change->add_prop_change( | ||
| 353 | name => 'created', | ||
| 354 | old => undef, | ||
| 355 | new => $args{attachment}->created_at->ymd . ' ' | ||
| 356 | . $args{attachment}->created_at->hms, | ||
| 357 | ); | ||
| 358 | $change->add_prop_change( | ||
| 359 | name => 'creator', | ||
| 360 | old => undef, | ||
| 361 | new => | ||
| 362 | $self->resolve_user_id_to( email_address => | ||
| 363 | $args{'attachment'}->uploader_id ) | ||
| 364 | ); | ||
| 365 | |||
| 366 | $change->add_prop_change( | ||
| 367 | name => 'content', | ||
| 368 | old => undef, | ||
| 369 | new => $args{'attachment'}->content, | ||
| 370 | ); | ||
| 371 | $change->add_prop_change( | ||
| 372 | name => 'name', | ||
| 373 | old => undef, | ||
| 374 | new => $args{'attachment'}->filename, | ||
| 375 | ); | ||
| 376 | $change->add_prop_change( | ||
| 377 | name => 'ticket', | ||
| 378 | old => undef, | ||
| 379 | new => $args{ticket_uuid}, | ||
| 380 | ); | ||
| 381 | $args{'changeset'}->add_change( { change => $change } ); | ||
| 382 | } | ||
| 383 | |||
| 384 | sub translate_prop_status { | ||
| 385 | my $self = shift; | ||
| 386 | my $status = shift; | ||
| 387 | return lc($status); | ||
| 388 | } | ||
| 389 | |||
| 390 | sub resolve_user_id_to { | ||
| 391 | my $self = shift; | ||
| 392 | shift; | ||
| 393 | my $id = shift; | ||
| 394 | if ( $id =~ /^\d+$/ ) { | ||
| 395 | my $user = Net::Lighthouse::User->new( | ||
| 396 | map { $_ => $self->sync_source->lighthouse->$_ } | ||
| 397 | grep { $self->sync_source->lighthouse->$_ } | ||
| 398 | qw/account auth/ | ||
| 399 | ); | ||
| 400 | $user->load( $id ); | ||
| 401 | return $user->name; | ||
| 402 | } | ||
| 403 | else { | ||
| 404 | return $id; | ||
| 405 | } | ||
| 406 | } | ||
| 407 | |||
| 408 | __PACKAGE__->meta->make_immutable; | ||
| 409 | no Any::Moose; | ||
| 410 | 1; |