[Rt-commit] rt branch 4.4/current-interface created. rt-4.4.5-4-gb9e5e706f3

BPS Git Server git at git.bestpractical.com
Wed Apr 13 17:23:08 UTC 2022


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  b9e5e706f31fd3a4a19fb0ca729342802301cc31 (commit)

- Log -----------------------------------------------------------------
commit b9e5e706f31fd3a4a19fb0ca729342802301cc31
Author: Jim Brandt <jbrandt at bestpractical.com>
Date:   Mon Apr 11 15:37:31 2022 -0400

    Fix incorrect internal doc link

diff --git a/lib/RT.pm b/lib/RT.pm
index 61d3882d6d..59dda45e1b 100644
--- a/lib/RT.pm
+++ b/lib/RT.pm
@@ -211,7 +211,7 @@ sub Init {
 
 =head2 ConnectToDatabase
 
-Get a database connection. See also L</Handle>.
+Get a database connection. See also L</DatabaseHandle>.
 
 =cut
 

commit 33a5927342c3fe5dab3a9e09f8382f15a7fa1ad4
Author: sunnavy <sunnavy at 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 at 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 5651211016e67da2f9ac23023ae54c04cce8484e
Author: sunnavy <sunnavy at 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: API, Email, REST, Web, and CLI.
    
    Previously we set header "X-RT-Interface" to "Mobile" for ticket
    create/update requests from mobile devices. Even though "Mobile" is not
    available in the new interface(as mobile devices have much larger
    screens nowadays, which makes RT mobile UI not that useful as before,
    not mentioning that the mobile detection is not 100% reliable), we still
    set that header like before for back compatibility.

diff --git a/lib/RT.pm b/lib/RT.pm
index 9faaf2b26e..61d3882d6d 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,61 @@ sub UnprivilegedUsers {
 }
 
 
+=head2 CurrentInterface
+
+Returns the interface used to make the current request. Possible values
+are the following:
+
+=over 4
+
+=item Web
+
+Requests handled by RT::Interface::Web, which are all typical web-based
+requests over http (usually from a browser) that are not REST-type.
+
+=item Email
+
+Requests handled by RT::Interface::Email, which are incoming emails.
+
+=item CLI
+
+Requests handled by RT::Interface::CLI, which is most, but not all
+command-line scripts.
+
+=item REST
+
+Requests to the RT REST (version 1) API.
+
+=item API
+
+Requests that appear to be directly to RT code. This is the default
+and stays set if not updated by one of the interfaces above.
+
+=back
+
+=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..5a66e18acf 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,8 @@ sub CreateTicket {
         Date    => $date_now->RFC2822(Timezone => 'user'),
         Body    => $sigless,
         Type    => $ARGS{'ContentType'},
-        Interface => RT::Interface::Web::MobileClient() ? 'Mobile' : 'Web',
+        # Stick to "Mobile" for back compatibility, unless current interface is customized to something else
+        RT->CurrentInterface eq 'Web' && RT::Interface::Web::MobileClient() ? ( Interface => 'Mobile' ) : (),
     );
 
     my @attachments;
@@ -2435,7 +2437,8 @@ sub ProcessUpdateMessage {
         Subject => $args{ARGSRef}->{'UpdateSubject'},
         Body    => $args{ARGSRef}->{'UpdateContent'},
         Type    => $args{ARGSRef}->{'UpdateContentType'},
-        Interface => RT::Interface::Web::MobileClient() ? 'Mobile' : 'Web',
+        # Stick to "Mobile" for back compatibility, unless current interface is customized to something else
+        RT->CurrentInterface eq 'Web' && RT::Interface::Web::MobileClient() ? ( Interface => 'Mobile' ) : (),
     );
 
     $Message->head->replace( 'Message-ID' => Encode::encode( "UTF-8",
@@ -2634,13 +2637,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


More information about the rt-commit mailing list