Ticket.cgi
Jump to navigation
Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
- !/usr/bin/perl -w
use strict; use lib '/usr/share/request-tracker3.6/lib'; use lib '/root/ws/';
use SOAP::Transport::HTTP; use MIME::Entity; use MIME::Base64; use IO::Scalar; use File::MimeInfo::Magic; use RT; use RT::User; use RT::CurrentUser; use RT::Ticket; use GGUS::Transaction; use GGUS::Transactions;
SOAP::Transport::HTTP::CGI
-> dispatch_to('TicketWS') -> handle;
package TicketWS;
- Create a new ticket
- Parameters:
- Necessary:
- ggus_id: int, id in ggus database
- requestor: string, email address of requestor
- subject: string
- content: string
- priority: string, as defined in ggus wsdl
- affected_site: string, site that affects the ticket
sub create_ticket {
my $self = shift;
RT::LoadConfig(); RT::Init();
my $num_args = 6; unless (@_ == $num_args) { $RT::Logger->log( level => 'err', message => "Not enough arguments." ); return (0, "ngigrnet"); }
my $ggus_id = shift; my $requestor = shift; $requestor =~ s/ //g; my $subject = shift; my $content = shift; my $priority = shift; $priority = convert_priority($priority); my $affected_site = shift;
# queue name my $queue = get_queue($affected_site);
unless (is_int($ggus_id)) { $RT::Logger->log(level=>'alert', message => "Requested ticket from ggus $ggus_id"); return (0, "ngigrnet"); }
$RT::Logger->log(level => 'info', message => "ggus $ggus_id added by $requestor."); $RT::Logger->log(level => 'crit', message => "affected site: $affected_site");
# build MIME encoded content my $MIMEContent = MIME::Entity->build( Data => [ $content ] ); my $ticket = new RT::Ticket($RT::SystemUser); my %ticket_args = ( "Queue" => $queue, "Requestor" => [$requestor,], "Subject" => $subject, "Priority" => $priority, "MIMEObj" => $MIMEContent, ); my @res = $ticket->Create(%ticket_args); if ($res[0] == 0) { # error creating ticket $RT::Logger->log(level => 'err', message => $res[2]); return (0, "ngigrnet"); } $RT::Logger->log(level => 'debug', message => $res[2]); # Add ggus id to ggus_rt table my $rt_handle = $RT::Handle->new($RT::SystemUser); $rt_handle->BeginTransaction; $rt_handle->Insert('ggus_rt', ('ggus_id', $ggus_id, 'rt_id', $res[0])); $rt_handle->Commit;
# get user from requestor's address my $user = RT::User->new($RT::SystemUser); $RT::Logger->log( level => 'info', message => "user: $requestor", ); $user->LoadOrCreateByEmail($requestor); # set Creator and Created fields ticket_set_creat($res[0], $user->id); # set corresponding transaction's creater and created my $trans_id = $res[1]; trans_set_creat($trans_id, $user->id);
return ($res[0], "ngigrnet");
}
- Edit an existing ticket.
- Parameters:
- Necessary:
- ggus_id: int, from ggus
- requestor: email address
- content: string
- status: string
- priority: string
sub edit_ticket {
my $self = shift;
RT::LoadConfig(); RT::Init();
my $num_args = 5; unless (@_ == $num_args) { $RT::Logger->log( level => 'err', message => 'Not enough arguments.' ); return (0, "ngigrnet"); }
my $ggus_id = shift; my $requestor = shift; $requestor =~ s/ //g; my $content = shift; my $status = shift; my $priority = shift;
unless (is_int($ggus_id)) { $RT::Logger->log(level=>'alert', message => "Requested ticket from ggus $ggus_id"); return (0, "ngigrnet"); }
- get user from requestor's address
my $user = RT::User->new($RT::SystemUser); $RT::Logger->log( level => 'crit', message => "user: $requestor", ); $user->LoadOrCreateByEmail($requestor);
- get rt_id for this ggus_id
my $rt_handle = $RT::Handle->new($RT::SystemUser); my $query = 'SELECT rt_id FROM ggus_rt WHERE ggus_id=?'; my @res = $rt_handle->FetchResult($query, ($ggus_id, )); unless (@res) { $RT::Logger->log( level => 'err', message => "No rt ticket for ggus $ggus_id" ); return (0, "ngigrnet"); } my $ticket_id = $res[0]; $RT::Logger->log( level => 'crit', message => "ggus: $ticket_id for ggus $ggus_id edited." );
unless ($content eq ) { my %args = ( 'Content' => $content, );
my $ticket = new RT::Ticket($RT::SystemUser); $ticket->Load($ticket_id); @res = $ticket->Comment(%args); if ($res[0] == 0) { $RT::Logger->log(level => 'crit', message => $res[1]); return (0, "ngigrnet"); } $RT::Logger->log(level => 'crit', message => $res[1]);
# set creator of transaction trans_set_creat($res[0], $user->id); }
# set status set_status($ggus_id, $status); # set priority set_priority($ggus_id, $status);
# scrips have been run # mark transaction done mark_transaction_done($res[0]);
return ($ticket_id, "ngigrnet");
}
- Add an attachemnt
- params:
- ggus_id: int from ggus
- atch_data: base64string the data of the attachment
- atch_name: string the name of the attachment
sub add_attachment {
my $self = shift;
RT::LoadConfig(); RT::Init();
my $num_args = 3; unless (@_ == $num_args) { $RT::Logger->log( level => 'err', message => 'Not enough arguments.' ); return (0, "ngigrnet"); } my $ggus_id = shift; my $atch_data = shift; my $atch_name = shift;
unless (is_int($ggus_id)) { $RT::Logger->log(level=>'alert', message => "Requested ticket from ggus $ggus_id"); return (0, "ngigrnet"); }
- get rt_id for this ggus_id
my $rt_handle = $RT::Handle->new($RT::SystemUser); my $query = 'SELECT rt_id FROM ggus_rt WHERE ggus_id=?'; my @res = $rt_handle->FetchResult($query, ($ggus_id, )); unless (@res) { $RT::Logger->log( level => 'err', message => "No rt ticket for ggus $ggus_id" ); return (0, "ngigrnet"); } my $ticket_id = $res[0]; $RT::Logger->log( level => 'info', message => "ggus: $ticket_id for ggus $ggus_id attachment added." ); my $data = MIME::Base64::decode_base64($atch_data); $RT::Logger->log( level => 'crit', message => "ggus: attachment $data" );
my $content = ;
- build MIME encoded content
my $MIMEContent = MIME::Entity->build( Data => [ $content ] ); my $atch_type = get_mime_type($atch_data); add_attachment_to_mimeobj(\$MIMEContent, $atch_data, $atch_type, $atch_name);
my %args = ( 'MIMEObj' => $MIMEContent, );
- make transaction
my $ticket = new RT::Ticket($RT::SystemUser); $ticket->Load($ticket_id); @res = $ticket->Comment(%args); if ($res[0] == 0) { $RT::Logger->log(level => 'err', message => $res[1]); return (0, "ngigrnet"); } $RT::Logger->log(level => 'info', message => $res[1]);
- scrips have been run
- mark transaction done
mark_transaction_done($res[0]);
return ($ticket_id, "ngigrnet");
}
- Change priority
- params:
- id: int, from ggus
- priority: string
sub set_priority {
RT::LoadConfig(); RT::Init();
my $num_args = 2; unless (@_ >= $num_args) { $RT::Logger->log( level => 'err', message => 'Not enough arguments.' ); return (0, "ngigrnet"); }
my $ggus_id = shift; unless (is_int($ggus_id)) { $RT::Logger->log(level=>'alert', message => "Requested ticket from ggus $ggus_id"); return (0, "ngigrnet"); }
my $ticket_id = ggus_to_rt($ggus_id); my $priority = shift; $priority = convert_priority($priority); my $ticket = new RT::Ticket($RT::SystemUser); $ticket->Load($ticket_id); $ticket->SetPriority($priority);
return ($ticket_id, "ngigrnet");
}
- Change status
- params:
- id: int, from ggus
- status: string
sub set_status {
RT::LoadConfig(); RT::Init();
my $num_args = 2; unless (@_ >= $num_args) { $RT::Logger->log( level => 'err', message => 'Not enough arguments.' ); return (0, "ngigrnet"); }
my $ggus_id = shift; unless (is_int($ggus_id)) { $RT::Logger->log(level=>'alert', message => "Requested ticket from ggus $ggus_id"); return (0, "ngigrnet"); }
my $ticket_id = ggus_to_rt($ggus_id); my $status = shift; $status = convert_status($status); my $ticket = new RT::Ticket($RT::SystemUser); $ticket->Load($ticket_id); $ticket->SetStatus($status);
return ($ticket_id, "ngigrnet");
}
- Handle attachments.
- Add them as part of a MIMEEntity. parameteres are passwd as references.
- MIMEContent: MIMEEntity, where to add the attachment
- atch_data: base64string, the data of the attachment
- atch_type: string, the MIME type of the attachment
- atch_filename: string, the filename of the attachment
sub add_attachment_to_mimeobj {
unless (@_ == 4) { die 'Incorrect number of arguments.'; } my $MIMEContent = shift; my $atch_data = shift; my $atch_type = shift; my $atch_filename = shift;
$atch_data = MIME::Base64::decode_base64($atch_data);
$$MIMEContent->make_multipart; $$MIMEContent->attach( Data => [ $atch_data ], Type => $atch_type, Filename => $atch_filename, );
}
- Check if argument(s) is(are) integers.
sub is_int {
for (@_) { return 0 if (/\D/); } return 1;
}
- Mark transaction as done
sub mark_transaction_done {
my $id = shift; my $rt_handle = $RT::Handle->new($RT::SystemUser); my $t = GGUS::Transaction->new( $rt_handle); $t->LoadByCol('transaction_id', $id); $t->Setdone(1);
return 0;
}
- Convert priority from ggus to rt
sub convert_priority {
my $priority = shift; $RT::Logger->log(level=>'crit', message => "Requested ticket with priority $priority"); my %priorities = ( 'less urgent' => 0, 'urgent' => 50, 'very urgent' => 75, 'top priority' => 99, ); return $priorities{$priority};
}
- Convert status from ggus to rt
sub convert_status {
my $status = shift; return "resolved" if ($status eq "solved"); return "open";
}
- Set the creator and created to transaction
sub trans_set_creat {
my $transaction = shift; my $user = shift; my $query_t = "UPDATE Transactions SET Creator=? WHERE id=?"; my $query_a = "UPDATE Attachments SET Creator=? WHERE TransactionId=?"; my $rt_handle = $RT::Handle->new($RT::SystemUser); $rt_handle->BeginTransaction; $rt_handle->SimpleQuery($query_t, $user, $transaction); $rt_handle->SimpleQuery($query_a, $user, $transaction); $rt_handle->Commit;
}
- Set the creator to transaction
sub trans_set_creator {
my $transaction = shift; my $user = shift; my $query = "UPDATE Transactions SET Creator=? WHERE id=?"; my $rt_handle = $RT::Handle->new($RT::SystemUser); $rt_handle->BeginTransaction; $rt_handle->SimpleQuery($query, $user, $transaction); $rt_handle->Commit;
}
- Set create fields for new tickets
- params:
- ticket: id
- user: id
sub ticket_set_creat {
my $ticket = shift; my $user = shift; my $query = "UPDATE Tickets SET Creator=? WHERE id=?"; my $rt_handle = $RT::Handle->new($RT::SystemUser); $rt_handle->BeginTransaction; $rt_handle->SimpleQuery($query, $user, $ticket); $rt_handle->Commit;
}
- Get rt id from ggus id
- params:
- ggus_id
- returns:
- ticket_id
sub ggus_to_rt {
my $ggus_id = shift;
my $rt_handle = $RT::Handle->new($RT::SystemUser); my $query = 'SELECT rt_id FROM ggus_rt WHERE ggus_id=?'; my @res = $rt_handle->FetchResult($query, ($ggus_id, )); unless (@res) { $RT::Logger->log( level => 'err', message => "No rt ticket for ggus $ggus_id" ); exit(0); } my $ticket_id = $res[0]; return $ticket_id;
}
- Get the MIME type of base64 encoded data
- params:
- $data base64 encoded data
sub get_mime_type {
my $data = shift; my $raw_data = MIME::Base64::decode($data); my $sh = new IO::Scalar \$raw_data; return File::MimeInfo::Magic::mimetype($sh);
}
sub get_queue {
my $site = shift; return "GR-01-AUTH" if ($site eq "GR-01-AUTH"); return "GR-02-UoM" if ($site eq "GR-02-UoM"); return "GR-03-HEPNTUA" if ($site eq "GR-03-HEPNTUA"); return "GR-04-FORTH-ICS" if ($site eq "GR-04-FORTH-ICS"); return "GR-05-DEMOKRITOS" if ($site eq "GR-05-DEMOKRITOS"); return "GR-06-IASA" if ($site eq "GR-06-IASA"); return "GR-07-UOI-HEPLAB" if ($site eq "GR-07-UOI-HEPLAB"); return "GR-08-CEID" if ($site eq "GR-08-CEID"); return "GR-09-UoA" if ($site eq "GR-09-UoA"); return "GR-10-UOI" if ($site eq "GR-10-UOI"); return "HG-01-GRNET" if ($site eq "HG-01-GRNET"); return "HG-02-IASA" if ($site eq "HG-02-IASA"); return "HG-03-AUTH" if ($site eq "HG-03-AUTH"); return "HG-04-CTI-CEID" if ($site eq "HG-04-CTI-CEID"); return "HG-05-FORTH" if ($site eq "HG-05-FORTH"); return "HG-06-EKT" if ($site eq "HG-06-EKT"); return "NGI_GRNET";
}