Skip to content

Commit

Permalink
a generic _ConvertItem wrapper
Browse files Browse the repository at this point in the history
Gets things ready and then calls the proper import routine.

Also handles retrying if we had an id collision.

Plus accessor methods for each type.
  • Loading branch information
afresh1 committed Aug 11, 2012
1 parent 9438467 commit f5203a3
Showing 1 changed file with 54 additions and 37 deletions.
91 changes: 54 additions & 37 deletions rt-ticket-importer
Expand Up @@ -103,53 +103,70 @@ sub ConvertQueue {
return $QUEUES{$queue} = $queueObj->Id || ConvertQueue('General'); return $QUEUES{$queue} = $queueObj->Id || ConvertQueue('General');
} }


sub ConvertTicket { sub _ConvertItem {

my $type = shift;
my $id = shift; my $item = shift;
return $TICKETS{$id} if ( $TICKETS{$id} );
return undef unless ($id); my %T = (
Tickets => {
import => \&import_ticket,
children => 'Transactions',
},
Transactions => {
import => \&import_transaction,
children => 'Attachments',
parent_id => 'ObjectId',
},
Attachments => {
import => \&import_attachment,
parent_id => 'TransactionId',
},
Links => { import => \&import_link, }
);


my $ticket = $tickets->{$id}; die "Unknown type [$type]!" unless $T{$type};
return undef unless ( defined($ticket) );


foreach my $key ( keys %$ticket ) { my $old_id = $item->{id};
$ticket->{$key} = undef if ( ref( $ticket->{$key} ) ); my $new_id = $E{$type}{$old_id} || $T{$type}{import}( $item, @_ );
if ($new_id) {
$E{$type}{$old_id} = $new_id;
print "Created $type #$old_id\n";
} }
else {


my $ticketObj = new RT::Ticket($RT::SystemUser); # Get rid of the old id to try to create with a new id

delete $item->{id};
$ticket->{'Owner'} = ConvertUser( $ticket->{'Owner'} ); $new_id = $T{$type}{import}( $item, @_ );
$ticket->{'Creator'} = ConvertUser( $ticket->{'Creator'} ); $E{$type}{$old_id} = $new_id;
$ticket->{'LastUpdatedBy'} = ConvertUser( $ticket->{'LastUpdatedBy'} ); print "Created $type #$old_id as #$new_id\n";

if ( $ticket->{'Requestor'} ) {
my $watchers = $ticket->{'Requestor'};
my @trq = split( /, /, $watchers );
$ticket->{'Requestor'} = \@trq;
} }
if ( $ticket->{'Cc'} ) { unless ( $E{$type}{$old_id} ) {
my $watchers = $ticket->{'Cc'}; print STDERR "Failed to create $type $old_id!!!\n";
my @tcc = split( /, /, $watchers ); return;
$ticket->{'Cc'} = \@tcc;
} }
if ( $ticket->{'AdminCc'} ) {
my $watchers = $ticket->{'AdminCc'}; my $child_type = $T{$type}{children};
my @tacc = split( /, /, $watchers ); if ( $child_type and $item->{$child_type} ) {
$ticket->{'AdminCc'} = \@tacc; my $parent_id = $T{$child_type}{parent_id};
foreach my $child_id (
sort { $a <=> $b }
keys %{ $item->{$child_type} }
)
{
my $child = $item->{$child_type}->{$child_id};
$child->{$parent_id} = $new_id if $parent_id;
_ConvertItem( $child_type, $child, $new_id );
}
} }


my $eid = $ticket->{'EffectiveId'}; return $new_id;
$ticket->{'EffectiveId'} = '0'; }


my ( $newid, $msg ) = $ticketObj->Import(%$ticket); sub ConvertTicket { _ConvertItem( 'Tickets', @_ ) }
sub ConvertTransaction { _ConvertItem( 'Transactions', @_ ) }
sub ConvertAttachment { _ConvertItem( 'Attachments', @_ ) }
sub ConvertLink { _ConvertItem( 'Links', @_ ) }


unless ($newid) {
print STDERR "Failed to create Ticket $id!!!\n";
return undef;
}

print "Created Ticket # $id : as # $newid\n";
$| = 1;


$ticketObj->SetEffectiveId( ConvertTicket($eid) ) if ( $eid != $id ); $ticketObj->SetEffectiveId( ConvertTicket($eid) ) if ( $eid != $id );
$ticketObj->{'_AccessibleCache'}{Created} $ticketObj->{'_AccessibleCache'}{Created}
Expand Down

0 comments on commit f5203a3

Please sign in to comment.