Rt-clonequeue

From Request Tracker Wiki
Revision as of 12:23, 16 December 2015 by 82.71.28.44 (talk)
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 "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
# v1.3 (2013/01/15) Alastair Firth
#  - Remove duplicate lines, remove line number copy/paste bug, comment use lib options
#  - Add missing closing brace
#
#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;
#specify your rt lib path (default /usr/local/rt4/lib)
use lib '/usr/local/rt4/lib';
use warnings;
#uncomment if you install this in [...]/rt4/bin
#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;

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->LoadRoleGroup(Object => $NewQueueObj);
    $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";
    }
}