[Rt-commit] r15556 - in rt/branches/3.999-DANGEROUS: bin

sunnavy at bestpractical.com sunnavy at bestpractical.com
Wed Aug 27 14:02:37 EDT 2008


Author: sunnavy
Date: Wed Aug 27 14:02:37 2008
New Revision: 15556

Modified:
   rt/branches/3.999-DANGEROUS/   (props changed)
   rt/branches/3.999-DANGEROUS/bin/rt-crontool

Log:
 r16167 at sunnavys-mb:  sunnavy | 2008-08-28 01:55:00 +0800
 merged rt-crontool from 3.7 to 3.8


Modified: rt/branches/3.999-DANGEROUS/bin/rt-crontool
==============================================================================
--- rt/branches/3.999-DANGEROUS/bin/rt-crontool	(original)
+++ rt/branches/3.999-DANGEROUS/bin/rt-crontool	Wed Aug 27 14:02:37 2008
@@ -1,4 +1,4 @@
-#!/opt/local/bin/perl
+#!/usr/bin/perl
 # BEGIN BPS TAGGED BLOCK {{{
 # 
 # COPYRIGHT:
@@ -55,18 +55,18 @@
 
 use Getopt::Long;
 
-use RT::Interface::CLI qw(CleanEnv get_current_user get_message_content loc);
+use RT::Interface::CLI qw(clean_env get_current_user get_message_content loc);
 use RT::Model::TicketCollection;
 use RT::Model::Template;
 
 #Clean out all the nasties from the environment
-CleanEnv();
+clean_env();
 
 # Load the config file
 RT::load_config();
 
 #Connect to the database and get RT->system_user and RT::nobody loaded
-RT::Init();
+RT::init();
 
 #Get the current user all loaded
 my $CurrentUser = get_current_user();
@@ -92,12 +92,11 @@
 
 help() if $help or not $search or not $action;
 
-$transaction ||= 'first';
-unless ( $transaction =~ /^(first|last)$/i ) {
-    print STDERR _("--transaction argument could be only 'first' or 'last'");
+$transaction = lc( $transaction||'' );
+if ( $transaction && $transaction !~ /^(first|all|last)$/i ) {
+    print STDERR _("--transaction argument could be only 'first', 'last' or 'all'");
     exit 1;
 }
-$transaction = lc($transaction) eq 'first'? 'ASC': 'DESC';
 
 # We _must_ have a search object
 load_module($search);
@@ -118,8 +117,8 @@
 #find a bunch of tickets
 my $tickets = RT::Model::TicketCollection->new( current_user => $CurrentUser );
 my $search  = $search->new(
-    TicketsObj  => $tickets,
-    Argument    => $search_arg,
+    tickets_obj  => $tickets,
+    argument    => $search_arg,
     current_user => $CurrentUser
 );
 
@@ -132,65 +131,91 @@
 while ( my $ticket = $tickets->next() ) {
     print $ticket->id() . ": " if ($verbose);
 
-    my $transaction = get_transaction($ticket);
-    print _("Using transaction #%1...", $transaction->id)
-        if $verbose && $transaction;
+    if ( $transaction ) {
+        my $txns = get_transactions($ticket);
+        my $found = 0;
+        while ( my $txn = $txns->Next ) {
+            print _("Using transaction #[_1]...", $txn->id)
+                if $verbose;
+            process($ticket, $txn);
+            $found = 1;
+        }
+        print _("Couldn't find suitable transaction, skipping")
+            if $verbose && !$found;
+    } else {
+        print _("Processing without transaction, some conditions and actions may fail. Consider using --transaction argument")
+            if $verbose;
+
+        process($ticket);
+    }
+}
+
+sub process {
+    my $ticket = shift;
+    my $transaction = shift;
 
     # perform some more advanced check
     if ($condition) {
-        my $scrip_condition = $condition->new(
+        my $condition_obj = $condition->new(
             transaction_obj => $transaction,
             ticket_obj      => $ticket,
             scrip_obj       => $void_scrip,
             template_obj    => $template_obj,
-            Argument       => $condition_arg,
+            argument       => $condition_arg,
             current_user    => $CurrentUser,
         );
 
         # if the condition doesn't apply, get out of here
 
-        next unless ( $scrip_condition->is_applicable );
-        print _("Condition matches...") if ($verbose);
+        return unless $condition_obj->is_applicable;
+        print _("Condition matches...") if $verbose;
     }
 
     #prepare our action
-    my $scrip_action = $action->new(
+    my $action_obj = $action->new(
         ticket_obj      => $ticket,
         transaction_obj => $transaction,
         template_obj    => $template_obj,
-        Argument       => $action_arg,
+        argument       => $action_arg,
         scrip_obj       => $void_scrip,
         scrip_action_obj => $void_scrip_action,
         current_user    => $CurrentUser,
     );
 
     #if our preparation, move onto the next ticket
-    next unless ( $scrip_action->prepare );
-    print _("Action prepared...") if ($verbose);
+    next unless $action_obj->prepare;
+    print _("Action prepared...") if $verbose;
 
     #commit our action.
-    next unless ( $scrip_action->commit );
-    print _("Action committed.\n") if ($verbose);
+    next unless $action_obj->commit;
+    print _("Action committed.\n") if $verbose;
 }
 
-=head2 get_transaction
+=head2 get_transactions
 
-Takes ticket and returns its transaction acording to command
-line arguments C<--transaction> and <--transaction-type>.
+Takes ticket and returns L<RT::Model:;TransactionCollection> object 
+with transactions of the ticket according to command line arguments
+C<--transaction> and <--transaction-type>.
 
 =cut
 
-sub get_transaction {
+sub get_transactions {
     my $ticket = shift;
     my $txns = $ticket->transactions;
+    my $order = $transaction eq 'last'? 'DESC': 'ASC';
     $txns->order_by(
-        { column => 'Created', order => $transaction },
-        { column => 'id', order => $transaction },
+        { column => 'created', order => $order },
+        { column => 'id', order => $order },
     );
-    $txns->limit( column => 'type', value => $transaction_type )
-        if $transaction_type;
-    $txns->rows_per_page(1);
-    return $txns->first;
+    if ( $transaction_type ) {
+        $transaction_type =~ s/^\s+//;
+        $transaction_type =~ s/\s+$//;
+        foreach my $type ( split /\s*,\s*/, $transaction_type ) {
+            $txns->limit( column => 'type', value => $type, entry_aggregator => 'OR' );
+        }
+    }
+    $txns->rows_per_page(1) unless $transaction eq 'all';
+    return $txns;
 }
 
 # {{{ load_module 
@@ -212,19 +237,6 @@
 
 # }}}
 
-# {{{ loc 
-
-=head2 loc LIST
-
-Localize this string, with the current user's currentuser object
-
-=cut
-
-sub loc {
-    $CurrentUser->_(@_);
-}
-
-# }}}
 
 sub help {
 
@@ -236,29 +248,29 @@
       . _( "%1 - Specify the search module you want to use", "--search" )
       . "\n";
     print "	"
-      . _( "%1 - An argument to pass to %2", "--search-argument", "--search" )
+      . _( "%1 - An argument to pass to %2", "--search-arg", "--search" )
       . "\n";
 
     print "	"
       . _( "%1 - Specify the condition module you want to use", "--condition" )
       . "\n";
     print "	"
-      . _( "%1 - An argument to pass to %2", "--condition-argument", "--condition" )
+      . _( "%1 - An argument to pass to %2", "--condition-arg", "--condition" )
       . "\n";
     print "	"
       . _( "%1 - Specify the action module you want to use", "--action" )
       . "\n";
     print "	"
-      . _( "%1 - An argument to pass to %2", "--action-argument", "--action" )
+      . _( "%1 - An argument to pass to %2", "--action-arg", "--action" )
       . "\n";
     print "	"
       . _( "%1 - Specify id of the template you want to use", "--template-id" )
       . "\n";
     print "	"
-      . _( "%1 - Specify if you want to use either 'first' or 'last' transaction", "--transaction" )
+      . _( "%1 - Specify if you want to use either 'first', 'last' or 'all' transaction", "--transaction" )
       . "\n";
     print "	"
-      . _( "%1 - Specify the type of a transaction you want to use", "--transaction-type" )
+      . _( "%1 - Specify the comma separated list of transactions' types you want to use", "--transaction-type" )
       . "\n";
     print "	"
       . _( "%1 - Output status updates to STDOUT", "--verbose" ) . "\n";


More information about the Rt-commit mailing list