ConvertLegacyToRt

From Request Tracker Wiki
Jump to: navigation, search

#!/tools/bin/perl -w #----------------------------------------------------------------------------- # convert change request data from a Flat File from a Legacy sys to RT. # Turn OFF RT and then change your RT_SiteConfig.pm to turn off logging. # Be sure to TURN OFF all scrips either before being RT down or via DBA/SQL. # scrips will try to run and logging will make the program run FOREVER!! # flat file input can be created by via Data::Dumper; # Obviously, field names will be different. OUR Data Names are all CAPS_CAPS # We built tickets, CF's into two different queues, ES-HRIS & ES-LETS # We also have an extra Ticket Status value of "QA testing". #---------------------------------------------------------------------------- use strict; use lib "/$RTHome/rt/perl/lib"; use lib "/$RTHome/rt/lib"; use RT; use RT::Ticket; use RT::Transaction; use RT::Attachment; use MIME::Entity; #----------------------------------------------------------------------------- # process all requests into tickets #----------------------------------------------------------------------------- sub Read_Temp_File { my ( $fh ) = @_; <code><pre>my ( $in, $objectId, $evt, $VAR1, #Dumper default variable name $k, $n, ); while ( $in = &lt;$fh&gt; ) { chomp $in; eval $in; #instantiates $VAR1 as hash object of change_request and associated change_events #print $VAR1-&gt;{REQUEST}, ' ', $VAR1-&gt;{DESCR_SHORT}, "\n"; foreach $k ( qw/MOVED_TO_PRODUCTION_DT QA_APPROVAL_DT/ ) { if ( $VAR1-&gt;{$k} gt ' ' ) { if ( $VAR1-&gt;{$k} &gt; '2038' || $VAR1-&gt;{$k} &lt; '2001' ) { $VAR1-&gt;{$k} = '2038-01-01'; #max RT date year } } } $objectId = New_Ticket( $VAR1 ); #ObjectId = Ticket.id foreach $evt ( @{$VAR1-&gt;{EVENTS}} ) { $evt-&gt;{ObjectId} = $objectId; $evt-&gt;{TransactionId} = New_Transaction( $evt ); #TransactionId = Transaction.id New_Attachment( $evt ); } } </pre></code> } #----------------------------------------------------------------------------- # create Ticket for one request #----------------------------------------------------------------------------- sub New_Ticket { my ( $req ) = @_; <code><pre>my ( $ticket, $mimeObj, $ticketid, $transaction_object, $err, $rc, $status, $cc, $ccid, $grp, $mem, $memberid, $priority, $rejected, %ticket_vals, %req_type_HRIS, %req_type_LETS, ); %req_type_LETS = ( B =&gt; "Batch", D =&gt; "Data Problems", E =&gt; "Enhancements", G =&gt; "Role removal", L =&gt; "LETS Forms", M =&gt; "LETSLite", O =&gt; "Other", R =&gt; "Reports", T =&gt; "Weblets", U =&gt; "Urgent Production Problem", W =&gt; "Weblets Forms", ); %req_type_HRIS = ( A =&gt; "Access", C =&gt; "Crystal", G =&gt; "Upgrade", H =&gt; "HRIS Online", I =&gt; "LANOPS", K =&gt; "Shell Script", L =&gt; "LDRS Data Problems", M =&gt; "HR Data Correction", O =&gt; "Other", P =&gt; "Perl Batch", Q =&gt; "PS Query", S =&gt; "SQR", T =&gt; "WebLETS", U =&gt; "UNIX", V =&gt; "Inquiry", W =&gt; "HR Web Apps", X =&gt; "QA Test w/o System Change", ); $ticket = RT::Ticket-&gt;new( $RT::SystemUser ); $mimeObj = MIME::Entity-&gt;build(Data =&gt; $req-&gt;{DESCR_LONG}, Type =&gt; 'text/plain'); if ( $req-&gt;{PRIORITY} le '5' ) { $priority = $req-&gt;{PRIORITY}; $rejected = 0; } else { $priority = '5'; $rejected = 1; } %ticket_vals = ( Subject =&gt; $req-&gt;{DESCR_SHORT}, MIMEObj =&gt; $mimeObj, Creator =&gt; User_Id_For_Name( $req-&gt;{INITIATED_BY} ), #API bug won't look up corresponding id for name Owner =&gt; $req-&gt;{DEVELOPER}, Requestor =&gt; $req-&gt;{INITIATED_BY}, Priority =&gt; $priority, InitialPriority =&gt; $priority, FinalPriority =&gt; $priority, Created =&gt; $req-&gt;{CREATE_DT}, Started =&gt; $req-&gt;{DEVELOPER_ASSIGNED_DT}, Resolved =&gt; $req-&gt;{MOVED_TO_PRODUCTION_DT}, LastUpdated =&gt; $req-&gt;{ACTION_DT}, Status =&gt; StatusLookup( $req, $rejected ), ); if ( $req-&gt;{TEAM} eq 'L' ) { $ticket_vals{Queue} = 'ES-LETS'; } else { $ticket_vals{Queue} = 'ES-HRIS'; } ( $ticketid, $transaction_object, $err ) = $ticket-&gt;Create( %ticket_vals ); print "New_Ticket request=", $req-&gt;{REQUEST}, " $err \n"; #debug die "New_Ticket Error: $err \n" unless $ticketid; ( $rc, $err ) = $ticket-&gt;AddCustomFieldValue( Field =&gt; 'Description', Value =&gt; $req-&gt;{DESCR_LONG} ); ( $rc, $err ) = $ticket-&gt;AddCustomFieldValue( Field =&gt; 'Reference Number', Value =&gt; $req-&gt;{REQUEST} ); ( $rc, $err ) = $ticket-&gt;AddCustomFieldValue( Field =&gt; 'Work-Completed Date', Value =&gt; $req-&gt;{DEVELOPER_APPROVAL_DT} ); print "Work-Completed Date $rc $err\n"; ( $rc, $err ) = $ticket-&gt;AddCustomFieldValue( Field =&gt; 'QA Approval Date', Value =&gt; $req-&gt;{QA_APPROVAL_DT} ); print "QA Approval Date $rc $err\n"; if ( $req-&gt;{TEAM} eq 'L' ) { ( $rc, $err ) = $ticket-&gt;AddCustomFieldValue( Field =&gt; 'LETS-QA Approvers List', Value =&gt; $req-&gt;{QA_PERSON} ); ( $rc, $err ) = $ticket-&gt;AddCustomFieldValue( Field =&gt; 'LETS-Migrators List', Value =&gt; $req-&gt;{ZZ_PROMOTER} ); ( $rc, $err ) = $ticket-&gt;AddCustomFieldValue( Field =&gt; 'LETS-Request Type', Value =&gt; $req_type_lets{$req-&gt;{REQUEST_TYPE}} ); print "LETS-Request Type $rc $err\n"; } else { ( $rc, $err ) = $ticket-&gt;AddCustomFieldValue( Field =&gt; 'HRIS-QA Approvers List', Value =&gt; $req-&gt;{QA_PERSON} ); ( $rc, $err ) = $ticket-&gt;AddCustomFieldValue( Field =&gt; 'HRIS-Migrators List', Value =&gt; $req-&gt;{ZZ_PROMOTER} ); ( $rc, $err ) = $ticket-&gt;AddCustomFieldValue( Field =&gt; 'HRIS-Request Type', Value =&gt; $req_type_hris{$req-&gt;{REQUEST_TYPE}} ); print "HRIS-Request Type $rc $err\n"; } if ( $req-&gt;{CC} ) { foreach $cc ( split( /[,\s+]/, $req-&gt;{CC} ) ) { if ( $cc &amp;&amp; $cc =~ /xxx\.xxx/i ) #xxx is your email address stuff like yahoo.com { ( $cc ) = $cc =~ /(.*?)\@/; #remove @domain.ext $ccid = User_Id_For_Name( $cc ); unless ( $ccid == 1 ) #skip Nobody { print "Adding CC $cc $ccid\n"; #debug $ticket-&gt;AddWatcher( Type =&gt; 'Cc', PrincipalId =&gt; $ccid ); } } } } return $ticketid; #Ticket.id </pre></code>

}

#----------------------------------------------------------------------------- # create email-like mime object from event as attachment. #----------------------------------------------------------------------------- sub New_Attachment { my ( $evt ) = @_; <code><pre>my ( $att, $mimeObj, $id, $transaction_object, $err, ); $mimeObj = MIME::Entity-&gt;build( Subject =&gt; substr( $evt-&gt;{DESCR_LONG}, 0, 77 ) . '...', Data =&gt; $evt-&gt;{DESCR_LONG}, Date =&gt; $evt-&gt;{CREATE_DT}, From =&gt; $evt-&gt;{INITIATED_BY}, ); $att = RT::Attachment-&gt;new( $RT::SystemUser ); ( $id, $transaction_object, $err ) = $att-&gt;Create( TransactionId =&gt; $evt-&gt;{TransactionId}, Attachment =&gt; $mimeObj, ); die "New_Attachment Error $err \n" unless $id; </pre></code> } #----------------------------------------------------------------------------- # create Transaction for change_event #----------------------------------------------------------------------------- sub New_Transaction { my ( $evt ) = @_; <code><pre>my ( $trn, $id, $transaction_object, $err, ); $trn = RT::Transaction-&gt;new( $RT::SystemUser ); ( $id, $transaction_object, $err ) = $trn-&gt;Create( ObjectId =&gt; $evt-&gt;{ObjectId}, ObjectType =&gt; 'RT::Ticket', Type =&gt; 'Comment', Created =&gt; $evt-&gt;{CREATE_DT}, ); die "New_Transaction Error $err \n" unless $id; return $id; </pre></code> } #------------------------------------------------------------------------------ # status logic based on dates #------------------------------------------------------------------------------ sub StatusLookup { my ( $req, $rejected ) = @_; <code><pre>my ( $status, ); if ( $rejected ) { $status = 'rejected'; } elsif ( $req-&gt;{MOVED_TO_PRODUCTION_DT} gt ' ' ) { $status = 'resolved'; } elsif ( $req-&gt;{QA_APPROVAL_DT} gt ' ' ) { $status = 'QA testing'; } elsif ( $req-&gt;{DEVELOPER_APPROVAL_DT} gt ' ' ) { $status = 'QA testing'; } elsif ( $req-&gt;{DEVELOPER_ASSIGNED_DT} gt ' ' ) { $status = 'open'; } else { $status = 'new'; } return $status; </pre></code> } #----------------------------------------------------------------------------- # return Id for LDAP Name - case insensitive #----------------------------------------------------------------------------- { #scope my $ids; <code><pre>sub User_Id_For_Name { my ( $name ) = @_; my ( $users, $u, ); unless ( $ids-&gt;{XXXXXXX} ) #XXXXXX is the UserID of the person running the job { $users = RT::Users-&gt;new($RT::SystemUser); $users-&gt;UnLimit(); while ( $u = $users-&gt;Next() ) { $ids-&gt;{uc $u-&gt;Name} = $u-&gt;id; } } return $ids-&gt;{uc $name} || 1; #default Nobody } </pre></code> } #scope #----------------------------------------------------------------------------- # main #----------------------------------------------------------------------------- my ( $fh, $fname, ); RT::LoadConfig(); RT::Init(); $fname = '/var/tmp/chg_req_2_rt.txt'; $fh = IO::File->new( "$fname" ) or die "cannot open $fname $!\n"; Read_Temp_File( $fh ); $fh->close();

Contributed by Kenn