[rt-devel] Contrib: SetKeyword

Bruce Campbell bruce_campbell at ripe.net
Sun May 26 13:28:16 EDT 2002


The attached RT::Action and insert script will attempt to set a keyword on
the tickets based on the Template it is called with.

The usage that I created it for is:

	OnCreate SetKeyword with template Keyword_First

The contents of the template 'Keyword_First' is as follows:

	# This is a comment line and is ignored
	# Expecting KeywordSelectsName '/' KeywordDescendentName
	# Does not support more than one '/', sorry.
	Level/First Line Support

So when a ticket is created on our Operations queue, it is firstly
assigned to the First Line Support people.  Caveat - if what you type in
the Template does not match (via 'eq' operator) any keyword on the queue,
the requested Keyword won't be applied.

The Scrip allows for multiple keywords to be applied at one time.

-- 
                             Bruce Campbell                            RIPE
                   Systems/Network Engineer                             NCC
                 www.ripe.net - PGP562C8B1B                      Operations


-------------- next part --------------
# $Header: /home/rt2/etc/site_scrips/Action/RCS/SetKeyword.pm,v 1.4 2002/05/26 17:13:01 bc Exp $

package RT::Action::SetKeyword;
require RT::Action::Generic;
@ISA=qw(RT::Action::Generic);

#What does this type of Action does

# {{{ sub Describe 
sub Describe  {
  my $self = shift;
  return (ref $self . " will add the Keywords in the Template to the ticket.");
}
# }}}


# {{{ sub Prepare 
sub Prepare  {
    # nothing to prepare
    return 1;
}
# }}}

sub Commit {
    my $self = shift;
    # split up the list.
    my $retval = undef;
    my @tsplit = split( '\n', $self->TemplateObj->Content );


    foreach my $this_one ( @tsplit ){
        # Ignore Comments.
	next if( $this_one =~ /^\s*#/ );

	# Split on the '/'.  The template should read something like:
	# 'Level/First Line Support'
        my @tsplit2 = split( '/', $this_one );

	# Did we catch anything?
	next unless defined( $tsplit2[1] );

	# Walk through the keywords on the queue.
        my $KeywordSelects = $self->TicketObj->QueueObj->KeywordSelects();

	while( my $kw = $KeywordSelects->Next ){
		# Does this keyword match part of the request?
		next unless( $kw->Name eq $tsplit2[0] );

		# Yup. Find out what kids we need.
		my $kw_kids=$kw->KeywordObj->Descendents;
		foreach my $kid( keys %{$kw_kids} ){

			# Does this kid match the second item?
			next unless( ${$kw_kids}{$kid} eq $tsplit2[1] );

			# Right.  Now add it.
			$retval += $self->TicketObj->AddKeyword( 
						KeywordSelect => $kw->id,
						Keyword => $kid );
		}
	}
    }
    return( $retval );
}

1;
-------------- next part --------------
#!/usr/bin/perl -w
#

package RT;
use strict;
use vars qw($VERSION $Handle $Nobody $SystemUser $item);

use lib "/home/rt2/lib";
use lib "/home/rt2/etc";

#This drags in RT's config.pm
use config;
use Carp;

use RT::Handle;
use RT::User;
use RT::CurrentUser;

#connect to the db
$RT::Handle = new RT::Handle($RT::DatabaseType);
$RT::Handle->Connect();


#Put together a current user object so we can create a User object
my $CurrentUser = new RT::CurrentUser();


#now that we bootstrapped that little bit, we can use the standard RT cli
# helpers  to do what we need

use RT::Interface::CLI  qw(CleanEnv LoadConfig DBConnect 
			   GetCurrentUser GetMessageContent);

#Clean out all the nasties from the environment
CleanEnv();

#Load etc/config.pm and drop privs
LoadConfig();

#Connect to the database and get RT::SystemUser and RT::Nobody loaded
DBConnect();


$CurrentUser->LoadByName('RT_System');

# {{{ ScripActions

my @ScripActions = (
		       {
			Name => 'SetKeyword',
			Description =>  'Updates ticket Keywords based on the Template Object',
			ExecModule => 'SetKeyword',
			Argument =>  '',
		       },
		       
		       
		      );

# }}}
print "Creating ScripActions...";

use RT::ScripAction;
for $item (@ScripActions) {
    my $new_entry = new RT::ScripAction($CurrentUser);
    my $return = $new_entry->Create(%$item);
    print $return.".";
}

print "done.\n";

$RT::Handle->Disconnect();


1;



More information about the Rt-devel mailing list