Mailing List Archive

rt branch 4.4/current-interface created. rt-4.4.5-3-g1ea3063026
This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "rt".

The branch, 4.4/current-interface has been created
at 1ea30630262537144f5863b04ff4dc560e90d575 (commit)

- Log -----------------------------------------------------------------
commit 1ea30630262537144f5863b04ff4dc560e90d575
Author: sunnavy <sunnavy@bestpractical.com>
Date: Sat Nov 20 04:07:06 2021 +0800

Add tests for current interface

diff --git a/t/ticket/interface.t b/t/ticket/interface.t
new file mode 100644
index 0000000000..fd3ee581bb
--- /dev/null
+++ b/t/ticket/interface.t
@@ -0,0 +1,103 @@
+use strict;
+use warnings;
+
+use RT::Test tests => undef, actual_server => 1;
+
+my ( $baseurl, $m ) = RT::Test->started_ok;
+
+my $queue = RT::Test->load_or_create_queue( Name => 'General' );
+my $cf = RT::Test->load_or_create_custom_field( Name => 'Interface', Type => 'FreeformSingle', Queue => $queue->Id );
+
+my $scrip = RT::Scrip->new( RT->SystemUser );
+my ( $ret, $msg ) = $scrip->Create(
+ Queue => $queue->id,
+ ScripCondition => 'On Create',
+ ScripAction => 'User Defined',
+ CustomPrepareCode => 'return 1',
+ CustomCommitCode => q{
+ $self->TicketObj->AddCustomFieldValue( Field => 'Interface', Value => RT->CurrentInterface );
+ },
+ Template => 'Blank',
+);
+ok( $ret, $msg );
+
+
+diag 'Test API interface';
+my $ticket = RT::Ticket->new( RT->SystemUser );
+( $ret, undef, $msg ) = $ticket->Create( Queue => $queue, Subject => 'Test API interface' );
+ok( $ret, $msg );
+is( $ticket->FirstCustomFieldValue('Interface'), 'API', 'Interface is set to API' );
+
+
+diag 'Test CLI interface';
+my $template = RT::Template->new( RT->SystemUser );
+$template->Create( Name => 'CLICreateTicket', Content => <<'EOF');
+===Create-Ticket: test
+Queue: General
+Subject: Test CLI interface
+Content: test
+ENDOFCONTENT
+EOF
+
+my $root = RT::Test->load_or_create_user( Name => 'root' );
+$root->SetGecos( ( getpwuid($<) )[0] );
+
+system(
+ "$RT::BinPath/rt-crontool", '--search', 'RT::Search::FromSQL', '--search-arg',
+ "id = " . $ticket->Id, '--action', 'RT::Action::CreateTickets', '--template',
+ $template->Id, '--transaction', 'first',
+) && die $?;
+
+$ticket = RT::Test->last_ticket;
+is( $ticket->Subject, 'Test CLI interface', 'Created ticket via rt-crontool' );
+is( $ticket->FirstCustomFieldValue('Interface'), 'CLI', 'Interface is set to CLI' );
+
+
+diag 'Test Email interface';
+my ( $status, $id ) = RT::Test->send_via_mailgate_and_http(<<'EOF');
+From: root@localhost
+Subject: Test Email interface
+
+Test
+EOF
+is( $status >> 8, 0, "The mail gateway exited normally" );
+ok( $id, "Created ticket" );
+$ticket = RT::Test->last_ticket;
+is( $ticket->FirstCustomFieldValue('Interface'), 'Email', 'Interface is set to Email' );
+
+
+diag 'Test Web interface';
+ok( $m->login(), 'Logged in' );
+$m->goto_create_ticket( $queue->Id );
+$m->submit_form( form_name => 'TicketCreate', fields => { Subject => 'Test Web interface' }, button => 'SubmitTicket' );
+$ticket = RT::Test->last_ticket;
+is( $ticket->FirstCustomFieldValue('Interface'), 'Web', 'Interface is set to Web' );
+
+
+diag 'Test REST interface';
+my $content = "id: ticket/new
+Queue: General
+Requestor: root
+Subject: Test REST interface
+Cc:
+AdminCc:
+Text: Test
+";
+
+$m->post(
+ "$baseurl/REST/1.0/ticket/new",
+ [.
+ user => 'root',
+ pass => 'password',
+ content => $content,
+ ],
+ Content_Type => 'form-data'
+);
+
+($id) = $m->content =~ /Ticket (\d+) created/;
+ok( $id, "Created ticket #$id" );
+
+$ticket->Load($id);
+is( $ticket->FirstCustomFieldValue('Interface'), 'REST', 'Interface is set to REST' );
+
+done_testing;

commit 65d5c2bf14bd3020b816e1502a6fb387f82436bd
Author: sunnavy <sunnavy@bestpractical.com>
Date: Mon Nov 22 22:28:30 2021 +0800

Abstract methods to get/set/reset current interface and use them accordingly

Currently we have 4 interfaces: API, Email, REST and Web. Note that
previously we used "Mobile" for /m/ pages, which is merged into 'Web',
considering /m/ is kinda obsolete.

diff --git a/lib/RT.pm b/lib/RT.pm
index 9faaf2b26e..95271f9ee7 100644
--- a/lib/RT.pm
+++ b/lib/RT.pm
@@ -59,7 +59,7 @@ use Cwd ();
use Scalar::Util qw(blessed);
use UNIVERSAL::require;

-use vars qw($Config $System $SystemUser $Nobody $Handle $Logger $_Privileged $_Unprivileged $_INSTALL_MODE);
+use vars qw($Config $System $SystemUser $Nobody $Handle $Logger $CurrentInterface $_Privileged $_Unprivileged $_INSTALL_MODE);

use vars qw($BasePath
$EtcPath
@@ -667,6 +667,34 @@ sub UnprivilegedUsers {
}


+=head2 CurrentInterface
+
+Returns current interface. Possible values are: API, CLI, Email, REST and Web.
+Note that CLI is set for scripts that use RT::Interface::CLI.
+
+=cut
+
+sub CurrentInterface { return $CurrentInterface || 'API' }
+
+=head2 SetCurrentInterface API|CLI|Email|REST|Web
+
+Sets current interface and returns it.
+
+=cut
+
+sub SetCurrentInterface {
+ shift if ( $_[0] // '' ) eq 'RT'; # shift package info
+ $CurrentInterface = shift;
+}
+
+=head2 ResetCurrentInterface
+
+Resets current interface(i.e. it will default to API)
+
+=cut
+
+sub ResetCurrentInterface { $CurrentInterface = undef }
+
=head2 Plugins

Returns a listref of all Plugins currently configured for this RT instance.
diff --git a/lib/RT/Condition/ViaInterface.pm b/lib/RT/Condition/ViaInterface.pm
index 849e107183..dac72e757a 100644
--- a/lib/RT/Condition/ViaInterface.pm
+++ b/lib/RT/Condition/ViaInterface.pm
@@ -65,11 +65,8 @@ sub IsApplicable {
return 0 unless $self->Argument;
my @interfaces = split /,/, $self->Argument;

- if (my $msg = $self->TransactionObj->Message->First) {
- my $interface = $msg->GetHeader('X-RT-Interface');
- return 0 unless $interface;
- return 1 if grep { lc $interface eq lc $_ } @interfaces;
- }
+ my $current_interface = lc RT->CurrentInterface;
+ return 1 if grep { $current_interface eq lc $_ } @interfaces;
return 0;
}

diff --git a/lib/RT/Interface/CLI.pm b/lib/RT/Interface/CLI.pm
index 821e71b31b..59f2bf4b7a 100644
--- a/lib/RT/Interface/CLI.pm
+++ b/lib/RT/Interface/CLI.pm
@@ -225,6 +225,7 @@ sub Init {
if not $exists{help} and $hash->{help};

require RT;
+ RT->SetCurrentInterface('CLI');
RT::LoadConfig();

if (not $exists{quiet} and $hash->{quiet}) {
diff --git a/lib/RT/Interface/Email.pm b/lib/RT/Interface/Email.pm
index 71eda2462f..779811510b 100644
--- a/lib/RT/Interface/Email.pm
+++ b/lib/RT/Interface/Email.pm
@@ -130,6 +130,8 @@ sub Gateway {
%$argsref
);

+ RT->SetCurrentInterface('Email');
+
# Set the scope to return from with TMPFAIL/FAILURE/SUCCESS
$SCOPE = HERE;

diff --git a/lib/RT/Interface/Web.pm b/lib/RT/Interface/Web.pm
index 8db5b7b32b..9777373824 100644
--- a/lib/RT/Interface/Web.pm
+++ b/lib/RT/Interface/Web.pm
@@ -262,6 +262,7 @@ sub WebRemoteUserAutocreateInfo {
sub HandleRequest {
my $ARGS = shift;

+ RT->SetCurrentInterface('Web');
if (RT->Config->Get('DevelMode')) {
require Module::Refresh;
Module::Refresh->refresh;
@@ -2252,7 +2253,6 @@ sub CreateTicket {
Date => $date_now->RFC2822(Timezone => 'user'),
Body => $sigless,
Type => $ARGS{'ContentType'},
- Interface => RT::Interface::Web::MobileClient() ? 'Mobile' : 'Web',
);

my @attachments;
@@ -2634,13 +2634,13 @@ sub MakeMIMEEntity {
Body => undef,
AttachmentFieldName => undef,
Type => undef,
- Interface => 'API',
+ Interface => undef,
@_,
);
my $Message = MIME::Entity->build(
Type => 'multipart/mixed',
"Message-Id" => Encode::encode( "UTF-8", RT::Interface::Email::GenMessageId ),
- "X-RT-Interface" => $args{Interface},
+ "X-RT-Interface" => $args{Interface} || RT->CurrentInterface,
map { $_ => Encode::encode( "UTF-8", $args{ $_} ) }
grep defined $args{$_}, qw(Subject From Cc To Date)
);
diff --git a/lib/RT/Interface/Web/Handler.pm b/lib/RT/Interface/Web/Handler.pm
index fdd146499a..02308240ea 100644
--- a/lib/RT/Interface/Web/Handler.pm
+++ b/lib/RT/Interface/Web/Handler.pm
@@ -201,6 +201,7 @@ sub CleanupRequest {
unless $INC{'Test/WWW/Mechanize/PSGI.pm'};

RT::ObjectCustomFieldValues::ClearOCFVCache();
+ RT->ResetCurrentInterface;
}


diff --git a/lib/RT/Ticket.pm b/lib/RT/Ticket.pm
index a6f403e0ef..254b649055 100644
--- a/lib/RT/Ticket.pm
+++ b/lib/RT/Ticket.pm
@@ -1670,7 +1670,7 @@ sub _RecordNote {
);
}

- $args{'MIMEObj'}->head->replace('X-RT-Interface' => 'API')
+ $args{'MIMEObj'}->head->replace('X-RT-Interface' => RT->CurrentInterface)
unless $args{'MIMEObj'}->head->get('X-RT-Interface');

# convert text parts into utf-8
diff --git a/share/html/REST/1.0/autohandler b/share/html/REST/1.0/autohandler
index fe70b8d0b8..e755943a02 100644
--- a/share/html/REST/1.0/autohandler
+++ b/share/html/REST/1.0/autohandler
@@ -49,6 +49,7 @@
%#
<%INIT>
use RT::Interface::REST;
+RT->SetCurrentInterface('REST');
$r->content_type('text/plain; charset=utf-8');
$m->error_format('text');
$m->call_next();

-----------------------------------------------------------------------


hooks/post-receive
--
rt
_______________________________________________
rt-commit mailing list
rt-commit@lists.bestpractical.com
https://lists.bestpractical.com/mailman/listinfo/rt-commit