| 1 | package App::SD::Replica::gcode::PushEncoder; | ||
| 2 | use Any::Moose; | ||
| 3 | use Params::Validate; | ||
| 4 | use Net::Google::Code::Issue; | ||
| 5 | use Net::Google::Code; | ||
| 6 | use Try::Tiny; | ||
| 7 | |||
| 8 | has sync_source => ( | ||
| 9 | isa => 'App::SD::Replica::gcode', | ||
| 10 | is => 'rw', | ||
| 11 | ); | ||
| 12 | |||
| 13 | sub integrate_change { | ||
| 14 | my $self = shift; | ||
| 15 | my ( $change, $changeset ) = validate_pos( | ||
| 16 | @_, | ||
| 17 | { isa => 'Prophet::Change' }, | ||
| 18 | { isa => 'Prophet::ChangeSet' } | ||
| 19 | ); | ||
| 20 | my ( $id, $record ); | ||
| 21 | |||
| 22 | # if the original_sequence_no of this changeset is <= | ||
| 23 | # the last changeset our sync source for the original_sequence_no, we can skip it. | ||
| 24 | # XXX TODO - this logic should be at the changeset level, not the cahnge level, as it applies to all | ||
| 25 | # changes in the changeset | ||
| 26 | # warn $self->sync_source->app_handle->handle->last_changeset_from_source( | ||
| 27 | # $changeset->original_source_uuid ), "\n"; | ||
| 28 | return | ||
| 29 | if $self->sync_source->app_handle->handle->last_changeset_from_source( | ||
| 30 | $changeset->original_source_uuid ) >= $changeset->original_sequence_no; | ||
| 31 | |||
| 32 | my $before_integration = time(); | ||
| 33 | |||
| 34 | # grab any email/password that may have been specified in the --to url | ||
| 35 | my ( $email, $password ) = ($self->sync_source->gcode->email, | ||
| 36 | $self->sync_source->gcode->password); | ||
| 37 | |||
| 38 | ($email, $password) = $self->sync_source->login_loop( | ||
| 39 | uri => 'gcode:' . $self->sync_source->project, | ||
| 40 | username => $email, password => $password, | ||
| 41 | # remind the user that gcode logins are email addresses | ||
| 42 | username_prompt => sub { | ||
| 43 | my $project = shift; | ||
| 44 | return "Login email for $project: "; | ||
| 45 | }, | ||
| 46 | login_callback => sub { | ||
| 47 | my ($self, $email, $password) = @_; | ||
| 48 | |||
| 49 | # gcode.pm's BUILD has already been called, so we don't need to | ||
| 50 | # create the initial object, just specify auth | ||
| 51 | $self->gcode->email($email); | ||
| 52 | $self->gcode->password($email); | ||
| 53 | $self->gcode->sign_in; | ||
| 54 | }, | ||
| 55 | catch_callback => sub { | ||
| 56 | my $error = shift; | ||
| 57 | $error =~ s/at .* line [0-9]+\.//; | ||
| 58 | |||
| 59 | warn "\n$error\n"; | ||
| 60 | }, | ||
| 61 | ); | ||
| 62 | |||
| 63 | try { | ||
| 64 | if ( $change->record_type eq 'ticket' | ||
| 65 | and $change->change_type eq 'add_file' ) | ||
| 66 | { | ||
| 67 | $id = $self->integrate_ticket_create( $change, $changeset ); | ||
| 68 | $self->sync_source->record_remote_id_for_pushed_record( | ||
| 69 | uuid => $change->record_uuid, | ||
| 70 | remote_id => $id, | ||
| 71 | ); | ||
| 72 | } | ||
| 73 | elsif ( $change->record_type eq 'attachment' | ||
| 74 | and $change->change_type eq 'add_file' ) | ||
| 75 | { | ||
| 76 | $id = $self->integrate_attachment( $change, $changeset ); | ||
| 77 | } | ||
| 78 | elsif ( $change->record_type eq 'comment' | ||
| 79 | and $change->change_type eq 'add_file' ) | ||
| 80 | { | ||
| 81 | $id = $self->integrate_comment( $change, $changeset ); | ||
| 82 | } | ||
| 83 | elsif ( $change->record_type eq 'ticket' ) { | ||
| 84 | $id = $self->integrate_ticket_update( $change, $changeset ); | ||
| 85 | } | ||
| 86 | else { | ||
| 87 | $self->sync_source->log( | ||
| 88 | 'I have no idea what I am doing for ' . $change->record_uuid ); | ||
| 89 | return; | ||
| 90 | } | ||
| 91 | |||
| 92 | $self->sync_source->record_pushed_transactions( | ||
| 93 | start_time => $before_integration, | ||
| 94 | ticket => $id, | ||
| 95 | changeset => $changeset, | ||
| 96 | ); | ||
| 97 | } catch { | ||
| 98 | $self->sync_source->log( "Push error: " . $_ ); | ||
| 99 | }; | ||
| 100 | |||
| 101 | return $id; | ||
| 102 | } | ||
| 103 | |||
| 104 | sub integrate_ticket_update { | ||
| 105 | my $self = shift; | ||
| 106 | my ( $change, $changeset ) = validate_pos( | ||
| 107 | @_, | ||
| 108 | { isa => 'Prophet::Change' }, | ||
| 109 | { isa => 'Prophet::ChangeSet' } | ||
| 110 | ); | ||
| 111 | |||
| 112 | # Figure out the remote site's ticket ID for this change's record | ||
| 113 | my $remote_ticket_id = | ||
| 114 | $self->sync_source->remote_id_for_uuid( $change->record_uuid ); | ||
| 115 | my $ticket = $self->sync_source->gcode->issue(); | ||
| 116 | $ticket->load($remote_ticket_id); | ||
| 117 | $ticket->update( %{ $self->_recode_props_for_integrate($change) }, ); | ||
| 118 | return $remote_ticket_id; | ||
| 119 | } | ||
| 120 | |||
| 121 | sub integrate_ticket_create { | ||
| 122 | my $self = shift; | ||
| 123 | my ( $change, $changeset ) = validate_pos( | ||
| 124 | @_, | ||
| 125 | { isa => 'Prophet::Change' }, | ||
| 126 | { isa => 'Prophet::ChangeSet' } | ||
| 127 | ); | ||
| 128 | |||
| 129 | # Build up a ticket object out of all the record's attributes | ||
| 130 | my $ticket = $self->sync_source->gcode->issue; | ||
| 131 | my $id = | ||
| 132 | $ticket->create( %{ $self->_recode_props_for_integrate($change) } ); | ||
| 133 | |||
| 134 | return $id; | ||
| 135 | } | ||
| 136 | |||
| 137 | sub integrate_comment { | ||
| 138 | my $self = shift; | ||
| 139 | my ( $change, $changeset ) = validate_pos( | ||
| 140 | @_, | ||
| 141 | { isa => 'Prophet::Change' }, | ||
| 142 | { isa => 'Prophet::ChangeSet' } | ||
| 143 | ); | ||
| 144 | |||
| 145 | # Figure out the remote site's ticket ID for this change's record | ||
| 146 | |||
| 147 | my %props = map { $_->name => $_->new_value } $change->prop_changes; | ||
| 148 | |||
| 149 | my $ticket_id = $self->sync_source->remote_id_for_uuid( $props{'ticket'} ); | ||
| 150 | my $ticket = $self->sync_source->gcode->issue( id => $ticket_id ); | ||
| 151 | |||
| 152 | my %content = ( comment => $props{'content'}, ); | ||
| 153 | |||
| 154 | $ticket->update(%content); | ||
| 155 | return $ticket_id; | ||
| 156 | } | ||
| 157 | |||
| 158 | sub integrate_attachment { | ||
| 159 | my ( $self, $change, $changeset ) = validate_pos( | ||
| 160 | @_, | ||
| 161 | { isa => 'App::SD::Replica::gcode::PushEncoder' }, | ||
| 162 | { isa => 'Prophet::Change' }, | ||
| 163 | { isa => 'Prophet::ChangeSet' } | ||
| 164 | ); | ||
| 165 | |||
| 166 | my %props = map { $_->name => $_->new_value } $change->prop_changes; | ||
| 167 | my $ticket_id = $self->sync_source->remote_id_for_uuid( $props{'ticket'} ); | ||
| 168 | my $ticket = $self->sync_source->gcode->issue( id => $ticket_id, ); | ||
| 169 | |||
| 170 | my $tempdir = File::Temp::tempdir( CLEANUP => 1 ); | ||
| 171 | my $file = File::Spec->catfile( $tempdir, ( $props{'name'} || 'unnamed' ) ); | ||
| 172 | open my $fh, '>', $file or die $!; | ||
| 173 | print $fh $props{content}; | ||
| 174 | close $fh; | ||
| 175 | my %content = ( comment => '(See attachments)', files => ["$file"] ); | ||
| 176 | $ticket->update(%content); | ||
| 177 | return $ticket_id; | ||
| 178 | } | ||
| 179 | |||
| 180 | sub _recode_props_for_integrate { | ||
| 181 | my $self = shift; | ||
| 182 | my ($change) = validate_pos( @_, { isa => 'Prophet::Change' } ); | ||
| 183 | |||
| 184 | my %props = map { $_->name => $_->new_value } $change->prop_changes; | ||
| 185 | my %attr; | ||
| 186 | |||
| 187 | for my $key ( keys %props ) { | ||
| 188 | if ( $key =~ /^(summary|owner|cc|blocked_on)/ ) { | ||
| 189 | $attr{$key} = $props{$key}; | ||
| 190 | } | ||
| 191 | elsif ( $key eq 'status' ) { | ||
| 192 | $attr{$key} = ucfirst $props{$key}; | ||
| 193 | } | ||
| 194 | elsif ( $key eq 'merged_into' ) { | ||
| 195 | # yeah, the comment form use 'merge_into' name | ||
| 196 | $attr{merge_into} = $props{$key}; | ||
| 197 | } | ||
| 198 | elsif ( $key eq 'tags' ) { | ||
| 199 | $attr{labels} ||= []; | ||
| 200 | push @{$attr{labels}}, split /\s*,\s*/, $props{$key}; | ||
| 201 | } | ||
| 202 | else { | ||
| 203 | $attr{labels} ||= []; | ||
| 204 | push @{ $attr{labels} }, | ||
| 205 | ( ucfirst $key ) . '-' . ucfirst $props{$key}; | ||
| 206 | } | ||
| 207 | } | ||
| 208 | return \%attr; | ||
| 209 | } | ||
| 210 | |||
| 211 | __PACKAGE__->meta->make_immutable; | ||
| 212 | no Any::Moose; | ||
| 213 | |||
| 214 | 1; |