#
# COPYRIGHT:
#
-# This software is Copyright (c) 1996-2012 Best Practical Solutions, LLC
+# This software is Copyright (c) 1996-2016 Best Practical Solutions, LLC
# <sales@bestpractical.com>
#
# (Except where explicitly superseded by other copyright notices)
#
# END BPS TAGGED BLOCK }}}
use strict;
+use warnings;
use Carp;
# fix lib paths, some may be relative
-BEGIN {
+BEGIN { # BEGIN RT CMD BOILERPLATE
require File::Spec;
+ require Cwd;
my @libs = ("@RT_LIB_PATH@", "@LOCAL_LIB_PATH@");
my $bin_path;
for my $lib (@libs) {
unless ( File::Spec->file_name_is_absolute($lib) ) {
- unless ($bin_path) {
- if ( File::Spec->file_name_is_absolute(__FILE__) ) {
- $bin_path = ( File::Spec->splitpath(__FILE__) )[1];
- }
- else {
- require FindBin;
- no warnings "once";
- $bin_path = $FindBin::Bin;
- }
- }
+ $bin_path ||= ( File::Spec->splitpath(Cwd::abs_path(__FILE__)) )[1];
$lib = File::Spec->catfile( $bin_path, File::Spec->updir, $lib );
}
unshift @INC, $lib;
use Getopt::Long;
-use RT::Interface::CLI qw(CleanEnv GetCurrentUser GetMessageContent loc);
+use RT::Interface::CLI qw(GetCurrentUser loc);
-#Clean out all the nasties from the environment
-CleanEnv();
-
-my ( $search, $condition, $action, $search_arg, $condition_arg, $action_arg,
+my ( $search, $condition, $actions, $search_arg, $condition_arg, $actions_arg,
$template, $template_id, $transaction, $transaction_type, $help, $log, $verbose );
GetOptions(
"search=s" => \$search,
"search-arg=s" => \$search_arg,
"condition=s" => \$condition,
"condition-arg=s" => \$condition_arg,
- "action-arg=s" => \$action_arg,
- "action=s" => \$action,
+ "action-arg=s@" => \$actions_arg,
+ "action=s@" => \$actions,
"template=s" => \$template,
"template-id=s" => \$template_id,
"transaction=s" => \$transaction,
RT::LoadConfig();
# adjust logging to the screen according to options
-RT->Config->Set( LogToScreen => $log ) if $log;
+RT->Config->Set( LogToSTDERR => $log ) if $log;
#Connect to the database and get RT::SystemUser and RT::Nobody loaded
RT::Init();
help() if $help;
unless ( $CurrentUser->Id ) {
- print loc("No RT user found. Please consult your RT administrator.");
+ print loc("No RT user found. Please consult your RT administrator.") . "\n";
exit(1);
}
-help() unless $search && $action;
+help() unless $search && $actions;
$transaction = lc( $transaction||'' );
if ( $transaction && $transaction !~ /^(first|all|last)$/i ) {
# We _must_ have a search object
load_module($search);
-load_module($action) if ($action);
+for my $action (@$actions) {
+ load_module($action);
+}
load_module($condition) if ($condition);
my $void_scrip = RT::Scrip->new( $CurrentUser );
#find a bunch of tickets
my $tickets = RT::Tickets->new($CurrentUser);
-my $search = $search->new(
+$search = $search->new(
TicketsObj => $tickets,
Argument => $search_arg,
CurrentUser => $CurrentUser
);
-
$search->Prepare();
-# TicketsFound is an RT::Tickets object
-my $tickets = $search->TicketsObj;
-
#for each ticket we've found
while ( my $ticket = $tickets->Next() ) {
print $ticket->Id() . ":\n" if ($verbose);
print "\t".loc("Condition matches...")."\n" if $verbose;
}
- #prepare our action
- my $action_obj = $action->new(
- TicketObj => $ticket,
- TransactionObj => $transaction,
- TemplateObj => $template_obj,
- Argument => $action_arg,
- ScripObj => $void_scrip,
- ScripActionObj => $void_scrip_action,
- CurrentUser => $CurrentUser,
- );
+ my $i = 0;
+ for my $action (@$actions) {
+
+ # Given the current index of the actions array, see
+ # if there is still a corresponding element in the
+ # actions_arg array. If so, then use that argument,
+ # else use undef.
+ my $action_argument = ($i <= $#$actions_arg) ? $actions_arg->[$i] : undef;
+ $i++;
+
+ # prepare our action
+ my $action_obj = $action->new(
+ TicketObj => $ticket,
+ TransactionObj => $transaction,
+ TemplateObj => $template_obj,
+ Argument => $action_argument,
+ ScripObj => $void_scrip,
+ ScripActionObj => $void_scrip_action,
+ CurrentUser => $CurrentUser,
+ );
- #if our preparation, move onto the next ticket
- return unless $action_obj->Prepare;
- print "\t".loc("Action prepared...")."\n" if $verbose;
+ # if our preparation failed, move onto the next action
+ next unless $action_obj->Prepare;
+ print "\t".loc("Action prepared...")."\n" if $verbose;
- #commit our action.
- return unless $action_obj->Commit;
- print "\t".loc("Action committed.")."\n" if $verbose;
+ # commit our action.
+ next unless $action_obj->Commit;
+ print "\t".loc("Action committed.")."\n" if $verbose;
+ }
}
# =head2 get_transactions
#
# =cut
-{ my $cache = undef;
sub get_template {
my $ticket = shift;
return undef unless $template;
unless ( $template =~ /\D/ ) {
# by id
- return $cache if $cache;
-
- my $cache = RT::Template->new( RT->SystemUser );
- $cache->Load( $template );
+ my $template_obj = RT::Template->new( RT->SystemUser );
+ $template_obj->Load( $template );
die "Failed to load template '$template'"
- unless $cache->id;
- return $cache;
+ unless $template_obj->id;
+ return $template_obj;
}
my $queue = $ticket->Queue;
- return $cache->{ $queue } if $cache->{ $queue };
my $res = RT::Template->new( RT->SystemUser );
$res->LoadQueueTemplate( Queue => $queue, Name => $template );
die "Failed to load template '$template', either for queue #$queue or global"
unless $res->id;
}
- return $cache->{ $queue } = $res;
-} }
+ return $res;
+}
# =head2 load_module
sub load_module {
my $modname = shift;
- eval "require $modname";
- if ($@) {
- die loc( "Failed to load module [_1]. ([_2])", $modname, $@ );
+ unless ($modname->require) {
+ my $error = $@;
+ die loc( "Failed to load module [_1]. ([_2])", $modname, $error );
}
}
-
-# =head2 loc LIST
-#
-# Localize this string, with the current user's currentuser object
-#
-# =cut
-
-sub loc {
- $CurrentUser->loc(@_);
-}
-
-
sub help {
print loc( "[_1] is a tool to act on tickets from an external scheduling tool, such as cron.", $0 )
. "\n";
print loc("It takes several arguments:") . "\n\n";
- print " "
+ print " "
. loc( "[_1] - Specify the search module you want to use", "--search" )
. "\n";
- print " "
+ print " "
. loc( "[_1] - An argument to pass to [_2]", "--search-arg", "--search" )
. "\n";
- print " "
+ print " "
. loc( "[_1] - Specify the condition module you want to use", "--condition" )
. "\n";
- print " "
+ print " "
. loc( "[_1] - An argument to pass to [_2]", "--condition-arg", "--condition" )
. "\n";
- print " "
- . loc( "[_1] - Specify the action module you want to use", "--action" )
+ print " "
+ . loc( "[_1] - Specify the action module you want to use. This option may be repeated to apply multiple actions to found tickets.", "--action" )
. "\n";
- print " "
- . loc( "[_1] - An argument to pass to [_2]", "--action-arg", "--action" )
+ print " "
+ . loc( "[_1] - An argument to pass to [_2]. This option may be repeated to pass corresponding arguments to multiple calls of [_2].", "--action-arg", "--action" )
. "\n";
- print " "
+ print " "
. loc( "[_1] - Specify name or id of template(s) you want to use", "--template" )
. "\n";
- print " "
+ print " "
. loc( "[_1] - Specify if you want to use either 'first', 'last' or 'all' transactions", "--transaction" )
. "\n";
- print " "
+ print " "
. loc( "[_1] - Specify the comma separated list of transactions' types you want to use", "--transaction-type" )
. "\n";
- print " "
- . loc( "[_1] - Adjust LogToScreen config option", "--log" ) . "\n";
- print " "
+ print " "
+ . loc( "[_1] - Adjust LogToSTDERR config option", "--log" ) . "\n";
+ print " "
. loc( "[_1] - Output status updates to STDOUT", "--verbose" ) . "\n";
print "\n";
print "\n";
print " bin/rt-crontool \\\n";
print " --search RT::Search::ActiveTicketsInQueue --search-arg general \\\n";
print" --action RT::Action::EscalatePriority\n";
-
-
-
-
-
exit(0);
}
=item action
-Specify the action module you want to use
+Specify the action module you want to use. This option may be repeated to apply multiple actions to found tickets.
=item action-arg
-An argument to pass to --action
+An argument to pass to --action. This option may be repeated to pass corresponding arguments to multiple calls of --action.
=item template
=item log
-Adjust LogToScreen config option
+Adjust LogToSTDERR config option
=item verbose