[Rt-commit] r9537 - rtir/branches/2.3-EXPERIMENTAL/etc
ruz at bestpractical.com
ruz at bestpractical.com
Fri Nov 2 12:59:35 EDT 2007
Author: ruz
Date: Fri Nov 2 12:59:34 2007
New Revision: 9537
Modified:
rtir/branches/2.3-EXPERIMENTAL/etc/add_constituency
Log:
* add --rename option (sub-command) to add_constituency
Modified: rtir/branches/2.3-EXPERIMENTAL/etc/add_constituency
==============================================================================
--- rtir/branches/2.3-EXPERIMENTAL/etc/add_constituency (original)
+++ rtir/branches/2.3-EXPERIMENTAL/etc/add_constituency Fri Nov 2 12:59:34 2007
@@ -11,6 +11,8 @@
add_constituency --name EDUNET
add_constituency --name EDUNET --correspond 'edunet at example.com' --quiet
+ add_constituency --name <constituency> --rename <new value>
+
=head1 OPTIONS
=over 4
@@ -27,6 +29,10 @@
Set/update comment address.
+=item --rename
+
+Rename constituency, use this option to set new value when --name defines old.
+
=item --quiet
Disable output.
@@ -61,7 +67,7 @@
};
use Getopt::Long;
-GetOptions( \%opt, "name=s", "correspond=s", "comment=s", "help", "quiet" );
+GetOptions( \%opt, "name=s", "rename=s", "correspond=s", "comment=s", "help", "quiet" );
if ( $opt{'help'} ) {
require Pod::Usage;
@@ -73,12 +79,7 @@
);
}
-my $constituency = $opt{'name'};
-# cleanup value
-$constituency = '' unless defined $constituency;
-$constituency =~ s/^\s+//;
-$constituency =~ s/\s+$//;
-$constituency =~ s/\s+/ /gs;
+my $constituency = sanitize_value( $opt{'name'} );
unless ( defined $constituency && length $constituency ) {
require Pod::Usage;
import Pod::Usage;
@@ -88,10 +89,16 @@
);
}
+my @queue_prefixes = ('Incidents', 'Incident Reports', 'Investigations', 'Blocks');
-$RT::Handle->BeginTransaction;
+if ( exists $opt{'rename'} ) {
+ exit rename_value(
+ $constituency => sanitize_value( $opt{'rename'} )
+ );
+}
-my @queue_prefixes = ('Incidents', 'Incident Reports', 'Investigations', 'Blocks');
+
+$RT::Handle->BeginTransaction;
my $exist = cf_value_exists( $constituency );
@@ -210,7 +217,7 @@
my $values = $cf->Values;
$values->Limit( FIELD => 'Name', VALUE => $value );
my $value_obj = $values->First;
- return $value_obj && $value_obj->id? 1 : 0;
+ return $value_obj && $value_obj->id? $value_obj : undef;
}
sub add_cf_value {
@@ -276,6 +283,13 @@
}
}
+sub group_exists {
+ my $name = shift;
+ my $group = RT::Group->new( $RT::SystemUser );
+ $group->LoadByCols( Name => $name );
+ return $group && $group->id ? $group : undef;
+}
+
sub create_or_load_group {
my $name = shift;
my $group = RT::Group->new($RT::SystemUser);
@@ -292,12 +306,17 @@
return $group;
}
-sub load_queue {
+sub queue_exists {
my $name = shift;
-
my $queue = RT::Queue->new( $RT::SystemUser );
$queue->LoadByCols( Name => $name );
- die "Couldn't load queue '$name'" unless $queue->id;
+ return $queue && $queue->id ? $queue : undef;
+}
+
+sub load_queue {
+ my $name = shift;
+ my $queue = queue_exists( $name );
+ die "Couldn't load queue '$name'" unless $queue;
return $queue;
}
@@ -337,3 +356,82 @@
return $queue;
}
+sub rename_value {
+ my $old = shift;
+ my $new = shift;
+
+ my $value_obj;
+ unless ( $value_obj = cf_value_exists( $old ) ) {
+ die "Couldn't rename. Constituency '$old' doesn't exist.";
+ }
+ if ( cf_value_exists( $new ) ) {
+ die "Couldn't rename. Constituency '$new' already exists.";
+ }
+
+ $RT::Handle->BeginTransaction;
+
+ print "\nIf you continue then we'll...\n";
+ print "* rename constituency value '$old' into '$new'.\n";
+ foreach (@queue_prefixes) {
+ print "* rename queue '$_ - $old' if it exists into '$_ - $new'.\n";
+ }
+ print <<END;
+* rename group 'DutyTeam $old' if it exists into 'DutyTeam $new'
+* rename group 'ReadOnly $old' if it exists into 'ReadOnly $new'
+
+END
+ return 0 unless prompt_yN( "Do you want to proceed?" );
+
+ {
+ my ($status, $msg) = $value_obj->SetName( $new );
+ die $msg unless $status;
+ debug "Renamed constituency value '$old' -> '$new'.";
+ }
+
+ foreach (@queue_prefixes) {
+ my $queue = queue_exists( "$_ - $old" );
+ unless ( $queue ) {
+ debug "Queue '$_ - $old' doesn't exist. Skiping...";
+ next;
+ }
+ if ( queue_exists( "$_ - $new" ) ) {
+ die "Couldn't rename queue, target '$_ - $new' already exist.";
+ }
+
+ my ($status, $msg) = $queue->SetName( "$_ - $new" );
+ die $msg unless $status;
+
+ debug "Renamed queue '$_ - $old' -> '$_ - $new'";
+ }
+
+ foreach (qw(DutyTeam Readonly)) {
+ my $group = group_exists( "$_ $old" );
+ unless ( $group ) {
+ debug "Group '$_ $old' doesn't exist. Skiping...";
+ next;
+ }
+ if ( group_exists( "$_ $new" ) ) {
+ die "Couldn't rename group, target '$_ $new' already exist.";
+ }
+
+ my ($status, $msg) = $group->SetName( "$_ $new" );
+ die $msg unless $status;
+
+ debug "Renamed group '$_ $old' -> '$_ $new'";
+ }
+
+ $RT::Handle->Commit;
+
+ return 0;
+}
+
+sub sanitize_value {
+ my $value = shift;
+ # cleanup value
+ $value = '' unless defined $value;
+ $value =~ s/^\s+//;
+ $value =~ s/\s+$//;
+ $value =~ s/\s+/ /gs;
+ return $value;
+}
+
More information about the Rt-commit
mailing list