Rt-clonequeue

From Request Tracker Wiki
Revision as of 03:32, 26 September 2011 by 160.228.28.55 (talk) (Added the script rt-clonequeue)
Jump to navigation Jump to search

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 PERL5LIB environment variable before calling rt-clonequeue:

   export PERL5LIB=/opt/rt4/lib

   ./rt-clone-queue


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
#
#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 warnings;
 use lib qw(../lib);
 use RT;
 use RT::Queues;
 use RT::Tickets;
 use Data::Dumper;

 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;
 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);

    my ($queueid) = $NewQueueObj->Create(
        Name              => $newqueue,
        Description       => $qdescr,
        CorrespondAddress => $emailcorrespond,
        CommentAddress    => $emailcomment,
    );
    $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;
     }

     my $testScrips = RT::Scrips->new($CurrentUser);
     $testScrips->LimitToQueue($NewQueueId);
     $testScrips->Limit(FIELD => "Description", VALUE => $s->Description);
     if ($testScrips->count > 0) {
       print '   ** Skipping existing scrip \.$s->Description."' (".$testScrips->Next->id.") 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";
     }
 }