Rt-clonequeue: Difference between revisions

From Request Tracker Wiki
Jump to navigation Jump to search
(Small update to the script)
No edit summary
Line 1: Line 1:
This scipt is a simple adaptation of the [[CloningQueues ]]customization.
This scipt is a simple adaptation of the [[CloningQueues ]]customization.


If the script complains that it can't find RT.pm, don't forget to set the "use lib" line at the beginning of the script to point to your RT library directory.
If the script complains that it can't find RT.pm, don't forget to set the "use lib" line at the beginning of the script to point to your RT library directory.
 


Here is the code:
Here is the code:
Line 26: Line 25:
  #  - added subjectTag to queue creation
  #  - added subjectTag to queue creation
  #  - added "use lib" to set default RT lib path for RT4 in /opt/rt4/lib
  #  - added "use lib" to set default RT lib path for RT4 in /opt/rt4/lib
# v1.2 (2012/05/01) Haïm Dimermanas
#  - Bug Fixes when copying scrips
  #
  #
  #No warranty
  #No warranty
Line 39: Line 40:
  #
  #
  #
  #
  use strict;
use strict;
  use lib '/opt/rt4/lib';
use lib '/usr/local/rt4/lib';
  use warnings;
use warnings;
  use lib qw(../lib);
use lib '/usr/local/rt4/lib';
  use RT;
use warnings;
  use RT::Queues;
use lib qw(../lib);
  use RT::Tickets;
use RT;
  use Data::Dumper;
use RT::Queues;
use RT::Tickets;
   
   
  my $newqueue;
my $newqueue;
  my $clonequeue;
my $clonequeue;
   
   
  print "\nRT Queue and setup cloning/merging\n";
print "\nRT Queue and setup cloning/merging\n";
  print "-----------------------------------\n";
print "-----------------------------------\n";
  print "-This script clones or merges    -\n";
print "-This script clones or merges    -\n";
  print "-a queue's parameters (rights,    -\n";
print "-a queue's parameters (rights,    -\n";
  print "-templates,scrips, custom fields) -\n";
print "-templates,scrips, custom fields) -\n";
  print "-to an existing queue (merge mode)-\n";
print "-to an existing queue (merge mode)-\n";
  print "-or to a new queue (clone mode)  -\n";
print "-or to a new queue (clone mode)  -\n";
  print "-----------------------------------\n";
print "-----------------------------------\n";
  print "Name of cloned Queue (source queue):";
print "Name of cloned Queue (source queue):";
  $clonequeue = <>;
$clonequeue = <>;
  chomp($clonequeue);
chomp($clonequeue);
  print "Name of new Queue (destination queue):";
print "Name of new Queue (destination queue):";
  $newqueue = <>;
$newqueue = <>;
  chomp($newqueue);
chomp($newqueue);
   
   
  RT::LoadConfig();
RT::LoadConfig();
  RT::Init();
RT::Init();
   
   
  my $CurrentUser = RT::SystemUser;
my $CurrentUser = RT::SystemUser;
   
   
  unless ( $CurrentUser->Id ) {
unless ( $CurrentUser->Id ) {
     print loc("No RT user found. Please consult your RT administrator.\n");
     print loc("No RT user found. Please consult your RT administrator.\n");
     exit(1);
     exit(1);
  }
}
   
   
   
   
  my $CloneQueueObj = RT::Queue->new($CurrentUser);
my $CloneQueueObj = RT::Queue->new($CurrentUser);
  my $res = $CloneQueueObj->Load($clonequeue);
my $res = $CloneQueueObj->Load($clonequeue);
  my $CloneQueueId = $CloneQueueObj->id;
my $CloneQueueId = $CloneQueueObj->id;
  if ( ! defined($res) || $res eq "" ) {
   
   
my $res = $CloneQueueObj->Load($clonequeue);
my $CloneQueueId = $CloneQueueObj->id;
if ( ! defined($res) || $res eq "" ) {
     print "ERROR: Cloned queue '$clonequeue' doesn't exist... EXITING\n";
     print "ERROR: Cloned queue '$clonequeue' doesn't exist... EXITING\n";
     exit 1;
     exit 1;
  }
} else {
  else {
     print "  * Loading existing Cloned queue '$clonequeue' (".$CloneQueueObj->id.")\n";
     print "  * Loading existing Cloned queue '$clonequeue' (".$CloneQueueObj->id.")\n";
  }
}
  my $NewQueueObj = RT::Queue->new($CurrentUser);
  $res = $NewQueueObj->Load($newqueue);
  my $NewQueueId;
  if ( ! defined($res) || $res eq "" ) {
   
   
    print "  * Cloning mode: Creating new queue '$newqueue'\n";
my $NewQueueObj = RT::Queue->new($CurrentUser);
    print "  ** Please enter the correspond email address for queue '$newqueue':";
$res = $NewQueueObj->Load($newqueue);
    my $emailcorrespond = <>;
my $NewQueueId;
    chomp($emailcorrespond);
if ( ! defined($res) || $res eq "" ) {
    print "  ** Please enter the comment email address for queue '$newqueue':";
    print "  * Cloning mode: Creating new queue '$newqueue'\n";
    my $emailcomment = <>;
    print "  ** Please enter the correspond email address for queue '$newqueue':";
    chomp($emailcomment);
    my $emailcorrespond = <>;
    print "  ** Please enter the description for queue '$newqueue':";
    chomp($emailcorrespond);
    my $qdescr = <>;
    print "  ** Please enter the comment email address for queue '$newqueue':";
    chomp($qdescr);
    my $emailcomment = <>;
    print "  ** Please enter the subjectTag for queue '$newqueue':";
    chomp($emailcomment);
    my $subjecttag = <>;
    print "  ** Please enter the description for queue '$newqueue':";
    chomp($subjecttag);
    my $qdescr = <>;
    chomp($qdescr);
    print "  ** Please enter the subjectTag for queue '$newqueue':";
    my $subjecttag = <>;
    chomp($subjecttag);
   
   
    my ($queueid) = $NewQueueObj->Create(
    my ($queueid) = $NewQueueObj->Create(
        Name              => $newqueue,
        Name              => $newqueue,
        Description      => $qdescr,
        Description      => $qdescr,
        CorrespondAddress => $emailcorrespond,
        CorrespondAddress => $emailcorrespond,
        CommentAddress    => $emailcomment,
        CommentAddress    => $emailcomment,
        SubjectTag => $subjecttag
        SubjectTag => $subjecttag
    );
    );
    $NewQueueId = $queueid
    $NewQueueId = $queueid
  }
} else {
  else {
    $NewQueueId  = $NewQueueObj->id;
    $NewQueueId  = $NewQueueObj->id;
    print "  * Merging mode: merging to existing queue '$newqueue' (".$NewQueueObj->id.")\n";
    print "  * Merging mode: merging to existing queue '$newqueue' (".$NewQueueObj->id.")\n";
  }
   
   
  #### assign group and user privileges from clone queue to the new queue
#### assign group and user privileges from clone queue to the new queue
   
   
  my $objACL = RT::ACL->new($CurrentUser);
my $objACL = RT::ACL->new($CurrentUser);
  $objACL->LimitToObject($CloneQueueObj);
$objACL->LimitToObject($CloneQueueObj);
   
   
  my $objACE = RT::ACE->new($CurrentUser);
my $objACE = RT::ACE->new($CurrentUser);
  my $objGroup = RT::Group->new($CurrentUser);
my $objGroup = RT::Group->new($CurrentUser);
   
   
  while (my $acl = $objACL->Next) {
while (my $acl = $objACL->Next) {
      $objGroup->LoadQueueRoleGroup(Queue=>$NewQueueId, Type=>$acl->PrincipalType);
    $objGroup->LoadQueueRoleGroup(Queue=>$NewQueueId, Type=>$acl->PrincipalType);
      $objACE->Create(Object=>$NewQueueObj,
    $objACE->Create(Object=>$NewQueueObj,
                      PrincipalId=>$objGroup->id || $acl->PrincipalId,
                    PrincipalId=>$objGroup->id || $acl->PrincipalId,
                      PrincipalType=>$acl->PrincipalType,
                    PrincipalType=>$acl->PrincipalType,
                      RightName=>$acl->RightName);
                    RightName=>$acl->RightName);
      print "  ** Creating ACL '".$acl->RightName."'\n";
    print "  ** Creating ACL '".$acl->RightName."'\n";
  }
}
   
   
   
   
   
   
  ####  Copy CustomeFields
####  Copy CustomeFields
  my $objCFs = RT::ObjectCustomFields->new($CurrentUser);
my $objCFs = RT::ObjectCustomFields->new($CurrentUser);
  $objCFs->LimitToObjectId($CloneQueueId);
$objCFs->LimitToObjectId($CloneQueueId);
  my $objNewCF = RT::ObjectCustomField->new($CurrentUser);
my $objNewCF = RT::ObjectCustomField->new($CurrentUser);
  while (my $cf = $objCFs->Next) {
while (my $cf = $objCFs->Next) {
    $objNewCF->Create(ObjectId=>$newqueue,SortOrder=>$cf->SortOrder,CustomField=>$cf->CustomField);
  $objNewCF->Create(ObjectId=>$newqueue,SortOrder=>$cf->SortOrder,CustomField=>$cf->CustomField);
    print '   ** Creating CustomField \''.$cf->id."'\n";
  print "   ** Creating CustomField '".$cf->id."'\n";
  }
}
   
   
   
   
  #### create templates for the new queue
#### create templates for the new queue
  my $objTemplates = RT::Templates->new($CurrentUser);
my $objTemplates = RT::Templates->new($CurrentUser);
  $objTemplates->LimitToQueue($CloneQueueId);
$objTemplates->LimitToQueue($CloneQueueId);
   
   
  my $objTemplate = RT::Template->new($CurrentUser);
my $objTemplate = RT::Template->new($CurrentUser);
   
   
  while (my $t = $objTemplates->Next) {
while (my $t = $objTemplates->Next) {
   
   
    my $objLocalTemplate = RT::Template->new($CurrentUser);
  my $objLocalTemplate = RT::Template->new($CurrentUser);
    $objLocalTemplate->LoadQueueTemplate(Queue=>$NewQueueId,Name=>$t->Name);
  $objLocalTemplate->LoadQueueTemplate(Queue=>$NewQueueId,Name=>$t->Name);
    if ($objLocalTemplate->id) {
  if ($objLocalTemplate->id) {
      print '   ** Skipping existing template \''.$t->Name."' (".$objLocalTemplate->id.")\n";
      print "   ** Skipping existing template '".$t->Name."' (".$objLocalTemplate->id.")\n";
    }
  }
    else {
  else {
      $objTemplate->Create(Content=>$t->Content,Queue=>$newqueue,Description=>$t->Description,Type=>$t->Type,Name=>$t->Name);
      $objTemplate->Create(Content=>$t->Content,Queue=>$newqueue,Description=>$t->Description,Type=>$t->Type,Name=>$t->Name);
      print '   ** Creating template \''.$t->Name."'\n";
      print "   ** Creating template '".$t->Name."'\n";
    }
  }
  }
}
   
   
  #### create scrips for the new queue
#### create scrips for the new queue
   
   
  my $objScrips = RT::Scrips->new($CurrentUser);
my $objScrips = RT::Scrips->new($CurrentUser);
  $objScrips->LimitToQueue($CloneQueueId);
$objScrips->LimitToQueue($CloneQueueId);
   
   
  my $objScrip = RT::Scrip->new($CurrentUser);
my $objScrip = RT::Scrip->new($CurrentUser);
   
   
  while (my $s = $objScrips->Next) {
while (my $s = $objScrips->Next) {
      my $template;
    my $template;
      $objTemplate->Load($s->Template);
    $objTemplate->Load($s->Template);
      if ($objTemplate->Queue == 0) { ### global template
    if ($objTemplate->Queue == 0) { ### global template
              $template = $s->Template;
            $template = $s->Template;
      }
    }
      else { ### local template, go find out which one
    else { ### local template, go find out which one
              my $objLocalTemplate = RT::Template->new($CurrentUser);
            my $objLocalTemplate = RT::Template->new($CurrentUser);
              $objLocalTemplate->LoadQueueTemplate(Queue=>$NewQueueId,Name=>$objTemplate->Name);
            $objLocalTemplate->LoadQueueTemplate(Queue=>$NewQueueId,Name=>$objTemplate->Name);
              $template = $objLocalTemplate->id;
            $template = $objLocalTemplate->id;
       }
    }
                                                                                                                                                                                              181,0-1       85%
   
   
      my $testScrips = RT::Scrips->new($CurrentUser);
    my $testScrips = RT::Scrips->new($CurrentUser);
      $testScrips->LimitToQueue($NewQueueId);
    $testScrips->LimitToQueue($NewQueueId);
      $testScrips->Limit(FIELD => "Description", VALUE => $s->Description);
    $testScrips->Limit(FIELD => "Description", VALUE => $s->Description);
      if ($testScrips->count > 0) {
    if ($testScrips->Next) {
        print '   ** Skipping existing scrip \''.$s->Description."' (".$testScrips->Next->id.") action=".$s->ScripAction." cond=".$s->ScripCondition." tmpl=$template\n";
      print "   ** Skipping existing scrip ".$s->Description."' action=".$s->ScripAction." cond=".$s->ScripCondition." tmpl=$template\n";
      }
    }
      else {
    else {
      $objScrip->Create(
      $objScrip->Create(
              Queue=>$newqueue,
            Queue=>$newqueue,
              Description=>$s->Description,
            Description=>$s->Description,
              ScripAction=>$s->ScripAction,
            ScripAction=>$s->ScripAction,
              ScripCondition=>$s->ScripCondition,
            ScripCondition=>$s->ScripCondition,
              CustomPrepareCode=>$s->CustomPrepareCode,
            CustomPrepareCode=>$s->CustomPrepareCode,
              CustomCommitCode=>$s->CustomCommitCode,
            CustomCommitCode=>$s->CustomCommitCode,
              CustomIsApplicableCode=>$s->CustomIsApplicableCode,
            CustomIsApplicableCode=>$s->CustomIsApplicableCode,
              Stage=>$s->Stage,
            Stage=>$s->Stage,
              Template=>$template);
            Template=>$template);
      print '   ** Creating scrip \''.$s->Description."' action=".$s->ScripAction." cond=".$s->ScripCondition." tmpl=$template\n";
    print "   ** Creating scrip '".$s->Description."' action=".$s->ScripAction." cond=".$s->ScripCondition." tmpl=$template\n";
      }
    }
  }
}

Revision as of 07:29, 1 May 2012

This scipt is a simple adaptation of the CloningQueues customization.

If the script complains that it can't find RT.pm, don't forget to set the "use lib" line at the beginning of the script to point to your RT library directory.

Here is the code:

#!/usr/bin/perl
#
# RT CloneQueue script
#
# This script will create a new queue called destination
# and will copy several parameters from the source queue:
# custom fields, permissions, templates and scrips
#
# It is a simple adapation of the CloningQueues hack
# see http://requesttracker.wikia.com/wiki/CloningQueues
#
# As of version 1.0 it only works in interactive mode
# (ie arguments are asked interactively and can't be
#  given as command-line arguments)
#
# History
# v1.0 (2011/09/26) Thibault Le Meur (thibault.lemeur-AT-supelec.fr)
#  - initial version: tested on RT 4.0.2
# v1.1 (2011/09/26) Thibault Le Meur (thibault.lemeur-AT-supelec.fr)
#  - added subjectTag to queue creation
#  - added "use lib" to set default RT lib path for RT4 in /opt/rt4/lib
# v1.2 (2012/05/01) Haïm Dimermanas
#  - Bug Fixes when copying scrips
#
#No warranty
#-----------
#THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
#APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
#HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM “AS IS” WITHOUT WARRANTY
#OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,*
#THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.
#SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY
#SERVICING, REPAIR OR CORRECTION.
#
#
use strict;
use lib '/usr/local/rt4/lib';
use warnings;
use lib '/usr/local/rt4/lib';
use warnings;
use lib qw(../lib);
use RT;
use RT::Queues;
use RT::Tickets;

my $newqueue;
my $clonequeue;

print "\nRT Queue and setup cloning/merging\n";
print "-----------------------------------\n";
print "-This script clones or merges     -\n";
print "-a queue's parameters (rights,    -\n";
print "-templates,scrips, custom fields) -\n";
print "-to an existing queue (merge mode)-\n";
print "-or to a new queue (clone mode)   -\n";
print "-----------------------------------\n";
print "Name of cloned Queue (source queue):";
$clonequeue = <>;
chomp($clonequeue);
print "Name of new Queue (destination queue):";
$newqueue = <>;
chomp($newqueue);

RT::LoadConfig();
RT::Init();

my $CurrentUser = RT::SystemUser;

unless ( $CurrentUser->Id ) {
    print loc("No RT user found. Please consult your RT administrator.\n");
    exit(1);
}


my $CloneQueueObj = RT::Queue->new($CurrentUser);
my $res = $CloneQueueObj->Load($clonequeue);
my $CloneQueueId = $CloneQueueObj->id;

my $res = $CloneQueueObj->Load($clonequeue);
my $CloneQueueId = $CloneQueueObj->id;
if ( ! defined($res) || $res eq "" ) {
    print "ERROR: Cloned queue '$clonequeue' doesn't exist... EXITING\n";
    exit 1;
} else {
    print "   * Loading existing Cloned queue '$clonequeue' (".$CloneQueueObj->id.")\n";
}

my $NewQueueObj = RT::Queue->new($CurrentUser);
$res = $NewQueueObj->Load($newqueue);
my $NewQueueId;
if ( ! defined($res) || $res eq "" ) {
   print "   * Cloning mode: Creating new queue '$newqueue'\n";
   print "   ** Please enter the correspond email address for queue '$newqueue':";
   my $emailcorrespond = <>;
   chomp($emailcorrespond);
   print "   ** Please enter the comment email address for queue '$newqueue':";
   my $emailcomment = <>;
   chomp($emailcomment);
   print "   ** Please enter the description for queue '$newqueue':";
   my $qdescr = <>;
   chomp($qdescr);
   print "   ** Please enter the subjectTag for queue '$newqueue':";
   my $subjecttag = <>;
   chomp($subjecttag);

   my ($queueid) = $NewQueueObj->Create(
       Name              => $newqueue,
       Description       => $qdescr,
       CorrespondAddress => $emailcorrespond,
       CommentAddress    => $emailcomment,
       SubjectTag => $subjecttag
   );
   $NewQueueId = $queueid
} else {
   $NewQueueId  = $NewQueueObj->id;
   print "   * Merging mode: merging to existing queue '$newqueue' (".$NewQueueObj->id.")\n";

#### assign group and user privileges from clone queue to the new queue

my $objACL = RT::ACL->new($CurrentUser);
$objACL->LimitToObject($CloneQueueObj);

my $objACE = RT::ACE->new($CurrentUser);
my $objGroup = RT::Group->new($CurrentUser);

while (my $acl = $objACL->Next) {
    $objGroup->LoadQueueRoleGroup(Queue=>$NewQueueId, Type=>$acl->PrincipalType);
    $objACE->Create(Object=>$NewQueueObj,
                    PrincipalId=>$objGroup->id || $acl->PrincipalId,
                    PrincipalType=>$acl->PrincipalType,
                    RightName=>$acl->RightName);
    print "   ** Creating ACL '".$acl->RightName."'\n";
}



####  Copy CustomeFields
my $objCFs = RT::ObjectCustomFields->new($CurrentUser);
$objCFs->LimitToObjectId($CloneQueueId);
my $objNewCF = RT::ObjectCustomField->new($CurrentUser);
while (my $cf = $objCFs->Next) {
  $objNewCF->Create(ObjectId=>$newqueue,SortOrder=>$cf->SortOrder,CustomField=>$cf->CustomField);
  print "   ** Creating CustomField '".$cf->id."'\n";
}


#### create templates for the new queue
my $objTemplates = RT::Templates->new($CurrentUser);
$objTemplates->LimitToQueue($CloneQueueId);

my $objTemplate = RT::Template->new($CurrentUser);

while (my $t = $objTemplates->Next) {

  my $objLocalTemplate = RT::Template->new($CurrentUser);
  $objLocalTemplate->LoadQueueTemplate(Queue=>$NewQueueId,Name=>$t->Name);
  if ($objLocalTemplate->id) {
     print "   ** Skipping existing template '".$t->Name."' (".$objLocalTemplate->id.")\n";
  }
  else {
     $objTemplate->Create(Content=>$t->Content,Queue=>$newqueue,Description=>$t->Description,Type=>$t->Type,Name=>$t->Name);
     print "   ** Creating template '".$t->Name."'\n";
  }
}

#### create scrips for the new queue

my $objScrips = RT::Scrips->new($CurrentUser);
$objScrips->LimitToQueue($CloneQueueId);

my $objScrip = RT::Scrip->new($CurrentUser);

while (my $s = $objScrips->Next) {
    my $template;
    $objTemplate->Load($s->Template);
    if ($objTemplate->Queue == 0) { ### global template
            $template = $s->Template;
    }
    else { ### local template, go find out which one
            my $objLocalTemplate = RT::Template->new($CurrentUser);
            $objLocalTemplate->LoadQueueTemplate(Queue=>$NewQueueId,Name=>$objTemplate->Name);
            $template = $objLocalTemplate->id;
    }
                                                                                                                                                                                              181,0-1       85%

    my $testScrips = RT::Scrips->new($CurrentUser);
    $testScrips->LimitToQueue($NewQueueId);
    $testScrips->Limit(FIELD => "Description", VALUE => $s->Description);
    if ($testScrips->Next) {
      print "   ** Skipping existing scrip ".$s->Description."' action=".$s->ScripAction." cond=".$s->ScripCondition." tmpl=$template\n";
    }
    else {
     $objScrip->Create(
            Queue=>$newqueue,
            Description=>$s->Description,
            ScripAction=>$s->ScripAction,
            ScripCondition=>$s->ScripCondition,
            CustomPrepareCode=>$s->CustomPrepareCode,
            CustomCommitCode=>$s->CustomCommitCode,
            CustomIsApplicableCode=>$s->CustomIsApplicableCode,
            Stage=>$s->Stage,
            Template=>$template);
    print "   ** Creating scrip '".$s->Description."' action=".$s->ScripAction." cond=".$s->ScripCondition." tmpl=$template\n";
    }
}