Difference between revisions of "Rt-clonequeue"

From Request Tracker Wiki
Jump to navigation Jump to search
(Fixed the error noted in the comment)
Line 188: Line 188:
             $template = $objLocalTemplate->id;
             $template = $objLocalTemplate->id;
     }
     }
                                                                                                                                                                                              181,0-1      85%
   
   
     my $testScrips = RT::Scrips->new($CurrentUser);
     my $testScrips = RT::Scrips->new($CurrentUser);

Revision as of 12:29, 15 January 2013

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 and custom fields
#
#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=>$NewQueueId,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;
    }

    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";
    }
}