[Bps-public-commit] rt-extension-automaticassignment branch, master, updated. ca41f0a8ea5e96da24838e37c937ae6ba1b79cdc

Shawn Moore shawn at bestpractical.com
Wed Sep 7 11:51:33 EDT 2016


The branch, master has been updated
       via  ca41f0a8ea5e96da24838e37c937ae6ba1b79cdc (commit)
       via  c4738f273a2afa72738b1ee58b3cb1d0f46e4e5b (commit)
      from  71fcb89e687ec3aaae2587e7a9b8d96a06584558 (commit)

Summary of changes:
 .gitignore                                      |   1 +
 META.yml                                        |   1 +
 Makefile.PL                                     |  20 ++++
 html/Admin/Queues/AutomaticAssignment.html      |  17 +--
 inc/Module/Install/Substitute.pm                | 131 ++++++++++++++++++++++++
 lib/RT/Extension/AutomaticAssignment.pm         |  23 +++++
 lib/RT/Extension/AutomaticAssignment/Test.pm.in |  29 ++++++
 t/compile.t                                     |  25 +++++
 8 files changed, 231 insertions(+), 16 deletions(-)
 create mode 100644 inc/Module/Install/Substitute.pm
 create mode 100644 lib/RT/Extension/AutomaticAssignment/Test.pm.in
 create mode 100644 t/compile.t

- Log -----------------------------------------------------------------
commit c4738f273a2afa72738b1ee58b3cb1d0f46e4e5b
Author: Shawn M Moore <shawn at bestpractical.com>
Date:   Wed Sep 7 15:44:38 2016 +0000

    Testing infrastructure

diff --git a/.gitignore b/.gitignore
index d22b294..12bcf70 100644
--- a/.gitignore
+++ b/.gitignore
@@ -7,6 +7,7 @@ pm_to_blib*
 cover_db
 pod2htm*.tmp
 /RT-Extension-AutomaticAssignment*
+/lib/RT/Extension/AutomaticAssignment/Test.pm
 *.bak
 *.swp
 /MYMETA.*
diff --git a/META.yml b/META.yml
index c3f764d..550719c 100644
--- a/META.yml
+++ b/META.yml
@@ -20,6 +20,7 @@ no_index:
     - html
     - inc
     - static
+    - t
 requires:
   Business::Hours: 0
   perl: 5.8.3
diff --git a/Makefile.PL b/Makefile.PL
index 1207c3a..6c03aa5 100644
--- a/Makefile.PL
+++ b/Makefile.PL
@@ -7,6 +7,26 @@ requires 'Business::Hours';
 requires_rt '4.2';
 rt_too_new '4.6';
 
+my ($lib_path) = $INC{'RT.pm'} =~ /^(.*)[\\\/]/;
+my $local_lib_path = "$RT::LocalPath/lib";
+
+# Straight from perldoc perlvar
+use Config;
+my $secure_perl_path = $Config{perlpath};
+if ($^O ne 'VMS') {
+    $secure_perl_path .= $Config{_exe}
+        unless $secure_perl_path =~ m/$Config{_exe}$/i;
+}
+
+substitute(
+    {
+        RT_LIB_PATH => join( ' ', $local_lib_path, $lib_path ),
+        PERL        => $ENV{PERL} || $secure_perl_path,
+    },
+    { sufix => '.in'},
+    'lib/RT/Extension/AutomaticAssignment/Test.pm',
+);
+
 WriteAll;
 sign;
 
diff --git a/inc/Module/Install/Substitute.pm b/inc/Module/Install/Substitute.pm
new file mode 100644
index 0000000..56af7fe
--- /dev/null
+++ b/inc/Module/Install/Substitute.pm
@@ -0,0 +1,131 @@
+#line 1
+package Module::Install::Substitute;
+
+use strict;
+use warnings;
+use 5.008; # I don't care much about earlier versions
+
+use Module::Install::Base;
+our @ISA = qw(Module::Install::Base);
+
+our $VERSION = '0.03';
+
+require File::Temp;
+require File::Spec;
+require Cwd;
+
+#line 89
+
+sub substitute
+{
+	my $self = shift;
+	$self->{__subst} = shift;
+	$self->{__option} = {};
+	if( UNIVERSAL::isa( $_[0], 'HASH' ) ) {
+		my $opts = shift;
+		while( my ($k,$v) = each( %$opts ) ) {
+			$self->{__option}->{ lc( $k ) } = $v || '';
+		}
+	}
+	$self->_parse_options;
+
+	my @file = @_;
+	foreach my $f (@file) {
+		$self->_rewrite_file( $f );
+	}
+
+	return;
+}
+
+sub _parse_options
+{
+	my $self = shift;
+	my $cwd = Cwd::getcwd();
+	foreach my $t ( qw(from to) ) {
+        $self->{__option}->{$t} = $cwd unless $self->{__option}->{$t};
+		my $d = $self->{__option}->{$t};
+		die "Couldn't read directory '$d'" unless -d $d && -r _;
+	}
+}
+
+sub _rewrite_file
+{
+	my ($self, $file) = @_;
+	my $source = File::Spec->catfile( $self->{__option}{from}, $file );
+	$source .= $self->{__option}{sufix} if $self->{__option}{sufix};
+	unless( -f $source && -r _ ) {
+		print STDERR "Couldn't find file '$source'\n";
+		return;
+	}
+	my $dest = File::Spec->catfile( $self->{__option}{to}, $file );
+	return $self->__rewrite_file( $source, $dest );
+}
+
+sub __rewrite_file
+{
+	my ($self, $source, $dest) = @_;
+
+	my $mode = (stat($source))[2];
+
+	open my $sfh, "<$source" or die "Couldn't open '$source' for read";
+	print "Open input '$source' file for substitution\n";
+
+	my ($tmpfh, $tmpfname) = File::Temp::tempfile('mi-subst-XXXX', UNLINK => 1);
+	$self->__process_streams( $sfh, $tmpfh, ($source eq $dest)? 1: 0 );
+	close $sfh;
+
+	seek $tmpfh, 0, 0 or die "Couldn't seek in tmp file";
+
+	open my $dfh, ">$dest" or die "Couldn't open '$dest' for write";
+	print "Open output '$dest' file for substitution\n";
+
+	while( <$tmpfh> ) {
+		print $dfh $_;
+	}
+	close $dfh;
+	chmod $mode, $dest or "Couldn't change mode on '$dest'";
+}
+
+sub __process_streams
+{
+	my ($self, $in, $out, $replace) = @_;
+	
+	my @queue = ();
+	my $subst = $self->{'__subst'};
+	my $re_subst = join('|', map {"\Q$_"} keys %{ $subst } );
+
+	while( my $str = <$in> ) {
+		if( $str =~ /^###\s*(before|replace|after)\:\s?(.*)$/s ) {
+			my ($action, $nstr) = ($1,$2);
+			$nstr =~ s/\@($re_subst)\@/$subst->{$1}/ge;
+
+			die "Replace action is bad idea for situations when dest is equal to source"
+                if $replace && $action eq 'replace';
+			if( $action eq 'before' ) {
+				die "no line before 'before' action" unless @queue;
+				# overwrite prev line;
+				pop @queue;
+				push @queue, $nstr;
+				push @queue, $str;
+			} elsif( $action eq 'replace' ) {
+				push @queue, $nstr;
+			} elsif( $action eq 'after' ) {
+				push @queue, $str;
+				push @queue, $nstr;
+				# skip one line;
+				<$in>;
+			}
+		} else {
+			push @queue, $str;
+		}
+		while( @queue > 3 ) {
+			print $out shift(@queue);
+		}
+	}
+	while( scalar @queue ) {
+		print $out shift(@queue);
+	}
+}
+
+1;
+
diff --git a/lib/RT/Extension/AutomaticAssignment/Test.pm.in b/lib/RT/Extension/AutomaticAssignment/Test.pm.in
new file mode 100644
index 0000000..c6cab16
--- /dev/null
+++ b/lib/RT/Extension/AutomaticAssignment/Test.pm.in
@@ -0,0 +1,29 @@
+use strict;
+use warnings;
+
+### after: use lib qw(@RT_LIB_PATH@);
+use lib qw(/opt/rt4/local/lib /opt/rt4/lib);
+
+package RT::Extension::AutomaticAssignment::Test;
+
+use base qw(RT::Test);
+
+sub import {
+    my $class = shift;
+    my %args  = @_;
+
+    $args{'requires'} ||= [];
+    if ( $args{'testing'} ) {
+        unshift @{ $args{'requires'} }, 'RT::Extension::AutomaticAssignment';
+    } else {
+        $args{'testing'} = 'RT::Extension::AutomaticAssignment';
+    }
+
+    $class->SUPER::import( %args );
+    $class->export_to_level(1);
+
+    require RT::Extension::AutomaticAssignment;
+}
+
+1;
+
diff --git a/t/compile.t b/t/compile.t
new file mode 100644
index 0000000..9a18c77
--- /dev/null
+++ b/t/compile.t
@@ -0,0 +1,25 @@
+use strict;
+use warnings;
+
+use RT::Extension::AutomaticAssignment::Test tests => undef;
+
+use_ok('RT::Extension::AutomaticAssignment');
+
+use_ok('RT::Action::AutomaticAssignment');
+use_ok('RT::Action::AutomaticReassignment');
+
+use_ok('RT::Extension::AutomaticAssignment::Chooser');
+use_ok('RT::Extension::AutomaticAssignment::Chooser::ActiveTickets');
+use_ok('RT::Extension::AutomaticAssignment::Chooser::Random');
+use_ok('RT::Extension::AutomaticAssignment::Chooser::RoundRobin');
+use_ok('RT::Extension::AutomaticAssignment::Chooser::TimeLeft');
+
+use_ok('RT::Extension::AutomaticAssignment::Filter');
+use_ok('RT::Extension::AutomaticAssignment::Filter::ExcludedDates');
+use_ok('RT::Extension::AutomaticAssignment::Filter::MemberOfGroup');
+use_ok('RT::Extension::AutomaticAssignment::Filter::MemberOfRole');
+use_ok('RT::Extension::AutomaticAssignment::Filter::WorkSchedule');
+
+use_ok('RT::CustomFieldValues::ServiceBusinessHours');
+
+done_testing;

commit ca41f0a8ea5e96da24838e37c937ae6ba1b79cdc
Author: Shawn M Moore <shawn at bestpractical.com>
Date:   Wed Sep 7 15:51:26 2016 +0000

    Factor out a _ScripsForQueue function

diff --git a/html/Admin/Queues/AutomaticAssignment.html b/html/Admin/Queues/AutomaticAssignment.html
index c03c5e2..8dc6893 100644
--- a/html/Admin/Queues/AutomaticAssignment.html
+++ b/html/Admin/Queues/AutomaticAssignment.html
@@ -158,22 +158,7 @@ my $config = $attr ? $attr->Content : {
     chooser => { _name => $choosers[0] },
 };
 
-my $scrips = RT::Scrips->new($session{'CurrentUser'});
-$scrips->LimitToQueue($id);
-$scrips->LimitToGlobal;
-my $scripactions = $scrips->Join(
-    ALIAS1 => 'main',
-    FIELD1 => 'ScripAction',
-    TABLE2 => 'ScripActions',
-    FIELD2 => 'id',
-);
-$scrips->Limit(
-    ALIAS    => $scripactions,
-    FIELD    => 'ExecModule',
-    OPERATOR => 'IN',
-    VALUE    => ['AutomaticAssignment', 'AutomaticReassignment'],,
-);
-
+my $scrips = RT::Extension::AutomaticAssignment->_ScripsForQueue($QueueObj);
 if ($scrips->Count == 0) {
     push @results, "No scrips for automatic assignment are applied to this queue. Please see the AutomaticAssignment documentation for setup instructions.";
 }
diff --git a/lib/RT/Extension/AutomaticAssignment.pm b/lib/RT/Extension/AutomaticAssignment.pm
index 539cbcc..cdfd7c3 100644
--- a/lib/RT/Extension/AutomaticAssignment.pm
+++ b/lib/RT/Extension/AutomaticAssignment.pm
@@ -121,6 +121,29 @@ sub _ConfigForTicket {
     return $config;
 }
 
+sub _ScripsForQueue {
+    my $self = shift;
+    my $queue = shift;
+
+    my $scrips = RT::Scrips->new($queue->CurrentUser);
+    $scrips->LimitToQueue($queue->Id);
+    $scrips->LimitToGlobal;
+    my $scripactions = $scrips->Join(
+        ALIAS1 => 'main',
+        FIELD1 => 'ScripAction',
+        TABLE2 => 'ScripActions',
+        FIELD2 => 'id',
+    );
+    $scrips->Limit(
+        ALIAS    => $scripactions,
+        FIELD    => 'ExecModule',
+        OPERATOR => 'IN',
+        VALUE    => ['AutomaticAssignment', 'AutomaticReassignment'],
+    );
+
+    return $scrips;
+}
+
 sub OwnerForTicket {
     my $self   = shift;
     my $ticket = shift;

-----------------------------------------------------------------------


More information about the Bps-public-commit mailing list